-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelements.go
48 lines (40 loc) · 828 Bytes
/
elements.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
package main
type Elements struct {
items []Element
}
type ElementAction func(int, Element)
type ElementQuery func(Element) bool
func (this *Elements) Add(e Element) {
this.items = append(this.items, e)
}
func (this *Elements) Delete(e Element) {
i := this.IndexOf(e)
if i > -1 {
this.items = append(this.items[:i], this.items[i+1:]...)
}
}
func (this *Elements) IndexOf(e Element) int {
for i, el := range this.items {
if el == e {
return i
}
}
debugf("!Could not find element %v", e)
return -1
}
func (this *Elements) Each(action ElementAction) {
for i, e := range this.items {
action(i, e)
}
}
func (this *Elements) Any(query ElementQuery) bool {
for _, e := range this.items {
if query(e) {
return true
}
}
return false
}
func (this *Elements) Count() int {
return len(this.items)
}