@@ -110,6 +110,72 @@ func TestMap_Len(t *testing.T) {
110110 assert.Equal(t, 0, m.Len())
111111}
112112
113+func TestMap_Take(t *testing.T) {
114+ t.Parallel()
115+
116+ m := NewMap[string, int]()
117+ m.Set("key1", 42)
118+ m.Set("key2", 100)
119+
120+ assert.Equal(t, 2, m.Len())
121+
122+ value, ok := m.Take("key1")
123+ assert.True(t, ok)
124+ assert.Equal(t, 42, value)
125+ assert.Equal(t, 1, m.Len())
126+
127+ _, exists := m.Get("key1")
128+ assert.False(t, exists)
129+
130+ value, ok = m.Get("key2")
131+ assert.True(t, ok)
132+ assert.Equal(t, 100, value)
133+}
134+
135+func TestMap_Take_NonexistentKey(t *testing.T) {
136+ t.Parallel()
137+
138+ m := NewMap[string, int]()
139+ m.Set("key1", 42)
140+
141+ value, ok := m.Take("nonexistent")
142+ assert.False(t, ok)
143+ assert.Equal(t, 0, value)
144+ assert.Equal(t, 1, m.Len())
145+
146+ value, ok = m.Get("key1")
147+ assert.True(t, ok)
148+ assert.Equal(t, 42, value)
149+}
150+
151+func TestMap_Take_EmptyMap(t *testing.T) {
152+ t.Parallel()
153+
154+ m := NewMap[string, int]()
155+
156+ value, ok := m.Take("key1")
157+ assert.False(t, ok)
158+ assert.Equal(t, 0, value)
159+ assert.Equal(t, 0, m.Len())
160+}
161+
162+func TestMap_Take_SameKeyTwice(t *testing.T) {
163+ t.Parallel()
164+
165+ m := NewMap[string, int]()
166+ m.Set("key1", 42)
167+
168+ value, ok := m.Take("key1")
169+ assert.True(t, ok)
170+ assert.Equal(t, 42, value)
171+ assert.Equal(t, 0, m.Len())
172+
173+ value, ok = m.Take("key1")
174+ assert.False(t, ok)
175+ assert.Equal(t, 0, value)
176+ assert.Equal(t, 0, m.Len())
177+}
178+
113179func TestMap_Seq2(t *testing.T) {
114180 t.Parallel()
115181
@@ -158,6 +224,57 @@ func TestMap_Seq2_EmptyMap(t *testing.T) {
158224 assert.Equal(t, 0, count)
159225}
160226
227+func TestMap_Seq(t *testing.T) {
228+ t.Parallel()
229+
230+ m := NewMap[string, int]()
231+ m.Set("key1", 1)
232+ m.Set("key2", 2)
233+ m.Set("key3", 3)
234+
235+ collected := make([]int, 0)
236+ for v := range m.Seq() {
237+ collected = append(collected, v)
238+ }
239+
240+ assert.Equal(t, 3, len(collected))
241+ assert.Contains(t, collected, 1)
242+ assert.Contains(t, collected, 2)
243+ assert.Contains(t, collected, 3)
244+}
245+
246+func TestMap_Seq_EarlyReturn(t *testing.T) {
247+ t.Parallel()
248+
249+ m := NewMap[string, int]()
250+ m.Set("key1", 1)
251+ m.Set("key2", 2)
252+ m.Set("key3", 3)
253+
254+ count := 0
255+ for range m.Seq() {
256+ count++
257+ if count == 2 {
258+ break
259+ }
260+ }
261+
262+ assert.Equal(t, 2, count)
263+}
264+
265+func TestMap_Seq_EmptyMap(t *testing.T) {
266+ t.Parallel()
267+
268+ m := NewMap[string, int]()
269+
270+ count := 0
271+ for range m.Seq() {
272+ count++
273+ }
274+
275+ assert.Equal(t, 0, count)
276+}
277+
161278func TestMap_MarshalJSON(t *testing.T) {
162279 t.Parallel()
163280
@@ -371,6 +488,82 @@ func TestMap_ConcurrentSeq2(t *testing.T) {
371488 wg.Wait()
372489}
373490
491+func TestMap_ConcurrentSeq(t *testing.T) {
492+ t.Parallel()
493+
494+ m := NewMap[int, int]()
495+ for i := range 100 {
496+ m.Set(i, i*2)
497+ }
498+
499+ var wg sync.WaitGroup
500+ const numIterators = 10
501+
502+ wg.Add(numIterators)
503+ for range numIterators {
504+ go func() {
505+ defer wg.Done()
506+ count := 0
507+ values := make(map[int]bool)
508+ for v := range m.Seq() {
509+ values[v] = true
510+ count++
511+ }
512+ assert.Equal(t, 100, count)
513+ for i := range 100 {
514+ assert.True(t, values[i*2])
515+ }
516+ }()
517+ }
518+
519+ wg.Wait()
520+}
521+
522+func TestMap_ConcurrentTake(t *testing.T) {
523+ t.Parallel()
524+
525+ m := NewMap[int, int]()
526+ const numItems = 1000
527+
528+ for i := range numItems {
529+ m.Set(i, i*2)
530+ }
531+
532+ var wg sync.WaitGroup
533+ const numWorkers = 10
534+ taken := make([][]int, numWorkers)
535+
536+ wg.Add(numWorkers)
537+ for i := range numWorkers {
538+ go func(workerID int) {
539+ defer wg.Done()
540+ taken[workerID] = make([]int, 0)
541+ for j := workerID; j < numItems; j += numWorkers {
542+ if value, ok := m.Take(j); ok {
543+ taken[workerID] = append(taken[workerID], value)
544+ }
545+ }
546+ }(i)
547+ }
548+
549+ wg.Wait()
550+
551+ assert.Equal(t, 0, m.Len())
552+
553+ allTaken := make(map[int]bool)
554+ for _, workerTaken := range taken {
555+ for _, value := range workerTaken {
556+ assert.False(t, allTaken[value], "Value %d was taken multiple times", value)
557+ allTaken[value] = true
558+ }
559+ }
560+
561+ assert.Equal(t, numItems, len(allTaken))
562+ for i := range numItems {
563+ assert.True(t, allTaken[i*2], "Expected value %d to be taken", i*2)
564+ }
565+}
566+
374567func TestMap_TypeSafety(t *testing.T) {
375568 t.Parallel()
376569
@@ -431,6 +624,38 @@ func BenchmarkMap_Seq2(b *testing.B) {
431624 }
432625}
433626
627+func BenchmarkMap_Seq(b *testing.B) {
628+ m := NewMap[int, int]()
629+ for i := range 1000 {
630+ m.Set(i, i*2)
631+ }
632+
633+ for b.Loop() {
634+ for range m.Seq() {
635+ }
636+ }
637+}
638+
639+func BenchmarkMap_Take(b *testing.B) {
640+ m := NewMap[int, int]()
641+ for i := range 1000 {
642+ m.Set(i, i*2)
643+ }
644+
645+ b.ResetTimer()
646+ for i := 0; b.Loop(); i++ {
647+ key := i % 1000
648+ m.Take(key)
649+ if i%1000 == 999 {
650+ b.StopTimer()
651+ for j := range 1000 {
652+ m.Set(j, j*2)
653+ }
654+ b.StartTimer()
655+ }
656+ }
657+}
658+
434659func BenchmarkMap_ConcurrentReadWrite(b *testing.B) {
435660 m := NewMap[int, int]()
436661 for i := range 1000 {