@@ -64,7 +64,7 @@ func (sm *SortedMap[Map, K, V]) Delete(key K) (val *V, existed bool) {
64
64
return (* V )(nil ), false
65
65
}
66
66
67
- // All returns a sequence of key-value pairs in the map
67
+ // All returns a sequence of key-value pairs
68
68
func (sm * SortedMap [Map , K , V ]) All () iter.Seq2 [K , V ] {
69
69
return func (yield func (K , V ) bool ) {
70
70
tempHeap := * sm .h
@@ -77,7 +77,7 @@ func (sm *SortedMap[Map, K, V]) All() iter.Seq2[K, V] {
77
77
}
78
78
}
79
79
80
- // Keys returns a sequence of keys in the map
80
+ // Keys returns a sequence of keys
81
81
func (sm * SortedMap [Map , K , V ]) Keys () iter.Seq [K ] {
82
82
return func (yield func (K ) bool ) {
83
83
tempHeap := * sm .h
@@ -90,7 +90,7 @@ func (sm *SortedMap[Map, K, V]) Keys() iter.Seq[K] {
90
90
}
91
91
}
92
92
93
- // Values returns a sequence of values in the map
93
+ // Values returns a sequence of values
94
94
func (sm * SortedMap [Map , K , V ]) Values () iter.Seq [V ] {
95
95
return func (yield func (V ) bool ) {
96
96
tempHeap := * sm .h
@@ -103,7 +103,7 @@ func (sm *SortedMap[Map, K, V]) Values() iter.Seq[V] {
103
103
}
104
104
}
105
105
106
- // Insert adds a key-value pair to the map. If the key already exists, the value is updated.
106
+ // Insert adds a key-value pair to the map. If the key already exists, the value is updated
107
107
func (sm * SortedMap [Map , K , V ]) Insert (key K , val V ) {
108
108
if _ , exists := sm .m [key ]; exists {
109
109
sm .Delete (key )
@@ -112,7 +112,7 @@ func (sm *SortedMap[Map, K, V]) Insert(key K, val V) {
112
112
heap .Push (sm .h , KV [K , V ]{key , val })
113
113
}
114
114
115
- // Collect returns a map with the same contents as the SortedMap
115
+ // Collect returns a regular map with an *unordered* content off the SortedMap
116
116
func (sm * SortedMap [Map , K , V ]) Collect () Map {
117
117
m := make (Map )
118
118
for key , val := range sm .All () {
@@ -122,6 +122,36 @@ func (sm *SortedMap[Map, K, V]) Collect() Map {
122
122
return m
123
123
}
124
124
125
+ // CollectAll returns a slice of key-value pairs
126
+ func (sm * SortedMap [Map , K , V ]) CollectAll () []KV [K , V ] {
127
+ pairs := make ([]KV [K , V ], 0 , sm .Len ())
128
+ for k , v := range sm .All () {
129
+ pairs = append (pairs , KV [K , V ]{k , v })
130
+ }
131
+
132
+ return pairs
133
+ }
134
+
135
+ // CollectKeys returns a slice of the map’s keys
136
+ func (sm * SortedMap [Map , K , V ]) CollectKeys () []K {
137
+ ks := make ([]K , 0 , sm .Len ())
138
+ for k := range sm .Keys () {
139
+ ks = append (ks , k )
140
+ }
141
+
142
+ return ks
143
+ }
144
+
145
+ // CollectValues returns a slice of the map's values
146
+ func (sm * SortedMap [Map , K , V ]) CollectValues () []V {
147
+ vals := make ([]V , 0 , sm .Len ())
148
+ for val := range sm .Values () {
149
+ vals = append (vals , val )
150
+ }
151
+
152
+ return vals
153
+ }
154
+
125
155
// Len returns length of underlying map
126
156
func (sm * SortedMap [Map , K , V ]) Len () int {
127
157
return len (sm .m )
0 commit comments