-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyDeque.java
More file actions
740 lines (661 loc) · 21.3 KB
/
PyDeque.java
File metadata and controls
740 lines (661 loc) · 21.3 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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
package org.python.modules._collections;
import org.python.core.ArgParser;
import org.python.core.PyIterator;
import org.python.core.PyList;
import org.python.core.PyObject;
import org.python.core.PyTuple;
import org.python.core.PyType;
import org.python.core.Py;
import org.python.core.PyException;
import org.python.core.ThreadState;
import org.python.core.Traverseproc;
import org.python.core.Visitproc;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedSet;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
/**
* PyDeque - This class implements the functionalities of Deque data structure. Deques are a
* generalization of stacks and queues (the name is pronounced 'deck' and is short for 'double-ended
* queue'). Deques support thread-safe, memory efficient appends and pops from either side of the
* deque with approximately the same O(1) performance in either direction.
*
* Though list objects support similar operations, they are optimized for fast fixed-length
* operations and incur O(n) memory movement costs for pop(0) and insert(0, v) operations which
* change both the size and position of the underlying data representation.
*
* collections.deque([iterable[, maxlen]]) - returns a new deque object initialized left-to-right
* (using append()) with data from iterable. If iterable is not specified, the new deque is empty.
* If maxlen is not specified or is None, deques may grow to an arbitrary length. Otherwise, the
* deque is bounded to the specified maximum length. Once a bounded length deque is full, when new
* items are added, a corresponding number of items are discarded from the opposite end.
*/
@ExposedType(name = "collections.deque")
public class PyDeque extends PyObject implements Traverseproc {
public static final PyType TYPE = PyType.fromClass(PyDeque.class);
private long state = 0;
private int size = 0;
private int maxlen = -1;
private Node header = new Node(null, null, null);
public PyDeque() {
this(TYPE);
}
public PyDeque(PyType subType) {
super(subType);
header.left = header.right = header;
}
@ExposedNew
@ExposedMethod
public synchronized final void deque___init__(PyObject[] args, String[] kwds) {
ArgParser ap = new ArgParser("deque", args, kwds, new String[] {"iterable", "maxlen",}, 0);
PyObject maxlenobj = ap.getPyObject(1, null);
if (maxlenobj != null) {
if (maxlenobj == Py.None) {
maxlen = -1;
} else {
maxlen = ap.getInt(1);
if (maxlen < 0) {
throw Py.ValueError("maxlen must be non-negative");
}
}
} else {
maxlen = -1;
}
PyObject iterable = ap.getPyObject(0, null);
if (iterable != null) {
if (size != 0) {
// initializing a deque with an iterator when this deque is not empty means that we discard to empty first
deque_clear();
}
deque_extend(iterable);
}
}
/**
* If maxlen is not specified or is None, deques may grow to an arbitrary length.
* Otherwise, the deque is bounded to the specified maximum length.
*/
@ExposedGet(name = "maxlen")
public PyObject getMaxlen() {
if (maxlen < 0) {
return Py.None;
}
return Py.newInteger(maxlen);
}
@ExposedSet(name = "maxlen")
public void setMaxlen(PyObject o) {
// this has to be here because by default, if not defined,
// the Jython object model raise a TypeError, where we usually expect
// AttributeError here; due to a CPython 2.7 idiosyncracy that has
// since been fixed for 3.x in http://bugs.python.org/issue1687163
throw Py.AttributeError("attribute 'maxlen' of 'collections.deque' objects is not writable");
}
/**
* Add obj to the right side of the deque.
*/
@ExposedMethod
public synchronized final void deque_append(PyObject obj) {
if (maxlen >= 0) {
assert (size <= maxlen);
if (maxlen == 0) {
// do nothing; this deque will always be empty
return;
} else if (size == maxlen) {
deque_popleft();
}
}
addBefore(obj, header);
}
/**
* Add obj to the left side of the deque.
*/
@ExposedMethod
public synchronized final void deque_appendleft(PyObject obj) {
if (maxlen >= 0) {
assert (size <= maxlen);
if (maxlen == 0) {
// do nothing; this deque will always be empty
return;
} else if (size == maxlen) {
deque_pop();
}
}
addBefore(obj, header.right);
}
private Node addBefore(PyObject obj, Node node) {
// should ALWAYS be called inside a synchronized block
Node newNode = new Node(obj, node, node.left);
newNode.left.right = newNode;
newNode.right.left = newNode;
size++;
state++;
return newNode;
}
/**
* Remove all elements from the deque leaving it with length 0.
*/
@ExposedMethod
public synchronized final void deque_clear() {
Node node = header.right;
while (node != header) {
Node right = node.right;
node.left = null;
node.right = null;
node.data = null;
node = right;
state++;
}
header.right = header.left = header;
size = 0;
}
/**
* Extend the right side of the deque by appending elements from the
* iterable argument.
*/
@ExposedMethod
public synchronized final void deque_extend(PyObject iterable) {
// handle case where iterable == this
if (this == iterable) {
deque_extend(new PyList(iterable));
} else {
for (PyObject item : iterable.asIterable()) {
deque_append(item);
}
}
}
/**
* Extend the left side of the deque by appending elements from iterable.
* Note, the series of left appends results in reversing the order of
* elements in the iterable argument.
*/
@ExposedMethod
public synchronized final void deque_extendleft(PyObject iterable) {
// handle case where iterable == this
if (this == iterable) {
deque_extendleft(new PyList(iterable));
} else {
for (PyObject item : iterable.asIterable()) {
deque_appendleft(item);
}
}
}
/**
* Remove and return an element from the right side of the deque. If no
* elements are present, raises an IndexError.
*/
@ExposedMethod
public synchronized final PyObject deque_pop() {
return removeNode(header.left);
}
/**
* Remove and return an element from the left side of the deque. If no
* elements are present, raises an IndexError.
*/
@ExposedMethod
public synchronized final PyObject deque_popleft() {
return removeNode(header.right);
}
private PyObject removeNode(Node node) {
// should ALWAYS be called inside a synchronized block
if (node == header) {
throw Py.IndexError("pop from an empty deque");
}
PyObject obj = node.data;
node.left.right = node.right;
node.right.left = node.left;
node.right = null;
node.left = null;
node.data = null;
size--;
state++;
return obj;
}
/**
* Removed the first occurrence of value. If not found, raises a
* ValueError.
*/
@ExposedMethod
public synchronized final PyObject deque_remove(PyObject value) {
int n = size;
Node tmp = header.right;
boolean match = false;
long startState = state;
for (int i = 0; i < n; i++) {
if (tmp.data.equals(value)) {
match = true;
}
if (startState != state) {
throw Py.IndexError("deque mutated during remove().");
}
if (match) {
return removeNode(tmp);
}
tmp = tmp.right;
}
throw Py.ValueError("deque.remove(x): x not in deque");
}
/**
* Count the number of deque elements equal to x.
*/
@ExposedMethod
public synchronized final PyObject deque_count(PyObject x) {
int n = size;
int count = 0;
Node tmp = header.right;
long startState = state;
for (int i = 0; i < n; i++) {
if (tmp.data.equals(x)) {
count++;
}
if (startState != state) {
throw Py.RuntimeError("deque mutated during count().");
}
tmp = tmp.right;
}
return Py.newInteger(count);
}
/**
* Rotate the deque n steps to the right. If n is negative, rotate to the
* left. Rotating one step to the right is equivalent to: d.appendleft(d.pop()).
*/
@ExposedMethod(defaults = {"1"})
public synchronized final void deque_rotate(int steps) {
if (size == 0) {
return;
}
int halfsize = (size + 1) >> 1;
if (steps > halfsize || steps < -halfsize) {
steps %= size;
if (steps > halfsize) {
steps -= size;
} else if (steps < -halfsize) {
steps += size;
}
}
//rotate right
for (int i = 0; i < steps; i++) {
deque_appendleft(deque_pop());
}
//rotate left
for (int i = 0; i > steps; i--) {
deque_append(deque_popleft());
}
}
/**
* Reverse the elements of the deque in-place and then return None.
* @return Py.None
*/
@ExposedMethod
public synchronized final PyObject deque_reverse() {
Node headerRight = header.right;
Node headerLeft = header.left;
Node node = header.right;
while (node != header) {
Node right = node.right;
Node left = node.left;
node.right = left;
node.left = right;
node = right;
}
header.right = headerLeft;
header.left = headerRight;
state++;
return Py.None;
}
@Override
public String toString() {
return deque_toString();
}
@ExposedMethod(names = "__repr__")
synchronized final String deque_toString() {
ThreadState ts = Py.getThreadState();
if (!ts.enterRepr(this)) {
return "[...]";
}
long startState = state;
StringBuilder buf = new StringBuilder("deque").append("([");
for (Node tmp = header.right; tmp != header; tmp = tmp.right) {
buf.append(tmp.data.__repr__().toString());
if (startState != state) {
throw Py.RuntimeError("deque mutated during iteration.");
}
if (tmp.right != header) {
buf.append(", ");
}
}
buf.append("]");
if (maxlen >= 0) {
buf.append(", maxlen=");
buf.append(maxlen);
}
buf.append(")");
ts.exitRepr(this);
return buf.toString();
}
@Override
public int __len__() {
return deque___len__();
}
@ExposedMethod
synchronized final int deque___len__() {
return size;
}
@Override
public boolean __nonzero__() {
return deque___nonzero__();
}
@ExposedMethod
synchronized final boolean deque___nonzero__() {
return size != 0;
}
@Override
public PyObject __finditem__(PyObject key) {
try {
return deque___getitem__(key);
} catch (PyException pe) {
if (pe.match(Py.KeyError)) {
return null;
}
throw pe;
}
}
@ExposedMethod
synchronized final PyObject deque___getitem__(PyObject index) {
return getNode(index).data;
}
@Override
public void __setitem__(PyObject index, PyObject value) {
deque___setitem__(index, value);
}
@ExposedMethod
synchronized final void deque___setitem__(PyObject index, PyObject value) {
Node node = getNode(index).right;
removeNode(node.left);
addBefore(value, node);
}
@Override
public void __delitem__(PyObject key) {
deque___delitem__(key);
}
@ExposedMethod
synchronized final void deque___delitem__(PyObject key) {
removeNode(getNode(key));
}
private Node getNode(PyObject index) {
// must ALWAYS be called inside a synchronized block
int pos = 0;
if (!index.isIndex()) {
throw Py.TypeError(String.format("sequence index must be integer, not '%.200s'",
index.getType().fastGetName()));
}
pos = index.asIndex(Py.IndexError);
if (pos < 0) {
pos += size;
}
if (pos < 0 || pos >= size) {
throw Py.IndexError("index out of range: " + index);
}
Node tmp = header;
if (pos < (size >> 1)) {
for (int i = 0; i <= pos; i++) {
tmp = tmp.right;
}
} else {
for (int i = size - 1; i >= pos; i--) {
tmp = tmp.left;
}
}
return tmp;
}
@Override
public PyObject __iter__() {
return deque___iter__();
}
@ExposedMethod
final PyObject deque___iter__() {
return new PyDequeIter();
}
@Override
public synchronized PyObject __eq__(PyObject o) {
return deque___eq__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___eq__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int tl = __len__();
int ol = o.__len__();
if (tl != ol) {
return Py.False;
}
int i = cmp(this, tl, o, ol);
return (i < 0) ? Py.True : Py.False;
}
@Override
public synchronized PyObject __ne__(PyObject o) {
return deque___ne__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___ne__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int tl = __len__();
int ol = o.__len__();
if (tl != ol) {
return Py.True;
}
int i = cmp(this, tl, o, ol);
return (i < 0) ? Py.False : Py.True;
}
@Override
public synchronized PyObject __lt__(PyObject o) {
return deque___lt__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___lt__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int i = cmp(this, -1, o, -1);
if (i < 0) {
return (i == -1) ? Py.True : Py.False;
}
return __finditem__(i)._lt(o.__finditem__(i));
}
@Override
public synchronized PyObject __le__(PyObject o) {
return deque___le__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___le__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int i = cmp(this, -1, o, -1);
if (i < 0) {
return (i == -1 || i == -2) ? Py.True : Py.False;
}
return __finditem__(i)._le(o.__finditem__(i));
}
@Override
public synchronized PyObject __gt__(PyObject o) {
return deque___gt__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___gt__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int i = cmp(this, -1, o, -1);
if (i < 0) {
return (i == -3) ? Py.True : Py.False;
}
return __finditem__(i)._gt(o.__finditem__(i));
}
@Override
public synchronized PyObject __ge__(PyObject o) {
return deque___ge__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___ge__(PyObject o) {
if (!(getType() == o.getType()) && !(getType().isSubType(o.getType()))) {
return null;
}
int i = cmp(this, -1, o, -1);
if (i < 0) {
return (i == -3 || i == -2) ? Py.True : Py.False;
}
return __finditem__(i)._ge(o.__finditem__(i));
}
@Override
public synchronized PyObject __iadd__(PyObject o) {
return deque___iadd__(o);
}
@ExposedMethod(type = MethodType.BINARY)
final synchronized PyObject deque___iadd__(PyObject o) {
deque_extend(o);
return this;
}
// Return value >= 0 is the index where the sequences differs.
// -1: reached the end of o1 without a difference
// -2: reached the end of both seqeunces without a difference
// -3: reached the end of o2 without a difference
protected static int cmp(PyObject o1, int ol1, PyObject o2, int ol2) {
if (ol1 < 0) {
ol1 = o1.__len__();
}
if (ol2 < 0) {
ol2 = o2.__len__();
}
for (int i = 0 ; i < ol1 && i < ol2; i++) {
if (!o1.__getitem__(i).equals(o2.__getitem__(i))) {
return i;
}
}
if (ol1 == ol2) {
return -2;
}
return (ol1 < ol2) ? -1 : -3;
}
@Override
public int hashCode() {
return deque_hashCode();
}
@ExposedMethod(names = "__hash__")
final int deque_hashCode() {
throw Py.TypeError("deque objects are unhashable");
}
@Override
public PyObject __reduce__() {
return deque___reduce__();
}
@ExposedMethod
final PyObject deque___reduce__() {
PyObject dict = getDict();
if (dict == null) {
dict = Py.None;
}
return new PyTuple(getType(), Py.EmptyTuple, dict, __iter__());
}
@ExposedMethod
final PyObject deque___copy__() {
PyDeque pd = (PyDeque)this.getType().__call__();
pd.deque_extend(this);
return pd;
}
@Override
public boolean isMappingType() {
return false;
}
@Override
public boolean isSequenceType() {
return true;
}
private static class Node {
private Node left;
private Node right;
private PyObject data;
Node(PyObject data, Node right, Node left) {
this.data = data;
this.right = right;
this.left = left;
}
}
private class PyDequeIter extends PyIterator {
private Node lastReturned = header;
private long startState;
public PyDequeIter() {
startState = state;
}
@Override
public PyObject __iternext__() {
synchronized (PyDeque.this) {
if (startState != state) {
throw Py.RuntimeError("deque changed size during iteration");
}
if (lastReturned.right != header) {
lastReturned = lastReturned.right;
return lastReturned.data;
}
return null;
}
}
/* Traverseproc implementation */
@Override
public int traverse(Visitproc visit, Object arg) {
int retVal = super.traverse(visit, arg);
if (retVal != 0) {
return retVal;
}
/* On first thought one would traverse the circular list
* starting with lastReturned. However due to synchronization
* it is guaranteed that this would traverse the same objects
* as starting with header would do. So we can simply call the
* traverse-method of PyDeque.this.
*/
return PyDeque.this.traverse(visit, arg);
}
@Override
public boolean refersDirectlyTo(PyObject ob) throws UnsupportedOperationException {
if (ob == null) {
return false;
} else if (super.refersDirectlyTo(ob)) {
return true;
} else {
throw new UnsupportedOperationException();
}
}
}
/* Traverseproc implementation */
@Override
public synchronized int traverse(Visitproc visit, Object arg) {
if (header == null) {
return 0;
}
int retVal = 0;
if (header.data != null) {
retVal = visit.visit(header.data, arg);
if (retVal != 0) {
return retVal;
}
}
Node tmp = header.right;
while (tmp != header) {
if (tmp.data != null) {
retVal = visit.visit(tmp.data, arg);
if (retVal != 0) {
return retVal;
}
}
tmp = tmp.right;
}
return retVal;
}
@Override
public boolean refersDirectlyTo(PyObject ob) throws UnsupportedOperationException {
throw new UnsupportedOperationException();
}
}