-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImmutableQueue.java
More file actions
564 lines (536 loc) · 15.9 KB
/
Copy pathImmutableQueue.java
File metadata and controls
564 lines (536 loc) · 15.9 KB
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import java.util.*;
/**
* An implementation of an Immutable Queue
*
* Inspired by work featured on Fabulous Adventures In Coding by Eric Lippert
* published 2008.02.12
* http://blogs.msdn.com/b/ericlippert/archive/2008/02/12/immutability-in-c-part-eleven-a-working-double-ended-queue.aspx
*
* @author Accalia de Elementia <accalia.de.elementia@gmail.com>
*
* @version 0.5.0
*/
public final class ImmutableQueue<E> implements ImmutableCollection<E> {
private final Quelette<E> right;
private final ImmutableQueue<Quelette<E>> mid;
private final Quelette<E> left;
/**
* The size of the queue.
*
* @since 0.5.0
*/
public final int length;
/**
* True if queue contains no data, false otherwise.
*
* @since 0.5.0
*/
public final boolean empty;
/**
* Create a new, empty, ImmutableQueue.
*
* @since 0.5.0
*/
public ImmutableQueue () {
right = new Zero();
mid = null;
left = new Zero();
length = 0;
empty = true;
}
/**
* Create a new ImmutableQueue with data.
*
* *PRIVATE* constructor.
*
* @param right The right Quelette
* @param mid The middle ImmutableQueue of Quelettes
* @param left The left Quelette
* @param length The size of the queue
*
* @since 0.5.0
*/
private ImmutableQueue (Quelette<E> right, ImmutableQueue<Quelette<E>> mid, Quelette<E> left, int length) {
//TODO: Get better way of determining size of the queue
this.right = right;
this.mid = mid;
this.left = left;
this.length = length;
empty = length == 0;
}
/**
* Returns the size of the queue.
*
* @see #length
*
* @since 0.5.0
*/
public int size() { return length; }
/**
* Returns true if the queue contains no data, false otherwise.
*
* @see #empty
*
* @since 0.5.0
*/
public boolean isEmpty() { return empty; }
/**
* Push a data value to the right end of the queue.
*
* @param e Data value to push
* @return A new ImmutableQueue with updated state
*
* @since 0.1.0
*/
public ImmutableQueue<E> pushRight(E e) {
Quelette<E> right = this.right;
ImmutableQueue<Quelette<E>> mid = this.mid;
Quelette<E> left = this.left;
if (right.isFull()) {
if (null != mid) {
// Mid already used
mid = mid.pushRight(right);
right = new One(e);
} else if (left.isFull()) {
// no mid, and left is full too
mid = new ImmutableQueue<Quelette<E>>().pushRight(right);
right = new One(e);
} else {
// no mid, and left has room
left = left.pushRight(right.peekLeft());
right = right.popLeft().pushRight(e);
}
} else {
right = right.pushRight(e);
}
return new ImmutableQueue<E>(right, mid, left, length + 1);
}
/**
* Push a data value to the left end of the queue.
*
* @param e Data value to push
* @return A new ImmutableQueue with updated state
*
* @since 0.1.0
*/
public ImmutableQueue<E> pushLeft(E e) {
Quelette<E> right = this.right;
ImmutableQueue<Quelette<E>> mid = this.mid;
Quelette<E> left = this.left;
if (left.isFull()) {
if (null != mid) {
mid = mid.pushLeft(left);
left = new One(e);
} else if (right.isFull()) {
mid = new ImmutableQueue<Quelette<E>>().pushLeft(left);
left = new One(e);
} else {
right = right.pushLeft(left.peekRight());
left = left.popRight().pushLeft(e);
}
} else {
left = left.pushLeft(e);
}
return new ImmutableQueue<E>(right, mid, left, length + 1);
}
/**
* Alias for pushLeft
*
* @param e Data value to push
* @return A new ImmutableQueue with updated state
*
* @see #pushLeft
*
* @since 0.5.0
*/
public ImmutableQueue<E> push(E e) { return pushLeft(e); }
/**
* Alias for pushLeft
*
* @param e Data value to push
* @return A new ImmutableQueue with updated state
*
* @see #pushLeft
*
* @since 0.5.0
*/
public ImmutableQueue<E> add(E e) { return pushLeft(e); }
/**
* Look at the first element on the right.
*
* @return The rightmost data value
* @throws EmptyQueueException if there is no data stored
*
* @since 0.2.0
*/
public E peekRight() {
if (empty) {
throw new EmptyQueueException();
}
if (right.size() == 0) {
if (null != mid) {
return mid.peekRight().peekRight();
}
return left.peekRight();
} else {
return right.peekRight();
}
}
/**
* Look at the first element on the left.
*
* @return The leftmost data value
* @throws EmptyQueueException if there is no data stored
*
* @since 0.2.0
*/
public E peekLeft() {
if (empty) {
throw new EmptyQueueException();
}
if (left.size() == 0) {
if (null != mid) {
return mid.peekLeft().peekLeft();
}
return right.peekLeft();
} else {
return left.peekLeft();
}
}
/**
* Alias for peekRight.
*
* @return The rightmost data value
* @throws EmptyQueueException if there is no data stored
*
* @see #peekRight
*
* @since 0.5.0
*/
public E peek() { return peekRight(); }
/**
* Remove the rightmost element.
*
* @return a new ImmutableQueue with updated state
* @throws EmptyQueueException if there is no data stored
*
* @since 0.3.0
*/
public ImmutableQueue<E> popRight() {
if (empty) {
throw new EmptyQueueException();
}
Quelette<E> right = this.right;
ImmutableQueue<Quelette<E>> mid = this.mid;
Quelette<E> left = this.left;
if (right.size() == 0) {
if (null == mid) {
left = left.popRight();
} else {
right = mid.peekRight();
mid = mid.popRight();
right = right.popRight();
if (mid.size() == 0) {
mid = null;
}
}
} else {
right = right.popRight();
}
return new ImmutableQueue<E> (right, mid, left, length - 1);
}
/**
* Remove the leftmost element.
*
* @return a new ImmutableQueue with updated state
* @throws EmptyQueueException if there is no data stored
*
* @since 0.3.0
*/
public ImmutableQueue<E> popLeft() {
if (empty) {
throw new EmptyQueueException();
}
Quelette<E> right = this.right;
ImmutableQueue<Quelette<E>> mid = this.mid;
Quelette<E> left = this.left;
if (left.size() == 0) {
if (null == mid) {
right = right.popLeft();
} else {
left = mid.peekLeft();
mid = mid.popLeft();
left = left.popLeft();
if (mid.size() == 0) {
mid = null;
}
}
} else {
left = left.popLeft();
}
return new ImmutableQueue<E> (right, mid, left, length - 1);
}
/**
* Alias for popRight.
*
* @return a new ImmutableQueue with updated state
* @throws EmptyQueueException if there is no data stored
*
* @see #popRight
*
* @since 0.3.0
*/
public ImmutableQueue<E> pop() { return popRight(); }
/**
* Add a lot of items to the queue.
*
* Inserts in iteration order through calls to {@link #add}
*
* @param c An iterable object containingobjects to add.
* @return A new ImmutableQueue with updated state
*
* @see #add
*
* @since 0.5.0
*/
public ImmutableQueue<E> addAll (Iterable<? extends E> c) {
ImmutableQueue<E> q = this;
for(E e: c) {
q = q.add(e);
}
return q;
}
/**
* Create an array of items to be found in the ImmutableQueue.
*
* @return And array of elements to be found in the ImmutableQueue
*
* @since 0.6.0
*/
public Object[] toArray() {
return toArray(new Object[length]);
}
/**
* Create an array of items to be found in the ImmutableQueue.
*
* @return And array of elements to be found in the ImmutableQueue
*
* @since 0.6.0
*/
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
if (a.length < length) {
a = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), length);
}
int i = 0;
for (E e: this) {
a[i] = (T) e;
i += 1;
}
return a;
}
/**
* Create an iterator to iterate over the items in the collection.
*
* @return an ImmutableQueueIterator
*
* @since 0.4.0
*/
public Iterator<E> iterator() { return new ImmutableQueueIterator(this); }
/**
* An iterator for ImmutableQueues
*
* @author Accalia de Elementia <accalia.de.elementia@gmail.com>
* @version 1.0.0
*/
private final class ImmutableQueueIterator implements Iterator<E> {
private ImmutableQueue<E> queue;
/**
* Store the collection to iterate.
*
* @since 1.0.0
*/
ImmutableQueueIterator(ImmutableQueue<E> queue){
this.queue = queue;
}
/**
* @return true if the queue still has data, false otherwise.
*
* @since 1.0.0
*/
public boolean hasNext() { return ! queue.empty; }
/**
* @return the next data value in the queue.
* @throws EmptyQueueException if the queue has no data
*
* @since 1.0.0
*/
public E next() {
if (queue.empty) {
throw new NoSuchElementException();
}
E e = queue.peek();
queue = queue.pop();
return e;
}
/**
* Not supported.
*
* @throws UnsupportedOperationException unconditionally
*
* @since 1.0.0
*/
public void remove() { throw new UnsupportedOperationException(); }
}
/**
* Interface for finite sized quelettes to be used to build a big queue.
*
* @author Accalia de Elementia <accalia.de.elementia@gmail.com>
* @version 1.0.0
*/
private interface Quelette<E> {
/**
* @return the capacity of the Quelette.
*
* @since 1.0.0
*/
public int size ();
/**
* @return True if the Quelette cannot be pushed to.
*
* @since 1.0.0
*/
public boolean isFull();
/**
* Look at the left value of the Quelette.
*
* @return The leftmost value in the quelette.
* @throws EmptyQueueException if has no data
*
* @since 1.0.0
*/
public E peekLeft();
/**
* Look at the right value of the Quelette
*
* @return The rightmost value in the Quelette
* @throws EmptyQueueException if has no data
*
* @since 1.0.0
*/
public E peekRight();
/**
* Push value to the left of the Quelette.
*
* @return A new Quelette with updated state.
* @throws EmptyQueueException if Quelette is full. This should be changed to better exception.
*
* @since 1.0.0
*/
public Quelette<E> pushLeft(E e);
/**
* Push falue to the right of the queue.
*
* @return A new Quelette with updated state.
* @throws EmptyQueueException if Quelette is full. This should be changed to better exception.
*
* @since 1.0.0
*/
public Quelette<E> pushRight(E e);
/**
* Remove leftmost value of the Quelette
*
* @return A new quelette with updated state.
* @throws EmptyQueueException if has no data.
*
* @since 1.0.0
*/
public Quelette<E> popLeft();
/**
* Remove rightmost value of the Quelette.
*
* @return A new Quelette with updated state.
* @throws EmptyQueueException if has no ata
*
* @since 1.0.0
*/
public Quelette<E> popRight();
}
private final class Zero implements Quelette<E> {
public Zero () {}
public int size() { return 0; }
public boolean isFull() { return false; }
public E peekLeft() { throw new EmptyQueueException(); }
public E peekRight() { throw new EmptyQueueException(); }
public Quelette<E> pushLeft(E e) { return new One(e); }
public Quelette<E> pushRight(E e) { return new One(e); }
public Quelette<E> popLeft() { throw new EmptyQueueException(); }
public Quelette<E> popRight() { throw new EmptyQueueException(); }
}
private final class One implements Quelette<E> {
private final E v1;
public One (E v1) { this.v1 = v1; }
public int size () { return 1; }
public boolean isFull() { return false; }
public E peekRight() { return v1; }
public E peekLeft() { return v1; }
public Quelette<E> pushRight(E e) { return new Two(e, v1); }
public Quelette<E> pushLeft(E e) { return new Two(v1, e); }
public Quelette<E> popRight() { return new Zero(); }
public Quelette<E> popLeft() { return new Zero(); }
}
private final class Two implements Quelette<E> {
private final E v1;
private final E v2;
public Two(E v1, E v2) {
this.v1 = v1;
this.v2 = v2;
}
public int size () { return 2; }
public boolean isFull() { return false; }
public E peekRight() { return v1; }
public E peekLeft() { return v2; }
public Quelette<E> pushRight(E e) { return new Three(e, v1, v2); }
public Quelette<E> pushLeft(E e) { return new Three(v1, v2, e); }
public Quelette<E> popRight() { return new One(v2); }
public Quelette<E> popLeft() { return new One(v1); }
}
private final class Three implements Quelette<E> {
private final E v1;
private final E v2;
private final E v3;
public Three(E v1, E v2, E v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public int size () { return 3; }
public boolean isFull() { return false; }
public E peekRight() { return v1; }
public E peekLeft() { return v3; }
public Quelette<E> pushRight(E e) { return new Four(e, v1, v2, v3); }
public Quelette<E> pushLeft(E e) { return new Four(v1, v2, v3, e); }
public Quelette<E> popRight() { return new Two(v2, v3); }
public Quelette<E> popLeft() { return new Two(v1, v2); }
}
private final class Four implements Quelette<E> {
private final E v1;
private final E v2;
private final E v3;
private final E v4;
public Four(E v1, E v2, E v3, E v4) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
}
public int size () { return 4; }
public boolean isFull() { return true; }
public E peekRight() { return v1; }
public E peekLeft() { return v4; }
public Quelette<E> pushRight(E e) { throw new EmptyQueueException(); }
public Quelette<E> pushLeft(E e) { throw new EmptyQueueException(); }
public Quelette<E> popRight() { return new Three(v2, v3, v4); }
public Quelette<E> popLeft() { return new Three(v1, v2, v3); }
}
}