From 6b1d2a80e9bbe10ee4fdb2a41fc068f5dad16d6d Mon Sep 17 00:00:00 2001 From: huangsimin Date: Wed, 13 Mar 2019 13:57:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E6=B7=BB=E5=8A=A0=E5=88=B0go?= =?UTF-8?q?path?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lastack/lastack_test.go | 52 ++++++++++++----------- stack/stack.go | 90 ++++++++++++++++++++++++++++++++++++++++ stack/stack_test.go | 91 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+), 25 deletions(-) create mode 100644 stack/stack.go create mode 100644 stack/stack_test.go diff --git a/lastack/lastack_test.go b/lastack/lastack_test.go index f412438..34d92e7 100644 --- a/lastack/lastack_test.go +++ b/lastack/lastack_test.go @@ -8,34 +8,34 @@ import ( "github.com/Pallinder/go-randomdata" ) -func TestPush(t *testing.T) { - s := New() - for i := 0; i < 10; i++ { - v := randomdata.Number(0, 10) - s.Push(v) - t.Error(v) - t.Error(s.String(), " size ", s.size) - } +// func TestPush(t *testing.T) { +// s := New() +// for i := 0; i < 10; i++ { +// v := randomdata.Number(0, 10) +// s.Push(v) +// t.Error(v) +// t.Error(s.String(), " size ", s.size) +// } - t.Error(s.Values()) +// t.Error(s.Values()) - for i := 0; i < 10; i++ { - v, ok := s.Pop() - t.Error(v, ok) - t.Error(s.String(), " size ", s.size) - } +// for i := 0; i < 10; i++ { +// v, ok := s.Pop() +// t.Error(v, ok) +// t.Error(s.String(), " size ", s.size) +// } - for i := 0; i < 10; i++ { - v := randomdata.Number(0, 10) - s.Push(v) - } +// for i := 0; i < 10; i++ { +// v := randomdata.Number(0, 10) +// s.Push(v) +// } - for i := -1; i < 11; i++ { - v, ok := s.Get(i) - t.Error(v, ok) - } +// for i := -1; i < 11; i++ { +// v, ok := s.Get(i) +// t.Error(v, ok) +// } -} +// } func BenchmarkGet(b *testing.B) { s := New() @@ -56,6 +56,7 @@ func BenchmarkGet(b *testing.B) { func BenchmarkPush(b *testing.B) { s := New() + b.N = 200000 for i := 0; i < b.N; i++ { v := randomdata.Number(0, 65535) s.Push(v) @@ -64,6 +65,7 @@ func BenchmarkPush(b *testing.B) { func BenchmarkGodsPush(b *testing.B) { s := arraystack.New() + b.N = 200000 for i := 0; i < b.N; i++ { v := randomdata.Number(0, 65535) s.Push(v) @@ -72,7 +74,7 @@ func BenchmarkGodsPush(b *testing.B) { func BenchmarkPop(b *testing.B) { s := New() - b.N = 20000000 + b.N = 200000 for i := 0; i < b.N; i++ { v := randomdata.Number(0, 65535) @@ -89,7 +91,7 @@ func BenchmarkPop(b *testing.B) { func BenchmarkGodsPop(b *testing.B) { s := arraystack.New() - b.N = 20000000 + b.N = 200 for i := 0; i < b.N; i++ { v := randomdata.Number(0, 65535) diff --git a/stack/stack.go b/stack/stack.go new file mode 100644 index 0000000..1972a3c --- /dev/null +++ b/stack/stack.go @@ -0,0 +1,90 @@ +package lastack + +import ( + "github.com/davecgh/go-spew/spew" +) + +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 +} + +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{} { + 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/stack_test.go new file mode 100644 index 0000000..ea6bea9 --- /dev/null +++ b/stack/stack_test.go @@ -0,0 +1,91 @@ +package lastack + +import ( + "testing" + + "github.com/emirpasic/gods/stacks/arraystack" + + "github.com/Pallinder/go-randomdata" +) + +func BenchmarkPush(b *testing.B) { + s := New() + b.N = 200000 + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } +} + +func BenchmarkGodsPush(b *testing.B) { + s := arraystack.New() + b.N = 200000 + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } +} + +func BenchmarkPop(b *testing.B) { + s := New() + b.N = 200 + + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } + + b.ResetTimer() + b.StartTimer() + + for i := 0; i < b.N; i++ { + s.Pop() + } +} + +func BenchmarkGodsPop(b *testing.B) { + s := arraystack.New() + b.N = 200 + + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } + + b.ResetTimer() + b.StartTimer() + + for i := 0; i < b.N; i++ { + s.Pop() + } +} + +func BenchmarkValues(b *testing.B) { + s := New() + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } + + b.ResetTimer() + b.StartTimer() + + for i := 0; i < b.N; i++ { + s.Values() + } +} + +func BenchmarkGodsValues(b *testing.B) { + s := arraystack.New() + for i := 0; i < b.N; i++ { + v := randomdata.Number(0, 65535) + s.Push(v) + } + + b.ResetTimer() + b.StartTimer() + + for i := 0; i < b.N; i++ { + s.Values() + } +}