1 Clazz.declarePackage ("java.util");
2 Clazz.load (["java.util.AbstractCollection", "$.Iterator", "java.util.Deque"], "java.util.ArrayDeque", ["java.lang.AssertionError", "$.IllegalStateException", "$.NullPointerException", "java.lang.reflect.Array", "java.util.Arrays", "$.ConcurrentModificationException", "$.NoSuchElementException"], function () {
3 c$ = Clazz.decorateAsClass (function () {
7 if (!Clazz.isClassDefined ("java.util.ArrayDeque.DeqIterator")) {
8 java.util.ArrayDeque.$ArrayDeque$DeqIterator$ ();
10 if (!Clazz.isClassDefined ("java.util.ArrayDeque.DescendingIterator")) {
11 java.util.ArrayDeque.$ArrayDeque$DescendingIterator$ ();
13 Clazz.instantialize (this, arguments);
14 }, java.util, "ArrayDeque", java.util.AbstractCollection, [java.util.Deque, Cloneable, java.io.Serializable]);
15 Clazz.defineMethod (c$, "allocateElements",
16 function (numElements) {
17 var initialCapacity = 8;
18 if (numElements >= initialCapacity) {
19 initialCapacity = numElements;
20 initialCapacity |= (initialCapacity >>> 1);
21 initialCapacity |= (initialCapacity >>> 2);
22 initialCapacity |= (initialCapacity >>> 4);
23 initialCapacity |= (initialCapacity >>> 8);
24 initialCapacity |= (initialCapacity >>> 16);
26 if (initialCapacity < 0) initialCapacity >>>= 1;
27 }this.elements = new Array (initialCapacity);
29 Clazz.defineMethod (c$, "doubleCapacity",
32 var n = this.elements.length;
34 var newCapacity = n << 1;
35 if (newCapacity < 0) throw new IllegalStateException ("Sorry, deque too big");
36 var a = new Array (newCapacity);
37 System.arraycopy (this.elements, p, a, 0, r);
38 System.arraycopy (this.elements, 0, a, r, p);
43 Clazz.defineMethod (c$, "copyElements",
45 if (this.head < this.tail) {
46 System.arraycopy (this.elements, this.head, a, 0, this.size ());
47 } else if (this.head > this.tail) {
48 var headPortionLen = this.elements.length - this.head;
49 System.arraycopy (this.elements, this.head, a, 0, headPortionLen);
50 System.arraycopy (this.elements, 0, a, headPortionLen, this.tail);
53 Clazz.makeConstructor (c$,
55 Clazz.superConstructor (this, java.util.ArrayDeque, []);
56 this.elements = new Array (16);
58 Clazz.makeConstructor (c$,
59 function (numElements) {
60 Clazz.superConstructor (this, java.util.ArrayDeque, []);
61 this.allocateElements (numElements);
63 Clazz.makeConstructor (c$,
65 Clazz.superConstructor (this, java.util.ArrayDeque, []);
66 this.allocateElements (c.size ());
68 }, "java.util.Collection");
69 Clazz.overrideMethod (c$, "addFirst",
71 if (e == null) throw new NullPointerException ();
72 this.elements[this.head = (this.head - 1) & (this.elements.length - 1)] = e;
73 if (this.head == this.tail) this.doubleCapacity ();
75 Clazz.overrideMethod (c$, "addLast",
77 if (e == null) throw new NullPointerException ();
78 this.elements[this.tail] = e;
79 if ((this.tail = (this.tail + 1) & (this.elements.length - 1)) == this.head) this.doubleCapacity ();
81 Clazz.overrideMethod (c$, "offerFirst",
86 Clazz.overrideMethod (c$, "offerLast",
91 Clazz.overrideMethod (c$, "removeFirst",
93 var x = this.pollFirst ();
94 if (x == null) throw new java.util.NoSuchElementException ();
97 Clazz.overrideMethod (c$, "removeLast",
99 var x = this.pollLast ();
100 if (x == null) throw new java.util.NoSuchElementException ();
103 Clazz.overrideMethod (c$, "pollFirst",
106 var result = this.elements[h];
107 if (result == null) return null;
108 this.elements[h] = null;
109 this.head = (h + 1) & (this.elements.length - 1);
112 Clazz.overrideMethod (c$, "pollLast",
114 var t = (this.tail - 1) & (this.elements.length - 1);
115 var result = this.elements[t];
116 if (result == null) return null;
117 this.elements[t] = null;
121 Clazz.overrideMethod (c$, "getFirst",
123 var x = this.elements[this.head];
124 if (x == null) throw new java.util.NoSuchElementException ();
127 Clazz.overrideMethod (c$, "getLast",
129 var x = this.elements[(this.tail - 1) & (this.elements.length - 1)];
130 if (x == null) throw new java.util.NoSuchElementException ();
133 Clazz.overrideMethod (c$, "peekFirst",
135 return this.elements[this.head];
137 Clazz.overrideMethod (c$, "peekLast",
139 return this.elements[(this.tail - 1) & (this.elements.length - 1)];
141 Clazz.overrideMethod (c$, "removeFirstOccurrence",
143 if (o == null) return false;
144 var mask = this.elements.length - 1;
147 while ((x = this.elements[i]) != null) {
155 Clazz.overrideMethod (c$, "removeLastOccurrence",
157 if (o == null) return false;
158 var mask = this.elements.length - 1;
159 var i = (this.tail - 1) & mask;
161 while ((x = this.elements[i]) != null) {
169 Clazz.overrideMethod (c$, "add",
174 Clazz.overrideMethod (c$, "offer",
176 return this.offerLast (e);
178 Clazz.defineMethod (c$, "remove",
180 return this.removeFirst ();
182 Clazz.overrideMethod (c$, "poll",
184 return this.pollFirst ();
186 Clazz.overrideMethod (c$, "element",
188 return this.getFirst ();
190 Clazz.overrideMethod (c$, "peek",
192 return this.peekFirst ();
194 Clazz.overrideMethod (c$, "push",
198 Clazz.overrideMethod (c$, "pop",
200 return this.removeFirst ();
202 Clazz.defineMethod (c$, "checkInvariants",
205 Clazz.defineMethod (c$, "$delete",
207 this.checkInvariants ();
208 var elements = this.elements;
209 var mask = elements.length - 1;
212 var front = (i - h) & mask;
213 var back = (t - i) & mask;
214 if (front >= ((t - h) & mask)) throw new java.util.ConcurrentModificationException ();
217 System.arraycopy (elements, h, elements, h + 1, front);
219 System.arraycopy (elements, 0, elements, 1, i);
220 elements[0] = elements[mask];
221 System.arraycopy (elements, h, elements, h + 1, mask - h);
223 this.head = (h + 1) & mask;
227 System.arraycopy (elements, i + 1, elements, i, back);
230 System.arraycopy (elements, i + 1, elements, i, mask - i);
231 elements[mask] = elements[0];
232 System.arraycopy (elements, 1, elements, 0, t);
233 this.tail = (t - 1) & mask;
236 Clazz.overrideMethod (c$, "size",
238 return (this.tail - this.head) & (this.elements.length - 1);
240 Clazz.overrideMethod (c$, "isEmpty",
242 return this.head == this.tail;
244 Clazz.overrideMethod (c$, "iterator",
246 return Clazz.innerTypeInstance (java.util.ArrayDeque.DeqIterator, this, null);
248 Clazz.overrideMethod (c$, "descendingIterator",
250 return Clazz.innerTypeInstance (java.util.ArrayDeque.DescendingIterator, this, null);
252 Clazz.overrideMethod (c$, "contains",
254 if (o == null) return false;
255 var mask = this.elements.length - 1;
258 while ((x = this.elements[i]) != null) {
259 if (o.equals (x)) return true;
264 Clazz.defineMethod (c$, "remove",
266 return this.removeFirstOccurrence (o);
268 Clazz.overrideMethod (c$, "clear",
273 this.head = this.tail = 0;
275 var mask = this.elements.length - 1;
277 this.elements[i] = null;
281 Clazz.defineMethod (c$, "toArray",
283 return this.copyElements ( new Array (this.size ()));
285 Clazz.defineMethod (c$, "toArray",
287 var size = this.size ();
288 if (a.length < size) a = java.lang.reflect.Array.newInstance (a.getClass ().getComponentType (), size);
289 this.copyElements (a);
290 if (a.length > size) a[size] = null;
293 Clazz.defineMethod (c$, "clone",
296 var result = Clazz.superCall (this, java.util.ArrayDeque, "clone", []);
297 result.elements = java.util.Arrays.copyOf (this.elements, this.elements.length);
300 if (Clazz.exceptionOf (e, CloneNotSupportedException)) {
301 throw new AssertionError ();
307 c$.$ArrayDeque$DeqIterator$ = function () {
309 c$ = Clazz.decorateAsClass (function () {
310 Clazz.prepareCallback (this, arguments);
314 Clazz.instantialize (this, arguments);
315 }, java.util.ArrayDeque, "DeqIterator", null, java.util.Iterator);
316 Clazz.prepareFields (c$, function () {
317 this.cursor = this.b$["java.util.ArrayDeque"].head;
318 this.fence = this.b$["java.util.ArrayDeque"].tail;
320 Clazz.overrideMethod (c$, "hasNext",
322 return this.cursor != this.fence;
324 Clazz.overrideMethod (c$, "next",
326 if (this.cursor == this.fence) throw new java.util.NoSuchElementException ();
327 var a = this.b$["java.util.ArrayDeque"].elements[this.cursor];
328 if (this.b$["java.util.ArrayDeque"].tail != this.fence || a == null) throw new java.util.ConcurrentModificationException ();
329 this.lastRet = this.cursor;
330 this.cursor = (this.cursor + 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
333 Clazz.overrideMethod (c$, "remove",
335 if (this.lastRet < 0) throw new IllegalStateException ();
336 if (this.b$["java.util.ArrayDeque"].$delete (this.lastRet)) {
337 this.cursor = (this.cursor - 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
338 this.fence = this.b$["java.util.ArrayDeque"].tail;
343 c$.$ArrayDeque$DescendingIterator$ = function () {
345 c$ = Clazz.decorateAsClass (function () {
346 Clazz.prepareCallback (this, arguments);
350 Clazz.instantialize (this, arguments);
351 }, java.util.ArrayDeque, "DescendingIterator", null, java.util.Iterator);
352 Clazz.prepareFields (c$, function () {
353 this.cursor = this.b$["java.util.ArrayDeque"].tail;
354 this.fence = this.b$["java.util.ArrayDeque"].head;
356 Clazz.overrideMethod (c$, "hasNext",
358 return this.cursor != this.fence;
360 Clazz.overrideMethod (c$, "next",
362 if (this.cursor == this.fence) throw new java.util.NoSuchElementException ();
363 this.cursor = (this.cursor - 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
364 var a = this.b$["java.util.ArrayDeque"].elements[this.cursor];
365 if (this.b$["java.util.ArrayDeque"].head != this.fence || a == null) throw new java.util.ConcurrentModificationException ();
366 this.lastRet = this.cursor;
369 Clazz.overrideMethod (c$, "remove",
371 if (this.lastRet < 0) throw new IllegalStateException ();
372 if (!this.b$["java.util.ArrayDeque"].$delete (this.lastRet)) {
373 this.cursor = (this.cursor + 1) & (this.b$["java.util.ArrayDeque"].elements.length - 1);
374 this.fence = this.b$["java.util.ArrayDeque"].head;
379 Clazz.defineStatics (c$,
380 "MIN_INITIAL_CAPACITY", 8);