优先链表是否需要设计一种新的接口类型
This commit is contained in:
parent
7c4b358bd4
commit
849254edda
|
@ -18,6 +18,7 @@ type ArrayList struct {
|
|||
|
||||
func assertImplementation() {
|
||||
var _ list.IList = (*ArrayList)(nil)
|
||||
var _ list.ILinkedList = (*ArrayList)(nil)
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -208,3 +209,11 @@ func (l *ArrayList) Values() []interface{} {
|
|||
copy(newElements, l.data[l.hidx+1:l.tidx])
|
||||
return newElements
|
||||
}
|
||||
|
||||
func (l *ArrayList) Traversal(every func(interface{}) bool) {
|
||||
for i := uint(0); i < l.size; i++ {
|
||||
if !every(l.data[i+l.hidx+1]) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -215,6 +215,35 @@ func TestRemove(t *testing.T) {
|
|||
|
||||
}
|
||||
|
||||
func TestTraversal(t *testing.T) {
|
||||
l := New()
|
||||
for i := 0; i < 5; i++ {
|
||||
l.PushFront(uint(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.PushBack(7, 8)
|
||||
result = nil
|
||||
l.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
return true
|
||||
})
|
||||
|
||||
if spew.Sprint(result) != "[4 3 2 1 0 7 8]" {
|
||||
t.Error(result)
|
||||
}
|
||||
}
|
||||
|
||||
func loadTestData() []int {
|
||||
data, err := ioutil.ReadFile("../../l.log")
|
||||
if err != nil {
|
||||
|
|
|
@ -3,6 +3,7 @@ package linkedlist
|
|||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/474420502/focus/list"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
|
@ -22,6 +23,11 @@ type LinkedList struct {
|
|||
size uint
|
||||
}
|
||||
|
||||
func assertImplementation() {
|
||||
var _ list.IList = (*LinkedList)(nil)
|
||||
var _ list.ILinkedList = (*LinkedList)(nil)
|
||||
}
|
||||
|
||||
func New() *LinkedList {
|
||||
l := &LinkedList{}
|
||||
l.head = &Node{}
|
||||
|
@ -44,8 +50,10 @@ func (l *LinkedList) CircularIterator() *CircularIterator {
|
|||
}
|
||||
|
||||
func (l *LinkedList) Clear() {
|
||||
l.head.next = nil
|
||||
l.tail.prev = nil
|
||||
|
||||
l.head.next = l.tail
|
||||
l.tail.prev = l.head
|
||||
|
||||
l.size = 0
|
||||
}
|
||||
|
||||
|
@ -448,6 +456,24 @@ TOPFOR:
|
|||
return
|
||||
}
|
||||
|
||||
func (l *LinkedList) 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 (l *LinkedList) Values() (result []interface{}) {
|
||||
l.Traversal(func(value interface{}) bool {
|
||||
result = append(result, value)
|
||||
|
|
|
@ -3,7 +3,6 @@ package linkedlist
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Pallinder/go-randomdata"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
)
|
||||
|
||||
|
@ -570,6 +569,35 @@ func TestRemoveIf2(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTraversal(t *testing.T) {
|
||||
l := New()
|
||||
for i := 0; i < 5; i++ {
|
||||
l.PushFront(uint(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.PushBack(7, 8)
|
||||
result = nil
|
||||
l.Traversal(func(v interface{}) bool {
|
||||
result = append(result, v)
|
||||
return true
|
||||
})
|
||||
|
||||
if spew.Sprint(result) != "[4 3 2 1 0 7 8]" {
|
||||
t.Error(result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
ll := New()
|
||||
for i := 0; i < 10; i++ {
|
||||
|
@ -639,46 +667,82 @@ func TestCircularIterator(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkPushBack(b *testing.B) {
|
||||
|
||||
ec := 5
|
||||
cs := 2000000
|
||||
b.N = cs * ec
|
||||
|
||||
for c := 0; c < ec; c++ {
|
||||
l := New()
|
||||
for i := 0; i < cs; i++ {
|
||||
l.PushBack(i)
|
||||
}
|
||||
func TestContains(t *testing.T) {
|
||||
ll := New()
|
||||
for i := 0; i < 10; i++ {
|
||||
ll.Push(i)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkPushFront(b *testing.B) {
|
||||
|
||||
ec := 5
|
||||
cs := 2000000
|
||||
b.N = cs * ec
|
||||
|
||||
for c := 0; c < ec; c++ {
|
||||
l := New()
|
||||
for i := 0; i < cs; i++ {
|
||||
l.PushFront(i)
|
||||
for i := 0; i < 10; i++ {
|
||||
if !ll.Contains(i) {
|
||||
t.Error(i)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func BenchmarkInsert(b *testing.B) {
|
||||
|
||||
ec := 10
|
||||
cs := 1000
|
||||
b.N = cs * ec
|
||||
|
||||
for c := 0; c < ec; c++ {
|
||||
l := New()
|
||||
for i := 0; i < cs; i++ {
|
||||
ridx := randomdata.Number(0, int(l.Size())+1)
|
||||
l.Insert(uint(ridx), i)
|
||||
for i := 10; i < 20; i++ {
|
||||
if ll.Contains(i) {
|
||||
t.Error(i)
|
||||
}
|
||||
}
|
||||
|
||||
if v, _ := ll.Front(); v != 0 {
|
||||
t.Error(v)
|
||||
}
|
||||
|
||||
if v, _ := ll.Back(); v != 9 {
|
||||
t.Error(v)
|
||||
}
|
||||
|
||||
ll.Clear()
|
||||
if !ll.Empty() {
|
||||
t.Error("not Empty?")
|
||||
}
|
||||
|
||||
if v, ok := ll.Front(); ok {
|
||||
t.Error(v)
|
||||
}
|
||||
}
|
||||
|
||||
// func BenchmarkPushBack(b *testing.B) {
|
||||
|
||||
// ec := 5
|
||||
// cs := 2000000
|
||||
// b.N = cs * ec
|
||||
|
||||
// for c := 0; c < ec; c++ {
|
||||
// l := New()
|
||||
// for i := 0; i < cs; i++ {
|
||||
// l.PushBack(i)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// func BenchmarkPushFront(b *testing.B) {
|
||||
|
||||
// ec := 5
|
||||
// cs := 2000000
|
||||
// b.N = cs * ec
|
||||
|
||||
// for c := 0; c < ec; c++ {
|
||||
// l := New()
|
||||
// for i := 0; i < cs; i++ {
|
||||
// l.PushFront(i)
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// func BenchmarkInsert(b *testing.B) {
|
||||
|
||||
// ec := 10
|
||||
// cs := 1000
|
||||
// b.N = cs * ec
|
||||
|
||||
// for c := 0; c < ec; c++ {
|
||||
// l := New()
|
||||
// for i := 0; i < cs; i++ {
|
||||
// ridx := randomdata.Number(0, int(l.Size())+1)
|
||||
// l.Insert(uint(ridx), i)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
|
|
@ -7,6 +7,7 @@ type IList interface {
|
|||
Index(idx uint) (interface{}, bool)
|
||||
Remove(idx uint) (result interface{}, isfound bool)
|
||||
Values() []interface{}
|
||||
Traversal(every func(interface{}) bool)
|
||||
|
||||
Clear()
|
||||
Empty() bool
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user