Skip to content

Commit 16402b4

Browse files
authored
Merge pull request #93 from pczajkowski/master
Added Clear and TestClear. As per #90.
2 parents 8c72a8b + 99cbf33 commit 16402b4

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

concurrent_map.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ func (m ConcurrentMap) IterBuffered() <-chan Tuple {
186186
return ch
187187
}
188188

189+
// Clear removes all items from map.
190+
func (m ConcurrentMap) Clear() {
191+
for item := range m.IterBuffered() {
192+
m.Remove(item.Key)
193+
}
194+
}
195+
189196
// Returns a array of channels that contains elements in each shard,
190197
// which likely takes a snapshot of `m`.
191198
// It returns once the size of each buffered channel is determined,

concurrent_map_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,21 @@ func TestBufferedIterator(t *testing.T) {
326326
}
327327
}
328328

329+
func TestClear(t *testing.T) {
330+
m := New()
331+
332+
// Insert 100 elements.
333+
for i := 0; i < 100; i++ {
334+
m.Set(strconv.Itoa(i), Animal{strconv.Itoa(i)})
335+
}
336+
337+
m.Clear()
338+
339+
if m.Count() != 0 {
340+
t.Error("We should have 0 elements.")
341+
}
342+
}
343+
329344
func TestIterCb(t *testing.T) {
330345
m := New()
331346

0 commit comments

Comments
 (0)