From 8e5760a16e7ff1bda62d57ea13d0923254900df3 Mon Sep 17 00:00:00 2001 From: emirpasic Date: Sat, 7 Mar 2015 18:19:58 +0100 Subject: [PATCH] - make ArrayStack make use of our ArrayList - add Values() method to stacks interface - write tests --- lists/arraylist/arraylist.go | 1 - stacks/arraystack/arraystack.go | 12 +++++++----- stacks/arraystack/arraystack_test.go | 9 ++++----- stacks/linkedliststack/linkedliststack.go | 5 ++--- stacks/linkedliststack/linkedliststack_test.go | 9 ++++----- utils/slices.go | 14 -------------- 6 files changed, 17 insertions(+), 33 deletions(-) delete mode 100644 utils/slices.go diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index 3e3f13f..e994022 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -82,7 +82,6 @@ func (list *List) Remove(index int) { return } - list.elements[index] = nil // cleanup reference copy(list.elements[index:], list.elements[index+1:list.size]) // shift to the left by one (slow operation, need ways to optimize this) list.size -= 1 diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index 4e0599a..bb3a89d 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -58,7 +58,9 @@ func (stack *Stack) Push(value interface{}) { // Pops (removes) top element on stack and returns it, or nil if stack is empty. // Second return parameter is true, unless the stack was empty and there was nothing to pop. func (stack *Stack) Pop() (value interface{}, ok bool) { - return stack.list.Get(stack.list.Size() - 1) + value, ok = stack.list.Get(stack.list.Size() - 1) + stack.list.Remove(stack.list.Size() - 1) + return } // Returns top element on the stack without removing it, or nil if stack is empty. @@ -79,15 +81,15 @@ func (stack *Stack) Size() int { // Removes all elements from the stack. func (stack *Stack) Clear() { - return stack.list.Clear() + stack.list.Clear() } // Returns all elements in the stack (LIFO order). func (stack *Stack) Values() []interface{} { size := stack.list.Size() elements := make([]interface{}, size, size) - for i := size - 1; i >= 0; i-- { // in reverse - elements[i] = stack.list.Get(i) + for i := 1; i <= size; i++ { + elements[size-i], _ = stack.list.Get(i - 1) // in reverse (LIFO) } return elements } @@ -95,7 +97,7 @@ func (stack *Stack) Values() []interface{} { func (stack *Stack) String() string { str := "ArrayStack\n" values := []string{} - for _, value := range stack.elements { + for _, value := range stack.list.Values() { values = append(values, fmt.Sprintf("%v", value)) } str += strings.Join(values, ", ") diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index e6358ba..62836db 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -27,7 +27,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package arraystack import ( - "github.com/emirpasic/gods/utils" "testing" ) @@ -44,8 +43,8 @@ func TestArrayStack(t *testing.T) { stack.Push(2) stack.Push(3) - if actualValue := stack.Values(); !utils.IdenticalSlices(actualValue, []interface{}{3, 2, 1}) { - t.Errorf("Got %v expected %v", actualValue, []interface{}{3, 2, 1}) + if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 { + t.Errorf("Got %v expected %v", actualValue, "[3,2,1]") } if actualValue := stack.Empty(); actualValue != false { @@ -82,8 +81,8 @@ func TestArrayStack(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := stack.Values(); !utils.IdenticalSlices(actualValue, []interface{}{}) { - t.Errorf("Got %v expected %v", actualValue, []interface{}{}) + if actualValue := stack.Values(); len(actualValue) != 0 { + t.Errorf("Got %v expected %v", actualValue, "[]") } } diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index 41d64ca..0fcc49d 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -100,11 +100,10 @@ func (stack *Stack) Clear() { // Returns all elements in the stack (LIFO order). func (stack *Stack) Values() []interface{} { - size := stack.list.Size() - elements := make([]interface{}, size, size) + elements := make([]interface{}, stack.size, stack.size) element := stack.top for index := 0; element != nil; index++ { - elements[index] = element + elements[index] = element.value element = element.next } diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index b2fca60..dea7ba0 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -27,7 +27,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package linkedliststack import ( - "github.com/emirpasic/gods/utils" "testing" ) @@ -44,8 +43,8 @@ func TestLinkedListStack(t *testing.T) { stack.Push(2) stack.Push(3) - if actualValue := stack.Values(); !utils.IdenticalSlices(actualValue, []interface{}{3, 2, 1}) { - t.Errorf("Got %v expected %v", actualValue, []interface{}{3, 2, 1}) + if actualValue := stack.Values(); actualValue[0].(int) != 3 || actualValue[1].(int) != 2 || actualValue[2].(int) != 1 { + t.Errorf("Got %v expected %v", actualValue, "[3,2,1]") } if actualValue := stack.Empty(); actualValue != false { @@ -82,8 +81,8 @@ func TestLinkedListStack(t *testing.T) { t.Errorf("Got %v expected %v", actualValue, true) } - if actualValue := stack.Values(); !utils.IdenticalSlices(actualValue, []interface{}{}) { - t.Errorf("Got %v expected %v", actualValue, []interface{}{}) + if actualValue := stack.Values(); len(actualValue) != 0 { + t.Errorf("Got %v expected %v", actualValue, "[]") } } diff --git a/utils/slices.go b/utils/slices.go deleted file mode 100644 index 1009f7c..0000000 --- a/utils/slices.go +++ /dev/null @@ -1,14 +0,0 @@ -package utils - -// order and elements in the two slices have to match -func IdenticalSlices(s1 []interface{}, s2 []interface{}) bool { - if len(s1) != len(s2) { - return false - } - for i := 0; i < len(s1); i++ { - if s1[i] != s2[i] { - return false - } - } - return true -}