From 342941bb48fcf804e257ce7dd166b18ccaba72ae Mon Sep 17 00:00:00 2001 From: amikai Date: Fri, 2 Aug 2024 06:02:19 +0800 Subject: [PATCH 1/4] ci: add 1.22 and 1.23.0-rc2 and upgrade action version --- .github/workflows/ci.yml | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e76bd6c..10efecb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,19 +4,18 @@ jobs: test: strategy: matrix: - go-version: [1.20.1, 1.21.1] + go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.0-rc.2] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - - name: Install Go - uses: actions/setup-go@v3 - with: - go-version: ${{ matrix.go-version }} - stable: false - - name: Checkout code - uses: actions/checkout@v3 - - name: Test - run: | - go test -v -race ./... - # go vet ./... - # go test -bench=. \ No newline at end of file + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + - name: Checkout code + uses: actions/checkout@v4 + - name: Test + run: | + go test -v -race ./... + # go vet ./... + # go test -bench=. From d49072bfa51d20e28124a19de94179ea51a4e022 Mon Sep 17 00:00:00 2001 From: amikai Date: Fri, 2 Aug 2024 09:54:21 +0800 Subject: [PATCH 2/4] Add Values function return 1.23 iterator --- set.go | 8 ++++++++ set123_test.go | 36 ++++++++++++++++++++++++++++++++++++ set_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 set123_test.go diff --git a/set.go b/set.go index 292089d..dadadb8 100644 --- a/set.go +++ b/set.go @@ -253,3 +253,11 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] { return s } + +func Values[T comparable](s Set[T]) func(func(element T) bool) { + return func(yield func(element T) bool) { + s.Each(func(t T) bool { + return !yield(t) + }) + } +} diff --git a/set123_test.go b/set123_test.go new file mode 100644 index 0000000..1102399 --- /dev/null +++ b/set123_test.go @@ -0,0 +1,36 @@ +//go:build go1.23 +// +build go1.23 + +package mapset + +import "testing" + +func TestAll123(t *testing.T) { + a := NewSet[string]() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet[string]() + for elem := range Values(a) { + b.Add(elem) + } + + if !a.Equal(b) { + t.Error("The sets are not equal after iterating (Each) through the first set") + } + + var count int + for range Values(a) { + if count == 2 { + break + } + count++ + } + + if count != 2 { + t.Error("Iteration should stop on the way") + } +} diff --git a/set_test.go b/set_test.go index a21153d..9b41116 100644 --- a/set_test.go +++ b/set_test.go @@ -1386,3 +1386,35 @@ func Test_Example(t *testing.T) { fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English")) */ } + +func TestAll(t *testing.T) { + a := NewSet[string]() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet[string]() + Values(a)(func(elem string) bool { + b.Add(elem) + return true + }) + + if !a.Equal(b) { + t.Error("The sets are not equal after iterating (Each) through the first set") + } + + var count int + Values(a)(func(elem string) bool { + if count == 2 { + return false + } + count++ + return true + }) + + if count != 2 { + t.Error("Iteration should stop on the way") + } +} From aab5b006e856a51306d607c70fa473b073040b66 Mon Sep 17 00:00:00 2001 From: amikai Date: Tue, 4 Mar 2025 07:22:59 +0900 Subject: [PATCH 3/4] ci: add go versions in ci to 1.23.x and 1.24.x --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 10efecb..3c215ef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ jobs: test: strategy: matrix: - go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.0-rc.2] + go-version: [1.20.x, 1.21.x, 1.22.x, 1.23.x, 1.24.x] os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: From 24f21dd8caecdd4ecea9a21e6fa5a437ef369841 Mon Sep 17 00:00:00 2001 From: amikai Date: Tue, 4 Mar 2025 08:04:14 +0900 Subject: [PATCH 4/4] Rename Values to Elements Use the name "Element" as it is more intuitive. --- set.go | 4 +++- set123_test.go | 10 ++++---- set_test.go | 64 +++++++++++++++++++++++++------------------------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/set.go b/set.go index dadadb8..07cc80b 100644 --- a/set.go +++ b/set.go @@ -254,7 +254,9 @@ func NewThreadUnsafeSetFromMapKeys[T comparable, V any](val map[T]V) Set[T] { return s } -func Values[T comparable](s Set[T]) func(func(element T) bool) { +// Elements returns an iterator that yields the elements of the set. Starting +// with Go 1.23, users can use a for loop to iterate over it. +func Elements[T comparable](s Set[T]) func(func(element T) bool) { return func(yield func(element T) bool) { s.Each(func(t T) bool { return !yield(t) diff --git a/set123_test.go b/set123_test.go index 1102399..df36506 100644 --- a/set123_test.go +++ b/set123_test.go @@ -3,9 +3,11 @@ package mapset -import "testing" +import ( + "testing" +) -func TestAll123(t *testing.T) { +func Test_Elements123(t *testing.T) { a := NewSet[string]() a.Add("Z") @@ -14,7 +16,7 @@ func TestAll123(t *testing.T) { a.Add("W") b := NewSet[string]() - for elem := range Values(a) { + for elem := range Elements(a) { b.Add(elem) } @@ -23,7 +25,7 @@ func TestAll123(t *testing.T) { } var count int - for range Values(a) { + for range Elements(a) { if count == 2 { break } diff --git a/set_test.go b/set_test.go index 9b41116..5a17ca2 100644 --- a/set_test.go +++ b/set_test.go @@ -1346,6 +1346,38 @@ func Test_NewThreadUnsafeSetFromMapKey_Strings(t *testing.T) { } } +func Test_Elements(t *testing.T) { + a := NewSet[string]() + + a.Add("Z") + a.Add("Y") + a.Add("X") + a.Add("W") + + b := NewSet[string]() + Elements(a)(func(elem string) bool { + b.Add(elem) + return true + }) + + if !a.Equal(b) { + t.Error("The sets are not equal after iterating (Each) through the first set") + } + + var count int + Elements(a)(func(elem string) bool { + if count == 2 { + return false + } + count++ + return true + }) + + if count != 2 { + t.Error("Iteration should stop on the way") + } +} + func Test_Example(t *testing.T) { /* requiredClasses := NewSet() @@ -1386,35 +1418,3 @@ func Test_Example(t *testing.T) { fmt.Println(allClasses.ContainsAll("Welding", "Automotive", "English")) */ } - -func TestAll(t *testing.T) { - a := NewSet[string]() - - a.Add("Z") - a.Add("Y") - a.Add("X") - a.Add("W") - - b := NewSet[string]() - Values(a)(func(elem string) bool { - b.Add(elem) - return true - }) - - if !a.Equal(b) { - t.Error("The sets are not equal after iterating (Each) through the first set") - } - - var count int - Values(a)(func(elem string) bool { - if count == 2 { - return false - } - count++ - return true - }) - - if count != 2 { - t.Error("Iteration should stop on the way") - } -}