Skip to content

Commit cf03d44

Browse files
authored
Merge pull request #289 from skitt/golang-1.21-set-clear
On Go 1.21, use clear() to clear sets
2 parents 3b25d92 + 571d869 commit cf03d44

File tree

4 files changed

+63
-17
lines changed

4 files changed

+63
-17
lines changed

.github/workflows/test.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
build:
55
strategy:
66
matrix:
7-
go-version: [1.19.x, 1.20.x]
7+
go-version: [1.20.x, 1.21.x]
88
platform: [windows-latest]
99
runs-on: ${{ matrix.platform }}
1010
steps:
@@ -24,7 +24,7 @@ jobs:
2424
test:
2525
strategy:
2626
matrix:
27-
go-version: [1.19.x, 1.20.x]
27+
go-version: [1.20.x, 1.21.x]
2828
platform: [ubuntu-latest, macos-latest]
2929
runs-on: ${{ matrix.platform }}
3030
steps:
@@ -56,7 +56,7 @@ jobs:
5656
- name: Install Go
5757
uses: actions/setup-go@v3
5858
with:
59-
go-version: 1.20.x
59+
go-version: 1.21.x
6060
- name: Add GOBIN to PATH
6161
run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
6262
- name: Install dependencies

set/set.go

-14
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,3 @@ func (s Set[T]) Clone() Set[T] {
213213
func (s Set[T]) SymmetricDifference(s2 Set[T]) Set[T] {
214214
return s.Difference(s2).Union(s2.Difference(s))
215215
}
216-
217-
// Clear empties the set.
218-
// It is preferable to replace the set with a newly constructed set,
219-
// but not all callers can do that (when there are other references to the map).
220-
// In some cases the set *won't* be fully cleared, e.g. a Set[float32] containing NaN
221-
// can't be cleared because NaN can't be removed.
222-
// For sets containing items of a type that is reflexive for ==,
223-
// this is optimized to a single call to runtime.mapclear().
224-
func (s Set[T]) Clear() Set[T] {
225-
for key := range s {
226-
delete(s, key)
227-
}
228-
return s
229-
}

set/set_go_1.20.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build !go1.21
2+
3+
/*
4+
Copyright 2023 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package set
20+
21+
// Clear empties the set.
22+
// It is preferable to replace the set with a newly constructed set,
23+
// but not all callers can do that (when there are other references to the map).
24+
// In some cases the set *won't* be fully cleared, e.g. a Set[float32] containing NaN
25+
// can't be cleared because NaN can't be removed.
26+
// For sets containing items of a type that is reflexive for ==,
27+
// this is optimized to a single call to runtime.mapclear().
28+
func (s Set[T]) Clear() Set[T] {
29+
for key := range s {
30+
delete(s, key)
31+
}
32+
return s
33+
}

set/set_go_1.21.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:build go1.21
2+
3+
/*
4+
Copyright 2023 The Kubernetes Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package set
20+
21+
// Clear empties the set.
22+
// It is preferable to replace the set with a newly constructed set,
23+
// but not all callers can do that (when there are other references to the map).
24+
func (s Set[T]) Clear() Set[T] {
25+
clear(s)
26+
return s
27+
}

0 commit comments

Comments
 (0)