• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static com.google.protobuf.Internal.checkNotNull;
34 
35 import com.google.protobuf.Internal.DoubleList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
39 
40 /**
41  * An implementation of {@link DoubleList} on top of a primitive array.
42  *
43  * @author dweis@google.com (Daniel Weis)
44  */
45 final class DoubleArrayList extends AbstractProtobufList<Double>
46     implements DoubleList, RandomAccess, PrimitiveNonBoxingCollection {
47 
48   private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList(new double[0], 0);
49   static {
EMPTY_LIST.makeImmutable()50     EMPTY_LIST.makeImmutable();
51   }
52 
emptyList()53   public static DoubleArrayList emptyList() {
54     return EMPTY_LIST;
55   }
56 
57   /** The backing store for the list. */
58   private double[] array;
59 
60   /**
61    * The size of the list distinct from the length of the array. That is, it is the number of
62    * elements set in the list.
63    */
64   private int size;
65 
66   /** Constructs a new mutable {@code DoubleArrayList} with default capacity. */
DoubleArrayList()67   DoubleArrayList() {
68     this(new double[DEFAULT_CAPACITY], 0);
69   }
70 
71   /**
72    * Constructs a new mutable {@code DoubleArrayList} containing the same elements as {@code other}.
73    */
DoubleArrayList(double[] other, int size)74   private DoubleArrayList(double[] other, int size) {
75     array = other;
76     this.size = size;
77   }
78 
79   @Override
removeRange(int fromIndex, int toIndex)80   protected void removeRange(int fromIndex, int toIndex) {
81     ensureIsMutable();
82     if (toIndex < fromIndex) {
83       throw new IndexOutOfBoundsException("toIndex < fromIndex");
84     }
85 
86     System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
87     size -= (toIndex - fromIndex);
88     modCount++;
89   }
90 
91   @Override
equals(Object o)92   public boolean equals(Object o) {
93     if (this == o) {
94       return true;
95     }
96     if (!(o instanceof DoubleArrayList)) {
97       return super.equals(o);
98     }
99     DoubleArrayList other = (DoubleArrayList) o;
100     if (size != other.size) {
101       return false;
102     }
103 
104     final double[] arr = other.array;
105     for (int i = 0; i < size; i++) {
106       if (Double.doubleToLongBits(array[i]) != Double.doubleToLongBits(arr[i])) {
107         return false;
108       }
109     }
110 
111     return true;
112   }
113 
114   @Override
hashCode()115   public int hashCode() {
116     int result = 1;
117     for (int i = 0; i < size; i++) {
118       long bits = Double.doubleToLongBits(array[i]);
119       result = (31 * result) + Internal.hashLong(bits);
120     }
121     return result;
122   }
123 
124   @Override
mutableCopyWithCapacity(int capacity)125   public DoubleList mutableCopyWithCapacity(int capacity) {
126     if (capacity < size) {
127       throw new IllegalArgumentException();
128     }
129     return new DoubleArrayList(Arrays.copyOf(array, capacity), size);
130   }
131 
132   @Override
get(int index)133   public Double get(int index) {
134     return getDouble(index);
135   }
136 
137   @Override
getDouble(int index)138   public double getDouble(int index) {
139     ensureIndexInRange(index);
140     return array[index];
141   }
142 
143   @Override
indexOf(Object element)144   public int indexOf(Object element) {
145     if (!(element instanceof Double)) {
146       return -1;
147     }
148     double unboxedElement = (Double) element;
149     int numElems = size();
150     for (int i = 0; i < numElems; i++) {
151       if (array[i] == unboxedElement) {
152         return i;
153       }
154     }
155     return -1;
156   }
157 
158   @Override
contains(Object element)159   public boolean contains(Object element) {
160     return indexOf(element) != -1;
161   }
162 
163   @Override
size()164   public int size() {
165     return size;
166   }
167 
168   @Override
set(int index, Double element)169   public Double set(int index, Double element) {
170     return setDouble(index, element);
171   }
172 
173   @Override
setDouble(int index, double element)174   public double setDouble(int index, double element) {
175     ensureIsMutable();
176     ensureIndexInRange(index);
177     double previousValue = array[index];
178     array[index] = element;
179     return previousValue;
180   }
181 
182   @Override
add(Double element)183   public boolean add(Double element) {
184     addDouble(element);
185     return true;
186   }
187 
188   @Override
add(int index, Double element)189   public void add(int index, Double element) {
190     addDouble(index, element);
191   }
192 
193   /** Like {@link #add(Double)} but more efficient in that it doesn't box the element. */
194   @Override
addDouble(double element)195   public void addDouble(double element) {
196     ensureIsMutable();
197     if (size == array.length) {
198       // Resize to 1.5x the size
199       int length = ((size * 3) / 2) + 1;
200       double[] newArray = new double[length];
201 
202       System.arraycopy(array, 0, newArray, 0, size);
203       array = newArray;
204     }
205 
206     array[size++] = element;
207   }
208 
209   /** Like {@link #add(int, Double)} but more efficient in that it doesn't box the element. */
addDouble(int index, double element)210   private void addDouble(int index, double element) {
211     ensureIsMutable();
212     if (index < 0 || index > size) {
213       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
214     }
215 
216     if (size < array.length) {
217       // Shift everything over to make room
218       System.arraycopy(array, index, array, index + 1, size - index);
219     } else {
220       // Resize to 1.5x the size
221       int length = ((size * 3) / 2) + 1;
222       double[] newArray = new double[length];
223 
224       // Copy the first part directly
225       System.arraycopy(array, 0, newArray, 0, index);
226 
227       // Copy the rest shifted over by one to make room
228       System.arraycopy(array, index, newArray, index + 1, size - index);
229       array = newArray;
230     }
231 
232     array[index] = element;
233     size++;
234     modCount++;
235   }
236 
237   @Override
addAll(Collection<? extends Double> collection)238   public boolean addAll(Collection<? extends Double> collection) {
239     ensureIsMutable();
240 
241     checkNotNull(collection);
242 
243     // We specialize when adding another DoubleArrayList to avoid boxing elements.
244     if (!(collection instanceof DoubleArrayList)) {
245       return super.addAll(collection);
246     }
247 
248     DoubleArrayList list = (DoubleArrayList) collection;
249     if (list.size == 0) {
250       return false;
251     }
252 
253     int overflow = Integer.MAX_VALUE - size;
254     if (overflow < list.size) {
255       // We can't actually represent a list this large.
256       throw new OutOfMemoryError();
257     }
258 
259     int newSize = size + list.size;
260     if (newSize > array.length) {
261       array = Arrays.copyOf(array, newSize);
262     }
263 
264     System.arraycopy(list.array, 0, array, size, list.size);
265     size = newSize;
266     modCount++;
267     return true;
268   }
269 
270   @Override
remove(Object o)271   public boolean remove(Object o) {
272     ensureIsMutable();
273     for (int i = 0; i < size; i++) {
274       if (o.equals(array[i])) {
275         System.arraycopy(array, i + 1, array, i, size - i - 1);
276         size--;
277         modCount++;
278         return true;
279       }
280     }
281     return false;
282   }
283 
284   @Override
remove(int index)285   public Double remove(int index) {
286     ensureIsMutable();
287     ensureIndexInRange(index);
288     double value = array[index];
289     if (index < size - 1) {
290       System.arraycopy(array, index + 1, array, index, size - index - 1);
291     }
292     size--;
293     modCount++;
294     return value;
295   }
296 
297   /**
298    * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an
299    * {@link IndexOutOfBoundsException} if it is not.
300    *
301    * @param index the index to verify is in range
302    */
ensureIndexInRange(int index)303   private void ensureIndexInRange(int index) {
304     if (index < 0 || index >= size) {
305       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
306     }
307   }
308 
makeOutOfBoundsExceptionMessage(int index)309   private String makeOutOfBoundsExceptionMessage(int index) {
310     return "Index:" + index + ", Size:" + size;
311   }
312 }
313