From db5b5b54bbd93226b5ba543c811c3615fd3a6dcc Mon Sep 17 00:00:00 2001 From: Adam Baratz Date: Mon, 23 Sep 2024 16:02:53 -0400 Subject: [PATCH 1/2] Allow creating sets from iterators --- set.go | 23 +++++++++++++++++++++++ set_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/set.go b/set.go index 292089d..ccab277 100644 --- a/set.go +++ b/set.go @@ -253,3 +253,26 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] { return s } + +// NewSetFromSeq creates and returns a new set with the values from an iterator. +// Operations on the resulting set are thread-safe. +func NewSetFromSeq[T comparable](seq func(T) bool) Set[T] { + s := NewSetWithSize[T](0) + addAllFromSeq(s, seq) + return s +} + +// NewThreadUnsafeSetFromSeq creates and returns a new set with the given keys of the map. +// Operations on the resulting set are not thread-safe. +func NewThreadUnsafeSetFromSeq[T comparable](seq func(T) bool) Set[T] { + s := NewThreadUnsafeSetWithSize[T](0) + addAllFromSeq(s, seq) + return s +} + +func addAllFromSeq[T comparable](s Set[T], seq func(T) bool) { + seq(func(v T) bool { + s.Add(v) + return true + }) +} diff --git a/set_test.go b/set_test.go index a21153d..28ed019 100644 --- a/set_test.go +++ b/set_test.go @@ -1346,6 +1346,54 @@ func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) { } } +func Test_NewSetFromSeq(t *testing.T) { + values := []int{1, 2, 3} + + seq := func(yield func(E) bool) { + for _, v := range values { + if !yield(v) { + return + } + } + } + + s := NewSetFromSeq(seq) + + if len(values) != s.Cardinality() { + t.Errorf("Length of Set is not the same as the iterator. Expected: %d. Actual: %d", len(values), s.Cardinality()) + } + + for v := range values { + if !s.Contains(v) { + t.Errorf("Set is missing element: %v", v) + } + } +} + +func Test_NewThreadUnsafeSetFromSeq(t *testing.T) { + values := []int{1, 2, 3} + + seq := func(yield func(E) bool) { + for _, v := range values { + if !yield(v) { + return + } + } + } + + s := NewThreadUnsafeSetFromSeq(seq) + + if len(values) != s.Cardinality() { + t.Errorf("Length of Set is not the same as the iterator. Expected: %d. Actual: %d", len(values), s.Cardinality()) + } + + for v := range values { + if !s.Contains(v) { + t.Errorf("Set is missing element: %v", v) + } + } +} + func Test_Example(t *testing.T) { /* requiredClasses := NewSet() From 4634803e27968bebf2799b55d2e0a90afa4c559d Mon Sep 17 00:00:00 2001 From: Adam Baratz Date: Mon, 23 Sep 2024 16:12:58 -0400 Subject: [PATCH 2/2] Fixes --- set.go | 6 +++--- set_test.go | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/set.go b/set.go index ccab277..4e7da09 100644 --- a/set.go +++ b/set.go @@ -256,7 +256,7 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] { // NewSetFromSeq creates and returns a new set with the values from an iterator. // Operations on the resulting set are thread-safe. -func NewSetFromSeq[T comparable](seq func(T) bool) Set[T] { +func NewSetFromSeq[T comparable](seq func(func(T) bool)) Set[T] { s := NewSetWithSize[T](0) addAllFromSeq(s, seq) return s @@ -264,13 +264,13 @@ func NewSetFromSeq[T comparable](seq func(T) bool) Set[T] { // NewThreadUnsafeSetFromSeq creates and returns a new set with the given keys of the map. // Operations on the resulting set are not thread-safe. -func NewThreadUnsafeSetFromSeq[T comparable](seq func(T) bool) Set[T] { +func NewThreadUnsafeSetFromSeq[T comparable](seq func(func(T) bool)) Set[T] { s := NewThreadUnsafeSetWithSize[T](0) addAllFromSeq(s, seq) return s } -func addAllFromSeq[T comparable](s Set[T], seq func(T) bool) { +func addAllFromSeq[T comparable](s Set[T], seq func(func(T) bool)) { seq(func(v T) bool { s.Add(v) return true diff --git a/set_test.go b/set_test.go index 28ed019..24fa08c 100644 --- a/set_test.go +++ b/set_test.go @@ -1349,7 +1349,7 @@ func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) { func Test_NewSetFromSeq(t *testing.T) { values := []int{1, 2, 3} - seq := func(yield func(E) bool) { + seq := func(yield func(int) bool) { for _, v := range values { if !yield(v) { return @@ -1363,7 +1363,7 @@ func Test_NewSetFromSeq(t *testing.T) { t.Errorf("Length of Set is not the same as the iterator. Expected: %d. Actual: %d", len(values), s.Cardinality()) } - for v := range values { + for _, v := range values { if !s.Contains(v) { t.Errorf("Set is missing element: %v", v) } @@ -1373,7 +1373,7 @@ func Test_NewSetFromSeq(t *testing.T) { func Test_NewThreadUnsafeSetFromSeq(t *testing.T) { values := []int{1, 2, 3} - seq := func(yield func(E) bool) { + seq := func(yield func(int) bool) { for _, v := range values { if !yield(v) { return @@ -1387,7 +1387,7 @@ func Test_NewThreadUnsafeSetFromSeq(t *testing.T) { t.Errorf("Length of Set is not the same as the iterator. Expected: %d. Actual: %d", len(values), s.Cardinality()) } - for v := range values { + for _, v := range values { if !s.Contains(v) { t.Errorf("Set is missing element: %v", v) }