-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderlist.go
343 lines (298 loc) · 8.32 KB
/
orderlist.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package orderbook
// orderList is a doubly-linked list of orders.
type orderList struct {
first *order
last *order
size int
}
// New instantiates a new list and adds the passed values, if any, to the list
func newOrderList(orders ...*order) orderList {
list := orderList{}
if len(orders) > 0 {
list.add(orders...)
}
return list
}
// Add appends a value (one or more) at the end of the list.
func (list *orderList) add(orders ...*order) {
for _, o := range orders {
//newElement := &element{value: value, prev: list.last}
if list.size == 0 {
list.first = o
list.last = o
} else {
list.last.next = o
list.last = o
}
list.size++
}
}
// // Prepend prepends a values (or more)
// func (list *orderList) Prepend(values ...interface{}) {
// // in reverse to keep passed order i.e. ["c","d"] -> Prepend(["a","b"]) -> ["a","b","c",d"]
// for v := len(values) - 1; v >= 0; v-- {
// newElement := &element{value: values[v], next: list.first}
// if list.size == 0 {
// list.first = newElement
// list.last = newElement
// } else {
// list.first.prev = newElement
// list.first = newElement
// }
// list.size++
// }
// }
// get returns the element at index.
// Second return parameter is true if index is within bounds of the array and array is not empty, otherwise false.
func (list *orderList) get(index int) (*order, bool) {
if !list.withinRange(index) {
return nil, false
}
// determine traveral direction, last to first or first to last
if list.size-index < index {
element := list.last
for e := list.size - 1; e != index; e, element = e-1, element.prev {
}
return element, true
}
element := list.first
for e := 0; e != index; e, element = e+1, element.next {
}
return element, true
}
func (list *orderList) removeID(id OrderID) {
var element *order
element = list.first
for {
if element.id == id {
break
}
element = element.next
}
if element == list.first {
list.first = element.next
}
if element == list.last {
list.last = element.prev
}
if element.prev != nil {
element.prev.next = element.next
}
if element.next != nil {
element.next.prev = element.prev
}
element = nil
list.size--
}
// Remove removes the element at the given index from the list.
func (list *orderList) remove(index int) {
if !list.withinRange(index) {
return
}
if list.size == 1 {
list.Clear()
return
}
var element *order
// determine traversal direction, last to first or first to last
if list.size-index < index {
element = list.last
for e := list.size - 1; e != index; e, element = e-1, element.prev {
}
} else {
element = list.first
for e := 0; e != index; e, element = e+1, element.next {
}
}
if element == list.first {
list.first = element.next
}
if element == list.last {
list.last = element.prev
}
if element.prev != nil {
element.prev.next = element.next
}
if element.next != nil {
element.next.prev = element.prev
}
element = nil
list.size--
}
// Contains check if values (one or more) are present in the set.
// All values have to be present in the set for the method to return true.
// Performance time complexity of n^2.
// Returns true if no arguments are passed at all, i.e. set is always super-set of empty set.
// func (list *orderList) Contains(values ...interface{}) bool {
// if len(values) == 0 {
// return true
// }
// if list.size == 0 {
// return false
// }
// for _, value := range values {
// found := false
// for element := list.first; element != nil; element = element.next {
// if element.value == value {
// found = true
// break
// }
// }
// if !found {
// return false
// }
// }
// return true
// }
// Values returns all elements in the list.
func (list *orderList) Values() []*order {
values := make([]*order, list.size, list.size)
for e, element := 0, list.first; element != nil; e, element = e+1, element.next {
values[e] = element
}
return values
}
//IndexOf returns index of provided element
func (list *orderList) IndexOf(value interface{}) int {
if list.size == 0 {
return -1
}
for index, element := range list.Values() {
if element == value {
return index
}
}
return -1
}
// Empty returns true if list does not contain any elements.
func (list *orderList) Empty() bool {
return list.size == 0
}
// Size returns number of elements within the list.
func (list *orderList) Size() int {
return list.size
}
// Clear removes all elements from the list.
func (list *orderList) Clear() {
list.size = 0
list.first = nil
list.last = nil
}
// // Sort sorts values (in-place) using.
// func (list *orderList) Sort(comparator utils.Comparator) {
// if list.size < 2 {
// return
// }
// values := list.Values()
// utils.Sort(values, comparator)
// list.Clear()
// list.Add(values...)
// }
// Swap swaps values of two elements at the given indices.
// func (list *orderList) Swap(i, j int) {
// if list.withinRange(i) && list.withinRange(j) && i != j {
// var element1, element2 *element
// for e, currentElement := 0, list.first; element1 == nil || element2 == nil; e, currentElement = e+1, currentElement.next {
// switch e {
// case i:
// element1 = currentElement
// case j:
// element2 = currentElement
// }
// }
// element1.value, element2.value = element2.value, element1.value
// }
// }
// Insert inserts values at specified index position shifting the value at that position (if any) and any subsequent elements to the right.
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
// func (list *orderList) Insert(index int, values ...interface{}) {
// if !list.withinRange(index) {
// // Append
// if index == list.size {
// list.add(values...)
// }
// return
// }
// list.size += len(values)
// var beforeElement *element
// var foundElement *element
// // determine traversal direction, last to first or first to last
// if list.size-index < index {
// foundElement = list.last
// for e := list.size - 1; e != index; e, foundElement = e-1, foundElement.prev {
// beforeElement = foundElement.prev
// }
// } else {
// foundElement = list.first
// for e := 0; e != index; e, foundElement = e+1, foundElement.next {
// beforeElement = foundElement
// }
// }
// if foundElement == list.first {
// oldNextElement := list.first
// for i, value := range values {
// newElement := &element{value: value}
// if i == 0 {
// list.first = newElement
// } else {
// newElement.prev = beforeElement
// beforeElement.next = newElement
// }
// beforeElement = newElement
// }
// oldNextElement.prev = beforeElement
// beforeElement.next = oldNextElement
// } else {
// oldNextElement := beforeElement.next
// for _, value := range values {
// newElement := &element{value: value}
// newElement.prev = beforeElement
// beforeElement.next = newElement
// beforeElement = newElement
// }
// oldNextElement.prev = beforeElement
// beforeElement.next = oldNextElement
// }
// }
// Set value at specified index position
// Does not do anything if position is negative or bigger than list's size
// Note: position equal to list's size is valid, i.e. append.
// func (list *orderList) Set(index int, value interface{}) {
// if !list.withinRange(index) {
// // Append
// if index == list.size {
// list.Add(value)
// }
// return
// }
// var foundElement *element
// // determine traversal direction, last to first or first to last
// if list.size-index < index {
// foundElement = list.last
// for e := list.size - 1; e != index; {
// fmt.Println("Set last", index, value, foundElement, foundElement.prev)
// e, foundElement = e-1, foundElement.prev
// }
// } else {
// foundElement = list.first
// for e := 0; e != index; {
// e, foundElement = e+1, foundElement.next
// }
// }
// foundElement.value = value
// }
// // String returns a string representation of container
// func (list *orderList) String() string {
// str := "DoublyLinkedList\n"
// values := []string{}
// for element := list.first; element != nil; element = element.next {
// values = append(values, fmt.Sprintf("%v", element.value))
// }
// str += strings.Join(values, ", ")
// return str
// }
// Check that the index is within bounds of the list
func (list *orderList) withinRange(index int) bool {
return index >= 0 && index < list.size
}