- split iterators into two type (iterator with index and iterator with key)

pull/12/head
Emir Pasic 8 years ago
parent 255a3095cb
commit e4c3d8a0d8

@ -28,11 +28,28 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package containers
type Iterator interface {
// Stateful iterator for ordered containers whose values can be fetched by an index.
type IteratorWithIndex interface {
// Moves the iterator to the next element and returns true if there was a next element in the container.
// Modifies the state of the iterator.
Next() bool
// Returns the current element's value.
// Does not modify the state of the iterator.
Value() interface{}
// Returns the current element's index(key).
Index() interface{}
// Returns the current element's index.
// Does not modify the state of the iterator.
Index() int
}
// Stateful iterator for ordered containers whose elements are key value pairs.
type IteratorWithKey interface {
// Moves the iterator to the next element and returns true if there was a next element in the container.
// Modifies the state of the iterator.
Next() bool
// Returns the current element's value.
// Does not modify the state of the iterator.
Value() interface{}
// Returns the current element's key.
// Does not modify the state of the iterator.
Key() interface{}
}

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type List struct {
@ -196,7 +196,7 @@ func (iterator *Iterator) Value() interface{} {
return iterator.list.elements[iterator.index]
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type List struct {
@ -328,7 +328,7 @@ func (iterator *Iterator) Value() interface{} {
return iterator.element.value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ lists.List = (*List)(nil)
var _ containers.Enumerable = (*List)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type List struct {
@ -298,7 +298,7 @@ func (iterator *Iterator) Value() interface{} {
return iterator.element.value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -40,7 +40,7 @@ import (
func assertInterfaceImplementation() {
var _ stacks.Stack = (*Stack)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type Stack struct {
@ -115,7 +115,7 @@ func (iterator *Iterator) Value() interface{} {
return value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ stacks.Stack = (*Stack)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type Stack struct {
@ -111,7 +111,7 @@ func (iterator *Iterator) Value() interface{} {
return value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -42,7 +42,7 @@ import (
func assertInterfaceImplementation() {
var _ trees.Tree = (*Heap)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithIndex = (*Iterator)(nil)
}
type Heap struct {
@ -130,7 +130,7 @@ func (iterator *Iterator) Value() interface{} {
return value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Index() int {
return iterator.index
}

@ -41,7 +41,7 @@ import (
func assertInterfaceImplementation() {
var _ trees.Tree = (*Tree)(nil)
var _ containers.Iterator = (*Iterator)(nil)
var _ containers.IteratorWithKey = (*Iterator)(nil)
}
type color bool
@ -311,7 +311,7 @@ func (iterator *Iterator) Value() interface{} {
return iterator.left.Value
}
func (iterator *Iterator) Index() interface{} {
func (iterator *Iterator) Key() interface{} {
return iterator.left.Key
}

@ -234,7 +234,7 @@ func TestRedBlackTreeIterator(t *testing.T) {
count := 0
for it.Next() {
count += 1
index := it.Index()
index := it.Key()
switch index {
case count:
if actualValue, expectedValue := index, count; actualValue != expectedValue {
@ -259,7 +259,7 @@ func TestRedBlackTreeIterator(t *testing.T) {
count = 0
for it.Next() {
count += 1
index := it.Index()
index := it.Key()
switch index {
case count:
if actualValue, expectedValue := index, count; actualValue != expectedValue {
@ -282,7 +282,7 @@ func TestRedBlackTreeIterator(t *testing.T) {
count = 0
for it.Next() {
count += 1
index := it.Index()
index := it.Key()
switch index {
case count:
if actualValue, expectedValue := index, count; actualValue != expectedValue {

Loading…
Cancel
Save