-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrangefunc.go
More file actions
57 lines (51 loc) · 1.46 KB
/
rangefunc.go
File metadata and controls
57 lines (51 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//go:build go1.23
package orderedmap
import "iter"
// All returns an iterator over key-value pairs from m.
// The ordering will be oldest to newest, based on when a key was first set.
func (m *OrderedMap[K, V]) All() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for el := m.list.Front(); el != nil; el = el.Next() {
p := el.Value.(*pair[K, V])
if !yield(p.Key, p.Value) {
return
}
}
}
}
// Backward returns an iterator over key-value pairs from m in reverse.
// The ordering will be newest to oldest, based on when a key was first set.
func (m *OrderedMap[K, V]) Backward() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
for el := m.list.Back(); el != nil; el = el.Prev() {
p := el.Value.(*pair[K, V])
if !yield(p.Key, p.Value) {
return
}
}
}
}
// Keys returns an iterator over keys in m.
// The ordering will be oldest to newest, based on when a key was first set.
func (m *OrderedMap[K, V]) Keys() iter.Seq[K] {
return func(yield func(K) bool) {
for el := m.list.Front(); el != nil; el = el.Next() {
p := el.Value.(*pair[K, V])
if !yield(p.Key) {
return
}
}
}
}
// Values returns an iterator over values in m.
// The ordering will be oldest to newest, based on when a key was first set.
func (m *OrderedMap[K, V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
for el := m.list.Front(); el != nil; el = el.Next() {
p := el.Value.(*pair[K, V])
if !yield(p.Value) {
return
}
}
}
}