OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 20 matching lines...) Expand all Loading... |
31 package com.google.protobuf; | 31 package com.google.protobuf; |
32 | 32 |
33 import com.google.protobuf.Internal.FloatList; | 33 import com.google.protobuf.Internal.FloatList; |
34 | 34 |
35 import java.util.Arrays; | 35 import java.util.Arrays; |
36 import java.util.Collection; | 36 import java.util.Collection; |
37 import java.util.RandomAccess; | 37 import java.util.RandomAccess; |
38 | 38 |
39 /** | 39 /** |
40 * An implementation of {@link FloatList} on top of a primitive array. | 40 * An implementation of {@link FloatList} on top of a primitive array. |
41 * | 41 * |
42 * @author dweis@google.com (Daniel Weis) | 42 * @author dweis@google.com (Daniel Weis) |
43 */ | 43 */ |
44 final class FloatArrayList extends AbstractProtobufList<Float> implements FloatL
ist, RandomAccess { | 44 final class FloatArrayList |
45 | 45 extends AbstractProtobufList<Float> |
| 46 implements FloatList, RandomAccess { |
| 47 |
46 private static final FloatArrayList EMPTY_LIST = new FloatArrayList(); | 48 private static final FloatArrayList EMPTY_LIST = new FloatArrayList(); |
47 static { | 49 static { |
48 EMPTY_LIST.makeImmutable(); | 50 EMPTY_LIST.makeImmutable(); |
49 } | 51 } |
50 | 52 |
51 public static FloatArrayList emptyList() { | 53 public static FloatArrayList emptyList() { |
52 return EMPTY_LIST; | 54 return EMPTY_LIST; |
53 } | 55 } |
54 | 56 |
55 /** | 57 /** |
56 * The backing store for the list. | 58 * The backing store for the list. |
57 */ | 59 */ |
58 private float[] array; | 60 private float[] array; |
59 | 61 |
60 /** | 62 /** |
61 * The size of the list distinct from the length of the array. That is, it is
the number of | 63 * 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. | 64 * elements set in the list. |
63 */ | 65 */ |
64 private int size; | 66 private int size; |
65 | 67 |
66 /** | 68 /** |
67 * Constructs a new mutable {@code FloatArrayList} with default capacity. | 69 * Constructs a new mutable {@code FloatArrayList} with default capacity. |
68 */ | 70 */ |
69 FloatArrayList() { | 71 FloatArrayList() { |
70 this(new float[DEFAULT_CAPACITY], 0); | 72 this(new float[DEFAULT_CAPACITY], 0); |
71 } | 73 } |
72 | 74 |
73 /** | 75 /** |
74 * Constructs a new mutable {@code FloatArrayList} containing the same element
s as {@code other}. | 76 * Constructs a new mutable {@code FloatArrayList} |
| 77 * containing the same elements as {@code other}. |
75 */ | 78 */ |
76 private FloatArrayList(float[] array, int size) { | 79 private FloatArrayList(float[] other, int size) { |
77 this.array = array; | 80 array = other; |
78 this.size = size; | 81 this.size = size; |
79 } | 82 } |
80 | 83 |
81 @Override | 84 @Override |
82 public boolean equals(Object o) { | 85 public boolean equals(Object o) { |
83 if (this == o) { | 86 if (this == o) { |
84 return true; | 87 return true; |
85 } | 88 } |
86 if (!(o instanceof FloatArrayList)) { | 89 if (!(o instanceof FloatArrayList)) { |
87 return super.equals(o); | 90 return super.equals(o); |
88 } | 91 } |
89 FloatArrayList other = (FloatArrayList) o; | 92 FloatArrayList other = (FloatArrayList) o; |
90 if (size != other.size) { | 93 if (size != other.size) { |
91 return false; | 94 return false; |
92 } | 95 } |
93 | 96 |
94 final float[] arr = other.array; | 97 final float[] arr = other.array; |
95 for (int i = 0; i < size; i++) { | 98 for (int i = 0; i < size; i++) { |
96 if (array[i] != arr[i]) { | 99 if (array[i] != arr[i]) { |
97 return false; | 100 return false; |
98 } | 101 } |
99 } | 102 } |
100 | 103 |
101 return true; | 104 return true; |
102 } | 105 } |
103 | 106 |
104 @Override | 107 @Override |
105 public int hashCode() { | 108 public int hashCode() { |
106 int result = 1; | 109 int result = 1; |
107 for (int i = 0; i < size; i++) { | 110 for (int i = 0; i < size; i++) { |
108 result = (31 * result) + Float.floatToIntBits(array[i]); | 111 result = (31 * result) + Float.floatToIntBits(array[i]); |
109 } | 112 } |
110 return result; | 113 return result; |
111 } | 114 } |
112 | 115 |
113 @Override | 116 @Override |
114 public FloatList mutableCopyWithCapacity(int capacity) { | 117 public FloatList mutableCopyWithCapacity(int capacity) { |
115 if (capacity < size) { | 118 if (capacity < size) { |
116 throw new IllegalArgumentException(); | 119 throw new IllegalArgumentException(); |
117 } | 120 } |
118 return new FloatArrayList(Arrays.copyOf(array, capacity), size); | 121 return new FloatArrayList(Arrays.copyOf(array, capacity), size); |
119 } | 122 } |
120 | 123 |
121 @Override | 124 @Override |
122 public Float get(int index) { | 125 public Float get(int index) { |
123 return getFloat(index); | 126 return getFloat(index); |
124 } | 127 } |
125 | 128 |
126 @Override | 129 @Override |
127 public float getFloat(int index) { | 130 public float getFloat(int index) { |
128 ensureIndexInRange(index); | 131 ensureIndexInRange(index); |
129 return array[index]; | 132 return array[index]; |
130 } | 133 } |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 165 } |
163 | 166 |
164 /** | 167 /** |
165 * Like {@link #add(int, Float)} but more efficient in that it doesn't box the
element. | 168 * Like {@link #add(int, Float)} but more efficient in that it doesn't box the
element. |
166 */ | 169 */ |
167 private void addFloat(int index, float element) { | 170 private void addFloat(int index, float element) { |
168 ensureIsMutable(); | 171 ensureIsMutable(); |
169 if (index < 0 || index > size) { | 172 if (index < 0 || index > size) { |
170 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); | 173 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); |
171 } | 174 } |
172 | 175 |
173 if (size < array.length) { | 176 if (size < array.length) { |
174 // Shift everything over to make room | 177 // Shift everything over to make room |
175 System.arraycopy(array, index, array, index + 1, size - index); | 178 System.arraycopy(array, index, array, index + 1, size - index); |
176 } else { | 179 } else { |
177 // Resize to 1.5x the size | 180 // Resize to 1.5x the size |
178 int length = ((size * 3) / 2) + 1; | 181 int length = ((size * 3) / 2) + 1; |
179 float[] newArray = new float[length]; | 182 float[] newArray = new float[length]; |
180 | 183 |
181 // Copy the first part directly | 184 // Copy the first part directly |
182 System.arraycopy(array, 0, newArray, 0, index); | 185 System.arraycopy(array, 0, newArray, 0, index); |
183 | 186 |
184 // Copy the rest shifted over by one to make room | 187 // Copy the rest shifted over by one to make room |
185 System.arraycopy(array, index, newArray, index + 1, size - index); | 188 System.arraycopy(array, index, newArray, index + 1, size - index); |
186 array = newArray; | 189 array = newArray; |
187 } | 190 } |
188 | 191 |
189 array[index] = element; | 192 array[index] = element; |
190 size++; | 193 size++; |
191 modCount++; | 194 modCount++; |
192 } | 195 } |
193 | 196 |
194 @Override | 197 @Override |
195 public boolean addAll(Collection<? extends Float> collection) { | 198 public boolean addAll(Collection<? extends Float> collection) { |
196 ensureIsMutable(); | 199 ensureIsMutable(); |
197 | 200 |
198 if (collection == null) { | 201 if (collection == null) { |
199 throw new NullPointerException(); | 202 throw new NullPointerException(); |
200 } | 203 } |
201 | 204 |
202 // We specialize when adding another FloatArrayList to avoid boxing elements
. | 205 // We specialize when adding another FloatArrayList to avoid boxing elements
. |
203 if (!(collection instanceof FloatArrayList)) { | 206 if (!(collection instanceof FloatArrayList)) { |
204 return super.addAll(collection); | 207 return super.addAll(collection); |
205 } | 208 } |
206 | 209 |
207 FloatArrayList list = (FloatArrayList) collection; | 210 FloatArrayList list = (FloatArrayList) collection; |
208 if (list.size == 0) { | 211 if (list.size == 0) { |
209 return false; | 212 return false; |
210 } | 213 } |
211 | 214 |
212 int overflow = Integer.MAX_VALUE - size; | 215 int overflow = Integer.MAX_VALUE - size; |
213 if (overflow < list.size) { | 216 if (overflow < list.size) { |
214 // We can't actually represent a list this large. | 217 // We can't actually represent a list this large. |
215 throw new OutOfMemoryError(); | 218 throw new OutOfMemoryError(); |
216 } | 219 } |
217 | 220 |
218 int newSize = size + list.size; | 221 int newSize = size + list.size; |
219 if (newSize > array.length) { | 222 if (newSize > array.length) { |
220 array = Arrays.copyOf(array, newSize); | 223 array = Arrays.copyOf(array, newSize); |
221 } | 224 } |
222 | 225 |
223 System.arraycopy(list.array, 0, array, size, list.size); | 226 System.arraycopy(list.array, 0, array, size, list.size); |
224 size = newSize; | 227 size = newSize; |
225 modCount++; | 228 modCount++; |
226 return true; | 229 return true; |
227 } | 230 } |
228 | 231 |
229 @Override | 232 @Override |
230 public boolean remove(Object o) { | 233 public boolean remove(Object o) { |
231 ensureIsMutable(); | 234 ensureIsMutable(); |
232 for (int i = 0; i < size; i++) { | 235 for (int i = 0; i < size; i++) { |
233 if (o.equals(array[i])) { | 236 if (o.equals(array[i])) { |
234 System.arraycopy(array, i + 1, array, i, size - i); | 237 System.arraycopy(array, i + 1, array, i, size - i); |
235 size--; | 238 size--; |
236 modCount++; | 239 modCount++; |
237 return true; | 240 return true; |
238 } | 241 } |
239 } | 242 } |
240 return false; | 243 return false; |
241 } | 244 } |
242 | 245 |
243 @Override | 246 @Override |
244 public Float remove(int index) { | 247 public Float remove(int index) { |
245 ensureIsMutable(); | 248 ensureIsMutable(); |
246 ensureIndexInRange(index); | 249 ensureIndexInRange(index); |
247 float value = array[index]; | 250 float value = array[index]; |
248 System.arraycopy(array, index + 1, array, index, size - index); | 251 System.arraycopy(array, index + 1, array, index, size - index); |
249 size--; | 252 size--; |
250 modCount++; | 253 modCount++; |
251 return value; | 254 return value; |
252 } | 255 } |
253 | 256 |
254 /** | 257 /** |
255 * Ensures that the provided {@code index} is within the range of {@code [0, s
ize]}. Throws an | 258 * Ensures that the provided {@code index} is within the range of {@code [0, s
ize]}. Throws an |
256 * {@link IndexOutOfBoundsException} if it is not. | 259 * {@link IndexOutOfBoundsException} if it is not. |
257 * | 260 * |
258 * @param index the index to verify is in range | 261 * @param index the index to verify is in range |
259 */ | 262 */ |
260 private void ensureIndexInRange(int index) { | 263 private void ensureIndexInRange(int index) { |
261 if (index < 0 || index >= size) { | 264 if (index < 0 || index >= size) { |
262 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); | 265 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)
); |
263 } | 266 } |
264 } | 267 } |
265 | 268 |
266 private String makeOutOfBoundsExceptionMessage(int index) { | 269 private String makeOutOfBoundsExceptionMessage(int index) { |
267 return "Index:" + index + ", Size:" + size; | 270 return "Index:" + index + ", Size:" + size; |
268 } | 271 } |
269 } | 272 } |
OLD | NEW |