2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.util;
20 import gov.nasa.jpf.JPFException;
24 * (more efficient?) alternative to Vector<Integer>
27 public final class IntVector implements Comparable<IntVector>, Cloneable {
28 public static final int defaultInitCap = 40;
31 /** <i>size</i> as in a java.util.Vector. */
34 /** the backing array. */
37 /** growth strategy. */
38 protected Growth growth;
41 public IntVector(Growth initGrowth, int initCap) {
43 data = new int[initCap];
47 public IntVector(int... init) {
48 this(Growth.defaultGrowth, init.length);
50 System.arraycopy(init, 0, data, 0, size);
53 public IntVector(Growth initGrowth) { this(initGrowth,defaultInitCap); }
55 public IntVector(int initCap) { this(Growth.defaultGrowth, initCap); }
57 public IntVector() { this(Growth.defaultGrowth,defaultInitCap); }
60 public IntVector clone() {
62 IntVector v = (IntVector)super.clone();
63 v.data = data.clone();
66 } catch (CloneNotSupportedException cnsx){
67 throw new JPFException("IntVector clone failed");
71 public void add(int x) {
75 } catch (ArrayIndexOutOfBoundsException aobx){
76 ensureCapacity(size+1);
82 public boolean addIfAbsent (int x) {
83 for (int i=0; i<size; i++){
92 public void add (long x) {
93 if (size+2 > data.length) {
94 ensureCapacity(size+2);
96 data[size] = (int)(x >> 32);
102 public void add(int x1, int x2) {
103 if (size+2 > data.length) {
104 ensureCapacity(size+2);
112 public void add(int x1, int x2, int x3) {
113 if (size+3 > data.length) {
114 ensureCapacity(size+3);
124 public void addZeros (int length) {
125 int newSize = size + length;
126 if (newSize > data.length) {
127 ensureCapacity(size + length);
129 for (int i = size; i < newSize; i++) {
135 public void append(int[] a) {
136 int newSize = size + a.length;
137 if (newSize > data.length) {
138 ensureCapacity(newSize);
140 System.arraycopy(a, 0, data, size, a.length);
144 public void append(int[] x, int pos, int len) {
145 int newSize = size + len;
146 if (newSize > data.length) {
147 ensureCapacity(newSize);
149 System.arraycopy(x, pos, data, size, len);
153 public void append(boolean[] a){
154 int newSize = size + a.length;
155 if (newSize > data.length) {
156 ensureCapacity(newSize);
158 for (int i = size; i < newSize; i++) {
159 data[i] = a[i] ? 1 : 0;
164 public void appendPacked(boolean[] a){
165 int len = (a.length+31) >> 5; // new data length, 32 booleans per word
166 int n = a.length >> 5; // number of full data words
168 int rem = a.length % 32;
170 int newSize = size + len;
171 if (newSize > data.length) {
172 ensureCapacity(newSize);
178 while (i<len1){ // store full data words
180 for (int j=1; j<32; j++){
189 if (rem > 0) { // store partial word
195 while (i < a.length) {
208 public void appendPacked(byte[] a){
209 int len = (a.length+3)/4; // new data length, 4 bytes per word
210 int n = a.length >> 2; // number of full data words
213 int newSize = size + len;
214 if (newSize > data.length) {
215 ensureCapacity(newSize);
222 x = a[j++] & 0xff; x <<= 8;
223 x |= a[j++] & 0xff; x <<= 8;
224 x |= a[j++] & 0xff; x <<= 8;
229 switch (a.length % 4){
233 data[k] = (a[j] << 24);
236 x = a[j++] & 0xff; x <<= 8;
237 x |= a[j] & 0xff; x <<= 16;
241 x = a[j++] & 0xff; x <<= 8;
242 x |= a[j++] & 0xff; x <<= 8;
243 x |= a[j] & 0xff; x <<= 8;
252 public void appendPacked(char[] a){
253 int len = (a.length+1)/2; // new data length, 2 chars per word
254 int n = a.length >> 1; // number of full data words
257 int newSize = size + len;
258 if (newSize > data.length) {
259 ensureCapacity(newSize);
265 int x = a[j++] & 0xffff; x <<= 16;
266 x |= a[j++] & 0xffff;
270 if (a.length % 2 > 0){
271 data[k] = (a[j] & 0xffff) << 16;
277 public void appendPacked(short[] a){
278 int len = (a.length+1)/2; // new data length, 2 chars per word
279 int n = a.length >> 1; // number of full data words
282 int newSize = size + len;
283 if (newSize > data.length) {
284 ensureCapacity(newSize);
290 int x = a[j++] & 0xffff; x <<= 16;
291 x |= a[j++] & 0xffff;
295 if (a.length % 2 > 0){
296 data[k] = (a[j] & 0xffff) << 16;
302 public void appendRawBits(float[] a){
303 int newSize = size + a.length;
304 if (newSize > data.length) {
305 ensureCapacity(newSize);
308 for (int i = 0; i < a.length; i++) {
309 data[k++] = Float.floatToRawIntBits(a[i]);
315 public void appendBits(long[] a){
316 int newSize = size + a.length * 2;
317 if (newSize > data.length) {
318 ensureCapacity(newSize);
322 for (int i = 0; i<a.length; i++){
324 data[k++] = (int) (l >>> 32);
325 data[k++] = (int) (l & 0xffffffff);
331 public void appendRawBits(double[] a){
332 int newSize = size + a.length * 2;
333 if (newSize > data.length) {
334 ensureCapacity(newSize);
338 for (int i = 0; i<a.length; i++){
339 long l = Double.doubleToRawLongBits(a[i]);
340 data[k++] = (int) (l >>> 32);
341 data[k++] = (int) (l & 0xffffffff);
348 public void append(IntVector x) {
349 if (x == null) return;
350 if (size + x.size > data.length) {
351 ensureCapacity(size + x.size);
353 System.arraycopy(x.data, 0, data, size, x.size);
357 public boolean removeFirst (int x){
358 for (int i=0; i<size; i++){
360 System.arraycopy(data,i+1, data,i, size-i);
369 public int get(int idx) {
377 public void set(int idx, int x) {
382 public int getFirstIndexOfValue(int x) {
383 for (int i=0; i<size; i++){
391 public boolean contains (int x) {
392 for (int i=0; i<size; i++){
401 public int[] toArray (int[] dst) {
402 System.arraycopy(data,0,dst,0,size);
406 public int dumpTo (int[] dst, int pos) {
407 System.arraycopy(data,0,dst,pos,size);
411 public void squeeze() {
412 while (size > 0 && data[size - 1] == 0) size--;
415 public void setSize(int sz) {
427 public void clear() { setSize(0); }
429 public int size() { return size; }
431 public int[] toArray() {
432 int[] out = new int[size];
433 System.arraycopy(data, 0, out, 0, size);
437 public void ensureSize(int sz) {
444 public void ensureCapacity(int desiredCap) {
445 if (data.length < desiredCap) {
446 int[] newData = new int[growth.grow(data.length, desiredCap)];
447 System.arraycopy(data, 0, newData, 0, size);
452 public static void copy(IntVector src, int srcPos, IntVector dst, int dstPos, int len) {
453 if (len == 0) return;
454 src.ensureCapacity(srcPos + len);
455 dst.ensureSize(dstPos+len);
456 System.arraycopy(src.data, srcPos, dst.data, dstPos, len);
459 public static void copy(int[] src, int srcPos, IntVector dst, int dstPos, int len) {
460 if (len == 0) return;
461 dst.ensureSize(dstPos+len);
462 System.arraycopy(src, srcPos, dst.data, dstPos, len);
465 public static void copy(IntVector src, int srcPos, int[] dst, int dstPos, int len) {
466 if (len == 0) return;
467 src.ensureCapacity(srcPos + len);
468 System.arraycopy(src.data, srcPos, dst, dstPos, len);
471 /** dictionary/lexicographic ordering */
473 public int compareTo (IntVector that) {
474 if (that == null) return this.size; // consider null and 0-length the same
477 int smaller = Math.min(this.size, that.size);
478 for (int i = 0; i < smaller; i++) {
479 comp = this.data[i] - that.data[i];
480 if (comp != 0) return comp;
482 // same up to shorter length => compare sizes
483 return this.size - that.size;