From b86d413e66d49930536535e5d3e43ea45c84e0ee Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Sun, 26 Jun 2016 20:50:49 +0200 Subject: [PATCH] - iterator reset on all structures --- containers/iterator.go | 8 ++++++++ lists/arraylist/arraylist.go | 7 +++++++ lists/doublylinkedlist/doublylinkedlist.go | 8 ++++++++ lists/singlylinkedlist/singlylinkedlist.go | 8 ++++++++ maps/treemap/treemap.go | 7 +++++++ sets/treeset/treeset.go | 7 +++++++ stacks/arraystack/arraystack.go | 7 +++++++ stacks/linkedliststack/linkedliststack.go | 7 +++++++ trees/binaryheap/binaryheap.go | 7 +++++++ trees/redblacktree/redblacktree.go | 7 +++++++ 10 files changed, 73 insertions(+) diff --git a/containers/iterator.go b/containers/iterator.go index c1a7c54..8c08ba1 100644 --- a/containers/iterator.go +++ b/containers/iterator.go @@ -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. diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index c08d928..909311b 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -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() diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index cb7bdfc..c5be4cf 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -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() diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index f65f728..4a0eb1e 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -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() diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index f1640bf..1b0f313 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -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() diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index 3a0de08..9a590ff 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -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() diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index ece76a9..07fa561 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -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" diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index 75f1948..f8cd31d 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -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" diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 0e5942f..a98f65e 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -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" diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index cf68bab..01d22b4 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -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"