mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-15 16:16:35 +03:00
- add reversible iterators to array stack
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user