From 06f4366e512ca0299c1a3b324019b91a787cc268 Mon Sep 17 00:00:00 2001 From: huangsimin Date: Thu, 25 Jul 2019 14:30:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90stack=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E6=A0=87=E5=87=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- priority_queue/iterator.go | 2 +- priority_queuekey/iterator.go | 2 +- {lastack => stack/listarraystack}/lastack.go | 14 ++- .../listarraystack}/lastack_test.go | 56 +++++++-- stack/liststack/stack.go | 102 ++++++++++++++++ stack/{ => liststack}/stack_test.go | 0 stack/stack.go | 110 +++--------------- tree/avl/iterator.go | 2 +- tree/avldup/iterator.go | 2 +- tree/avlkey/iterator.go | 2 +- tree/avlkeydup/iterator.go | 2 +- tree/vbt/iterator.go | 2 +- tree/vbtkey/iterator.go | 2 +- 13 files changed, 186 insertions(+), 112 deletions(-) rename {lastack => stack/listarraystack}/lastack.go (91%) rename {lastack => stack/listarraystack}/lastack_test.go (79%) create mode 100644 stack/liststack/stack.go rename stack/{ => liststack}/stack_test.go (100%) diff --git a/priority_queue/iterator.go b/priority_queue/iterator.go index a8feea8..1f6efc4 100644 --- a/priority_queue/iterator.go +++ b/priority_queue/iterator.go @@ -1,7 +1,7 @@ package pqueue import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/priority_queuekey/iterator.go b/priority_queuekey/iterator.go index 4b56206..95f5473 100644 --- a/priority_queuekey/iterator.go +++ b/priority_queuekey/iterator.go @@ -1,7 +1,7 @@ package pqueuekey import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/lastack/lastack.go b/stack/listarraystack/lastack.go similarity index 91% rename from lastack/lastack.go rename to stack/listarraystack/lastack.go index a0febff..74fd347 100644 --- a/lastack/lastack.go +++ b/stack/listarraystack/lastack.go @@ -1,6 +1,7 @@ package lastack import ( + "github.com/474420502/focus/stack" "github.com/davecgh/go-spew/spew" ) @@ -13,7 +14,11 @@ type Node struct { type Stack struct { top *Node cache *Node - size int + size uint +} + +func assertImplementation() { + var _ stack.IStack = (*Stack)(nil) } func (as *Stack) grow() bool { @@ -25,7 +30,7 @@ func (as *Stack) grow() bool { grownode.cur = -1 as.cache = nil } else { - var growsize int + var growsize uint if as.size <= 256 { growsize = as.size << 1 } else { @@ -78,10 +83,11 @@ func (as *Stack) Empty() bool { return as.size == 0 } -func (as *Stack) Size() int { +func (as *Stack) Size() uint { return as.size } +// String 左为Top func (as *Stack) String() string { content := "" cur := as.top @@ -119,7 +125,7 @@ func (as *Stack) Values() []interface{} { return result } -func (as *Stack) Get(idx int) (interface{}, bool) { +func (as *Stack) Index(idx int) (interface{}, bool) { if idx < 0 { return nil, false } diff --git a/lastack/lastack_test.go b/stack/listarraystack/lastack_test.go similarity index 79% rename from lastack/lastack_test.go rename to stack/listarraystack/lastack_test.go index 995c65d..cdf1c5a 100644 --- a/lastack/lastack_test.go +++ b/stack/listarraystack/lastack_test.go @@ -8,7 +8,7 @@ import ( func TestPush(t *testing.T) { var result string - s := New() + s := NewWithCap(10) result = spew.Sprint(s.Values()) if result != "[]" { @@ -67,9 +67,24 @@ func TestPush(t *testing.T) { t.Error(result) } + for i := 0; i < 100; i++ { + s.Push(i) + } + + if v, _ := s.Index(50); v != 49 { + t.Error(v) + } + + for i := 0; i < 50; i++ { + s.Pop() + } + + if v, _ := s.Peek(); v != 49 { + t.Error(s.Peek()) + } } -func TestGet(t *testing.T) { +func TestBase(t *testing.T) { s := New() l := []int{10, 7, 3, 4, 5, 15} @@ -77,7 +92,34 @@ func TestGet(t *testing.T) { s.Push(v) } - if v, isfound := s.Get(0); isfound { + if _, ok := s.Index(-1); ok { + t.Error("not ok") + } + + if s.String() != "15 5 4 3 7 10" { + t.Error(s.String()) + } + + if s.Size() != 6 { + t.Error("Size error, is", s.Size()) + } + + s.Clear() + if !s.Empty() { + t.Error("Size should be Empty, is Clean.") + } + +} + +func TestIndex(t *testing.T) { + s := New() + + l := []int{10, 7, 3, 4, 5, 15} + for _, v := range l { + s.Push(v) + } + + if v, isfound := s.Index(0); isfound { if v != 15 { t.Error("15 is not equal to 15") } @@ -86,7 +128,7 @@ func TestGet(t *testing.T) { } for i, tv := range l { - if v, isfound := s.Get(len(l) - 1 - i); isfound { + if v, isfound := s.Index(len(l) - 1 - i); isfound { if v != tv { t.Error(v, "is not equal to", tv) } @@ -100,7 +142,7 @@ func TestGet(t *testing.T) { l = l[0 : len(l)-1] for i, tv := range l { index := len(l) - 1 - i - if v, isfound := s.Get(index); isfound { + if v, isfound := s.Index(index); isfound { if v != tv { t.Error(v, "is not equal to", tv) } @@ -110,7 +152,7 @@ func TestGet(t *testing.T) { } } -// func BenchmarkGet(b *testing.B) { +// func BenchmarkIndex(b *testing.B) { // s := New() // b.N = 20000000 @@ -123,7 +165,7 @@ func TestGet(t *testing.T) { // b.StartTimer() // for i := 0; i < b.N; i++ { -// s.Get(i) +// s.Index(i) // } // } diff --git a/stack/liststack/stack.go b/stack/liststack/stack.go new file mode 100644 index 0000000..3599527 --- /dev/null +++ b/stack/liststack/stack.go @@ -0,0 +1,102 @@ +package lastack + +import ( + "github.com/474420502/focus/stack" + "github.com/davecgh/go-spew/spew" +) + +type Node struct { + value interface{} + down *Node +} + +type Stack struct { + top *Node + size uint +} + +func assertImplementation() { + var _ stack.IStack = (*Stack)(nil) +} + + +func New() *Stack { + s := &Stack{} + s.size = 0 + return s +} + +func (as *Stack) Clear() { + as.size = 0 + as.top = nil +} + +func (as *Stack) Empty() bool { + return as.size == 0 +} + +func (as *Stack) Size() uint { + return as.size +} + +// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c +func (as *Stack) String() string { + content := "" + cur := as.top + for ; cur != nil; cur = cur.down { + content += spew.Sprint(cur.value) + " " + } + + if len(content) > 0 { + content = content[0 : len(content)-1] + } else { + content = "" + } + + return content +} + +func (as *Stack) Values() []interface{} { + + if as.size == 0 { + return nil + } + + result := make([]interface{}, as.size, as.size) + + cur := as.top + n := 0 + for ; cur != nil; cur = cur.down { + result[n] = cur.value + n++ + } + + return result +} + +func (as *Stack) Push(v interface{}) { + nv := &Node{value: v} + nv.down = as.top + as.top = nv + as.size++ +} + +func (as *Stack) Pop() (interface{}, bool) { + if as.size == 0 { + return nil, false + } + + as.size-- + + result := as.top + as.top = as.top.down + result.down = nil + return result.value, true +} + +func (as *Stack) Peek() (interface{}, bool) { + if as.size == 0 { + return nil, false + } + return as.top.value, true +} diff --git a/stack/stack_test.go b/stack/liststack/stack_test.go similarity index 100% rename from stack/stack_test.go rename to stack/liststack/stack_test.go diff --git a/stack/stack.go b/stack/stack.go index df7c57b..ff772b1 100644 --- a/stack/stack.go +++ b/stack/stack.go @@ -1,96 +1,20 @@ -package lastack +package stack -import ( - "github.com/davecgh/go-spew/spew" -) +type IStack interface { + Clear() -type Node struct { - value interface{} - down *Node -} - -type Stack struct { - top *Node - size int -} - -func New() *Stack { - s := &Stack{} - s.size = 0 - return s -} - -func (as *Stack) Clear() { - as.size = 0 - as.top = nil -} - -func (as *Stack) Empty() bool { - return as.size == 0 -} - -func (as *Stack) Size() int { - return as.size -} - -// String 从左到右 左边第一个表示Top 如链表 a(top)->b->c -func (as *Stack) String() string { - content := "" - cur := as.top - for ; cur != nil; cur = cur.down { - content += spew.Sprint(cur.value) + " " - } - - if len(content) > 0 { - content = content[0 : len(content)-1] - } else { - content = "" - } - - return content -} - -func (as *Stack) Values() []interface{} { - - if as.size == 0 { - return nil - } - - result := make([]interface{}, as.size, as.size) - - cur := as.top - n := 0 - for ; cur != nil; cur = cur.down { - result[n] = cur.value - n++ - } - - return result -} - -func (as *Stack) Push(v interface{}) { - nv := &Node{value: v} - nv.down = as.top - as.top = nv - as.size++ -} - -func (as *Stack) Pop() (interface{}, bool) { - if as.size <= 0 { - return nil, false - } - - as.size-- - - result := as.top - as.top = as.top.down - result.down = nil - return result.value, true -} - -func (as *Stack) Peek() (interface{}, bool) { - if as.size <= 0 { - return nil, false - } - return as.top.value, true + Empty() bool + + Size() uint + + // String 从左到右 左边第一个表示Top 如链表 a(top)->b->c + String() string + + Values() []interface{} + + Push(v interface{}) + + Pop() (interface{}, bool) + + Peek() (interface{}, bool) } diff --git a/tree/avl/iterator.go b/tree/avl/iterator.go index 5884f1d..926c60a 100644 --- a/tree/avl/iterator.go +++ b/tree/avl/iterator.go @@ -1,7 +1,7 @@ package avl import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/tree/avldup/iterator.go b/tree/avldup/iterator.go index 1ec0716..d802dce 100644 --- a/tree/avldup/iterator.go +++ b/tree/avldup/iterator.go @@ -1,7 +1,7 @@ package avldup import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/tree/avlkey/iterator.go b/tree/avlkey/iterator.go index ac6306b..3110331 100644 --- a/tree/avlkey/iterator.go +++ b/tree/avlkey/iterator.go @@ -1,7 +1,7 @@ package avlkey import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/tree/avlkeydup/iterator.go b/tree/avlkeydup/iterator.go index 6511ee2..a95658e 100644 --- a/tree/avlkeydup/iterator.go +++ b/tree/avlkeydup/iterator.go @@ -1,7 +1,7 @@ package avlkeydup import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/tree/vbt/iterator.go b/tree/vbt/iterator.go index 3afdf0a..776238b 100644 --- a/tree/vbt/iterator.go +++ b/tree/vbt/iterator.go @@ -1,7 +1,7 @@ package vbt import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct { diff --git a/tree/vbtkey/iterator.go b/tree/vbtkey/iterator.go index 1cd3d31..a2f5875 100644 --- a/tree/vbtkey/iterator.go +++ b/tree/vbtkey/iterator.go @@ -1,7 +1,7 @@ package vbtkey import ( - "github.com/474420502/focus/lastack" + "github.com/474420502/focus/stack/listarraystack" ) type Iterator struct {