-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.go
53 lines (48 loc) · 1.07 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
package iterator
type Items struct {
Keys []interface{}
Items map[interface{}]interface{}
}
type Item struct {
Index int
Key interface{}
Value interface{}
}
func New() (items *Items){
items = new(Items)
items.Items = make(map[interface{}]interface{})
return
}
func (i *Items) Iter() <-chan Item {
ch := make(chan Item, 100)
go func() {
defer close(ch)
for index, key := range i.Keys {
val, ok := i.Items[key]
if ok {
ch <- Item{index, key, val}
}
}
}()
return ch
}
func (i *Items) Add(key interface{}, value interface{}) {
_, ok := i.Items[key]
i.Items[key] = value
if !ok {
i.Keys = append(i.Keys, key)
}
}
func (i *Items) Get(key interface{}) (interface{}, bool) {
value, ok := i.Items[key]
return value, ok
}
func (i *Items) Del(key interface{}) {
delete(i.Items, key)
for id, val := range i.Keys {
if val == key {
i.Keys = append(i.Keys[:id], i.Keys[id+1:]...)
return
}
}
}