Skip to content
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

feat: add support for iterators introduced in go1.23 #41

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/wk8/go-ordered-map/v2

go 1.18
go 1.23

require (
github.com/bahlo/generic-list-go v0.2.0
Expand Down
35 changes: 34 additions & 1 deletion orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
// All operations are constant-time.
//
// Github repo: https://github.com/wk8/go-ordered-map
//
package orderedmap

import (
"fmt"
"iter"

list "github.com/bahlo/generic-list-go"
)
Expand Down Expand Up @@ -294,3 +294,36 @@ func (om *OrderedMap[K, V]) GetAndMoveToFront(key K) (val V, err error) {

return
}

// All returns an iterator over all the key-value pairs in the map.
func (om *OrderedMap[K, V]) All() iter.Seq2[K, V] {
TristanSpeakEasy marked this conversation as resolved.
Show resolved Hide resolved
return func(yield func(K, V) bool) {
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
if !yield(pair.Key, pair.Value) {
return
}
}
}
}

// Keys returns an iterator over all the keys in the map.
func (om *OrderedMap[K, V]) Keys() iter.Seq[K] {
TristanSpeakEasy marked this conversation as resolved.
Show resolved Hide resolved
return func(yield func(K) bool) {
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
if !yield(pair.Key) {
return
}
}
}
}

// Values returns an iterator over all the values in the map.
func (om *OrderedMap[K, V]) Values() iter.Seq[V] {
TristanSpeakEasy marked this conversation as resolved.
Show resolved Hide resolved
return func(yield func(V) bool) {
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
if !yield(pair.Value) {
return
}
}
}
}
42 changes: 42 additions & 0 deletions orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,45 @@ func TestNilMap(t *testing.T) {
assert.Nil(t, om.Newest())
})
}

func TestIterators(t *testing.T) {
om := New[int, any]()
om.Set(1, "bar")
om.Set(2, 28)
om.Set(3, 100)
om.Set(4, "baz")
om.Set(5, "28")
om.Set(6, "100")
om.Set(7, "baz")
om.Set(8, "baz")

expectedKeys := []int{1, 2, 3, 4, 5, 6, 7, 8}
expectedValues := []any{"bar", 28, 100, "baz", "28", "100", "baz", "baz"}

var keys []int
var values []any

for k, v := range om.All() {
keys = append(keys, k)
values = append(values, v)
}

assert.Equal(t, expectedKeys, keys)
assert.Equal(t, expectedValues, values)

keys = []int{}

for k := range om.Keys() {
keys = append(keys, k)
}

assert.Equal(t, expectedKeys, keys)

values = []any{}

for v := range om.Values() {
values = append(values, v)
}

assert.Equal(t, expectedValues, values)
}