- add reversible iterators to array stack

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

@ -56,16 +56,16 @@ Containers are either ordered or unordered. All ordered containers provide [stat
| Container | Ordered | [Iterator](#iterator) | [Enumerable](#enumerable) | Ordered by | | Container | Ordered | [Iterator](#iterator) | [Enumerable](#enumerable) | Ordered by |
| :--- | :---: | :---: | :---: | :---: | | :--- | :---: | :---: | :---: | :---: |
| [ArrayList](#arraylist) | yes | yes* | yes | index | | [ArrayList](#arraylist) | yes | yes* | yes | index |
| [SinglyLinkedList](#singlylinkedlist) | yes | yes* | yes | index | | [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
| [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | index | | [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | index |
| [HashSet](#hashset) | no | no | no | index | | [HashSet](#hashset) | no | no | no | index |
| [TreeSet](#treeset) | yes | yes | yes | index | | [TreeSet](#treeset) | yes | yes* | yes | index |
| [LinkedListStack](#linkedliststack) | yes | yes | no | index | | [LinkedListStack](#linkedliststack) | yes | yes | no | index |
| [ArrayStack](#arraystack) | yes | yes | no | index | | [ArrayStack](#arraystack) | yes | yes* | no | index |
| [HashMap](#hashmap) | no | no | no | key | | [HashMap](#hashmap) | no | no | no | key |
| [TreeMap](#treemap) | yes | yes | yes | key | | [TreeMap](#treemap) | yes | yes* | yes | key |
| [RedBlackTree](#redblacktree) | yes | yes | no | key | | [RedBlackTree](#redblacktree) | yes | yes* | no | key |
| [BinaryHeap](#binaryheap) | yes | yes | no | index | | [BinaryHeap](#binaryheap) | yes | yes* | no | index |
| | | <sub><sup>*reversible</sup></sub> | | | | | | <sub><sup>*reversible</sup></sub> | | |
### Lists ### Lists

@ -318,7 +318,9 @@ func TestMapIterator(t *testing.T) {
m.Put("b", 2) m.Put("b", 2)
it := m.Iterator() it := m.Iterator()
count := 0
for it.Next() { for it.Next() {
count++
key := it.Key() key := it.Key()
value := it.Value() value := it.Value()
switch key { switch key {
@ -337,6 +339,12 @@ func TestMapIterator(t *testing.T) {
default: default:
t.Errorf("Too many") t.Errorf("Too many")
} }
if actualValue, expectedValue := value, 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)
} }
m.Clear() m.Clear()

@ -197,7 +197,9 @@ func TestSetIterator(t *testing.T) {
set.Add("c", "a", "b") set.Add("c", "a", "b")
it := set.Iterator() it := set.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 {
@ -216,6 +218,12 @@ func TestSetIterator(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)
} }
set.Clear() set.Clear()

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() { func assertInterfaceImplementation() {
var _ stacks.Stack = (*Stack)(nil) var _ stacks.Stack = (*Stack)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil) var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
} }
// Stack holds elements in an array-list // Stack holds elements in an array-list
@ -113,7 +113,19 @@ 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)
}
// 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.stack.withinRange(iterator.index) return iterator.stack.withinRange(iterator.index)
} }

@ -98,9 +98,11 @@ func TestStackIterator(t *testing.T) {
stack.Push("b") stack.Push("b")
stack.Push("c") stack.Push("c")
// Iterator // Iterator (next)
it := stack.Iterator() it := stack.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 {
@ -119,7 +121,44 @@ func TestStackIterator(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)
} }
// Iterator (prev)
count = 0
for it.Prev() {
count++
index := it.Index()
value := it.Value()
switch index {
case 0:
if actualValue, expectedValue := value, "c"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 1:
if actualValue, expectedValue := value, "b"; actualValue != expectedValue {
t.Errorf("Got %v expected %v", actualValue, expectedValue)
}
case 2:
if actualValue, expectedValue := value, "a"; 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)
}
stack.Clear() stack.Clear()
it = stack.Iterator() it = stack.Iterator()
for it.Next() { for it.Next() {

@ -100,7 +100,9 @@ func TestStackIterator(t *testing.T) {
// Iterator // Iterator
it := stack.Iterator() it := stack.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 {
@ -119,7 +121,14 @@ func TestStackIterator(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)
} }
stack.Clear() stack.Clear()
it = stack.Iterator() it = stack.Iterator()
for it.Next() { for it.Next() {

Loading…
Cancel
Save