Skip to content

Commit e3e6b43

Browse files
committed
Expand sorted_{,multi}set a little
1 parent 7aafc05 commit e3e6b43

4 files changed

Lines changed: 39 additions & 11 deletions

File tree

java/gazelle/private/sorted_multiset/multiset.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@ func (s *SortedMultiSet[K, V]) Keys() []K {
3333
return s.keys.SortedSlice()
3434
}
3535

36-
func (s *SortedMultiSet[K, V]) Values(key K) []V {
36+
func (s *SortedMultiSet[K, V]) Values(key K) *sorted_set.SortedSet[V] {
37+
if s == nil {
38+
return sorted_set.NewSortedSet[V](nil)
39+
}
40+
41+
return s.ms[key]
42+
}
43+
44+
func (s *SortedMultiSet[K, V]) SortedValues(key K) []V {
3745
if s == nil {
3846
return nil
3947
}

java/gazelle/private/sorted_multiset/multiset_test.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestNil(t *testing.T) {
1515
t.Errorf("want len(nil.Keys()) == 0, got %d", gotKeysLen)
1616
}
1717

18-
gotValues := s.Values("foo")
18+
gotValues := s.SortedValues("foo")
1919
if gotValuesLen := len(gotValues); gotValuesLen != 0 {
2020
t.Errorf("want len(nil.Values(\"foo\")) == 0, got %d", gotValuesLen)
2121
}
@@ -30,7 +30,12 @@ func TestMultiSet(t *testing.T) {
3030
t.Errorf("want no keys, got %d: %v", gotKeysLen, gotKeys)
3131
}
3232

33-
gotValues := s.Values("tasty")
33+
gotValuesSlice := s.SortedValues("tasty")
34+
if gotValuesLen := len(gotValuesSlice); gotValuesLen != 0 {
35+
t.Errorf("want no values for tasty, got %d: %v", gotValuesLen, gotValuesSlice)
36+
}
37+
38+
gotValues := s.Values("tasty").SortedSlice()
3439
if gotValuesLen := len(gotValues); gotValuesLen != 0 {
3540
t.Errorf("want no values for tasty, got %d: %v", gotValuesLen, gotValues)
3641
}
@@ -45,12 +50,12 @@ func TestMultiSet(t *testing.T) {
4550
}
4651

4752
wantTastyValues := []string{"hummus"}
48-
gotTastyValues := s.Values("tasty")
53+
gotTastyValues := s.SortedValues("tasty")
4954
if !reflect.DeepEqual(wantTastyValues, gotTastyValues) {
5055
t.Errorf("want tasty values %v got %v", wantTastyValues, gotTastyValues)
5156
}
5257

53-
gotBadValues := s.Values("bad")
58+
gotBadValues := s.SortedValues("bad")
5459
if gotBadValuesLen := len(gotBadValues); gotBadValuesLen != 0 {
5560
t.Errorf("want no values for tasty, got %d: %v", gotBadValuesLen, gotBadValues)
5661
}
@@ -65,12 +70,12 @@ func TestMultiSet(t *testing.T) {
6570
}
6671

6772
wantTastyValues := []string{"cheese", "hummus"}
68-
gotTastyValues := s.Values("tasty")
73+
gotTastyValues := s.SortedValues("tasty")
6974
if !reflect.DeepEqual(wantTastyValues, gotTastyValues) {
7075
t.Errorf("want tasty values %v got %v", wantTastyValues, gotTastyValues)
7176
}
7277

73-
gotBadValues := s.Values("bad")
78+
gotBadValues := s.SortedValues("bad")
7479
if gotBadValuesLen := len(gotBadValues); gotBadValuesLen != 0 {
7580
t.Errorf("want no values for tasty, got %d: %v", gotBadValuesLen, gotBadValues)
7681
}
@@ -85,12 +90,12 @@ func TestMultiSet(t *testing.T) {
8590
}
8691

8792
wantTastyValues := []string{"cheese", "hummus"}
88-
gotTastyValues := s.Values("tasty")
93+
gotTastyValues := s.SortedValues("tasty")
8994
if !reflect.DeepEqual(wantTastyValues, gotTastyValues) {
9095
t.Errorf("want tasty values %v got %v", wantTastyValues, gotTastyValues)
9196
}
9297

93-
gotBadValues := s.Values("bad")
98+
gotBadValues := s.SortedValues("bad")
9499
if gotBadValuesLen := len(gotBadValues); gotBadValuesLen != 0 {
95100
t.Errorf("want no values for tasty, got %d: %v", gotBadValuesLen, gotBadValues)
96101
}
@@ -105,13 +110,18 @@ func TestMultiSet(t *testing.T) {
105110
}
106111

107112
wantTastyValues := []string{"cheese", "hummus"}
108-
gotTastyValues := s.Values("tasty")
113+
gotTastyValues := s.SortedValues("tasty")
109114
if !reflect.DeepEqual(wantTastyValues, gotTastyValues) {
110115
t.Errorf("want tasty values %v got %v", wantTastyValues, gotTastyValues)
111116
}
112117

118+
gotTastySortedSetValues := s.Values("tasty")
119+
if !reflect.DeepEqual(wantTastyValues, gotTastySortedSetValues.SortedSlice()) {
120+
t.Errorf("want tasty values %v got %v", wantTastyValues, gotTastyValues)
121+
}
122+
113123
wantBadValues := []string{"soil"}
114-
gotBadValues := s.Values("bad")
124+
gotBadValues := s.SortedValues("bad")
115125
if !reflect.DeepEqual(wantBadValues, gotBadValues) {
116126
t.Errorf("want bad values %v got %v", wantBadValues, gotBadValues)
117127
}

java/gazelle/private/sorted_set/btreeset.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (s *SortedSet[T]) Add(v T) {
6666

6767
// AddAll adds all of the elements from the other SortedSet to this one.
6868
func (s *SortedSet[T]) AddAll(other *SortedSet[T]) {
69+
if other == nil {
70+
return
71+
}
6972
other.tree.Ascend(func(v T) bool {
7073
s.Add(v)
7174
return true

java/gazelle/private/sorted_set/btreeset_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,13 @@ func TestAddAll(t *testing.T) {
251251
if !reflect.DeepEqual(want, got) {
252252
t.Errorf("want %v got %v", want, got)
253253
}
254+
255+
s.AddAll(nil)
256+
257+
got = s.SortedSlice()
258+
if !reflect.DeepEqual(want, got) {
259+
t.Errorf("want %v got %v", want, got)
260+
}
254261
}
255262

256263
func TestFilter(t *testing.T) {

0 commit comments

Comments
 (0)