-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
236 lines (200 loc) · 5.57 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package immutableList
import "fmt"
type Object interface{}
type Processor func(Object)
type Visitor func(int, Object)
type reporter func(message string)
type Iterator interface {
Next() bool
Get() Object
}
type List interface {
Size() int
Get(index int) Object
GetFirst() Object
GetLast() Object
Append(value Object) List
AppendList(other List) List
Insert(indexBefore int, value Object) List
InsertList(indexBefore int, other List) List
Head(length int) List
Tail(index int) List
SubList(offset int, limit int) List
ForEach(proc Processor)
Visit(offset int, limit int, v Visitor)
Select(predicate func(Object) bool) List
Slice(offset, limit int) []Object
Delete(index int) List
DeleteRange(offset int, limit int) List
Set(index int, value Object) List
FwdIterate() Iterator
checkInvariants(r reporter)
IsEmpty() bool
Push(value Object) List
Pop() (Object, List)
}
type listImpl struct {
root node
}
var sharedEmptyListInstance List = &listImpl{sharedEmptyNode}
func Create() List {
return sharedEmptyListInstance
}
func createListNode(root node) List {
if root.size() == 0 {
return sharedEmptyListInstance
} else {
return &listImpl{root: root}
}
}
func (this *listImpl) FwdIterate() Iterator {
return createIterator(this.root)
}
func (this *listImpl) Size() int {
return this.root.size()
}
func (this *listImpl) Get(index int) Object {
return this.root.get(index)
}
func (this *listImpl) GetFirst() Object {
return this.root.getFirst()
}
func (this *listImpl) GetLast() Object {
return this.root.getLast()
}
func (this *listImpl) Append(value Object) List {
return createListNode(this.root.append(value))
}
func (this *listImpl) AppendList(other List) List {
otherImpl := other.(*listImpl)
return createListNode(appendNodes(this.root, otherImpl.root))
}
func (this *listImpl) Insert(indexBefore int, value Object) List {
return createListNode(this.root.insert(indexBefore, value))
}
func (this *listImpl) InsertList(indexBefore int, other List) List {
currentSize := this.root.size()
if indexBefore < 0 || indexBefore > currentSize {
panic(fmt.Sprintf("index out of bounds: size=%d index=%d", currentSize, indexBefore))
}
if indexBefore == 0 {
return other.AppendList(this)
}
if indexBefore == currentSize {
return this.AppendList(other)
}
return this.Head(indexBefore).AppendList(other).AppendList(this.Tail(indexBefore))
}
func (this *listImpl) Delete(index int) List {
return createListNode(this.root.delete(index))
}
func (this *listImpl) DeleteRange(offset int, limit int) List {
size := this.Size()
if offset < 0 || limit < offset || limit > size {
panic(fmt.Sprintf("invalid offset or limit: size=%d offset=%d limit=%d", size, offset, limit))
}
if offset == 0 && limit == size {
return sharedEmptyListInstance
}
if offset == limit {
return this
}
var root node
if offset == 0 {
root = this.root.tail(limit)
} else if limit == size {
root = this.root.head(offset)
} else {
root = appendNodes(this.root.head(offset), this.root.tail(limit))
}
return createListNode(root)
}
func (this *listImpl) Head(length int) List {
return createListNode(this.root.head(length))
}
func (this *listImpl) Tail(index int) List {
return createListNode(this.root.tail(index))
}
func (this *listImpl) SubList(offset int, limit int) List {
size := this.Size()
if offset < 0 || limit < offset || limit > size {
panic(fmt.Sprintf("invalid offset or limit: size=%d offset=%d limit=%d", size, offset, limit))
}
if offset == 0 && limit == size {
return this
}
if offset == limit {
return sharedEmptyListInstance
}
var root node
if offset == 0 {
root = this.root.head(limit)
} else if limit == size {
root = this.root.tail(offset)
} else {
root = this.root.head(limit).tail(offset)
}
return createListNode(root)
}
func (this *listImpl) ForEach(proc Processor) {
this.root.forEach(proc)
}
func (this *listImpl) Visit(offset int, limit int, visitor Visitor) {
if offset < 0 || limit < offset || limit > this.Size() {
panic(fmt.Sprintf("invalid offset or limit: size=%d offset=%d limit=%d", this.Size(), offset, limit))
}
this.root.visit(0, offset, limit, visitor)
}
func (this *listImpl) Select(predicate func(Object) bool) List {
answer := CreateBuilder()
this.root.forEach(func(obj Object) {
if predicate(obj) {
answer.Add(obj)
}
})
return answer.Build()
}
func (this *listImpl) Slice(offset, limit int) []Object {
if offset < 0 || limit < offset || limit > this.Size() {
panic(fmt.Sprintf("invalid offset or limit: size=%d offset=%d limit=%d", this.Size(), offset, limit))
}
if limit == offset {
return make([]Object, 0)
}
answer := make([]Object, limit-offset)
this.root.visit(0, offset, limit, func(index int, obj Object) {
answer[index-offset] = obj
})
return answer
}
func (this *listImpl) Set(index int, value Object) List {
if index == this.root.size() {
return createListNode(this.root.append(value))
} else {
return createListNode(this.root.set(index, value))
}
}
func (this *listImpl) checkInvariants(report reporter) {
if this.Size() == 0 && this != sharedEmptyListInstance {
report("empty list is not the sharedEmptyListInstance")
}
this.root.checkInvariants(report, true)
}
func (this *listImpl) IsEmpty() bool {
return this.root.size() == 0
}
func (this *listImpl) Push(value Object) List {
return createListNode(this.root.prepend(value))
}
func (this *listImpl) Pop() (Object, List) {
switch this.Size() {
case 0:
panic("Pop called on empty List")
case 1:
value := this.root.getFirst()
return value, sharedEmptyListInstance
default:
value, newRoot := this.root.pop()
return value, createListNode(newRoot)
}
}