• 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.FloatList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
39 
40 /**
41  * An implementation of {@link FloatList} on top of a primitive array.
42  *
43  * @author dweis@google.com (Daniel Weis)
44  */
45 final class FloatArrayList extends AbstractProtobufList<Float>
46     implements FloatList, RandomAccess, PrimitiveNonBoxingCollection {
47 
48   private static final FloatArrayList EMPTY_LIST = new FloatArrayList(new float[0], 0);
49 
50   static {
EMPTY_LIST.makeImmutable()51     EMPTY_LIST.makeImmutable();
52   }
53 
emptyList()54   public static FloatArrayList emptyList() {
55     return EMPTY_LIST;
56   }
57 
58   /** The backing store for the list. */
59   private float[] array;
60 
61   /**
62    * The size of the list distinct from the length of the array. That is, it is the number of
63    * elements set in the list.
64    */
65   private int size;
66 
67   /** Constructs a new mutable {@code FloatArrayList} with default capacity. */
FloatArrayList()68   FloatArrayList() {
69     this(new float[DEFAULT_CAPACITY], 0);
70   }
71 
72   /**
73    * Constructs a new mutable {@code FloatArrayList} containing the same elements as {@code other}.
74    */
FloatArrayList(float[] other, int size)75   private FloatArrayList(float[] other, int size) {
76     array = other;
77     this.size = size;
78   }
79 
80   @Override
removeRange(int fromIndex, int toIndex)81   protected void removeRange(int fromIndex, int toIndex) {
82     ensureIsMutable();
83     if (toIndex < fromIndex) {
84       throw new IndexOutOfBoundsException("toIndex < fromIndex");
85     }
86 
87     System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
88     size -= (toIndex - fromIndex);
89     modCount++;
90   }
91 
92   @Override
equals(Object o)93   public boolean equals(Object o) {
94     if (this == o) {
95       return true;
96     }
97     if (!(o instanceof FloatArrayList)) {
98       return super.equals(o);
99     }
100     FloatArrayList other = (FloatArrayList) o;
101     if (size != other.size) {
102       return false;
103     }
104 
105     final float[] arr = other.array;
106     for (int i = 0; i < size; i++) {
107       if (Float.floatToIntBits(array[i]) != Float.floatToIntBits(arr[i])) {
108         return false;
109       }
110     }
111 
112     return true;
113   }
114 
115   @Override
hashCode()116   public int hashCode() {
117     int result = 1;
118     for (int i = 0; i < size; i++) {
119       result = (31 * result) + Float.floatToIntBits(array[i]);
120     }
121     return result;
122   }
123 
124   @Override
mutableCopyWithCapacity(int capacity)125   public FloatList mutableCopyWithCapacity(int capacity) {
126     if (capacity < size) {
127       throw new IllegalArgumentException();
128     }
129     return new FloatArrayList(Arrays.copyOf(array, capacity), size);
130   }
131 
132   @Override
get(int index)133   public Float get(int index) {
134     return getFloat(index);
135   }
136 
137   @Override
getFloat(int index)138   public float getFloat(int index) {
139     ensureIndexInRange(index);
140     return array[index];
141   }
142 
143   @Override
size()144   public int size() {
145     return size;
146   }
147 
148   @Override
set(int index, Float element)149   public Float set(int index, Float element) {
150     return setFloat(index, element);
151   }
152 
153   @Override
setFloat(int index, float element)154   public float setFloat(int index, float element) {
155     ensureIsMutable();
156     ensureIndexInRange(index);
157     float previousValue = array[index];
158     array[index] = element;
159     return previousValue;
160   }
161 
162   @Override
add(int index, Float element)163   public void add(int index, Float element) {
164     addFloat(index, element);
165   }
166 
167   /** Like {@link #add(Float)} but more efficient in that it doesn't box the element. */
168   @Override
addFloat(float element)169   public void addFloat(float element) {
170     addFloat(size, element);
171   }
172 
173   /** Like {@link #add(int, Float)} but more efficient in that it doesn't box the element. */
addFloat(int index, float element)174   private void addFloat(int index, float element) {
175     ensureIsMutable();
176     if (index < 0 || index > size) {
177       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
178     }
179 
180     if (size < array.length) {
181       // Shift everything over to make room
182       System.arraycopy(array, index, array, index + 1, size - index);
183     } else {
184       // Resize to 1.5x the size
185       int length = ((size * 3) / 2) + 1;
186       float[] newArray = new float[length];
187 
188       // Copy the first part directly
189       System.arraycopy(array, 0, newArray, 0, index);
190 
191       // Copy the rest shifted over by one to make room
192       System.arraycopy(array, index, newArray, index + 1, size - index);
193       array = newArray;
194     }
195 
196     array[index] = element;
197     size++;
198     modCount++;
199   }
200 
201   @Override
addAll(Collection<? extends Float> collection)202   public boolean addAll(Collection<? extends Float> collection) {
203     ensureIsMutable();
204 
205     checkNotNull(collection);
206 
207     // We specialize when adding another FloatArrayList to avoid boxing elements.
208     if (!(collection instanceof FloatArrayList)) {
209       return super.addAll(collection);
210     }
211 
212     FloatArrayList list = (FloatArrayList) collection;
213     if (list.size == 0) {
214       return false;
215     }
216 
217     int overflow = Integer.MAX_VALUE - size;
218     if (overflow < list.size) {
219       // We can't actually represent a list this large.
220       throw new OutOfMemoryError();
221     }
222 
223     int newSize = size + list.size;
224     if (newSize > array.length) {
225       array = Arrays.copyOf(array, newSize);
226     }
227 
228     System.arraycopy(list.array, 0, array, size, list.size);
229     size = newSize;
230     modCount++;
231     return true;
232   }
233 
234   @Override
remove(Object o)235   public boolean remove(Object o) {
236     ensureIsMutable();
237     for (int i = 0; i < size; i++) {
238       if (o.equals(array[i])) {
239         System.arraycopy(array, i + 1, array, i, size - i - 1);
240         size--;
241         modCount++;
242         return true;
243       }
244     }
245     return false;
246   }
247 
248   @Override
remove(int index)249   public Float remove(int index) {
250     ensureIsMutable();
251     ensureIndexInRange(index);
252     float value = array[index];
253     if (index < size - 1) {
254       System.arraycopy(array, index + 1, array, index, size - index - 1);
255     }
256     size--;
257     modCount++;
258     return value;
259   }
260 
261   /**
262    * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an
263    * {@link IndexOutOfBoundsException} if it is not.
264    *
265    * @param index the index to verify is in range
266    */
ensureIndexInRange(int index)267   private void ensureIndexInRange(int index) {
268     if (index < 0 || index >= size) {
269       throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
270     }
271   }
272 
makeOutOfBoundsExceptionMessage(int index)273   private String makeOutOfBoundsExceptionMessage(int index) {
274     return "Index:" + index + ", Size:" + size;
275   }
276 }
277