-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_iter.go
191 lines (157 loc) · 2.89 KB
/
memory_iter.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package ezdb
import "sort"
type MemoryIterator[T any] struct {
k []string
m map[string]T
pos int
released bool
prev Iterator[T]
}
func (i *MemoryIterator[T]) Count() int {
return len(i.k)
}
func (i *MemoryIterator[T]) Filter(f FilterFunc[T]) Iterator[T] {
if i.released {
return i
}
k := []string{}
m := map[string]T{}
i.Reset()
for i.Next() {
key, value, _ := i.Get()
if f(key, value) {
k = append(k, key)
m[key] = value
}
}
return MemoryIter(m, k, i)
}
func (i *MemoryIterator[T]) First() bool {
if i.released {
return false
}
i.pos = 0
return true
}
func (i *MemoryIterator[T]) Get() (string, T, error) {
if i.released {
return "", i.m[""], ErrReleased
}
key := i.Key()
return key, i.m[key], nil
}
func (i *MemoryIterator[T]) GetAll() (map[string]T, error) {
m := map[string]T{}
if i.released {
return m, ErrReleased
}
i.Reset()
for i.Next() {
key, value, _ := i.Get()
m[key] = value
}
return m, nil
}
func (i *MemoryIterator[T]) GetAllKeys() []string {
keys := []string{}
if i.released {
return keys
}
i.Reset()
for i.Next() {
keys = append(keys, i.Key())
}
return keys
}
func (i *MemoryIterator[T]) Key() string {
if i.pos < 0 || i.pos >= i.Count() || i.released {
return ""
}
return i.k[i.pos]
}
func (i *MemoryIterator[T]) Last() bool {
if i.released {
return false
}
i.pos = len(i.k) - 1
return true
}
func (i *MemoryIterator[T]) Next() bool {
if i.released {
return false
}
hasNext := i.pos+1 < i.Count()
if hasNext {
i.pos++
}
return hasNext
}
func (i *MemoryIterator[T]) Prev() bool {
if i.released {
return false
}
end := i.pos > 0
if !end {
i.pos--
}
return end
}
func (i *MemoryIterator[T]) Release() {
i.k = []string{}
i.m = map[string]T{}
i.released = true
if i.prev != nil {
i.prev.Release()
}
}
func (i *MemoryIterator[T]) Reset() {
i.pos = -1
}
func (i *MemoryIterator[T]) Sort(f SortFunc[T]) Iterator[T] {
if i.released {
return i
}
s := &valueSort[T]{
a: makeSortable(i.m),
f: f,
}
sort.Stable(s)
k := s.Result()
return MemoryIter(i.m, k, i)
}
func (i *MemoryIterator[T]) SortKeys(f SortFunc[string]) Iterator[T] {
if i.released {
return i
}
s := &keySort{
a: i.k,
f: f,
}
sort.Stable(s)
k := s.Result()
return MemoryIter(i.m, k, i)
}
func (i *MemoryIterator[T]) Value() (T, error) {
key := i.Key()
return i.m[key], nil
}
// MemoryIter creates an in-memory iterator for any data.
// k must have a direct relation to m, representing the sorting of keys in the map, otherwise results may be unpredictable.
//
// If the iterator prev is non-nil, it will be released when this iterator is released.
func MemoryIter[T any](m map[string]T, k []string, prev Iterator[T]) *MemoryIterator[T] {
i := &MemoryIterator[T]{
k: []string{},
m: m,
pos: -1,
prev: prev,
}
if len(k) > 0 {
i.k = k
} else {
for k := range i.m {
i.k = append(i.k, k)
}
}
return i
}