Skip to content

Commit 5ff637d

Browse files
authored
Add Sorted method (#127)
* Add Sorted method This is a varient on `slices.Sorted` that makes it easy to get a sorted slice. This makes it easy to get the elements of the set into a sorted slice. Quite handy when using sets to build up unique elements but then needing a well defined iteration order. * Move to own file that can be build tagged * Also test go 1.21 * Fix error messages in test
1 parent 208a228 commit 5ff637d

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [1.20.1]
7+
go-version: [1.20.1, 1.21.1]
88
os: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.os }}
1010
steps:

sorted.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Open Source Initiative OSI - The MIT License (MIT):Licensing
6+
7+
The MIT License (MIT)
8+
Copyright (c) 2013 - 2023 Ralph Caraveo ([email protected])
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy of
11+
this software and associated documentation files (the "Software"), to deal in
12+
the Software without restriction, including without limitation the rights to
13+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14+
of the Software, and to permit persons to whom the Software is furnished to do
15+
so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
*/
28+
29+
package mapset
30+
31+
import (
32+
"cmp"
33+
"slices"
34+
)
35+
36+
// Sorted returns a sorted slice of a set of any ordered type in ascending order.
37+
// When sorting floating-point numbers, NaNs are ordered before other values.
38+
func Sorted[E cmp.Ordered](set Set[E]) []E {
39+
s := set.ToSlice()
40+
slices.Sort(s)
41+
return s
42+
}

sorted_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:build go1.21
2+
// +build go1.21
3+
4+
/*
5+
Open Source Initiative OSI - The MIT License (MIT):Licensing
6+
7+
The MIT License (MIT)
8+
Copyright (c) 2013 - 2022 Ralph Caraveo ([email protected])
9+
10+
Permission is hereby granted, free of charge, to any person obtaining a copy of
11+
this software and associated documentation files (the "Software"), to deal in
12+
the Software without restriction, including without limitation the rights to
13+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
14+
of the Software, and to permit persons to whom the Software is furnished to do
15+
so, subject to the following conditions:
16+
17+
The above copyright notice and this permission notice shall be included in all
18+
copies or substantial portions of the Software.
19+
20+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26+
SOFTWARE.
27+
*/
28+
29+
package mapset
30+
31+
import (
32+
"testing"
33+
)
34+
35+
func Test_Sorted(t *testing.T) {
36+
test := func(t *testing.T, ctor func(vals ...string) Set[string]) {
37+
set := ctor("apple", "banana", "pear")
38+
sorted := Sorted(set)
39+
40+
if len(sorted) != set.Cardinality() {
41+
t.Errorf("Length of slice is not the same as the set. Expected: %d. Actual: %d", set.Cardinality(), len(sorted))
42+
}
43+
44+
if sorted[0] != "apple" {
45+
t.Errorf("Element 0 was not equal to apple: %s", sorted[0])
46+
}
47+
if sorted[1] != "banana" {
48+
t.Errorf("Element 1 was not equal to banana: %s", sorted[1])
49+
}
50+
if sorted[2] != "pear" {
51+
t.Errorf("Element 2 was not equal to pear: %s", sorted[2])
52+
}
53+
}
54+
55+
t.Run("Safe", func(t *testing.T) {
56+
test(t, NewSet[string])
57+
})
58+
t.Run("Unsafe", func(t *testing.T) {
59+
test(t, NewThreadUnsafeSet[string])
60+
})
61+
}

0 commit comments

Comments
 (0)