DoubleArrayList.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 DoubleArrayList extends AbstractProtobufList<Double>
46  implements DoubleList, RandomAccess, PrimitiveNonBoxingCollection {
47 
48  private static final DoubleArrayList EMPTY_LIST = new DoubleArrayList(new double[0], 0);
49 
50  static {
51  EMPTY_LIST.makeImmutable();
52  }
53 
54  public static DoubleArrayList emptyList() {
55  return EMPTY_LIST;
56  }
57 
59  private double[] array;
60 
65  private int size;
66 
68  DoubleArrayList() {
69  this(new double[DEFAULT_CAPACITY], 0);
70  }
71 
75  private DoubleArrayList(double[] 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 DoubleArrayList)) {
98  return super.equals(o);
99  }
100  DoubleArrayList other = (DoubleArrayList) o;
101  if (size != other.size) {
102  return false;
103  }
104 
105  final double[] arr = other.array;
106  for (int i = 0; i < size; i++) {
107  if (Double.doubleToLongBits(array[i]) != Double.doubleToLongBits(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  long bits = Double.doubleToLongBits(array[i]);
120  result = (31 * result) + Internal.hashLong(bits);
121  }
122  return result;
123  }
124 
125  @Override
126  public DoubleList mutableCopyWithCapacity(int capacity) {
127  if (capacity < size) {
128  throw new IllegalArgumentException();
129  }
130  return new DoubleArrayList(Arrays.copyOf(array, capacity), size);
131  }
132 
133  @Override
134  public Double get(int index) {
135  return getDouble(index);
136  }
137 
138  @Override
139  public double getDouble(int index) {
140  ensureIndexInRange(index);
141  return array[index];
142  }
143 
144  @Override
145  public int size() {
146  return size;
147  }
148 
149  @Override
150  public Double set(int index, Double element) {
151  return setDouble(index, element);
152  }
153 
154  @Override
155  public double setDouble(int index, double element) {
156  ensureIsMutable();
157  ensureIndexInRange(index);
158  double previousValue = array[index];
159  array[index] = element;
160  return previousValue;
161  }
162 
163  @Override
164  public void add(int index, Double element) {
165  addDouble(index, element);
166  }
167 
169  @Override
170  public void addDouble(double element) {
171  addDouble(size, element);
172  }
173 
175  private void addDouble(int index, double element) {
176  ensureIsMutable();
178  throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
179  }
180 
181  if (size < array.length) {
182  // Shift everything over to make room
183  System.arraycopy(array, index, array, index + 1, size - index);
184  } else {
185  // Resize to 1.5x the size
186  int length = ((size * 3) / 2) + 1;
187  double[] newArray = new double[length];
188 
189  // Copy the first part directly
190  System.arraycopy(array, 0, newArray, 0, index);
191 
192  // Copy the rest shifted over by one to make room
193  System.arraycopy(array, index, newArray, index + 1, size - index);
194  array = newArray;
195  }
196 
197  array[index] = element;
198  size++;
199  modCount++;
200  }
201 
202  @Override
203  public boolean addAll(Collection<? extends Double> collection) {
204  ensureIsMutable();
205 
206  checkNotNull(collection);
207 
208  // We specialize when adding another DoubleArrayList to avoid boxing elements.
209  if (!(collection instanceof DoubleArrayList)) {
210  return super.addAll(collection);
211  }
212 
213  DoubleArrayList list = (DoubleArrayList) collection;
214  if (list.size == 0) {
215  return false;
216  }
217 
218  int overflow = Integer.MAX_VALUE - size;
219  if (overflow < list.size) {
220  // We can't actually represent a list this large.
221  throw new OutOfMemoryError();
222  }
223 
224  int newSize = size + list.size;
225  if (newSize > array.length) {
226  array = Arrays.copyOf(array, newSize);
227  }
228 
229  System.arraycopy(list.array, 0, array, size, list.size);
230  size = newSize;
231  modCount++;
232  return true;
233  }
234 
235  @Override
236  public boolean remove(Object o) {
237  ensureIsMutable();
238  for (int i = 0; i < size; i++) {
239  if (o.equals(array[i])) {
240  System.arraycopy(array, i + 1, array, i, size - i - 1);
241  size--;
242  modCount++;
243  return true;
244  }
245  }
246  return false;
247  }
248 
249  @Override
250  public Double remove(int index) {
251  ensureIsMutable();
252  ensureIndexInRange(index);
253  double value = array[index];
254  if (index < size - 1) {
255  System.arraycopy(array, index + 1, array, index, size - index - 1);
256  }
257  size--;
258  modCount++;
259  return value;
260  }
261 
268  private void ensureIndexInRange(int index) {
269  if (index < 0 || index >= size) {
270  throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index));
271  }
272  }
273 
274  private String makeOutOfBoundsExceptionMessage(int index) {
275  return "Index:" + index + ", Size:" + size;
276  }
277 }
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
testing::internal::Double
FloatingPoint< double > Double
Definition: gtest-internal.h:429
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
com.google.protobuf.Internal.DoubleList
Definition: Internal.java:651
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:50