-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterator.go
117 lines (94 loc) · 2.2 KB
/
iterator.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/skiplist
//
package skiplist
import (
"github.com/fogfish/golem/trait/pair"
"github.com/fogfish/golem/trait/seq"
)
// Build generic iterate over Set elements
//
// seq := skiplist.ForSet(set, set.Successor(key))
// for has := seq != nil; has; has = seq.Next() {
// seq.Key()
// }
func ForSet[K Key](set *Set[K], el *Element[K]) seq.Seq[K] {
if el == nil {
return nil
}
return &forSet[K]{el}
}
type forSet[K Key] struct {
el *Element[K]
}
func (it *forSet[K]) Value() K { return it.el.Key }
func (it *forSet[K]) Next() bool {
if it.el == nil {
return false
}
it.el = it.el.Next()
return it.el != nil
}
// Iterate over Map elements
//
// seq := skiplist.ForMap(kv, kv.Successor(key))
// for has := seq != nil; has; has = seq.Next() {
// seq.Key()
// }
func ForMap[K Key, V any](kv *Map[K, V], el *Pair[K, V]) pair.Seq[K, V] {
if el == nil {
return nil
}
return &forMap[K, V]{el: el}
}
type forMap[K Key, V any] struct {
el *Pair[K, V]
}
func (it *forMap[K, V]) Key() K { return it.el.Key }
func (it *forMap[K, V]) Value() V { return it.el.Value }
func (it *forMap[K, V]) Next() bool {
if it.el == nil {
return false
}
it.el = it.el.Next()
return it.el != nil
}
func ForHashMap[K Key, V any](kv *HashMap[K, V], key *Element[K]) pair.Seq[K, V] {
if key == nil {
return nil
}
val, _ := kv.Get(key.Key)
return &forHashMap[K, V]{key: key, val: val, kv: kv}
}
func ForGF2[K Num](gf2 *GF2[K], key *Element[K]) pair.Seq[K, Arc[K]] {
if key == nil {
return nil
}
val, _ := gf2.Get(key.Key)
return &forHashMap[K, Arc[K]]{key: key, val: val, kv: gf2}
}
type getter[K Key, V any] interface {
Get(K) (V, bool)
}
type forHashMap[K Key, V any] struct {
key *Element[K]
val V
kv getter[K, V]
}
func (it *forHashMap[K, V]) Key() K { return it.key.Key }
func (it *forHashMap[K, V]) Value() V { return it.val }
func (it *forHashMap[K, V]) Next() bool {
if it.key == nil {
return false
}
it.key = it.key.Next()
if it.key == nil {
return false
}
it.val, _ = it.kv.Get(it.key.Key)
return true
}