From 7c4b358bd4c0dedc4fed371cd34fca10feb2204e Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Thu, 25 Jul 2019 02:30:55 +0800 Subject: [PATCH] =?UTF-8?q?TODO:=20assertImplementation,=20add=20interface?= =?UTF-8?q?=20InsertIf=20=E7=BB=84=E5=90=88=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- for_test.go | 2 +- interface.go | 2 +- list/array_list/array_list.go | 27 +++++- list/array_list/array_list_test.go | 67 ++++++++++++++ list/array_list/iterator.go | 125 +++++++++++++++++++++++++++ list/linked_list/iterator.go | 4 +- list/linked_list/linked_list.go | 15 ++++ list/linked_list/linked_list_test.go | 20 ++++- list/list.go | 22 +++++ 9 files changed, 278 insertions(+), 6 deletions(-) create mode 100644 list/array_list/iterator.go create mode 100644 list/list.go diff --git a/for_test.go b/for_test.go index d2d038e..f9958d6 100644 --- a/for_test.go +++ b/for_test.go @@ -1,4 +1,4 @@ -package structure +package focus import ( "bytes" diff --git a/interface.go b/interface.go index 6fe4564..c3a3474 100644 --- a/interface.go +++ b/interface.go @@ -1 +1 @@ -package structure +package focus diff --git a/list/array_list/array_list.go b/list/array_list/array_list.go index 8b2aa7f..08dd5ec 100644 --- a/list/array_list/array_list.go +++ b/list/array_list/array_list.go @@ -1,6 +1,10 @@ package arraylist -import "log" +import ( + "log" + + "github.com/474420502/focus/list" +) type ArrayList struct { data []interface{} @@ -12,6 +16,10 @@ type ArrayList struct { shrinkSize uint } +func assertImplementation() { + var _ list.IList = (*ArrayList)(nil) +} + const ( listMaxLimit = uint(1) << 63 listMinLimit = uint(8) @@ -28,6 +36,14 @@ func New() *ArrayList { return l } +func (l *ArrayList) Iterator() *Iterator { + return &Iterator{al: l, cur: 0, isInit: false} +} + +func (l *ArrayList) CircularIterator() *CircularIterator { + return &CircularIterator{al: l, cur: 0, isInit: false} +} + func (l *ArrayList) Clear() { l.data = make([]interface{}, 8, 8) l.tidx = initCap / 2 @@ -83,6 +99,15 @@ func (l *ArrayList) growth() { } +func (l *ArrayList) Push(value interface{}) { + for l.tidx+1 > uint(len(l.data)) { + l.growth() + } + l.data[l.tidx] = value + l.tidx++ + l.size += 1 +} + func (l *ArrayList) PushFront(values ...interface{}) { psize := uint(len(values)) for l.hidx+1-psize > listMaxLimit { diff --git a/list/array_list/array_list_test.go b/list/array_list/array_list_test.go index 1054b60..1f2a3a3 100644 --- a/list/array_list/array_list_test.go +++ b/list/array_list/array_list_test.go @@ -10,6 +10,67 @@ import ( "github.com/davecgh/go-spew/spew" ) +func TestIterator(t *testing.T) { + l := New() + + for i := 0; i < 5; i++ { + l.Push(i) + } + + iter := l.Iterator() + + var result []int + for iter.Next() { + result = append(result, iter.Value().(int)) + } + + if spew.Sprint(result) != "[0 1 2 3 4]" { + t.Error(result) + } + + iter = l.Iterator() + result = nil + for iter.Prev() { + result = append(result, iter.Value().(int)) + } + + if spew.Sprint(result) != "[4 3 2 1 0]" { + t.Error(result) + } + + citer := l.CircularIterator() + result = nil + for i := 0; i < 11; i++ { + if citer.Next() { + result = append(result, citer.Value().(int)) + } + } + + if len(result) != 11 { + t.Error("len(result) != 11, is ", len(result)) + } + + if spew.Sprint(result) != "[0 1 2 3 4 0 1 2 3 4 0]" { + t.Error(result) + } + + citer = l.CircularIterator() + result = nil + for i := 0; i < 11; i++ { + if citer.Prev() { + result = append(result, citer.Value().(int)) + } + } + + if len(result) != 11 { + t.Error("len(result) != 11, is ", len(result)) + } + + if spew.Sprint(result) != "[4 3 2 1 0 4 3 2 1 0 4]" { + t.Error(result) + } +} + func TestPush(t *testing.T) { l := New() @@ -30,6 +91,12 @@ func TestPush(t *testing.T) { t.Error(result) } + l.Push(3) + result = spew.Sprint(l.Values()) + if result != "[1 1 2 2 3]" { + t.Error(result) + } + } func TestGrowth(t *testing.T) { diff --git a/list/array_list/iterator.go b/list/array_list/iterator.go new file mode 100644 index 0000000..fc3acdd --- /dev/null +++ b/list/array_list/iterator.go @@ -0,0 +1,125 @@ +package arraylist + +type Iterator struct { + al *ArrayList + cur uint + isInit bool +} + +func (iter *Iterator) Value() interface{} { + v, _ := iter.al.Index(iter.cur) + return v +} + +func (iter *Iterator) Prev() bool { + + if iter.isInit == false { + if iter.al.size != 0 { + iter.isInit = true + iter.cur = iter.al.size - 1 + return true + } + return false + } + + if iter.cur <= 0 { + return false + } + iter.cur-- + return true +} + +func (iter *Iterator) Next() bool { + + if iter.isInit == false { + if iter.al.size != 0 { + iter.isInit = true + iter.cur = 0 + return true + } + return false + } + + if iter.cur >= iter.al.size-1 { + return false + } + iter.cur++ + return true +} + +func (iter *Iterator) ToHead() { + iter.isInit = true + iter.cur = 0 +} + +func (iter *Iterator) ToTail() { + iter.isInit = true + iter.cur = iter.al.size - 1 +} + +type CircularIterator struct { + al *ArrayList + cur uint + isInit bool +} + +func (iter *CircularIterator) Value() interface{} { + v, _ := iter.al.Index(iter.cur) + return v +} + +func (iter *CircularIterator) Prev() bool { + + if iter.isInit == false { + if iter.al.size != 0 { + iter.isInit = true + iter.cur = iter.al.size - 1 + return true + } + return false + } + + if iter.al.size == 0 { + return false + } + + if iter.cur <= 0 { + iter.cur = iter.al.size - 1 + } else { + iter.cur-- + } + return true +} + +func (iter *CircularIterator) Next() bool { + + if iter.isInit == false { + if iter.al.size != 0 { + iter.isInit = true + iter.cur = 0 + return true + } + return false + } + + if iter.al.size == 0 { + return false + } + + if iter.cur >= iter.al.size-1 { + iter.cur = 0 + } else { + iter.cur++ + } + return true +} + +func (iter *CircularIterator) ToHead() { + iter.isInit = true + iter.cur = 0 +} + +func (iter *CircularIterator) ToTail() { + iter.isInit = true + iter.cur = iter.al.size - 1 +} diff --git a/list/linked_list/iterator.go b/list/linked_list/iterator.go index 2024612..013af73 100644 --- a/list/linked_list/iterator.go +++ b/list/linked_list/iterator.go @@ -78,10 +78,10 @@ func (iter *CircularIterator) Next() bool { return true } -func (iter *CircularIterator) MoveToHead() { +func (iter *CircularIterator) ToHead() { iter.cur = iter.pl.head } -func (iter *CircularIterator) MoveToTail() { +func (iter *CircularIterator) ToTail() { iter.cur = iter.pl.tail } diff --git a/list/linked_list/linked_list.go b/list/linked_list/linked_list.go index dea0ad0..1a36c76 100644 --- a/list/linked_list/linked_list.go +++ b/list/linked_list/linked_list.go @@ -57,6 +57,21 @@ func (l *LinkedList) Size() uint { return l.size } +func (l *LinkedList) Push(value interface{}) { + var node *Node + l.size++ + + node = &Node{} + node.value = value + + tprev := l.tail.prev + tprev.next = node + + node.prev = tprev + node.next = l.tail + l.tail.prev = node +} + func (l *LinkedList) PushFront(values ...interface{}) { var node *Node diff --git a/list/linked_list/linked_list_test.go b/list/linked_list/linked_list_test.go index 84eb948..9280810 100644 --- a/list/linked_list/linked_list_test.go +++ b/list/linked_list/linked_list_test.go @@ -7,6 +7,24 @@ import ( "github.com/davecgh/go-spew/spew" ) +func TestPush(t *testing.T) { + l := New() + for i := 0; i < 5; i++ { + l.Push(i) + } + var result string + result = spew.Sprint(l.Values()) + if result != "[0 1 2 3 4]" { + t.Error(result) + } + + l.Push(0) + result = spew.Sprint(l.Values()) + if result != "[0 1 2 3 4 0]" { + t.Error(result) + } +} + func TestPushFront(t *testing.T) { l := New() for i := 0; i < 5; i++ { @@ -602,7 +620,7 @@ func TestCircularIterator(t *testing.T) { } } - iter.MoveToTail() + iter.ToTail() for i := 0; i != 10; i++ { iter.Prev() if iter.Value() != i { diff --git a/list/list.go b/list/list.go new file mode 100644 index 0000000..b4606bf --- /dev/null +++ b/list/list.go @@ -0,0 +1,22 @@ +package list + +// IList 通用接口 +type IList interface { + Push(value interface{}) + Contains(values ...interface{}) bool + Index(idx uint) (interface{}, bool) + Remove(idx uint) (result interface{}, isfound bool) + Values() []interface{} + + Clear() + Empty() bool + Size() uint +} + +// ILinkedList 通用接口 +type ILinkedList interface { + PushFront(values ...interface{}) + PushBack(values ...interface{}) + PopFront() (result interface{}, found bool) + PopBack() (result interface{}, found bool) +}