Skip to content

Commit f96040c

Browse files
committed
docs: continue with examples for TreeSet
1 parent 86cca07 commit f96040c

4 files changed

Lines changed: 142 additions & 6 deletions

File tree

examples_treeset_test.go

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,22 @@ func ExampleTreeSet_InsertSlice() {
8888
// [blue green red]
8989
}
9090

91-
// InsertSet
91+
func ExampleTreeSet_InsertSet() {
92+
s1 := TreeSetFrom[string, Compare[string]]([]string{"red", "green"}, Cmp[string])
93+
s2 := TreeSetFrom[string, Compare[string]]([]string{"green", "blue"}, Cmp[string])
94+
95+
fmt.Println(s1)
96+
fmt.Println(s2)
97+
98+
s1.InsertSet(s2)
99+
100+
fmt.Println(s1)
101+
102+
// Output:
103+
// [green red]
104+
// [blue green]
105+
// [blue green red]
106+
}
92107

93108
func ExampleTreeSet_Remove() {
94109
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])
@@ -124,9 +139,39 @@ func ExampleTreeSet_RemoveSlice() {
124139
// [green]
125140
}
126141

127-
// RemoveSet
142+
func ExampleTreeSet_RemoveSet() {
143+
s1 := TreeSetFrom[string, Compare[string]]([]string{"a", "b", "c", "d", "e", "f"}, Cmp[string])
144+
s2 := TreeSetFrom[string, Compare[string]]([]string{"e", "z", "a"}, Cmp[string])
145+
146+
fmt.Println(s1)
147+
fmt.Println(s2)
148+
149+
s1.RemoveSet(s2)
150+
151+
fmt.Println(s1)
152+
153+
// Output:
154+
// [a b c d e f]
155+
// [a e z]
156+
// [b c d f]
157+
}
158+
159+
func ExampleTreeSet_RemoveFunc() {
160+
s := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, Cmp[int])
128161

129-
// RemoveFunc
162+
fmt.Println(s)
163+
164+
even := func(i int) bool {
165+
return i%2 == 0
166+
}
167+
s.RemoveFunc(even)
168+
169+
fmt.Println(s)
170+
171+
// Output:
172+
// [1 2 3 4 5 6 7 8 9]
173+
// [1 3 5 7 9]
174+
}
130175

