FloatArrayList.java
Go to the documentation of this file.
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 
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.RandomAccess;
39 
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 {
51  EMPTY_LIST.makeImmutable();
52  }
53 
54  public static FloatArrayList emptyList() {
55  return EMPTY_LIST;
56  }
57 
59  private float[] array;
60 
65  private int size;
66 
68  FloatArrayList() {
69  this(new float[DEFAULT_CAPACITY], 0);
70  }
71 
75  private FloatArrayList(float[] other, int size) {
76  array = other;
77  this.size = size;
78  }
79 
80  @Override
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
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
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
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
133  public Float get(int index) {
134  return getFloat(index);
135  }
136 
137  @Override
138  public float getFloat(int index) {
139  ensureIndexInRange(index);
140  return array[index];
141  }
142 
143  @Override
144  public int size() {
145  return size;
146  }
147 
148  @Override
149  public Float set(int index, Float element) {
150  return setFloat(index, element);
151  }
152 
153  @Override
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
163  public void add(int index, Float element) {
164  addFloat(index, element);
165  }
166 
168  @Override
169  public void addFloat(float element) {
170  addFloat(size, element);
171  }
172 
174  private void addFloat(int index, float element) {
175  ensureIsMutable();
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
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
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
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 
267  private void ensureIndexInRange(int index) {
268  if (index < 0 || index >= size) {
269  throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
270  }
271  }
272 
273  private String makeOutOfBoundsExceptionMessage(int index) {
274  return "Index:" + index + ", Size:" + size;
275  }
276 }
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
testing::internal::Float
FloatingPoint< float > Float
Definition: gtest-internal.h:428
com.google.protobuf.Internal.FloatList
Definition: Internal.java:671
size
#define size
Definition: glcorearb.h:2944
i
int i
Definition: gmock-matchers_test.cc:764
java
size
GLsizeiptr size
Definition: glcorearb.h:2943
com.google
com
com.google.protobuf.Internal
Definition: Internal.java:54
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
index
GLuint index
Definition: glcorearb.h:3055
array
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern array
Definition: array.c:111


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51