优先链表是否需要设计一种新的接口类型
This commit is contained in:
@@ -14,10 +14,15 @@ type Node struct {
|
||||
|
||||
type PriorityList struct {
|
||||
head, tail *Node
|
||||
size int
|
||||
size uint
|
||||
Compare compare.Compare
|
||||
}
|
||||
|
||||
// 优先队列的Index 正负都有作用要定义一种新的接口类型
|
||||
func assertImplementation() {
|
||||
// var _ list.IList = (*PriorityList)(nil)
|
||||
}
|
||||
|
||||
func New(Compare compare.Compare) *PriorityList {
|
||||
pl := &PriorityList{head: &Node{}, tail: &Node{}, size: 0, Compare: Compare}
|
||||
pl.head.next = pl.tail
|
||||
@@ -57,10 +62,20 @@ func (pl *PriorityList) CircularIterator() *CircularIterator {
|
||||
return &CircularIterator{pl: pl, cur: pl.head}
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Size() int {
|
||||
func (pl *PriorityList) Size() uint {
|
||||
return pl.size
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Empty() bool {
|
||||
return pl.size == 0
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Clear() {
|
||||
pl.head.next = pl.tail
|
||||
pl.tail.prev = pl.head
|
||||
pl.size = 0
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Push(value interface{}) {
|
||||
pl.size++
|
||||
pnode := &Node{value: value}
|
||||
@@ -110,13 +125,31 @@ func (pl *PriorityList) Pop() (result interface{}, ok bool) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (pl *PriorityList) Get(idx int) (interface{}, bool) {
|
||||
func (pl *PriorityList) Index(idx int) (interface{}, bool) {
|
||||
if n, ok := pl.GetNode(idx); ok {
|
||||
return n.value, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (l *PriorityList) Contains(values ...interface{}) bool {
|
||||
|
||||
for _, searchValue := range values {
|
||||
found := false
|
||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||
if cur.value == searchValue {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (pl *PriorityList) GetNode(idx int) (*Node, bool) {
|
||||
if idx >= 0 {
|
||||
cur := pl.head.next
|
||||
@@ -163,3 +196,11 @@ func (pl *PriorityList) Values() []interface{} {
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func (l *PriorityList) Traversal(every func(interface{}) bool) {
|
||||
for cur := l.head.next; cur != l.tail; cur = cur.next {
|
||||
if !every(cur.value) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/474420502/focus/compare"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
func loadTestData() []int {
|
||||
@@ -128,14 +129,14 @@ func TestGet(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, v := range []int{0, 9, 5, 7} {
|
||||
if g, ok := pl.Get(v); ok {
|
||||
if g, ok := pl.Index(v); ok {
|
||||
if g != (9 - v) {
|
||||
t.Error(v, "Get == ", g)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if n, ok := pl.Get(10); ok {
|
||||
if n, ok := pl.Index(10); ok {
|
||||
t.Error("index 10 is over size", n)
|
||||
}
|
||||
}
|
||||
@@ -207,14 +208,14 @@ func TestRemove(t *testing.T) {
|
||||
}
|
||||
|
||||
pl.RemoveWithIndex(0)
|
||||
if g, ok := pl.Get(0); ok {
|
||||
if g, ok := pl.Index(0); ok {
|
||||
if g != 8 {
|
||||
t.Error(g)
|
||||
}
|
||||
}
|
||||
|
||||
pl.RemoveWithIndex(-1)
|
||||
if g, ok := pl.Get(-1); ok {
|
||||
if g, ok := pl.Index(-1); ok {
|
||||
if g != 1 {
|
||||
t.Error(g)
|
||||
}
|
||||
@@ -222,6 +223,60 @@ func TestRemove(t *testing.T) {
|
||||
|
||||
}
|
||||
|
||||
func TestTraversal(t *testing.T) {
|
||||
l := New(compare.Int)
|
||||
for i := 0; i < 5; i++ {
|
||||
l.Push(i)
|
||||
}
|
||||
|
||||
var result []interface{}
|
||||
|
||||
l.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
return true
|
||||
})
|
||||
|
||||
if spew.Sprint(result) != "[4 3 2 1 0]" {
|
||||
t.Error(result)
|
||||
}
|
||||
|
||||
l.Push(7)
|
||||
l.Push(6)
|
||||
result = nil
|
||||
l.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
return true
|
||||
})
|
||||
|
||||
if spew.Sprint(result) != "[7 6 4 3 2 1 0]" {
|
||||
t.Error(result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
ll := New(compare.Int)
|
||||
for i := 0; i < 10; i++ {
|
||||
ll.Push(i)
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
if !ll.Contains(i) {
|
||||
t.Error(i)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 10; i < 20; i++ {
|
||||
if ll.Contains(i) {
|
||||
t.Error(i)
|
||||
}
|
||||
}
|
||||
|
||||
if spew.Sprint(ll.Values()) != "[9 8 7 6 5 4 3 2 1 0]" {
|
||||
t.Error(spew.Sprint(ll.Values()))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// func BenchmarkGet(b *testing.B) {
|
||||
// pl := New(compare.Int)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user