131176
func ExampleTreeSet_Contains() {
132177
s := TreeSetFrom[string, Compare[string]]([]string{"red", "green", "blue"}, Cmp[string])

set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (s *Set[T]) RemoveSet(o *Set[T]) bool {
159159
// RemoveFunc will remove each element from s that satisfies condition f.
160160
//
161161
// Return true if s was modified, false otherwise.
162-
func (s *Set[T]) RemoveFunc(f func(item T) bool) bool {
162+
func (s *Set[T]) RemoveFunc(f func(T) bool) bool {
163163
modified := false
164164
for item := range s.items {
165165
if applies := f(item); applies {

treeset.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ func (s *TreeSet[T, C]) InsertSlice(items []T) bool {
101101
return modified
102102
}
103103

104+
// InsertSet will insert each element of o into s.
105+
//
106+
// Return true if s was modified (at least one item of o was not already in s), false otherwise.
107+
func (s *TreeSet[T, C]) InsertSet(o *TreeSet[T, C]) bool {
108+
modified := false
109+
insert := func(item T) bool {
110+
if s.Insert(item) {
111+
modified = true
112+
}
113+
return true
114+
}
115+
o.ForEach(insert)
116+
return modified
117+
}
118+
104119
// Remove item from s.
105120
//
106121
// Returns true if s was modified (item was in s), false otherwise.
@@ -121,6 +136,36 @@ func (s *TreeSet[T, C]) RemoveSlice(items []T) bool {
121136
return modified
122137
}
123138

139+
// RemoveSet will remove each element in o from s.
140+
//
141+
// Returns true if s was modified (at least one item in o was in s), false otherwise.
142+
func (s *TreeSet[T, C]) RemoveSet(o *TreeSet[T, C]) bool {
143+
modified := false
144+
remove := func(item T) bool {
145+
if s.Remove(item) {
146+
modified = true
147+
}
148+
return true
149+
}
150+
o.ForEach(remove)
151+
return modified
152+
}
153+
154+
// RemoveFunc will remove each element from s that satisifies condition f.
155+
//
156+
// Return true if s was modified, false otherwise.
157+
func (s *TreeSet[T, C]) RemoveFunc(f func(T) bool) bool {
158+
modified := false
159+
remove := func(item T) bool {
160+
if f(item) && s.Remove(item) {
161+
modified = true
162+
}
163+
return true
164+
}
165+
s.ForEach(remove)
166+
return modified
167+
}
168+
124169
// Min returns the smallest item in the set.
125170
//
126171
// Must not be called on an empty set.

treeset_test.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package set
66
import (
77
"context"
88
"fmt"
9-
"github.com/shoenig/test/must"
10-
"go.uber.org/goleak"
119
"math/rand"
1210
"strings"
1311
"testing"
12+
13+
"github.com/shoenig/test/must"
14+
"go.uber.org/goleak"
1415
)
1516

1617
const (
@@ -150,6 +151,17 @@ func TestTreeSet_InsertSlice(t *testing.T) {
150151
must.False(t, ts.InsertSlice(numbers))
151152
}
152153

154+
func TestTreeSet_InsertSet(t *testing.T) {
155+
cmp := Cmp[int]
156+
157+
ts1 := TreeSetFrom[int, Compare[int]]([]int{1, 3, 5, 7, 9}, cmp)
158+
ts2 := TreeSetFrom[int, Compare[int]]([]int{1, 2, 3}, cmp)
159+
160+
must.True(t, ts1.InsertSet(ts2))
161+
must.Eq(t, []int{1, 2, 3, 5, 7, 9}, ts1.Slice())
162+
must.Eq(t, []int{1, 2, 3}, ts2.Slice())
163+
}
164+
153165
func TestTreeSet_Remove_int(t *testing.T) {
154166
cmp := Cmp[int]
155167
ts := NewTreeSet[int, Compare[int]](cmp)
@@ -192,6 +204,40 @@ func TestTreeSet_RemoveSlice(t *testing.T) {
192204
must.Empty(t, ts)
193205
}
194206

207+
func TestTreeSet_RemoveSet(t *testing.T) {
208+
cmp := Cmp[int]
209+
210+
ts1 := NewTreeSet[int, Compare[int]](cmp)
211+
ts2 := NewTreeSet[int, Compare[int]](cmp)
212+
213+
numbers := ints(size)
214+
random := shuffle(numbers)
215+
ts1.InsertSlice(random)
216+
217+
random2 := shuffle(numbers[5:])
218+
ts2.InsertSlice(random2)
219+
220+
ts1.RemoveSet(ts2)
221+
result := ts1.Slice()
222+
must.Eq(t, []int{1, 2, 3, 4, 5}, result)
223+
}
224+
225+
func TestTreeSet_RemoveFunc(t *testing.T) {
226+
cmp := Cmp[byte]
227+
228+
ts := TreeSetFrom[byte, Compare[byte]]([]byte{
229+
'a', 'b', '1', 'c', '2', 'd',
230+
}, cmp)
231+
232+
notAlpha := func(c byte) bool {
233+
return c < 'a' || c > 'z'
234+
}
235+
236+
ts.RemoveFunc(notAlpha)
237+
238+
must.Eq(t, []byte{'a', 'b', 'c', 'd'}, ts.Slice())
239+
}
240+
195241
func TestTreeSet_Contains(t *testing.T) {
196242
t.Run("empty", func(t *testing.T) {
197243
ts := NewTreeSet[int, Compare[int]](Cmp[int])

0 commit comments

Comments
 (0)