BooleanArrayList.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 BooleanArrayList extends AbstractProtobufList<Boolean>
46  implements BooleanList, RandomAccess, PrimitiveNonBoxingCollection {
47 
48  private static final BooleanArrayList EMPTY_LIST = new BooleanArrayList(new boolean[0], 0);
49 
50  static {
51  EMPTY_LIST.makeImmutable();
52  }
53 
54  public static BooleanArrayList emptyList() {
55  return EMPTY_LIST;
56  }
57 
59  private boolean[] array;
60 
65  private int size;
66 
68  BooleanArrayList() {
69  this(new boolean[DEFAULT_CAPACITY], 0);
70  }
71 
76  private BooleanArrayList(boolean[] other, int size) {
77  array = other;
78  this.size = size;
79  }
80 
81  @Override
82  protected void removeRange(int fromIndex, int toIndex) {
83  ensureIsMutable();
84  if (toIndex < fromIndex) {
85  throw new IndexOutOfBoundsException("toIndex < fromIndex");
86  }
87 
88  System.arraycopy(array, toIndex, array, fromIndex, size - toIndex);
89  size -= (toIndex - fromIndex);
90  modCount++;
91  }
92 
93  @Override
94  public boolean equals(Object o) {
95  if (this == o) {
96  return true;
97  }
98  if (!(o instanceof BooleanArrayList)) {
99  return super.equals(o);
100  }
101  BooleanArrayList other = (BooleanArrayList) o;
102  if (size != other.size) {
103  return false;
104  }
105 
106  final boolean[] arr = other.array;
107  for (int i = 0; i < size; i++) {
108  if (array[i] != arr[i]) {
109  return false;
110  }
111  }
112 
113  return true;
114  }
115 
116  @Override
117  public int hashCode() {
118  int result = 1;
119  for (int i = 0; i < size; i++) {
120  result = (31 * result) + Internal.hashBoolean(array[i]);
121  }
122  return result;
123  }
124 
125  @Override
126  public BooleanList mutableCopyWithCapacity(int capacity) {
127  if (capacity < size) {
128  throw new IllegalArgumentException();
129  }
130  return new BooleanArrayList(Arrays.copyOf(array, capacity), size);
131  }
132 
133  @Override
134  public Boolean get(int index) {
135  return getBoolean(index);
136  }
137 
138  @Override
139  public boolean getBoolean(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 Boolean set(int index, Boolean element) {
151  return setBoolean(index, element);
152  }
153 
154  @Override
155  public boolean setBoolean(int index, boolean element) {
156  ensureIsMutable();
157  ensureIndexInRange(index);
158  boolean previousValue = array[index];
159  array[index] = element;
160  return previousValue;
161  }
162 
163  @Override
164  public void add(int index, Boolean element) {
165  addBoolean(index, element);
166  }
167 
169  @Override
170  public void addBoolean(boolean element) {
171  addBoolean(size, element);
172  }
173 
175  private void addBoolean(int index, boolean 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  boolean[] newArray = new boolean[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 Boolean> collection) {
204  ensureIsMutable();
205 
206  checkNotNull(collection);
207 
208  // We specialize when adding another BooleanArrayList to avoid boxing elements.
209  if (!(collection instanceof BooleanArrayList)) {
210  return super.addAll(collection);
211  }
212 
213  BooleanArrayList list = (BooleanArrayList) 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 Boolean remove(int index) {
251  ensureIsMutable();
252  ensureIndexInRange(index);
253  boolean 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
size
#define size
Definition: glcorearb.h:2944
com.google.protobuf.Internal.BooleanList
Definition: Internal.java:611
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:48