1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/*
* Copyright (C) 2014-2021 Brian L. Browning
*
* This file is part of Beagle
*
* Beagle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Beagle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package ints;
import java.util.Arrays;
/**
* <p>Interface {@code IntArray} represents an immutable {@code int[]} array.
* </p>
* Instances of class {@code IntArray} are required to be immutable.
*
* @author Brian L. Browning {@code <browning@uw.edu>}
*/
public interface IntArray {
/**
* Returns the number of elements in this {@code IntArray}.
* @return the number of elements in this {@code IntArray}
*/
int size();
/**
* Returns the specified array element.
* @param index an array index
* @return the specified array element
* @throws IndexOutOfBoundsException if
* {@code index < 0 || index >= this.size()}
*/
int get(int index);
/**
* Returns a copy of the specified array.
* @param ia a list of integers
* @return a copy of the specified array
* @throws NullPointerException if {@code ia == null}
*/
static int[] toArray(IntArray ia) {
int[] copy = new int[ia.size()];
for (int j=0; j<copy.length; ++j) {
copy[j] = ia.get(j);
}
return copy;
}
/**
* Returns a string representation of this {@code IntArray} by applying
* {@code java.utils.Arrays.toString()} to an equivalent {@code int[]}
* object.
*
* @param ia a list of integers
* @return a string representation of this {@code IntArray}.
* @throws NullPointerException if {@code ia == null}
*/
static String asString(IntArray ia) {
return Arrays.toString(toArray(ia));
}
/**
* Returns {@code true} if the specified {@code IntArray} objects
* represent the same sequence of integer values, and returns {@code false}
* otherwise.
* @param a a sequence of integer values
* @param b a sequence of integer values
* @return {@code true} if the specified {@code IntArray} objects
* represent the same sequence of integer values
*/
static boolean equals(IntArray a, IntArray b) {
if (a==b) {
return true;
}
else if (a.size()!=b.size()) {
return false;
}
else {
for (int j=0, n=a.size(); j<n; ++j) {
if (a.get(j)!=b.get(j)) {
return false;
}
}
}
return true;
}
/**
* Returns the maximum element, or {@code Integer.MIN_VALUE} if
* {@code this.size() == 0}.
* @param ia a list of integers
* @return the maximum element
* @throws NullPointerException if {@code ia == null}
*/
static int max(IntArray ia) {
int max = Integer.MIN_VALUE;
for (int j=0, n=ia.size(); j<n; ++j) {
if (ia.get(j) > max) {
max = ia.get(j);
}
}
return max;
}
/**
* Returns the minimum element, or {@code Integer.MAX_VALUE} if
* {@code this.size() == 0}.
* @param ia a list of integers
* @return the minimum element
* @throws NullPointerException if {@code ia == null}
*/
static int min(IntArray ia) {
int min = Integer.MAX_VALUE;
for (int j=0, n=ia.size(); j<n; ++j) {
if (ia.get(j) < min) {
min = ia.get(j);
}
}
return min;
}
/**
* Returns a new {@code IntArray} instance that has the same
* sequence of integers as the specified array of non-negative integers.
* @param ia the array of non-negative integers to be copied
* @param valueSize the exclusive end of the range of
* array values
* @return a new {@code IntArray} instance that has
* the same sequence of integers as the specified array
* @throws IllegalArgumentException if {@code valueSize < 1}
* @throws IllegalArgumentException if
* {@code (ia[j] < 0 || ia[j] > valueSize)} for any index {@code j}
* satisfying {@code (j >= 0 && j < ia.length)}
* @throws NullPointerException if {@code ia == null}
*/
static IntArray packedCreate(int[] ia, int valueSize) {
if (valueSize < 1) {
throw new IllegalArgumentException(String.valueOf(valueSize));
}
if (valueSize<=16) {
return new PackedIntArray(ia, valueSize);
}
else if (valueSize<=256) {
return new UnsignedByteArray(ia, valueSize);
}
else if (valueSize<=65536) {
return new CharArray(ia, valueSize);
}
else {
return new WrappedIntArray(ia, valueSize);
}
}
/**
* Returns a new {@code IntArray} instance that has the same
* sequence of integers as the specified list of non-negative integers.
* @param il the list of non-negative integers to be copied
* @param valueSize the exclusive end of the range of list values
* @return a new {@code IntArray} instance that has
* the same sequence of integers as the specified list
* @throws IllegalArgumentException if {@code valueSize < 1}
* @throws IllegalArgumentException if
* {@code (il.get(j) < 0 || il.get(j)> valueSize)} for any index {@code j}
* satisfying {@code (j >= 0 && j < il.size())}
* @throws NullPointerException if {@code il == null}
*/
static IntArray packedCreate(IntList il, int valueSize) {
if (valueSize < 1) {
throw new IllegalArgumentException(String.valueOf(valueSize));
}
if (valueSize<=16) {
return new PackedIntArray(il, valueSize);
}
else if (valueSize<=256) {
return new UnsignedByteArray(il, valueSize);
}
else if (valueSize<=65536) {
return new CharArray(il, valueSize);
}
else {
return new WrappedIntArray(il, valueSize);
}
}
/**
* Returns a new {@code IntArray} instance that has the same
* sequence of integers as the specified array of non-negative integers.
* Each integer of the returned object is stored in 1, 2, or 4 bytes.
* @param ia the array of non-negative integers to be copied
* @param valueSize the exclusive end of the range of
* array values
* @return a new {@code IntArray} instance that has
* the same sequence of integers as the specified array
* @throws IllegalArgumentException if {@code valueSize < 1}
* @throws IllegalArgumentException if
* {@code (ia[j] < 0 || ia[j] > valueSize)} for any index {@code j}
* satisfying {@code (j >= 0 && j < ia.length)}
* @throws NullPointerException if {@code ia == null}
*/
static IntArray create(int[] ia, int valueSize) {
if (valueSize < 1) {
throw new IllegalArgumentException(String.valueOf(valueSize));
}
if (valueSize<=256) {
return new UnsignedByteArray(ia, valueSize);
}
else if (valueSize<=65536) {
return new CharArray(ia, valueSize);
}
else {
return new WrappedIntArray(ia, valueSize);
}
}
/**
* Returns a new {@code IntArray} instance that has the same
* sequence of integers as the specified list of non-negative integers.
* Each integer of the returned object is stored in 1, 2, or 4 bytes.
* @param il the list of non-negative integers to be copied
* @param valueSize the exclusive end of the range of list values
* @return a new {@code IntArray} instance that has
* the same sequence of integers as the specified list
* @throws IllegalArgumentException if {@code valueSize < 1}
* @throws IllegalArgumentException if
* {@code (il.get(j) < 0 || il.get(j)> valueSize)} for any index {@code j}
* satisfying {@code (j >= 0 && j < il.size())}
* @throws NullPointerException if {@code il == null}
*/
static IntArray create(IntList il, int valueSize) {
if (valueSize < 1) {
throw new IllegalArgumentException(String.valueOf(valueSize));
}
if (valueSize<=256) {
return new UnsignedByteArray(il, valueSize);
}
else if (valueSize<=65536) {
return new CharArray(il, valueSize);
}
else {
return new WrappedIntArray(il, valueSize);
}
}
}
|