From 57162feff5d4a7f343b131008eaab85e8732b74b Mon Sep 17 00:00:00 2001 From: Emir Pasic Date: Mon, 27 Jun 2016 00:08:01 +0200 Subject: [PATCH] - rename Reset() to Begin() in iterators (this will allow End() which will make reverse loops more readable) --- containers/iterator.go | 18 ++++++++++++++---- lists/arraylist/arraylist.go | 6 +++--- lists/arraylist/arraylist_test.go | 6 +++--- lists/doublylinkedlist/doublylinkedlist.go | 6 +++--- .../doublylinkedlist/doublylinkedlist_test.go | 6 +++--- lists/singlylinkedlist/singlylinkedlist.go | 6 +++--- .../singlylinkedlist/singlylinkedlist_test.go | 6 +++--- maps/treemap/treemap.go | 8 ++++---- maps/treemap/treemap_test.go | 6 +++--- sets/treeset/treeset.go | 8 ++++---- sets/treeset/treeset_test.go | 6 +++--- stacks/arraystack/arraystack.go | 6 +++--- stacks/arraystack/arraystack_test.go | 6 +++--- stacks/linkedliststack/linkedliststack.go | 6 +++--- stacks/linkedliststack/linkedliststack_test.go | 6 +++--- trees/binaryheap/binaryheap.go | 6 +++--- trees/binaryheap/binaryheap_test.go | 6 +++--- trees/redblacktree/redblacktree.go | 6 +++--- trees/redblacktree/redblacktree_test.go | 6 +++--- 19 files changed, 70 insertions(+), 60 deletions(-) diff --git a/containers/iterator.go b/containers/iterator.go index 144151e..1bd7b0e 100644 --- a/containers/iterator.go +++ b/containers/iterator.go @@ -33,15 +33,19 @@ type IteratorWithIndex interface { // 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. // Does not modify the state of the iterator. Value() 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. + + // Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. - Reset() + Begin() + // First moves the iterator to the first element and returns true if there was a first element in the container. // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. @@ -55,15 +59,19 @@ type IteratorWithKey interface { // 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. // Does not modify the state of the iterator. Value() 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. + + // Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. - Reset() + Begin() + // First moves the iterator to the first element and returns true if there was a first element in the container. // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). // Modifies the state of the iterator. @@ -82,6 +90,7 @@ type ReverseIteratorWithIndex interface { // 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. @@ -105,6 +114,7 @@ type ReverseIteratorWithKey interface { // If Prev() returns true, then previous element's key 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 key and value can be retrieved by Key() and Value(). // Modifies the state of the iterator. diff --git a/lists/arraylist/arraylist.go b/lists/arraylist/arraylist.go index dccfc88..57eca3c 100644 --- a/lists/arraylist/arraylist.go +++ b/lists/arraylist/arraylist.go @@ -224,9 +224,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 } @@ -234,7 +234,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/lists/arraylist/arraylist_test.go b/lists/arraylist/arraylist_test.go index fa2ba57..f87cfe9 100644 --- a/lists/arraylist/arraylist_test.go +++ b/lists/arraylist/arraylist_test.go @@ -379,14 +379,14 @@ func TestListIteratorPrev(t *testing.T) { } } -func TestListIteratorReset(t *testing.T) { +func TestListIteratorBegin(t *testing.T) { list := New() it := list.Iterator() - it.Reset() + it.Begin() list.Add("a", "b", "c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") diff --git a/lists/doublylinkedlist/doublylinkedlist.go b/lists/doublylinkedlist/doublylinkedlist.go index 7cb6778..2f5bb2e 100644 --- a/lists/doublylinkedlist/doublylinkedlist.go +++ b/lists/doublylinkedlist/doublylinkedlist.go @@ -363,9 +363,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 iterator.element = nil } @@ -374,7 +374,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/lists/doublylinkedlist/doublylinkedlist_test.go b/lists/doublylinkedlist/doublylinkedlist_test.go index fd8495d..621c750 100644 --- a/lists/doublylinkedlist/doublylinkedlist_test.go +++ b/lists/doublylinkedlist/doublylinkedlist_test.go @@ -379,14 +379,14 @@ func TestListIteratorPrev(t *testing.T) { } } -func TestListIteratorReset(t *testing.T) { +func TestListIteratorBegin(t *testing.T) { list := New() it := list.Iterator() - it.Reset() + it.Begin() list.Add("a", "b", "c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") diff --git a/lists/singlylinkedlist/singlylinkedlist.go b/lists/singlylinkedlist/singlylinkedlist.go index cbcf95a..41db255 100644 --- a/lists/singlylinkedlist/singlylinkedlist.go +++ b/lists/singlylinkedlist/singlylinkedlist.go @@ -316,9 +316,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 iterator.element = nil } @@ -327,7 +327,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/lists/singlylinkedlist/singlylinkedlist_test.go b/lists/singlylinkedlist/singlylinkedlist_test.go index c313347..03f9b2d 100644 --- a/lists/singlylinkedlist/singlylinkedlist_test.go +++ b/lists/singlylinkedlist/singlylinkedlist_test.go @@ -338,14 +338,14 @@ func TestListIteratorNext(t *testing.T) { } } -func TestListIteratorReset(t *testing.T) { +func TestListIteratorBegin(t *testing.T) { list := New() it := list.Iterator() - it.Reset() + it.Begin() list.Add("a", "b", "c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") diff --git a/maps/treemap/treemap.go b/maps/treemap/treemap.go index a24e902..1b531ee 100644 --- a/maps/treemap/treemap.go +++ b/maps/treemap/treemap.go @@ -165,17 +165,17 @@ func (iterator *Iterator) Key() interface{} { return iterator.iterator.Key() } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { - iterator.iterator.Reset() +func (iterator *Iterator) Begin() { + iterator.iterator.Begin() } // First moves the iterator to the first element and returns true if there was a first element in the container. // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). // Modifies the state of the iterator func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/maps/treemap/treemap_test.go b/maps/treemap/treemap_test.go index 2a4f82d..8b0857a 100644 --- a/maps/treemap/treemap_test.go +++ b/maps/treemap/treemap_test.go @@ -406,16 +406,16 @@ func TestMapIteratorPrev(t *testing.T) { } } -func TestMapIteratorReset(t *testing.T) { +func TestMapIteratorBegin(t *testing.T) { m := NewWithIntComparator() it := m.Iterator() - it.Reset() + it.Begin() m.Put(3, "c") m.Put(1, "a") m.Put(2, "b") for it.Next() { } - it.Reset() + it.Begin() it.Next() if key, value := it.Key(), it.Value(); key != 1 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a") diff --git a/sets/treeset/treeset.go b/sets/treeset/treeset.go index d33cb81..8acd804 100644 --- a/sets/treeset/treeset.go +++ b/sets/treeset/treeset.go @@ -151,18 +151,18 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 - iterator.iterator.Reset() + iterator.iterator.Begin() } // First moves the iterator to the first element and returns true if there was a first element in the container. // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/sets/treeset/treeset_test.go b/sets/treeset/treeset_test.go index c419a6b..a2a78fe 100644 --- a/sets/treeset/treeset_test.go +++ b/sets/treeset/treeset_test.go @@ -278,14 +278,14 @@ func TestSetIteratorPrev(t *testing.T) { } } -func TestSetIteratorReset(t *testing.T) { +func TestSetIteratorBegin(t *testing.T) { m := NewWithStringComparator() it := m.Iterator() - it.Reset() + it.Begin() m.Add("a", "b", "c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "a") diff --git a/stacks/arraystack/arraystack.go b/stacks/arraystack/arraystack.go index 3539538..81f6837 100644 --- a/stacks/arraystack/arraystack.go +++ b/stacks/arraystack/arraystack.go @@ -143,9 +143,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 } @@ -153,7 +153,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/stacks/arraystack/arraystack_test.go b/stacks/arraystack/arraystack_test.go index b744bc1..bb68e97 100644 --- a/stacks/arraystack/arraystack_test.go +++ b/stacks/arraystack/arraystack_test.go @@ -176,16 +176,16 @@ func TestStackIteratorPrev(t *testing.T) { } } -func TestStackIteratorReset(t *testing.T) { +func TestStackIteratorBegin(t *testing.T) { stack := New() it := stack.Iterator() - it.Reset() + it.Begin() stack.Push("a") stack.Push("b") stack.Push("c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "c" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") diff --git a/stacks/linkedliststack/linkedliststack.go b/stacks/linkedliststack/linkedliststack.go index 8ca7579..0778f6d 100644 --- a/stacks/linkedliststack/linkedliststack.go +++ b/stacks/linkedliststack/linkedliststack.go @@ -128,9 +128,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 } @@ -138,7 +138,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/stacks/linkedliststack/linkedliststack_test.go b/stacks/linkedliststack/linkedliststack_test.go index ce97b8a..ec218cb 100644 --- a/stacks/linkedliststack/linkedliststack_test.go +++ b/stacks/linkedliststack/linkedliststack_test.go @@ -136,16 +136,16 @@ func TestStackIterator(t *testing.T) { } } -func TestStackIteratorReset(t *testing.T) { +func TestStackIteratorBegin(t *testing.T) { stack := New() it := stack.Iterator() - it.Reset() + it.Begin() stack.Push("a") stack.Push("b") stack.Push("c") for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != "c" { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "c") diff --git a/trees/binaryheap/binaryheap.go b/trees/binaryheap/binaryheap.go index 5188033..fc62d79 100644 --- a/trees/binaryheap/binaryheap.go +++ b/trees/binaryheap/binaryheap.go @@ -159,9 +159,9 @@ func (iterator *Iterator) Index() int { return iterator.index } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.index = -1 } @@ -169,7 +169,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's index and value can be retrieved by Index() and Value(). // Modifies the state of the iterator. func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/trees/binaryheap/binaryheap_test.go b/trees/binaryheap/binaryheap_test.go index 17311a3..83658ff 100644 --- a/trees/binaryheap/binaryheap_test.go +++ b/trees/binaryheap/binaryheap_test.go @@ -191,16 +191,16 @@ func TestBinaryHeapIteratorPrev(t *testing.T) { } } -func TestBinaryHeapIteratorReset(t *testing.T) { +func TestBinaryHeapIteratorBegin(t *testing.T) { heap := NewWithIntComparator() it := heap.Iterator() - it.Reset() + it.Begin() heap.Push(2) heap.Push(3) heap.Push(1) for it.Next() { } - it.Reset() + it.Begin() it.Next() if index, value := it.Index(), it.Value(); index != 0 || value != 1 { t.Errorf("Got %v,%v expected %v,%v", index, value, 0, 1) diff --git a/trees/redblacktree/redblacktree.go b/trees/redblacktree/redblacktree.go index 69c8baf..134c74e 100644 --- a/trees/redblacktree/redblacktree.go +++ b/trees/redblacktree/redblacktree.go @@ -357,9 +357,9 @@ func (iterator *Iterator) Key() interface{} { return iterator.node.Key } -// Reset sets the iterator to the initial state. +// Begin resets the iterator to its initial state (one-before-first) // Call Next() to fetch the first element if any. -func (iterator *Iterator) Reset() { +func (iterator *Iterator) Begin() { iterator.node = nil } @@ -367,7 +367,7 @@ func (iterator *Iterator) Reset() { // If First() returns true, then first element's key and value can be retrieved by Key() and Value(). // Modifies the state of the iterator func (iterator *Iterator) First() bool { - iterator.Reset() + iterator.Begin() return iterator.Next() } diff --git a/trees/redblacktree/redblacktree_test.go b/trees/redblacktree/redblacktree_test.go index 495b68e..ab2946a 100644 --- a/trees/redblacktree/redblacktree_test.go +++ b/trees/redblacktree/redblacktree_test.go @@ -496,16 +496,16 @@ func TestRedBlackTreeIterator4(t *testing.T) { } } -func TestRedBlackTreeIteratorReset(t *testing.T) { +func TestRedBlackTreeIteratorBegin(t *testing.T) { tree := NewWithIntComparator() tree.Put(3, "c") tree.Put(1, "a") tree.Put(2, "b") it := tree.Iterator() - it.Reset() + it.Begin() for it.Next() { } - it.Reset() + it.Begin() it.Next() if key, value := it.Key(), it.Value(); key != 1 || value != "a" { t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")