- iterator reset on all structures

pull/20/head
Emir Pasic 8 years ago
parent d5a7c62629
commit b86d413e66

@ -30,6 +30,7 @@ package containers
type IteratorWithIndex interface {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
Next() bool
// Value returns the current element's value.
@ -38,12 +39,16 @@ type IteratorWithIndex interface {
// Index returns the current element's index.
// Does not modify the state of the iterator.
Index() int
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
Reset()
}
// IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
type IteratorWithKey interface {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
Next() bool
// Value returns the current element's value.
@ -52,6 +57,9 @@ type IteratorWithKey interface {
// Key returns the current element's key.
// Does not modify the state of the iterator.
Key() interface{}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
Reset()
}
// ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.

@ -193,6 +193,7 @@ func (list *List) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.list.size {
@ -223,6 +224,12 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
}
// Each calls the given function once for each element, passing that element's index and value.
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()

@ -314,6 +314,7 @@ func (list *List) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.list.size {
@ -362,6 +363,13 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
iterator.element = nil
}
// Each calls the given function once for each element, passing that element's index and value.
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()

@ -286,6 +286,7 @@ func (list *List) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.list.size {
@ -315,6 +316,13 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
iterator.element = nil
}
// Each calls the given function once for each element, passing that element's index and value.
func (list *List) Each(f func(index int, value interface{})) {
iterator := list.Iterator()

@ -140,6 +140,7 @@ func (m *Map) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
return iterator.iterator.Next()
@ -164,6 +165,12 @@ func (iterator *Iterator) Key() interface{} {
return iterator.iterator.Key()
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.iterator.Reset()
}
// Each calls the given function once for each element, passing that element's key and value.
func (m *Map) Each(f func(key interface{}, value interface{})) {
iterator := m.Iterator()

@ -120,6 +120,7 @@ func (set *Set) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.tree.Size() {
@ -150,6 +151,12 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.iterator.Reset()
}
// Each calls the given function once for each element, passing that element's index and value.
func (set *Set) Each(f func(index int, value interface{})) {
iterator := set.Iterator()

@ -111,6 +111,7 @@ func (stack *Stack) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.stack.Size() {
@ -142,6 +143,12 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
}
// String returns a string representation of container
func (stack *Stack) String() string {
str := "ArrayStack\n"

@ -106,6 +106,7 @@ func (stack *Stack) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.stack.Size() {
@ -127,6 +128,12 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
}
// String returns a string representation of container
func (stack *Stack) String() string {
str := "LinkedListStack\n"

@ -127,6 +127,7 @@ func (heap *Heap) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.index < iterator.heap.Size() {
@ -158,6 +159,12 @@ func (iterator *Iterator) Index() int {
return iterator.index
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.index = -1
}
// String returns a string representation of container
func (heap *Heap) String() string {
str := "BinaryHeap\n"

@ -291,6 +291,7 @@ func (tree *Tree) Iterator() Iterator {
// Next moves the iterator to the next element and returns true if there was a next element in the container.
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
// Modifies the state of the iterator.
func (iterator *Iterator) Next() bool {
if iterator.node == nil {
@ -356,6 +357,12 @@ func (iterator *Iterator) Key() interface{} {
return iterator.node.Key
}
// Reset sets the iterator to the initial state.
// Call Next() to fetch the first element if any.
func (iterator *Iterator) Reset() {
iterator.node = nil
}
// String returns a string representation of container
func (tree *Tree) String() string {
str := "RedBlackTree\n"

Loading…
Cancel
Save