From f8b0747409454b9fb4a24c087758f94da04fb775 Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Sun, 26 Jun 2016 22:27:08 +0200 Subject: [PATCH] - iterator last on all structures with reversible iterators --- containers/iterator.go | 20 ++++++++++++++++++-- lists/arraylist/arraylist.go | 8 ++++++++ lists/doublylinkedlist/doublylinkedlist.go | 9 +++++++++ maps/treemap/treemap.go | 7 +++++++ sets/treeset/treeset.go | 8 ++++++++ stacks/arraystack/arraystack.go | 8 ++++++++ trees/binaryheap/binaryheap.go | 8 ++++++++ trees/redblacktree/redblacktree.go | 8 ++++++++ 8 files changed, 74 insertions(+), 2 deletions(-) diff --git a/containers/iterator.go b/containers/iterator.go index 8c08ba1..9251550 100644 --- a/containers/iterator.go +++ b/containers/iterator.go @@ -64,12 +64,20 @@ type IteratorWithKey interface { // ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index. // -// Essentially it is the same as IteratorWithIndex, but provides additional Prev() function to enable traversal in reverse. +// Essentially it is the same as IteratorWithIndex, but provides additional: +// +// Prev() function to enable traversal in reverse +// +// Last() function to move the iterator to the last element. type ReverseIteratorWithIndex interface { // 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. Prev() bool + // Last moves the iterator to the last element and returns true if there was a last element in the container. + // If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). + // Modifies the state of the iterator. + Last() bool IteratorWithIndex // Next() bool @@ -79,12 +87,20 @@ type ReverseIteratorWithIndex interface { // ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs. // -// Essentially it is the same as IteratorWithKey, but provides additional Prev() function to enable traversal in reverse. +// Essentially it is the same as IteratorWithKey, but provides additional: +// +// Prev() function to enable traversal in reverse +// +// Last() function to move the iterator to the last element. type ReverseIteratorWithKey interface { // 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 Key() and Value(). // Modifies the state of the iterator. Prev() bool + // Last moves the iterator to the last element and returns true if there was a last element in the container. + // If Last() returns true, then last element's index and value can be retrieved by Key() and Value(). + // Modifies the state of the iterator. + Last() bool IteratorWithKey // Next() bool diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index 909311b..17d84d7 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -230,6 +230,14 @@ func (iterator *Iterator) Reset() { iterator.index = -1 } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.index = iterator.list.size + return iterator.Prev() +} + // 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 c5be4cf..7ef4047 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -370,6 +370,15 @@ func (iterator *Iterator) Reset() { iterator.element = nil } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.index = iterator.list.size + iterator.element = iterator.list.last + return iterator.Prev() +} + // 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 1b0f313..424cae6 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -171,6 +171,13 @@ func (iterator *Iterator) Reset() { iterator.iterator.Reset() } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Key() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + return iterator.iterator.Last() +} + // 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 43a1717..46feceb 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -158,6 +158,14 @@ func (iterator *Iterator) Reset() { iterator.iterator.Reset() } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.index = iterator.tree.Size() + return iterator.iterator.Last() +} + // 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 07fa561..ea65bdd 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -149,6 +149,14 @@ func (iterator *Iterator) Reset() { iterator.index = -1 } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.index = iterator.stack.Size() + return iterator.Prev() +} + // String returns a string representation of container func (stack *Stack) String() string { str := "ArrayStack\n" diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index a98f65e..e7819da 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -165,6 +165,14 @@ func (iterator *Iterator) Reset() { iterator.index = -1 } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Index() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.index = iterator.heap.Size() + return iterator.Prev() +} + // 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 01d22b4..b1ab99b 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -363,6 +363,14 @@ func (iterator *Iterator) Reset() { iterator.node = nil } +// Last moves the iterator to the last element and returns true if there was a last element in the container. +// If Last() returns true, then last element's index and value can be retrieved by Key() and Value(). +// Modifies the state of the iterator. +func (iterator *Iterator) Last() bool { + iterator.node = iterator.tree.Right() + return iterator.node != nil +} + // String returns a string representation of container func (tree *Tree) String() string { str := "RedBlackTree\n"