- add reversible iterators to binary heap

pull/18/head
Emir Pasic 9 years ago
parent b304f5eb58
commit ae143689c6

@ -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 := New()
stack.Push("a") stack.Push("a")
stack.Push("b") stack.Push("b")
stack.Push("c") stack.Push("c")
// Iterator (next)
it := stack.Iterator() it := stack.Iterator()
count := 0 count := 0
for it.Next() { for it.Next() {
@ -128,9 +135,18 @@ func TestStackIterator(t *testing.T) {
if actualValue, expectedValue := count, 3; actualValue != expectedValue { if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue) t.Errorf("Got %v expected %v", actualValue, expectedValue)
} }
}
// Iterator (prev) func TestStackIteratorPrev(t *testing.T) {
count = 0 stack := New()
stack.Push("a")
stack.Push("b")
stack.Push("c")
it := stack.Iterator()
for it.Next() {
}
count := 0
for it.Prev() { for it.Prev() {
count++ count++
index := it.Index() index := it.Index()
@ -158,12 +174,6 @@ func TestStackIterator(t *testing.T) {
if actualValue, expectedValue := count, 3; actualValue != expectedValue { if actualValue, expectedValue := count, 3; actualValue != expectedValue {
t.Errorf("Got %v expected %v", 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) { func BenchmarkStack(b *testing.B) {

@ -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(). // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator. // Modifies the state of the iterator.
func (iterator *Iterator) Next() bool { func (iterator *Iterator) Next() bool {
iterator.index++ if iterator.index < iterator.stack.Size() {
iterator.index++
}
return iterator.stack.withinRange(iterator.index) return iterator.stack.withinRange(iterator.index)
} }

@ -44,7 +44,7 @@ import (
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ trees.Tree = (*Heap)(nil) var _ trees.Tree = (*Heap)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil) var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
} }
// Heap holds elements in an array-list // 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(). // If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// Modifies the state of the iterator. // Modifies the state of the iterator.
func (iterator *Iterator) Next() bool { 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) return iterator.heap.withinRange(iterator.index)
} }

@ -107,19 +107,24 @@ func TestBinaryHeapRandom(t *testing.T) {
} }
} }
func TestBinaryHeapIterator(t *testing.T) { func TestBinaryHeapIteratorOnEmpty(t *testing.T) {
heap := NewWithIntComparator() heap := NewWithIntComparator()
it := heap.Iterator()
if actualValue := heap.Empty(); actualValue != true { for it.Next() {
t.Errorf("Got %v expected %v", actualValue, true) t.Errorf("Shouldn't iterate on empty heap")
} }
}
func TestBinaryHeapIteratorNext(t *testing.T) {
heap := NewWithIntComparator()
heap.Push(3) // [3] heap.Push(3) // [3]
heap.Push(2) // [2,3] heap.Push(2) // [2,3]
heap.Push(1) // [1,3,2](2 swapped with 1, hence last) heap.Push(1) // [1,3,2](2 swapped with 1, hence last)
it := heap.Iterator() it := heap.Iterator()
count := 0
for it.Next() { for it.Next() {
count++
index := it.Index() index := it.Index()
value := it.Value() value := it.Value()
switch index { switch index {
@ -138,12 +143,51 @@ func TestBinaryHeapIterator(t *testing.T) {
default: default:
t.Errorf("Too many") 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() func TestBinaryHeapIteratorPrev(t *testing.T) {
it = heap.Iterator() 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() { 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)
} }
} }

Loading…
Cancel
Save