Skip to content

Commit

Permalink
Remove specialized, unused iterator implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Haudum <christian.haudum@gmail.com>
  • Loading branch information
chaudum committed Jul 2, 2024
1 parent 52adf71 commit 6dcaf31
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 98 deletions.
69 changes: 0 additions & 69 deletions pkg/iter/v2/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,75 +159,6 @@ func NewCancelableIter[T any](ctx context.Context, itr Iterator[T]) *Cancellable
return &CancellableIter[T]{ctx: ctx, Iterator: itr}
}

type IndexedValue[T any] struct {
idx int
val T
}

func (iv IndexedValue[T]) Value() T {
return iv.val
}

func (iv IndexedValue[T]) Index() int {
return iv.idx
}

type IterWithIndex[T any] struct {
Iterator[T]
zero T // zero value of T
cache IndexedValue[T]
}

func (it *IterWithIndex[T]) At() IndexedValue[T] {
it.cache.val = it.Iterator.At()
return it.cache
}

func NewIterWithIndex[T any](iter Iterator[T], idx int) Iterator[IndexedValue[T]] {
return &IterWithIndex[T]{
Iterator: iter,
cache: IndexedValue[T]{idx: idx},
}
}

type SliceIterWithIndex[T any] struct {
xs []T // source slice
pos int // position within the slice
zero T // zero value of T
cache IndexedValue[T]
}

func (it *SliceIterWithIndex[T]) Next() bool {
it.pos++
return it.pos < len(it.xs)
}

func (it *SliceIterWithIndex[T]) Err() error {
return nil
}

func (it *SliceIterWithIndex[T]) At() IndexedValue[T] {
it.cache.val = it.xs[it.pos]
return it.cache
}

func (it *SliceIterWithIndex[T]) Peek() (IndexedValue[T], bool) {
if it.pos+1 >= len(it.xs) {
it.cache.val = it.zero
return it.cache, false
}
it.cache.val = it.xs[it.pos+1]
return it.cache, true
}

func NewSliceIterWithIndex[T any](xs []T, idx int) PeekIterator[IndexedValue[T]] {
return &SliceIterWithIndex[T]{
xs: xs,
pos: -1,
cache: IndexedValue[T]{idx: idx},
}
}

func NewCloseableIterator[T io.Closer](itr Iterator[T]) *CloseIter[T] {
return &CloseIter[T]{itr}
}
Expand Down
29 changes: 0 additions & 29 deletions pkg/iter/v2/iter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,6 @@ import (
"github.com/stretchr/testify/require"
)

func TestSliceIterWithIndex(t *testing.T) {
t.Parallel()
t.Run("SliceIterWithIndex implements PeekingIterator interface", func(t *testing.T) {
xs := []string{"a", "b", "c"}
it := NewSliceIterWithIndex(xs, 123)

// peek at first item
p, ok := it.Peek()
require.True(t, ok)
require.Equal(t, "a", p.val)
require.Equal(t, 123, p.idx)

// proceed to first item
require.True(t, it.Next())
require.Equal(t, "a", it.At().val)
require.Equal(t, 123, it.At().idx)

// proceed to second and third item
require.True(t, it.Next())
require.True(t, it.Next())

// peek at non-existing fourth item
p, ok = it.Peek()
require.False(t, ok)
require.Equal(t, "", p.val) // "" is zero value for type string
require.Equal(t, 123, p.idx)
})
}

func TestPeekingIterator(t *testing.T) {
t.Parallel()
data := []int{1, 2, 3, 4, 5}
Expand Down

0 comments on commit 6dcaf31

Please sign in to comment.