Skip to content

Commit fae115c

Browse files
authored
Merge pull request #52 from semihbkgr/iterators
Adding Go 1.23 Iterator Support
2 parents 23da600 + f24bcf8 commit fae115c

File tree

4 files changed

+93
-11
lines changed

4 files changed

+93
-11
lines changed

.github/workflows/go.yml

+13-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@ name: Go
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
1010
e2e:
11+
strategy:
12+
matrix:
13+
go-version: ["=1.18", "^1.23"]
1114
runs-on: ubuntu-latest
1215
steps:
13-
- uses: actions/checkout@v2
16+
- uses: actions/checkout@v4
1417

15-
- name: Set up Go
16-
uses: actions/setup-go@v2
17-
with:
18-
go-version: 1.18
18+
- name: Set up Go
19+
uses: actions/setup-go@v5
20+
with:
21+
go-version: ${{ matrix.go-version }}
1922

20-
- name: Test
21-
run: |
22-
go test *.go
23+
- name: Test
24+
run: |
25+
go test -v .

e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func TestGetOrSet(t *testing.T) {
208208
}
209209
}
210210

211-
func TestIterator(t *testing.T) {
211+
func TestForEach(t *testing.T) {
212212
m := New[int, *Animal]()
213213

214214
m.ForEach(func(i int, a *Animal) bool {

iterator.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build go1.23
2+
// +build go1.23
3+
4+
package haxmap
5+
6+
import "iter"
7+
8+
func (m *Map[K, V]) Iterator() iter.Seq2[K, V] {
9+
return func(yield func(key K, value V) bool) {
10+
for item := m.listHead.next(); item != nil; item = item.next() {
11+
if !yield(item.key, *item.value.Load()) {
12+
return
13+
}
14+
}
15+
}
16+
}
17+
18+
func (m *Map[K, _]) Keys() iter.Seq[K] {
19+
return func(yield func(key K) bool) {
20+
for item := m.listHead.next(); item != nil; item = item.next() {
21+
if !yield(item.key) {
22+
return
23+
}
24+
}
25+
}
26+
}

iterator_test.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build go1.23
2+
// +build go1.23
3+
4+
package haxmap
5+
6+
import (
7+
"testing"
8+
)
9+
10+
func TestIterators(t *testing.T) {
11+
type Value = struct {
12+
key int
13+
}
14+
15+
m := New[int, *Value]()
16+
17+
itemCount := 16
18+
for i := itemCount; i > 0; i-- {
19+
m.Set(i, &Value{i})
20+
}
21+
22+
t.Run("iterator", func(t *testing.T) {
23+
counter := 0
24+
for k, v := range m.Iterator() {
25+
if v == nil {
26+
t.Error("Expecting an object.")
27+
} else if k != v.key {
28+
t.Error("Incorrect key/value pairs")
29+
}
30+
31+
counter++
32+
}
33+
34+
if counter != itemCount {
35+
t.Error("Iterated item count did not match.")
36+
}
37+
})
38+
39+
t.Run("keys", func(t *testing.T) {
40+
counter := 0
41+
for k := range m.Keys() {
42+
_, ok := m.Get(k)
43+
if !ok {
44+
t.Error("The key is not is the map")
45+
}
46+
counter++
47+
}
48+
49+
if counter != itemCount {
50+
t.Error("Iterated item count did not match.")
51+
}
52+
})
53+
}

0 commit comments

Comments
 (0)