forked from Workiva/go-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persistent.go
255 lines (212 loc) · 6.73 KB
/
persistent.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
Copyright 2015 Workiva, LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
Package list provides list implementations. Currently, this includes a
persistent, immutable linked list.
*/
package list
import "errors"
var (
// Empty is an empty PersistentList.
Empty PersistentList = &emptyList{}
// ErrEmptyList is returned when an invalid operation is performed on an
// empty list.
ErrEmptyList = errors.New("Empty list")
)
// PersistentList is an immutable, persistent linked list.
type PersistentList interface {
// Head returns the head of the list. The bool will be false if the list is
// empty.
Head() (interface{}, bool)
// Tail returns the tail of the list. The bool will be false if the list is
// empty.
Tail() (PersistentList, bool)
// IsEmpty indicates if the list is empty.
IsEmpty() bool
// Length returns the number of items in the list.
Length() uint
// Add will add the item to the list, returning the new list.
Add(head interface{}) PersistentList
// Insert will insert the item at the given position, returning the new
// list or an error if the position is invalid.
Insert(val interface{}, pos uint) (PersistentList, error)
// Get returns the item at the given position or an error if the position
// is invalid.
Get(pos uint) (interface{}, bool)
// Remove will remove the item at the given position, returning the new
// list or an error if the position is invalid.
Remove(pos uint) (PersistentList, error)
// Find applies the predicate function to the list and returns the first
// item which matches.
Find(func(interface{}) bool) (interface{}, bool)
// FindIndex applies the predicate function to the list and returns the
// index of the first item which matches or -1 if there is no match.
FindIndex(func(interface{}) bool) int
// Map applies the function to each entry in the list and returns the
// resulting slice.
Map(func(interface{}) interface{}) []interface{}
}
type emptyList struct{}
// Head returns the head of the list. The bool will be false if the list is
// empty.
func (e *emptyList) Head() (interface{}, bool) {
return nil, false
}
// Tail returns the tail of the list. The bool will be false if the list is
// empty.
func (e *emptyList) Tail() (PersistentList, bool) {
return nil, false
}
// IsEmpty indicates if the list is empty.
func (e *emptyList) IsEmpty() bool {
return true
}
// Length returns the number of items in the list.
func (e *emptyList) Length() uint {
return 0
}
// Add will add the item to the list, returning the new list.
func (e *emptyList) Add(head interface{}) PersistentList {
return &list{head, e}
}
// Insert will insert the item at the given position, returning the new list or
// an error if the position is invalid.
func (e *emptyList) Insert(val interface{}, pos uint) (PersistentList, error) {
if pos == 0 {
return e.Add(val), nil
}
return nil, ErrEmptyList
}
// Get returns the item at the given position or an error if the position is
// invalid.
func (e *emptyList) Get(pos uint) (interface{}, bool) {
return nil, false
}
// Remove will remove the item at the given position, returning the new list or
// an error if the position is invalid.
func (e *emptyList) Remove(pos uint) (PersistentList, error) {
return nil, ErrEmptyList
}
// Find applies the predicate function to the list and returns the first item
// which matches.
func (e *emptyList) Find(func(interface{}) bool) (interface{}, bool) {
return nil, false
}
// FindIndex applies the predicate function to the list and returns the index
// of the first item which matches or -1 if there is no match.
func (e *emptyList) FindIndex(func(interface{}) bool) int {
return -1
}
// Map applies the function to each entry in the list and returns the resulting
// slice.
func (e *emptyList) Map(func(interface{}) interface{}) []interface{} {
return nil
}
type list struct {
head interface{}
tail PersistentList
}
// Head returns the head of the list. The bool will be false if the list is
// empty.
func (l *list) Head() (interface{}, bool) {
return l.head, true
}
// Tail returns the tail of the list. The bool will be false if the list is
// empty.
func (l *list) Tail() (PersistentList, bool) {
return l.tail, true
}
// IsEmpty indicates if the list is empty.
func (l *list) IsEmpty() bool {
return false
}
// Length returns the number of items in the list.
func (l *list) Length() uint {
curr := l
length := uint(0)
for {
length += 1
tail, _ := curr.Tail()
if tail.IsEmpty() {
return length
}
curr = tail.(*list)
}
}
// Add will add the item to the list, returning the new list.
func (l *list) Add(head interface{}) PersistentList {
return &list{head, l}
}
// Insert will insert the item at the given position, returning the new list or
// an error if the position is invalid.
func (l *list) Insert(val interface{}, pos uint) (PersistentList, error) {
if pos == 0 {
return l.Add(val), nil
}
nl, err := l.tail.Insert(val, pos-1)
if err != nil {
return nil, err
}
return nl.Add(l.head), nil
}
// Get returns the item at the given position or an error if the position is
// invalid.
func (l *list) Get(pos uint) (interface{}, bool) {
if pos == 0 {
return l.head, true
}
return l.tail.Get(pos - 1)
}
// Remove will remove the item at the given position, returning the new list or
// an error if the position is invalid.
func (l *list) Remove(pos uint) (PersistentList, error) {
if pos == 0 {
nl, _ := l.Tail()
return nl, nil
}
nl, err := l.tail.Remove(pos - 1)
if err != nil {
return nil, err
}
return &list{l.head, nl}, nil
}
// Find applies the predicate function to the list and returns the first item
// which matches.
func (l *list) Find(pred func(interface{}) bool) (interface{}, bool) {
if pred(l.head) {
return l.head, true
}
return l.tail.Find(pred)
}
// FindIndex applies the predicate function to the list and returns the index
// of the first item which matches or -1 if there is no match.
func (l *list) FindIndex(pred func(interface{}) bool) int {
curr := l
idx := 0
for {
if pred(curr.head) {
return idx
}
tail, _ := curr.Tail()
if tail.IsEmpty() {
return -1
}
curr = tail.(*list)
idx += 1
}
}
// Map applies the function to each entry in the list and returns the resulting
// slice.
func (l *list) Map(f func(interface{}) interface{}) []interface{} {
return append(l.tail.Map(f), f(l.head))
}