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.DoubleList; 36 import java.util.Arrays; 37 import java.util.Collection; 38 import java.util.RandomAccess; 39 40 /** 41 * An implementation of {@link DoubleList} on top of a primitive array. 42 * 43 * @author dweis@google.com (Daniel Weis) 44 */ 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 { EMPTY_LIST.makeImmutable()51 EMPTY_LIST.makeImmutable(); 52 } 53 emptyList()54 public static DoubleArrayList emptyList() { 55 return EMPTY_LIST; 56 } 57 58 /** The backing store for the list. */ 59 private double[] 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 DoubleArrayList} with default capacity. */ DoubleArrayList()68 DoubleArrayList() { 69 this(new double[DEFAULT_CAPACITY], 0); 70 } 71 72 /** 73 * Constructs a new mutable {@code DoubleArrayList} containing the same elements as {@code other}. 74 */ DoubleArrayList(double[] other, int size)75 private DoubleArrayList(double[] 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 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 hashCode()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 mutableCopyWithCapacity(int capacity)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 get(int index)134 public Double get(int index) { 135 return getDouble(index); 136 } 137 138 @Override getDouble(int index)139 public double getDouble(int index) { 140 ensureIndexInRange(index); 141 return array[index]; 142 } 143 144 @Override size()145 public int size() { 146 return size; 147 } 148 149 @Override set(int index, Double element)150 public Double set(int index, Double element) { 151 return setDouble(index, element); 152 } 153 154 @Override setDouble(int index, double element)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 add(int index, Double element)164 public void add(int index, Double element) { 165 addDouble(index, element); 166 } 167 168 /** Like {@link #add(Double)} but more efficient in that it doesn't box the element. */ 169 @Override addDouble(double element)170 public void addDouble(double element) { 171 addDouble(size, element); 172 } 173 174 /** Like {@link #add(int, Double)} but more efficient in that it doesn't box the element. */ addDouble(int index, double element)175 private void addDouble(int index, double element) { 176 ensureIsMutable(); 177 if (index < 0 || index > size) { 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 addAll(Collection<? extends Double> collection)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 remove(Object o)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 remove(int index)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 262 /** 263 * Ensures that the provided {@code index} is within the range of {@code [0, size]}. Throws an 264 * {@link IndexOutOfBoundsException} if it is not. 265 * 266 * @param index the index to verify is in range 267 */ ensureIndexInRange(int index)268 private void ensureIndexInRange(int index) { 269 if (index < 0 || index >= size) { 270 throw new IndexOutOfBoundsException(makeOutOfBoundsExceptionMessage(index)); 271 } 272 } 273 makeOutOfBoundsExceptionMessage(int index)274 private String makeOutOfBoundsExceptionMessage(int index) { 275 return "Index:" + index + ", Size:" + size; 276 } 277 } 278