-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from semihbkgr/iterators
Adding Go 1.23 Iterator Support
- Loading branch information
Showing
4 changed files
with
93 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//go:build go1.23 | ||
// +build go1.23 | ||
|
||
package haxmap | ||
|
||
import "iter" | ||
|
||
func (m *Map[K, V]) Iterator() iter.Seq2[K, V] { | ||
return func(yield func(key K, value V) bool) { | ||
for item := m.listHead.next(); item != nil; item = item.next() { | ||
if !yield(item.key, *item.value.Load()) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (m *Map[K, _]) Keys() iter.Seq[K] { | ||
return func(yield func(key K) bool) { | ||
for item := m.listHead.next(); item != nil; item = item.next() { | ||
if !yield(item.key) { | ||
return | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//go:build go1.23 | ||
// +build go1.23 | ||
|
||
package haxmap | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIterators(t *testing.T) { | ||
type Value = struct { | ||
key int | ||
} | ||
|
||
m := New[int, *Value]() | ||
|
||
itemCount := 16 | ||
for i := itemCount; i > 0; i-- { | ||
m.Set(i, &Value{i}) | ||
} | ||
|
||
t.Run("iterator", func(t *testing.T) { | ||
counter := 0 | ||
for k, v := range m.Iterator() { | ||
if v == nil { | ||
t.Error("Expecting an object.") | ||
} else if k != v.key { | ||
t.Error("Incorrect key/value pairs") | ||
} | ||
|
||
counter++ | ||
} | ||
|
||
if counter != itemCount { | ||
t.Error("Iterated item count did not match.") | ||
} | ||
}) | ||
|
||
t.Run("keys", func(t *testing.T) { | ||
counter := 0 | ||
for k := range m.Keys() { | ||
_, ok := m.Get(k) | ||
if !ok { | ||
t.Error("The key is not is the map") | ||
} | ||
counter++ | ||
} | ||
|
||
if counter != itemCount { | ||
t.Error("Iterated item count did not match.") | ||
} | ||
}) | ||
} |