diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index 98af62c..fad4ac0 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -92,13 +92,20 @@ func TestStackPop(t *testing.T) { } } -func TestStackIterator(t *testing.T) { +func TestStackIteratorOnEmpty(t *testing.T) { + stack := New() + it := stack.Iterator() + for it.Next() { + t.Errorf("Shouldn't iterate on empty stack") + } +} + +func TestStackIteratorNext(t *testing.T) { stack := New() stack.Push("a") stack.Push("b") stack.Push("c") - // Iterator (next) it := stack.Iterator() count := 0 for it.Next() { @@ -128,9 +135,18 @@ func TestStackIterator(t *testing.T) { if actualValue, expectedValue := count, 3; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } +} - // Iterator (prev) - count = 0 +func TestStackIteratorPrev(t *testing.T) { + stack := New() + stack.Push("a") + stack.Push("b") + stack.Push("c") + + it := stack.Iterator() + for it.Next() { + } + count := 0 for it.Prev() { count++ index := it.Index() @@ -158,12 +174,6 @@ func TestStackIterator(t *testing.T) { if actualValue, expectedValue := count, 3; actualValue != expectedValue { t.Errorf("Got %v expected %v", actualValue, expectedValue) } - - stack.Clear() - it = stack.Iterator() - for it.Next() { - t.Errorf("Shouldn't iterate on empty stack") - } } func BenchmarkStack(b *testing.B) { diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index 858033e..75f1948 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -108,7 +108,9 @@ func (stack *Stack) Iterator() Iterator { // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) Next() bool { - iterator.index++ + if iterator.index < iterator.stack.Size() { + iterator.index++ + } return iterator.stack.withinRange(iterator.index) } diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 2bad245..0e5942f 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -44,7 +44,7 @@ import ( func assertInterfaceImplementation() { var _ trees.Tree = (*Heap)(nil) - var _ containers.IteratorWithIndex = (*Iterator)(nil) + var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil) } // Heap holds elements in an array-list @@ -129,7 +129,19 @@ func (heap *Heap) Iterator() Iterator { // If Next() returns true, then next element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) Next() bool { - iterator.index++ + if iterator.index < iterator.heap.Size() { + iterator.index++ + } + return iterator.heap.withinRange(iterator.index) +} + +// Prev moves the iterator to the previous element and returns true if there was a previous element in the container. +// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Prev() bool { + if iterator.index >= 0 { + iterator.index-- + } return iterator.heap.withinRange(iterator.index) } diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index 7699634..d72375a 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -107,19 +107,24 @@ func TestBinaryHeapRandom(t *testing.T) { } } -func TestBinaryHeapIterator(t *testing.T) { +func TestBinaryHeapIteratorOnEmpty(t *testing.T) { heap := NewWithIntComparator() - - if actualValue := heap.Empty(); actualValue != true { - t.Errorf("Got %v expected %v", actualValue, true) + it := heap.Iterator() + for it.Next() { + t.Errorf("Shouldn't iterate on empty heap") } +} +func TestBinaryHeapIteratorNext(t *testing.T) { + heap := NewWithIntComparator() heap.Push(3) // [3] heap.Push(2) // [2,3] heap.Push(1) // [1,3,2](2 swapped with 1, hence last) it := heap.Iterator() + count := 0 for it.Next() { + count++ index := it.Index() value := it.Value() switch index { @@ -138,12 +143,51 @@ func TestBinaryHeapIterator(t *testing.T) { default: t.Errorf("Too many") } + if actualValue, expectedValue := index, count-1; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } } + if actualValue, expectedValue := count, 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } +} - heap.Clear() - it = heap.Iterator() +func TestBinaryHeapIteratorPrev(t *testing.T) { + heap := NewWithIntComparator() + heap.Push(3) // [3] + heap.Push(2) // [2,3] + heap.Push(1) // [1,3,2](2 swapped with 1, hence last) + + it := heap.Iterator() for it.Next() { - t.Errorf("Shouldn't iterate on empty stack") + } + count := 0 + for it.Prev() { + count++ + index := it.Index() + value := it.Value() + switch index { + case 0: + if actualValue, expectedValue := value, 1; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + case 1: + if actualValue, expectedValue := value, 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + case 2: + if actualValue, expectedValue := value, 2; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + default: + t.Errorf("Too many") + } + if actualValue, expectedValue := index, 3-count; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) + } + } + if actualValue, expectedValue := count, 3; actualValue != expectedValue { + t.Errorf("Got %v expected %v", actualValue, expectedValue) } }