10000 Add 1.23 iterators support by errx · Pull Request #4 · kamstrup/intmap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add 1.23 iterators support #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 8000 go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/kamstrup/intmap

go 1.18
go 1.23
53 changes: 53 additions & 0 deletions map64.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package intmap

import (
"iter"
"math"
)

Expand Down Expand Up @@ -229,6 +230,58 @@ func (m *Map[K, V]) ForEach(f func(K, V) bool) {
forEach64(m.data, f)
}

// All returns an iterator over key-value pairs from m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) All() iter.Seq2[K, V] {
return m.ForEach
}

// Keys returns an iterator over keys in m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) Keys() iter.Seq[K] {
return func(yield func(k K) bool) {
if m == nil {
return
}

if m.hasZeroKey && !yield(K(0)) {
return
}

for _, p := range m.data {
if p.K != K(0) && !yield(p.K) {
return
}
}
}
}

// Values returns an iterator over values in m.
// The iterator returns immediately if invoked on a nil map.
//
// The iteration order of a Map is not defined, so please avoid relying on it.
func (m *Map[K, V]) Values() iter.Seq[V] {
return func(yield func(v V) bool) {
if m == nil {
return
}

if m.hasZeroKey && !yield(m.zeroVal) {
return
}

for _, p := range m.data {
if p.K != K(0) && !yield(p.V) {
return
}
}
}
}

// Clear removes all items from the map, but keeps the internal buffers for reuse.
func (m *Map[K, V]) Clear() {
var zero V
Expand Down
41 changes: 40 additions & 1 deletion map64_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package intmap

import "testing"
import (
"testing"
)

func TestMap64(t *testing.T) {
type pairs [][2]int64
Expand Down Expand Up @@ -184,3 +186,40 @@ func TestMap64ForEachStop(t *testing.T) {
t.Fatalf("unexpected number of elements processed: %d, want %d", have, want)
}
}

func TestMap64Iterators(t *testing.T) {
m := New[int, int](10)
for i := 0; i < 100; i++ {
m.Put(i, 99-i) // 0:99, 1:98, 2:97, ...
}
const sumTo99 = 99 * (99 + 1) / 2

sum := 0
for k, v := range m.All() {
sum += k + v
}

if sum != sumTo99*2 {
t.Fatalf("unexpected sum when iterating over keys and values: %d, want %d", sum, sumTo99*2)
}

sum = 0
for k := range m.Keys() {
if k == 9 {
continue
}
sum += k
}
expected := sumTo99 - 9
if sum != expected {
t.Fatalf("unexpected sum when iterating over keys: %d, want %d", sum, expected)
}

sum = 0
for v := range m.Values() {
sum += v
}
if sum != sumTo99 {
t.Fatalf("unexpected sum when iterating over values: %d, want %d", sum, sumTo99*2)
}
}
10 changes: 10 additions & 0 deletions set.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package intmap

import "iter"

// Set is a specialization of Map modelling a set of integers.
// Like Map, methods that read from the set are valid on the nil Set.
// This include Has, Len, and ForEach.
Expand Down Expand Up @@ -47,3 +49,11 @@ func (s *Set[K]) ForEach(visit func(k K) bool) {
return visit(k)
})
}

// All returns an iterator over keys from the set.
// The iterator returns immediately if the set is nil.
//
// The iteration order of a Set is not defined, so please avoid relying on it.
func (s *Set[K]) All() iter.Seq[K] {
return s.ForEach
}
17 changes: 17 additions & 0 deletions set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,20 @@ func TestNilSet(t *testing.T) {
}

}

func TestSetIter(t *testing.T) {
s := NewSet[int](10)
for i := 0; i < 100; i++ {
s.Add(i)
}

sum := 0
for k := range s.All() {
sum += k
}
44BB
const sumTo99 = 99 * (99 + 1) / 2
if sum != sumTo99 {
t.Fatalf("unexpected sum: %d, want %d", sum, sumTo99)
}
}
0