ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
Revision: 1.5
Committed: Tue Mar 22 01:29:00 2005 UTC (19 years, 2 months ago) by dl
Branch: MAIN
Changes since 1.4: +34 -30 lines
Log Message:
Repair typos and minor doc improvements

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by Josh Bloch of Google Inc. and released to the public domain,
3     * as explained at http://creativecommons.org/licenses/publicdomain.
4     */
5    
6     package java.util;
7     import java.io.*;
8    
9     /**
10     * Resizable-array implementation of the {@link Deque} interface. Array
11     * deques have no capacity restrictions; they grow as necessary to support
12     * usage. They are not thread-safe; in the absence of external
13     * synchronization, they do not support concurrent access by multiple threads.
14     * Null elements are prohibited. This class is likely to be faster than
15 dl 1.2 * {@link Stack} when used as a stack, and faster than {@link LinkedList}
16 dl 1.1 * when used as a queue.
17     *
18     * <p>Most <tt>ArrayDeque</tt> operations run in amortized constant time.
19     * Exceptions include {@link #remove(Object) remove}, {@link
20     * #removeFirstOccurrence removeFirstOccurrence}, {@link #removeLastOccurrence
21     * removeLastOccurrence}, {@link #contains contains }, {@link #iterator
22     * iterator.remove()}, and the bulk operations, all of which run in linear
23     * time.
24     *
25     * <p>The iterators returned by this class's <tt>iterator</tt> method are
26     * <i>fail-fast</i>: If the deque is modified at any time after the iterator
27     * is created, in any way except through the iterator's own remove method, the
28     * iterator will generally throw a {@link ConcurrentModificationException}.
29     * Thus, in the face of concurrent modification, the iterator fails quickly
30     * and cleanly, rather than risking arbitrary, non-deterministic behavior at
31     * an undetermined time in the future.
32     *
33     * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
34     * as it is, generally speaking, impossible to make any hard guarantees in the
35     * presence of unsynchronized concurrent modification. Fail-fast iterators
36 dl 1.5 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
37 dl 1.1 * Therefore, it would be wrong to write a program that depended on this
38     * exception for its correctness: <i>the fail-fast behavior of iterators
39     * should be used only to detect bugs.</i>
40     *
41     * <p>This class and its iterator implement all of the
42     * optional methods of the {@link Collection} and {@link
43     * Iterator} interfaces. This class is a member of the <a
44     * href="{@docRoot}/../guide/collections/index.html"> Java Collections
45     * Framework</a>.
46     *
47     * @author Josh Bloch and Doug Lea
48     * @since 1.6
49     * @param <E> the type of elements held in this collection
50     */
51     public class ArrayDeque<E> extends AbstractCollection<E>
52     implements Deque<E>, Cloneable, Serializable
53     {
54     /**
55 dl 1.4 * The array in which the elements of the deque are stored.
56 dl 1.1 * The capacity of the deque is the length of this array, which is
57     * always a power of two. The array is never allowed to become
58     * full, except transiently within an addX method where it is
59     * resized (see doubleCapacity) immediately upon becoming full,
60     * thus avoiding head and tail wrapping around to equal each
61     * other. We also guarantee that all array cells not holding
62     * deque elements are always null.
63     */
64     private transient E[] elements;
65    
66     /**
67     * The index of the element at the head of the deque (which is the
68     * element that would be removed by remove() or pop()); or an
69     * arbitrary number equal to tail if the deque is empty.
70     */
71     private transient int head;
72    
73     /**
74     * The index at which the next element would be added to the tail
75     * of the deque (via addLast(E), add(E), or push(E)).
76     */
77     private transient int tail;
78    
79     /**
80     * The minimum capacity that we'll use for a newly created deque.
81     * Must be a power of 2.
82     */
83     private static final int MIN_INITIAL_CAPACITY = 8;
84    
85     // ****** Array allocation and resizing utilities ******
86    
87     /**
88     * Allocate empty array to hold the given number of elements.
89     *
90     * @param numElements the number of elements to hold.
91     */
92 dl 1.5 private void allocateElements(int numElements) {
93 dl 1.1 int initialCapacity = MIN_INITIAL_CAPACITY;
94     // Find the best power of two to hold elements.
95     // Tests "<=" because arrays aren't kept full.
96     if (numElements >= initialCapacity) {
97     initialCapacity = numElements;
98     initialCapacity |= (initialCapacity >>> 1);
99     initialCapacity |= (initialCapacity >>> 2);
100     initialCapacity |= (initialCapacity >>> 4);
101     initialCapacity |= (initialCapacity >>> 8);
102     initialCapacity |= (initialCapacity >>> 16);
103     initialCapacity++;
104    
105     if (initialCapacity < 0) // Too many elements, must back off
106     initialCapacity >>>= 1;// Good luck allocating 2 ^ 30 elements
107     }
108     elements = (E[]) new Object[initialCapacity];
109     }
110    
111     /**
112     * Double the capacity of this deque. Call only when full, i.e.,
113     * when head and tail have wrapped around to become equal.
114     */
115     private void doubleCapacity() {
116 dl 1.5 assert head == tail;
117 dl 1.1 int p = head;
118     int n = elements.length;
119     int r = n - p; // number of elements to the right of p
120     int newCapacity = n << 1;
121     if (newCapacity < 0)
122     throw new IllegalStateException("Sorry, deque too big");
123     Object[] a = new Object[newCapacity];
124     System.arraycopy(elements, p, a, 0, r);
125     System.arraycopy(elements, 0, a, r, p);
126     elements = (E[])a;
127     head = 0;
128     tail = n;
129     }
130    
131     /**
132     * Copy the elements from our element array into the specified array,
133     * in order (from first to last element in the deque). It is assumed
134     * that the array is large enough to hold all elements in the deque.
135     *
136     * @return its argument
137     */
138     private <T> T[] copyElements(T[] a) {
139     if (head < tail) {
140     System.arraycopy(elements, head, a, 0, size());
141     } else if (head > tail) {
142     int headPortionLen = elements.length - head;
143     System.arraycopy(elements, head, a, 0, headPortionLen);
144     System.arraycopy(elements, 0, a, headPortionLen, tail);
145     }
146     return a;
147     }
148    
149     /**
150 dl 1.4 * Constructs an empty array deque with an initial capacity
151 dl 1.1 * sufficient to hold 16 elements.
152     */
153     public ArrayDeque() {
154     elements = (E[]) new Object[16];
155     }
156    
157     /**
158     * Constructs an empty array deque with an initial capacity
159     * sufficient to hold the specified number of elements.
160     *
161     * @param numElements lower bound on initial capacity of the deque
162     */
163     public ArrayDeque(int numElements) {
164     allocateElements(numElements);
165     }
166    
167     /**
168     * Constructs a deque containing the elements of the specified
169     * collection, in the order they are returned by the collection's
170     * iterator. (The first element returned by the collection's
171     * iterator becomes the first element, or <i>front</i> of the
172     * deque.)
173     *
174     * @param c the collection whose elements are to be placed into the deque
175     * @throws NullPointerException if the specified collection is null
176     */
177     public ArrayDeque(Collection<? extends E> c) {
178     allocateElements(c.size());
179     addAll(c);
180     }
181    
182     // The main insertion and extraction methods are addFirst,
183     // addLast, pollFirst, pollLast. The other methods are defined in
184     // terms of these.
185    
186     /**
187 dl 1.5 * Inserts the specified element at the front of this deque.
188 dl 1.1 *
189     * @param e the element to insert
190     * @throws NullPointerException if <tt>e</tt> is null
191     */
192     public void addFirst(E e) {
193     if (e == null)
194     throw new NullPointerException();
195     elements[head = (head - 1) & (elements.length - 1)] = e;
196 dl 1.5 if (head == tail)
197 dl 1.1 doubleCapacity();
198     }
199    
200     /**
201 dl 1.5 * Inserts the specified element to the end of this deque.
202 dl 1.1 * This method is equivalent to {@link Collection#add} and
203     * {@link #push}.
204     *
205     * @param e the element to insert
206     * @throws NullPointerException if <tt>e</tt> is null
207     */
208     public void addLast(E e) {
209     if (e == null)
210     throw new NullPointerException();
211     elements[tail] = e;
212     if ( (tail = (tail + 1) & (elements.length - 1)) == head)
213     doubleCapacity();
214     }
215    
216     /**
217     * Retrieves and removes the first element of this deque, or
218     * <tt>null</tt> if this deque is empty.
219     *
220     * @return the first element of this deque, or <tt>null</tt> if
221     * this deque is empty
222     */
223     public E pollFirst() {
224     int h = head;
225     E result = elements[h]; // Element is null if deque empty
226     if (result == null)
227     return null;
228     elements[h] = null; // Must null out slot
229     head = (h + 1) & (elements.length - 1);
230     return result;
231     }
232    
233     /**
234     * Retrieves and removes the last element of this deque, or
235     * <tt>null</tt> if this deque is empty.
236     *
237     * @return the last element of this deque, or <tt>null</tt> if
238     * this deque is empty
239     */
240     public E pollLast() {
241     int t = (tail - 1) & (elements.length - 1);
242     E result = elements[t];
243     if (result == null)
244     return null;
245 dl 1.5 elements[t] = null;
246 dl 1.1 tail = t;
247     return result;
248     }
249    
250     /**
251 dl 1.5 * Inserts the specified element at the front of this deque.
252 dl 1.1 *
253     * @param e the element to insert
254     * @return <tt>true</tt> (as per the spec for {@link Deque#offerFirst})
255     * @throws NullPointerException if <tt>e</tt> is null
256     */
257     public boolean offerFirst(E e) {
258     addFirst(e);
259     return true;
260     }
261    
262     /**
263 dl 1.5 * Inserts the specified element to the end of this deque.
264 dl 1.1 *
265     * @param e the element to insert
266     * @return <tt>true</tt> (as per the spec for {@link Deque#offerLast})
267     * @throws NullPointerException if <tt>e</tt> is null
268     */
269     public boolean offerLast(E e) {
270     addLast(e);
271     return true;
272     }
273    
274     /**
275     * Retrieves and removes the first element of this deque. This method
276     * differs from the <tt>pollFirst</tt> method in that it throws an
277     * exception if this deque is empty.
278     *
279     * @return the first element of this deque
280     * @throws NoSuchElementException if this deque is empty
281     */
282     public E removeFirst() {
283     E x = pollFirst();
284     if (x == null)
285     throw new NoSuchElementException();
286     return x;
287     }
288    
289     /**
290     * Retrieves and removes the last element of this deque. This method
291     * differs from the <tt>pollLast</tt> method in that it throws an
292     * exception if this deque is empty.
293     *
294     * @return the last element of this deque
295     * @throws NoSuchElementException if this deque is empty
296     */
297     public E removeLast() {
298     E x = pollLast();
299     if (x == null)
300     throw new NoSuchElementException();
301     return x;
302     }
303    
304     /**
305     * Retrieves, but does not remove, the first element of this deque,
306     * returning <tt>null</tt> if this deque is empty.
307     *
308     * @return the first element of this deque, or <tt>null</tt> if
309     * this deque is empty
310     */
311     public E peekFirst() {
312     return elements[head]; // elements[head] is null if deque empty
313     }
314    
315     /**
316     * Retrieves, but does not remove, the last element of this deque,
317     * returning <tt>null</tt> if this deque is empty.
318     *
319     * @return the last element of this deque, or <tt>null</tt> if this deque
320     * is empty
321     */
322     public E peekLast() {
323     return elements[(tail - 1) & (elements.length - 1)];
324     }
325    
326     /**
327     * Retrieves, but does not remove, the first element of this
328 dl 1.5 * deque. This method differs from the <tt>peekFirst</tt> method only
329 dl 1.1 * in that it throws an exception if this deque is empty.
330     *
331     * @return the first element of this deque
332     * @throws NoSuchElementException if this deque is empty
333     */
334     public E getFirst() {
335     E x = elements[head];
336     if (x == null)
337     throw new NoSuchElementException();
338     return x;
339     }
340    
341     /**
342     * Retrieves, but does not remove, the last element of this
343 dl 1.5 * deque. This method differs from the <tt>peekLast</tt> method only
344 dl 1.1 * in that it throws an exception if this deque is empty.
345     *
346     * @return the last element of this deque
347     * @throws NoSuchElementException if this deque is empty
348     */
349     public E getLast() {
350     E x = elements[(tail - 1) & (elements.length - 1)];
351     if (x == null)
352     throw new NoSuchElementException();
353     return x;
354     }
355    
356     /**
357     * Removes the first occurrence of the specified element in this
358 dl 1.5 * deque (when traversing the deque from head to tail). More
359     * formally, removes the first element e such that (o==null ?
360     * e==null : o.equals(e)). If the deque does not contain the
361     * element, it is unchanged.
362 dl 1.1 *
363 dl 1.5 * @param o element to be removed from this deque, if present
364 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
365     */
366 dl 1.5 public boolean removeFirstOccurrence(Object o) {
367     if (o == null)
368 dl 1.1 return false;
369     int mask = elements.length - 1;
370     int i = head;
371     E x;
372     while ( (x = elements[i]) != null) {
373 dl 1.5 if (o.equals(x)) {
374 dl 1.1 delete(i);
375     return true;
376     }
377     i = (i + 1) & mask;
378     }
379     return false;
380     }
381    
382     /**
383     * Removes the last occurrence of the specified element in this
384 dl 1.5 * deque (when traversing the deque from head to tail). More
385     * formally, removes the last element e such that (o==null ?
386     * e==null : o.equals(e)). If the deque
387 dl 1.1 * does not contain the element, it is unchanged.
388     *
389 dl 1.5 * @param o element to be removed from this deque, if present
390 dl 1.1 * @return <tt>true</tt> if the deque contained the specified element
391     */
392 dl 1.5 public boolean removeLastOccurrence(Object o) {
393     if (o == null)
394 dl 1.1 return false;
395     int mask = elements.length - 1;
396     int i = (tail - 1) & mask;
397     E x;
398     while ( (x = elements[i]) != null) {
399 dl 1.5 if (o.equals(x)) {
400 dl 1.1 delete(i);
401     return true;
402     }
403     i = (i - 1) & mask;
404     }
405     return false;
406     }
407    
408     // *** Queue methods ***
409    
410     /**
411     * Inserts the specified element to the end of this deque.
412     *
413     * <p>This method is equivalent to {@link #offerLast}.
414     *
415     * @param e the element to insert
416     * @return <tt>true</tt> (as per the spec for {@link Queue#offer})
417     * @throws NullPointerException if <tt>e</tt> is null
418     */
419     public boolean offer(E e) {
420     return offerLast(e);
421     }
422    
423     /**
424     * Inserts the specified element to the end of this deque.
425     *
426     * <p>This method is equivalent to {@link #addLast}.
427     *
428     * @param e the element to insert
429     * @return <tt>true</tt> (as per the spec for {@link Collection#add})
430     * @throws NullPointerException if <tt>e</tt> is null
431     */
432     public boolean add(E e) {
433     addLast(e);
434     return true;
435     }
436    
437     /**
438     * Retrieves and removes the head of the queue represented by
439     * this deque, or <tt>null</tt> if this deque is empty. In other words,
440     * retrieves and removes the first element of this deque, or <tt>null</tt>
441     * if this deque is empty.
442     *
443     * <p>This method is equivalent to {@link #pollFirst}.
444     *
445     * @return the first element of this deque, or <tt>null</tt> if
446     * this deque is empty
447     */
448     public E poll() {
449     return pollFirst();
450     }
451    
452     /**
453     * Retrieves and removes the head of the queue represented by this deque.
454     * This method differs from the <tt>poll</tt> method in that it throws an
455     * exception if this deque is empty.
456     *
457     * <p>This method is equivalent to {@link #removeFirst}.
458     *
459     * @return the head of the queue represented by this deque
460     * @throws NoSuchElementException if this deque is empty
461     */
462     public E remove() {
463     return removeFirst();
464     }
465    
466     /**
467     * Retrieves, but does not remove, the head of the queue represented by
468     * this deque, returning <tt>null</tt> if this deque is empty.
469     *
470     * <p>This method is equivalent to {@link #peekFirst}
471     *
472     * @return the head of the queue represented by this deque, or
473     * <tt>null</tt> if this deque is empty
474     */
475     public E peek() {
476     return peekFirst();
477     }
478    
479     /**
480     * Retrieves, but does not remove, the head of the queue represented by
481     * this deque. This method differs from the <tt>peek</tt> method only in
482     * that it throws an exception if this deque is empty.
483     *
484     * <p>This method is equivalent to {@link #getFirst}
485     *
486     * @return the head of the queue represented by this deque
487     * @throws NoSuchElementException if this deque is empty
488     */
489     public E element() {
490     return getFirst();
491     }
492    
493     // *** Stack methods ***
494    
495     /**
496     * Pushes an element onto the stack represented by this deque. In other
497 dl 1.5 * words, inserts the element at the front of this deque.
498 dl 1.1 *
499     * <p>This method is equivalent to {@link #addFirst}.
500     *
501     * @param e the element to push
502     * @throws NullPointerException if <tt>e</tt> is null
503     */
504     public void push(E e) {
505     addFirst(e);
506     }
507    
508     /**
509     * Pops an element from the stack represented by this deque. In other
510 dl 1.2 * words, removes and returns the first element of this deque.
511 dl 1.1 *
512     * <p>This method is equivalent to {@link #removeFirst()}.
513     *
514     * @return the element at the front of this deque (which is the top
515     * of the stack represented by this deque)
516     * @throws NoSuchElementException if this deque is empty
517     */
518     public E pop() {
519     return removeFirst();
520     }
521    
522     /**
523     * Remove the element at the specified position in the elements array,
524     * adjusting head, tail, and size as necessary. This can result in
525     * motion of elements backwards or forwards in the array.
526     *
527 dl 1.5 * <p>This method is called delete rather than remove to emphasize
528 dl 1.2 * that its semantics differ from those of List.remove(int).
529 dl 1.5 *
530 dl 1.1 * @return true if elements moved backwards
531     */
532     private boolean delete(int i) {
533     // Case 1: Deque doesn't wrap
534     // Case 2: Deque does wrap and removed element is in the head portion
535     if ((head < tail || tail == 0) || i >= head) {
536     System.arraycopy(elements, head, elements, head + 1, i - head);
537     elements[head] = null;
538     head = (head + 1) & (elements.length - 1);
539     return false;
540     }
541    
542     // Case 3: Deque wraps and removed element is in the tail portion
543     tail--;
544     System.arraycopy(elements, i + 1, elements, i, tail - i);
545     elements[tail] = null;
546     return true;
547     }
548    
549     // *** Collection Methods ***
550    
551     /**
552     * Returns the number of elements in this deque.
553     *
554     * @return the number of elements in this deque
555     */
556     public int size() {
557     return (tail - head) & (elements.length - 1);
558     }
559    
560     /**
561     * Returns <tt>true</tt> if this collection contains no elements.<p>
562     *
563     * @return <tt>true</tt> if this collection contains no elements.
564     */
565     public boolean isEmpty() {
566     return head == tail;
567     }
568    
569     /**
570     * Returns an iterator over the elements in this deque. The elements
571     * will be ordered from first (head) to last (tail). This is the same
572     * order that elements would be dequeued (via successive calls to
573     * {@link #remove} or popped (via successive calls to {@link #pop}).
574 dl 1.5 *
575 dl 1.1 * @return an <tt>Iterator</tt> over the elements in this deque
576     */
577     public Iterator<E> iterator() {
578     return new DeqIterator();
579     }
580    
581     private class DeqIterator implements Iterator<E> {
582     /**
583     * Index of element to be returned by subsequent call to next.
584     */
585     private int cursor = head;
586    
587     /**
588     * Tail recorded at construction (also in remove), to stop
589     * iterator and also to check for comodification.
590     */
591     private int fence = tail;
592    
593     /**
594     * Index of element returned by most recent call to next.
595     * Reset to -1 if element is deleted by a call to remove.
596     */
597     private int lastRet = -1;
598    
599     public boolean hasNext() {
600     return cursor != fence;
601     }
602    
603     public E next() {
604     E result;
605     if (cursor == fence)
606     throw new NoSuchElementException();
607     // This check doesn't catch all possible comodifications,
608     // but does catch the ones that corrupt traversal
609     if (tail != fence || (result = elements[cursor]) == null)
610     throw new ConcurrentModificationException();
611     lastRet = cursor;
612     cursor = (cursor + 1) & (elements.length - 1);
613     return result;
614     }
615    
616     public void remove() {
617     if (lastRet < 0)
618     throw new IllegalStateException();
619     if (delete(lastRet))
620     cursor--;
621     lastRet = -1;
622     fence = tail;
623     }
624     }
625    
626     /**
627     * Returns <tt>true</tt> if this deque contains the specified
628     * element. More formally, returns <tt>true</tt> if and only if this
629     * deque contains at least one element <tt>e</tt> such that
630     * <tt>e.equals(o)</tt>.
631     *
632     * @param o object to be checked for containment in this deque
633     * @return <tt>true</tt> if this deque contains the specified element
634     */
635     public boolean contains(Object o) {
636     if (o == null)
637     return false;
638     int mask = elements.length - 1;
639     int i = head;
640     E x;
641     while ( (x = elements[i]) != null) {
642     if (o.equals(x))
643     return true;
644     i = (i + 1) & mask;
645     }
646     return false;
647     }
648    
649     /**
650     * Removes a single instance of the specified element from this deque.
651     * This method is equivalent to {@link #removeFirstOccurrence}.
652     *
653     * @param e element to be removed from this deque, if present
654     * @return <tt>true</tt> if this deque contained the specified element
655     */
656     public boolean remove(Object e) {
657     return removeFirstOccurrence(e);
658     }
659    
660     /**
661     * Removes all of the elements from this deque.
662     */
663     public void clear() {
664     int h = head;
665     int t = tail;
666     if (h != t) { // clear all cells
667     head = tail = 0;
668     int i = h;
669     int mask = elements.length - 1;
670     do {
671     elements[i] = null;
672     i = (i + 1) & mask;
673     } while(i != t);
674     }
675     }
676    
677     /**
678 dl 1.5 * Returns an array containing all of the elements in this deque
679 dl 1.1 * in the correct order.
680     *
681 dl 1.5 * @return an array containing all of the elements in this deque
682 dl 1.1 * in the correct order
683     */
684     public Object[] toArray() {
685     return copyElements(new Object[size()]);
686     }
687    
688     /**
689     * Returns an array containing all of the elements in this deque in the
690     * correct order; the runtime type of the returned array is that of the
691     * specified array. If the deque fits in the specified array, it is
692     * returned therein. Otherwise, a new array is allocated with the runtime
693     * type of the specified array and the size of this deque.
694     *
695     * <p>If the deque fits in the specified array with room to spare (i.e.,
696     * the array has more elements than the deque), the element in the array
697     * immediately following the end of the collection is set to <tt>null</tt>.
698     *
699     * @param a the array into which the elements of the deque are to
700     * be stored, if it is big enough; otherwise, a new array of the
701     * same runtime type is allocated for this purpose
702     * @return an array containing the elements of the deque
703     * @throws ArrayStoreException if the runtime type of a is not a supertype
704     * of the runtime type of every element in this deque
705     */
706     public <T> T[] toArray(T[] a) {
707     int size = size();
708     if (a.length < size)
709     a = (T[])java.lang.reflect.Array.newInstance(
710     a.getClass().getComponentType(), size);
711     copyElements(a);
712     if (a.length > size)
713     a[size] = null;
714     return a;
715     }
716    
717     // *** Object methods ***
718    
719     /**
720     * Returns a copy of this deque.
721     *
722     * @return a copy of this deque
723     */
724     public ArrayDeque<E> clone() {
725 dl 1.5 try {
726 dl 1.1 ArrayDeque<E> result = (ArrayDeque<E>) super.clone();
727     // These two lines are currently faster than cloning the array:
728     result.elements = (E[]) new Object[elements.length];
729     System.arraycopy(elements, 0, result.elements, 0, elements.length);
730     return result;
731    
732 dl 1.5 } catch (CloneNotSupportedException e) {
733 dl 1.1 throw new AssertionError();
734     }
735     }
736    
737     /**
738     * Appease the serialization gods.
739     */
740     private static final long serialVersionUID = 2340985798034038923L;
741    
742     /**
743     * Serialize this deque.
744     *
745     * @serialData The current size (<tt>int</tt>) of the deque,
746     * followed by all of its elements (each an object reference) in
747     * first-to-last order.
748     */
749     private void writeObject(ObjectOutputStream s) throws IOException {
750     s.defaultWriteObject();
751    
752     // Write out size
753     int size = size();
754     s.writeInt(size);
755    
756     // Write out elements in order.
757     int i = head;
758     int mask = elements.length - 1;
759     for (int j = 0; j < size; j++) {
760     s.writeObject(elements[i]);
761     i = (i + 1) & mask;
762     }
763     }
764    
765     /**
766     * Deserialize this deque.
767     */
768     private void readObject(ObjectInputStream s)
769     throws IOException, ClassNotFoundException {
770     s.defaultReadObject();
771    
772     // Read in size and allocate array
773     int size = s.readInt();
774     allocateElements(size);
775     head = 0;
776     tail = size;
777    
778     // Read in all elements in the proper order.
779     for (int i = 0; i < size; i++)
780     elements[i] = (E)s.readObject();
781    
782     }
783     }