mirror of
https://github.com/emirpasic/gods.git
synced 2026-06-14 16:06:42 +03:00
Implement NextTo and PrevTo for all iterators and containers (index or key, forward-only or reversable)
This commit is contained in:
@@ -7,6 +7,7 @@ package arraylist
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/emirpasic/gods/utils"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -500,6 +501,106 @@ func TestListIteratorLast(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestListIteratorNextTo(t *testing.T) {
|
||||
// Sample seek function, i.e. string starting with "b"
|
||||
seek := func(index int, value interface{}) bool {
|
||||
return strings.HasSuffix(value.(string), "b")
|
||||
}
|
||||
|
||||
// NextTo (empty)
|
||||
{
|
||||
list := New()
|
||||
it := list.Iterator()
|
||||
for it.NextTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
}
|
||||
|
||||
// NextTo (not found)
|
||||
{
|
||||
list := New()
|
||||
list.Add("xx", "yy")
|
||||
it := list.Iterator()
|
||||
for it.NextTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
}
|
||||
|
||||
// NextTo (found)
|
||||
{
|
||||
list := New()
|
||||
list.Add("aa", "bb", "cc")
|
||||
it := list.Iterator()
|
||||
it.Begin()
|
||||
if !it.NextTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
if index, value := it.Index(), it.Value(); index != 1 || value.(string) != "bb" {
|
||||
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
|
||||
}
|
||||
if !it.Next() {
|
||||
t.Errorf("Should go to first element")
|
||||
}
|
||||
if index, value := it.Index(), it.Value(); index != 2 || value.(string) != "cc" {
|
||||
t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
|
||||
}
|
||||
if it.Next() {
|
||||
t.Errorf("Should not go past last element")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListIteratorPrevTo(t *testing.T) {
|
||||
// Sample seek function, i.e. string starting with "b"
|
||||
seek := func(index int, value interface{}) bool {
|
||||
return strings.HasSuffix(value.(string), "b")
|
||||
}
|
||||
|
||||
// PrevTo (empty)
|
||||
{
|
||||
list := New()
|
||||
it := list.Iterator()
|
||||
it.End()
|
||||
for it.PrevTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
}
|
||||
|
||||
// PrevTo (not found)
|
||||
{
|
||||
list := New()
|
||||
list.Add("xx", "yy")
|
||||
it := list.Iterator()
|
||||
it.End()
|
||||
for it.PrevTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
}
|
||||
|
||||
// PrevTo (found)
|
||||
{
|
||||
list := New()
|
||||
list.Add("aa", "bb", "cc")
|
||||
it := list.Iterator()
|
||||
it.End()
|
||||
if !it.PrevTo(seek) {
|
||||
t.Errorf("Shouldn't iterate on empty list")
|
||||
}
|
||||
if index, value := it.Index(), it.Value(); index != 1 || value.(string) != "bb" {
|
||||
t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
|
||||
}
|
||||
if !it.Prev() {
|
||||
t.Errorf("Should go to first element")
|
||||
}
|
||||
if index, value := it.Index(), it.Value(); index != 0 || value.(string) != "aa" {
|
||||
t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
|
||||
}
|
||||
if it.Prev() {
|
||||
t.Errorf("Should not go before first element")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestListSerialization(t *testing.T) {
|
||||
list := New()
|
||||
list.Add("a", "b", "c")
|
||||
|
||||
@@ -81,3 +81,31 @@ func (iterator *Iterator) Last() bool {
|
||||
iterator.End()
|
||||
return iterator.Prev()
|
||||
}
|
||||
|
||||
// NextTo moves the iterator to the next element from current position that satisfies the condition given by the
|
||||
// passed function, and returns true if there was a next element in the container.
|
||||
// If NextTo() returns true, then next element's index and value can be retrieved by Index() and Value().
|
||||
// Modifies the state of the iterator.
|
||||
func (iterator *Iterator) NextTo(f func(index int, value interface{}) bool) bool {
|
||||
for iterator.Next() {
|
||||
index, value := iterator.Index(), iterator.Value()
|
||||
if f(index, value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// PrevTo moves the iterator to the previous element from current position that satisfies the condition given by the
|
||||
// passed function, and returns true if there was a next element in the container.
|
||||
// If PrevTo() returns true, then next element's index and value can be retrieved by Index() and Value().
|
||||
// Modifies the state of the iterator.
|
||||
func (iterator *Iterator) PrevTo(f func(index int, value interface{}) bool) bool {
|
||||
for iterator.Prev() {
|
||||
index, value := iterator.Index(), iterator.Value()
|
||||
if f(index, value) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user