Skip to content

Commit

Permalink
Implement iterator deduplicator (#3381)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Sep 11, 2024
1 parent 75eb89f commit 878a6ce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
15 changes: 15 additions & 0 deletions utils/iterator/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

package iterator

import "github.com/ava-labs/avalanchego/utils/set"

var _ Iterator[any] = (*filtered[any])(nil)

type filtered[T any] struct {
Expand All @@ -19,6 +21,19 @@ func Filter[T any](it Iterator[T], filter func(T) bool) Iterator[T] {
}
}

// Deduplicate returns an iterator that skips the elements that have already
// been returned from [it].
func Deduplicate[T comparable](it Iterator[T]) Iterator[T] {
var seen set.Set[T]
return Filter(it, func(e T) bool {
if seen.Contains(e) {
return true
}
seen.Add(e)
return false
})
}

func (i *filtered[_]) Next() bool {
for i.it.Next() {
element := i.it.Value()
Expand Down
15 changes: 12 additions & 3 deletions utils/iterator/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/iterator"
"github.com/ava-labs/avalanchego/vms/platformvm/state"

. "github.com/ava-labs/avalanchego/utils/iterator"
)

func TestFilter(t *testing.T) {
Expand Down Expand Up @@ -40,8 +41,8 @@ func TestFilter(t *testing.T) {
stakers[3].TxID: stakers[3],
}

it := iterator.Filter(
iterator.FromSlice(stakers[:3]...),
it := Filter(
FromSlice(stakers[:3]...),
func(staker *state.Staker) bool {
_, ok := maskedStakers[staker.TxID]
return ok
Expand All @@ -55,3 +56,11 @@ func TestFilter(t *testing.T) {
it.Release()
require.False(it.Next())
}

func TestDeduplicate(t *testing.T) {
require.Equal(
t,
[]int{0, 1, 2, 3},
ToSlice(Deduplicate(FromSlice(0, 1, 2, 1, 2, 0, 3))),
)
}

0 comments on commit 878a6ce

Please sign in to comment.