- add reversible iterators to array stack

pull/18/head
Emir Pasic 8 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 |
| :--- | :---: | :---: | :---: | :---: |
| [ArrayList](#arraylist) | yes | yes* | yes | index |
| [SinglyLinkedList](#singlylinkedlist) | yes | yes* | yes | index |
| [SinglyLinkedList](#singlylinkedlist) | yes | yes | yes | index |
| [DoublyLinkedList](#doublylinkedlist) | yes | yes* | yes | 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 |
| [ArrayStack](#arraystack) | yes | yes | no | index |
| [ArrayStack](#arraystack) | yes | yes* | no | index |
| [HashMap](#hashmap) | no | no | no | key |
| [TreeMap](#treemap) | yes | yes | yes | key |
| [RedBlackTree](#redblacktree) | yes | yes | no | key |
| [BinaryHeap](#binaryheap) | yes | yes | no | index |
| [TreeMap](#treemap) | yes | yes* | yes | key |
| [RedBlackTree](#redblacktree) | yes | yes* | no | key |
| [BinaryHeap](#binaryheap) | yes | yes* | no | index |
| | | <sub><sup>*reversible</sup></sub> | | |
### Lists

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

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

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ stacks.Stack = (*Stack)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
var _ containers.ReverseIteratorWithIndex = (*Iterator)(nil)
}
// 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().
// 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)
}
// 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)
}

@ -98,9 +98,11 @@ func TestStackIterator(t *testing.T) {
stack.Push("b")
stack.Push("c")
// Iterator
// Iterator (next)
it := stack.Iterator()
count := 0
for it.Next() {
count++
index := it.Index()
value := it.Value()
switch index {
@ -119,7 +121,44 @@ func TestStackIterator(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)
}
// 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()
it = stack.Iterator()
for it.Next() {

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

Loading…
Cancel
Save