File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -125,7 +125,17 @@ func (v *Map) String() string {
125
125
return b .String ()
126
126
}
127
127
128
- func (v * Map ) Init () * Map { return v }
128
+ // Init removes all keys from the map.
129
+ func (v * Map ) Init () * Map {
130
+ v .keysMu .Lock ()
131
+ defer v .keysMu .Unlock ()
132
+ v .keys = v .keys [:0 ]
133
+ v .m .Range (func (k , _ interface {}) bool {
134
+ v .m .Delete (k )
135
+ return true
136
+ })
137
+ return v
138
+ }
129
139
130
140
// updateKeys updates the sorted list of keys in v.keys.
131
141
func (v * Map ) addKey (key string ) {
Original file line number Diff line number Diff line change @@ -161,6 +161,28 @@ func BenchmarkStringSet(b *testing.B) {
161
161
})
162
162
}
163
163
164
+ func TestMapInit (t * testing.T ) {
165
+ RemoveAll ()
166
+ colors := NewMap ("bike-shed-colors" )
167
+ colors .Add ("red" , 1 )
168
+ colors .Add ("blue" , 1 )
169
+ colors .Add ("chartreuse" , 1 )
170
+
171
+ n := 0
172
+ colors .Do (func (KeyValue ) { n ++ })
173
+ if n != 3 {
174
+ t .Errorf ("after three Add calls with distinct keys, Do should invoke f 3 times; got %v" , n )
175
+ }
176
+
177
+ colors .Init ()
178
+
179
+ n = 0
180
+ colors .Do (func (KeyValue ) { n ++ })
181
+ if n != 0 {
182
+ t .Errorf ("after Init, Do should invoke f 0 times; got %v" , n )
183
+ }
184
+ }
185
+
164
186
func TestMapCounter (t * testing.T ) {
165
187
RemoveAll ()
166
188
colors := NewMap ("bike-shed-colors" )
You can’t perform that action at this time.
0 commit comments