/
list.go
185 lines (145 loc) · 3.51 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
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
// List data structure
package golsp
type Item struct {
next *Item
Object Object
}
type List struct {
First *Item
Last *Item
Length int
branches map[int]*Item
}
func ListFromSlice(slice []Object) List {
l := List{}
for _, obj := range slice { l.Append(obj) }
return l
}
func (l *List) at(index int) *Item {
if index < 0 || index >= l.Length { return nil }
if index == l.Length - 1 { return l.Last }
current := l.First
for i := 0; i < index; i++ { current = l.Next(current, i) }
return current
}
func (l *List) slice(begin int, end int) List {
if end <= begin { return List{} }
slice := List{
First: l.at(begin),
Last: l.at(end - 1),
Length: end - begin,
branches: make(map[int]*Item),
}
for index, branch := range l.branches {
if index >= begin && index < end {
slice.branches[index - begin] = branch
}
}
return slice
}
func (l *List) sublist(begin *Item, index int) List {
sublist := List{
First: begin,
Last: l.Last,
Length: l.Length - index,
branches: make(map[int]*Item),
}
for i, branch := range l.branches {
if i >= index { sublist.branches[i] = branch }
}
return sublist
}
func (l *List) Next(item *Item, index int) *Item {
if (index >= l.Length) { return nil }
branch, exists := l.branches[index]
if exists { return branch }
return item.next
}
func (l *List) ToSlice() []Object {
slice := make([]Object, 0, l.Length)
ptr := l.First
for i := 0; i < l.Length; i++ {
slice = append(slice, ptr.Object)
ptr = l.Next(ptr, i)
}
return slice
}
func (l *List) Copy() List {
copy := List{}
slice := l.ToSlice()
for _, obj := range slice { copy.Append(CopyObject(obj)) }
return copy
}
func (l *List) Append(obj Object) {
newitem := Item{Object: obj}
defer func() {
l.Last = &newitem
l.Length++
}()
if l.Length == 0 {
l.First = &newitem
return
}
if l.Last.next == nil {
l.Last.next = &newitem
return
}
if l.branches == nil { l.branches = make(map[int]*Item) }
l.branches[l.Length - 1] = &newitem
}
func (self *List) Join(other List) {
if (other.Length == 0) { return }
defer func() {
if (other.Length == 0) { return }
for index, branch := range other.branches {
self.branches[index + self.Length] = branch
}
self.Last = other.Last
self.Length += other.Length
}()
if self.Length == 0 {
self.First = other.First
return
}
if self.Last.next == nil {
self.Last.next = other.First
return
}
if self.branches == nil { self.branches = make(map[int]*Item) }
self.branches[self.Length - 1] = other.First
}
func (l *List) Index(index int) Object {
if index < 0 { index += l.Length }
item := l.at(index)
if item == nil { return UndefinedObject() }
return item.Object
}
func (l *List) Slice(begin int, end int) Object {
if begin < 0 { begin += l.Length }
if begin < 0 || begin >= l.Length { return UndefinedObject() }
if end < 0 { end += l.Length }
if end < 0 { return UndefinedObject() }
if end > l.Length { end = l.Length }
return Object{
Type: ObjectTypeList,
Elements: l.slice(begin, end),
}
}
func (l *List) SliceStep(begin int, end int, step int, sliceAll bool) Object {
if begin < 0 { begin += l.Length }
if begin < 0 || begin >= l.Length { return UndefinedObject() }
if end < 0 && !sliceAll { end += l.Length }
if end < 0 && !sliceAll { return UndefinedObject() }
if end > l.Length { end = l.Length }
slice := l.ToSlice()
newlist := List{}
for i := begin; i != end; i += step {
if i >= len(slice) { break }
if i < 0 { break }
newlist.Append(slice[i])
}
return Object{
Type: ObjectTypeList,
Elements: newlist,
}
}