- rename Reset() to Begin() in iterators (this will allow End() which will make reverse loops more readable)

pull/20/head
Emir Pasic 8 years ago
parent cbc23a5b79
commit 57162feff5

@ -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.

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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")

@ -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()
}

@ -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)

@ -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()
}

@ -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")

Loading…
Cancel
Save