-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathring.go
327 lines (266 loc) · 6.68 KB
/
ring.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
package gring
import ()
type node struct {
next int
prev int
value interface{}
}
// Ring is a circular doubly linked list using array as its underlying storage.
// Nodes in the ring can be detached, reinserted, or swapped.
type Ring struct {
nodes []*node
length int
head int
}
// Creates a new empty ring.
func New() *Ring {
r := &Ring{
nodes: make([]*node, 0, 8),
length: 0,
head: 0,
}
return r
}
// Creates a new ring from a tour.
// A tour is an array of integers that specifies the order of the nodes, e.g. [2, 1, 0, 4, 3].
// It is assumed that the array contains integers from [0,n) where n is the length of the array,
// and each integer occurs only once.
func NewFromArray(tour []int) *Ring {
r := New()
if len(tour) > 0 {
for range tour {
n := &node{}
r.nodes = append(r.nodes, n)
}
for i, v := range tour {
next := i + 1
if next >= len(tour) {
next = 0
}
prev := i - 1
if prev < 0 {
prev = len(tour) - 1
}
r.nodes[v].next = tour[next]
r.nodes[v].prev = tour[prev]
}
r.length = len(tour)
}
return r
}
// Adds a new node to the "end" of the ring.
// "End" of the ring is whichever node that comes before the 0th or the head node.
// If ring is empty, adds the first node.
func (r *Ring) Add() {
r.AddWithValue(nil)
}
// Adds a new node to the "end" of the ring with the specified value.
// "End" of the ring is whichever node that comes before the 0th, or the head node.
// If ring is empty, adds the first node.
func (r *Ring) AddWithValue(v interface{}) {
n := &node{}
n.value = v
if len(r.nodes) == 0 {
n.next = -1
n.prev = -1
} else if len(r.nodes) == 1 {
n.next = 0
n.prev = 0
r.nodes[0].prev = 1
r.nodes[0].next = 1
} else {
index := len(r.nodes)
zprev := r.nodes[r.head].prev
n.next = r.head
n.prev = zprev
r.nodes[r.head].prev = index
r.nodes[zprev].next = index
}
r.nodes = append(r.nodes, n)
r.length++
}
// Sets the value of a particular node.
func (r *Ring) SetValue(n int, v interface{}) {
r.nodes[n].value = v
}
// Returns the value of a particular node
func (r *Ring) Value(n int) interface{} {
return r.nodes[n].value
}
// Detaches node n, and inserts it after the node p, such that the end result becomes p -> n.
// Returns error if ring is empty or p is a detached node.
func (r *Ring) InsertAfter(n, p int) error {
if len(r.nodes) == 0 {
return ErrEmptyRing
}
pnext := r.nodes[p].next
if pnext == n {
pnext = r.nodes[pnext].next
}
if anyIsInvalid(n, p, pnext) {
return ErrInvalidOperationOnDetachedNode
}
r.Detach(n)
r.nodes[n].prev = p
r.nodes[p].next = n
r.nodes[n].next = pnext
r.nodes[pnext].prev = n
r.length++
return nil
}
// Detaches node n, and inserts it before the node p, such that the end result becomes n -> p.
// Returns error if ring is empty or p is a detached node.
func (r *Ring) InsertBefore(n, p int) error {
if len(r.nodes) == 0 {
return ErrEmptyRing
}
pprev := r.nodes[p].prev
if pprev == n {
pprev = r.nodes[pprev].prev
}
if anyIsInvalid(n, p, pprev) {
return ErrInvalidOperationOnDetachedNode
}
r.Detach(n)
r.nodes[n].prev = pprev
r.nodes[pprev].next = n
r.nodes[n].next = p
r.nodes[p].prev = n
r.length++
return nil
}
// Detaches a particular node from the ring, connecting its prev and next nodes together.
// Since Ring is using arrays as its underlying storage, references to detached nodes are still kept, and can be reinserted later.
func (r *Ring) Detach(n int) {
prev := r.nodes[n].prev
next := r.nodes[n].next
if anyIsInvalid(prev, next) {
// already detached
return
}
r.nodes[prev].next = next
r.nodes[next].prev = prev
r.nodes[n].next = -1
r.nodes[n].next = -1
r.length--
if n == r.head {
r.head = next
}
}
// Swaps two nodes in the ring.
func (r *Ring) Swap(a, b int) error {
aprev := r.nodes[a].prev
bprev := r.nodes[b].prev
if anyIsInvalid(a, b, aprev, bprev) {
return ErrInvalidOperationOnDetachedNode
}
r.Detach(a)
r.Detach(b)
var err error
if aprev == b {
err = r.InsertAfter(a, bprev)
err = r.InsertAfter(b, a)
} else if bprev == a {
err = r.InsertAfter(b, aprev)
err = r.InsertAfter(a, b)
} else {
err = r.InsertAfter(a, bprev)
err = r.InsertAfter(b, aprev)
}
return err
}
// Given a node and a target node, sets node's next to target, while still maintaining the loop.
// This will reverse part of the tour.
func (r *Ring) TwoOptSwap(n, target int) error {
oldNext := r.nodes[n].next
oldTargetNext := r.nodes[target].next
if anyIsInvalid(n, target, oldNext, oldTargetNext) {
return ErrInvalidOperationOnDetachedNode
}
// disconnect
r.nodes[oldNext].prev = -1
r.nodes[oldTargetNext].prev = -1
// connect with new one
r.nodes[n].next = target
// reverse the direction. this will loop until we hit oldNext
var old = n
var current = target
for current != -1 {
var oldPrev = r.nodes[current].prev
r.nodes[current].prev = old
r.nodes[current].next = oldPrev
old = current
current = oldPrev
}
r.nodes[oldNext].next = oldTargetNext
r.nodes[oldTargetNext].prev = oldNext
return nil
}
// Reverses the ring direction.
func (r *Ring) Reverse() error {
if len(r.nodes) == 0 {
return ErrEmptyRing
}
current := r.head
prev := r.nodes[current].prev
// stupid golang has no do-while. have to hack it in with the first bool
first := true
for first || (current != r.head && current != -1) {
first = false
r.nodes[current].prev = r.nodes[current].next
r.nodes[current].next = prev
current = prev
prev = r.nodes[current].prev
}
return nil
}
// Tours the ring and returns the node order as an array starting from the "head" node.
func (r *Ring) tour() []int {
tour := make([]int, 0, len(r.nodes))
n := r.head
tour = append(tour, n)
n = r.nodes[n].next
for n != r.head && n != -1 {
tour = append(tour, n)
n = r.nodes[n].next
}
return tour
}
// Returns an iterator for this ring.
// Iterator usually starts from the 0th node, although not guaranteed.
// Returns an error if Ring is empty.
func (r *Ring) Iterator() (*Iterator, error) {
return r.iteratorFrom(r.head)
}
// Returns an iterator for this ring, starting from the node n.
func (r *Ring) iteratorFrom(n int) (*Iterator, error) {
if len(r.nodes) == 0 {
return nil, ErrEmptyRing
}
return &Iterator{start: n, current: -1, r: r}, nil
}
// Duplicates the ring.
func (r *Ring) Clone() *Ring {
clone := &Ring{
nodes: make([]*node, len(r.nodes)),
length: r.length,
head: r.head,
}
for i, n := range r.nodes {
clone.nodes[i] = &node{n.next, n.prev, n.value}
}
return clone
}
// Gets the size of the ring
func (r *Ring) Len() int {
return r.length
}
// Checks if any values is -1
func anyIsInvalid(values ...int) bool {
for _, v := range values {
if v == -1 {
return true
}
}
return false
}