- add reversible iterators to array stack

This commit is contained in:
Emir Pasic
2016-06-25 19:11:20 +02:00
parent d7a31571cc
commit b304f5eb58
6 changed files with 85 additions and 9 deletions
+14 -2
View File
@@ -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)
}