ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/ArrayDeque.java
(Generate patch)

Comparing jsr166/src/main/java/util/ArrayDeque.java (file contents):
Revision 1.77 by jsr166, Tue Oct 18 00:33:05 2016 UTC vs.
Revision 1.81 by jsr166, Sat Oct 22 18:16:56 2016 UTC

# Line 134 | Line 134 | public class ArrayDeque<E> extends Abstr
134       * to ensure that it can hold at least the given number of elements.
135       *
136       * @param minCapacity the desired minimum capacity
137 <     * @since 9
137 >     * @since TBD
138       */
139 <    public void ensureCapacity(int minCapacity) {
139 >    /* public */ void ensureCapacity(int minCapacity) {
140          if (minCapacity > elements.length)
141              grow(minCapacity - elements.length);
142          // checkInvariants();
# Line 145 | Line 145 | public class ArrayDeque<E> extends Abstr
145      /**
146       * Minimizes the internal storage of this collection.
147       *
148 <     * @since 9
148 >     * @since TBD
149       */
150 <    public void trimToSize() {
150 >    /* public */ void trimToSize() {
151          if (size < elements.length) {
152              elements = toArray();
153              head = 0;
# Line 196 | Line 196 | public class ArrayDeque<E> extends Abstr
196      }
197  
198      /**
199     * Returns the array index of the last element.
200     * May return invalid index -1 if there are no elements.
201     */
202    final int tail() {
203        return add(head, size - 1, elements.length);
204    }
205
206    /**
207     * Adds i and j, mod modulus.
208     * Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus.
209     */
210    static final int add(int i, int j, int modulus) {
211        if ((i += j) - modulus >= 0) i -= modulus;
212        return i;
213    }
214
215    /**
199       * Increments i, mod modulus.
200       * Precondition and postcondition: 0 <= i < modulus.
201       */
# Line 231 | Line 214 | public class ArrayDeque<E> extends Abstr
214      }
215  
216      /**
217 +     * Adds i and j, mod modulus.
218 +     * Precondition and postcondition: 0 <= i < modulus, 0 <= j <= modulus.
219 +     */
220 +    static final int add(int i, int j, int modulus) {
221 +        if ((i += j) - modulus >= 0) i -= modulus;
222 +        return i;
223 +    }
224 +
225 +    /**
226 +     * Returns the array index of the last element.
227 +     * May return invalid index -1 if there are no elements.
228 +     */
229 +    final int tail() {
230 +        return add(head, size - 1, elements.length);
231 +    }
232 +
233 +    /**
234       * Returns element at array index i.
235       */
236      @SuppressWarnings("unchecked")
# Line 305 | Line 305 | public class ArrayDeque<E> extends Abstr
305      public boolean addAll(Collection<? extends E> c) {
306          // checkInvariants();
307          Object[] a, elements;
308 <        int len, capacity, s = size;
309 <        if ((len = (a = c.toArray()).length) == 0)
308 >        int newcomers, capacity, s = size;
309 >        if ((newcomers = (a = c.toArray()).length) == 0)
310              return false;
311 <        while ((capacity = (elements = this.elements).length) - s < len)
312 <            grow(len - (capacity - s));
311 >        while ((capacity = (elements = this.elements).length) - s < newcomers)
312 >            grow(newcomers - (capacity - s));
313          int i = add(head, s, capacity);
314          for (Object x : a) {
315              Objects.requireNonNull(x);
# Line 689 | Line 689 | public class ArrayDeque<E> extends Abstr
689  
690          DeqIterator() { cursor = head; }
691  
692        int advance(int i, int modulus) {
693            return inc(i, modulus);
694        }
695
696        void doRemove() {
697            if (delete(lastRet))
698                // if left-shifted, undo advance in next()
699                cursor = dec(cursor, elements.length);
700        }
701
692          public final boolean hasNext() {
693              return remaining > 0;
694          }
695  
696 <        public final E next() {
696 >        public E next() {
697              if (remaining == 0)
698                  throw new NoSuchElementException();
699              E e = checkedElementAt(elements, cursor);
700              lastRet = cursor;
701 <            cursor = advance(cursor, elements.length);
701 >            cursor = inc(cursor, elements.length);
702              remaining--;
703              return e;
704          }
705  
706 +        void postDelete(boolean leftShifted) {
707 +            if (leftShifted)
708 +                cursor = dec(cursor, elements.length); // undo inc in next
709 +        }
710 +
711          public final void remove() {
712              if (lastRet < 0)
713                  throw new IllegalStateException();
714 <            doRemove();
714 >            postDelete(delete(lastRet));
715              lastRet = -1;
716          }
717  
718 <        public final void forEachRemaining(Consumer<? super E> action) {
718 >        public void forEachRemaining(Consumer<? super E> action) {
719              Objects.requireNonNull(action);
720              final Object[] elements = ArrayDeque.this.elements;
721              final int capacity = elements.length;
722              int k = remaining;
723              remaining = 0;
724 <            for (int i = cursor; --k >= 0; i = advance(i, capacity))
724 >            for (int i = cursor; --k >= 0; i = inc(i, capacity))
725                  action.accept(checkedElementAt(elements, i));
726          }
727      }
# Line 734 | Line 729 | public class ArrayDeque<E> extends Abstr
729      private class DescendingIterator extends DeqIterator {
730          DescendingIterator() { cursor = tail(); }
731  
732 <        @Override int advance(int i, int modulus) {
733 <            return dec(i, modulus);
732 >        public final E next() {
733 >            if (remaining == 0)
734 >                throw new NoSuchElementException();
735 >            E e = checkedElementAt(elements, cursor);
736 >            lastRet = cursor;
737 >            cursor = dec(cursor, elements.length);
738 >            remaining--;
739 >            return e;
740 >        }
741 >
742 >        void postDelete(boolean leftShifted) {
743 >            if (!leftShifted)
744 >                cursor = inc(cursor, elements.length); // undo dec in next
745          }
746  
747 <        @Override void doRemove() {
748 <            if (!delete(lastRet))
749 <                // if right-shifted, undo advance in next
750 <                cursor = inc(cursor, elements.length);
747 >        public final void forEachRemaining(Consumer<? super E> action) {
748 >            Objects.requireNonNull(action);
749 >            final Object[] elements = ArrayDeque.this.elements;
750 >            final int capacity = elements.length;
751 >            int k = remaining;
752 >            remaining = 0;
753 >            for (int i = cursor; --k >= 0; i = dec(i, capacity))
754 >                action.accept(checkedElementAt(elements, i));
755          }
756      }
757  
# Line 845 | Line 855 | public class ArrayDeque<E> extends Abstr
855       * operator to that element, as specified by {@link List#replaceAll}.
856       *
857       * @param operator the operator to apply to each element
858 <     * @since 9
858 >     * @since TBD
859       */
860 <    public void replaceAll(UnaryOperator<E> operator) {
860 >    /* public */ void replaceAll(UnaryOperator<E> operator) {
861          Objects.requireNonNull(operator);
862          final Object[] elements = this.elements;
863          final int capacity = elements.length;
# Line 902 | Line 912 | public class ArrayDeque<E> extends Abstr
912              }
913              return deleted > 0;
914          } catch (Throwable ex) {
915 <            for (; remaining > 0;
916 <                 remaining--, i = inc(i, capacity), j = inc(j, capacity))
917 <                elements[j] = elements[i];
915 >            if (deleted > 0)
916 >                for (; remaining > 0;
917 >                     remaining--, i = inc(i, capacity), j = inc(j, capacity))
918 >                    elements[j] = elements[i];
919              throw ex;
920          } finally {
921              size -= deleted;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines