-
Notifications
You must be signed in to change notification settings - Fork 6
/
list.go
93 lines (68 loc) · 1.62 KB
/
list.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
package stdlib
import (
"github.com/greenboxal/agibootstrap/pkg/platform/stdlib/iterators"
"github.com/greenboxal/agibootstrap/pkg/psi"
)
type List[T any] interface {
Collection[T]
}
type NodeCollection[T psi.Node] struct {
psi.NodeBase
}
func (c *NodeCollection[T]) Add(value T) int {
index := c.Length()
c.InsertAt(index, value)
return index
}
func (c *NodeCollection[T]) Get(index int) T {
return c.Children()[index].(T)
}
func (c *NodeCollection[T]) InsertAt(index int, value T) {
c.InsertChildrenAt(index, value)
}
func (c *NodeCollection[T]) Remove(value T) bool {
index := c.IndexOf(value)
if index == -1 {
return false
}
c.RemoveAt(index)
return true
}
func (c *NodeCollection[T]) RemoveAt(index int) T {
child := c.Children()[index]
c.RemoveChildNode(child)
return child.(T)
}
func (c *NodeCollection[T]) IndexOf(value T) int {
return c.IndexOfChild(value)
}
func (c *NodeCollection[T]) Contains(value T) bool {
return c.IndexOf(value) != -1
}
func (c *NodeCollection[T]) Length() int {
return len(c.Children())
}
func (c *NodeCollection[T]) Iterator() iterators.Iterator[T] {
return NewDirectChildrenListIterator(c)
}
type directChildrenListIterator[T psi.Node] struct {
c *NodeCollection[T]
current T
index int
}
func (d *directChildrenListIterator[T]) Next() bool {
if d.index >= d.c.Length() {
return false
}
d.current = d.c.Get(d.index)
d.index++
return true
}
func (d *directChildrenListIterator[T]) Value() T {
return d.current
}
func NewDirectChildrenListIterator[T psi.Node](c *NodeCollection[T]) iterators.Iterator[T] {
return &directChildrenListIterator[T]{
c: c,
}
}