Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / LongVector.java
1 /*
2  * Copyright (C) 2014, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
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
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
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.
17  */
18 package gov.nasa.jpf.util;
19
20 /**
21  * (more efficient?) alternative to Vector<Integer>
22  * @author pcd
23  */
24 public final class LongVector {
25   public static final int defaultInitCap = 40;
26
27   
28   /** <i>size</i> as in a java.util.Vector. */
29   protected int size;
30   
31   /** the backing array. */
32   protected long[] data;
33   
34   /** growth strategy. */
35   protected Growth growth;
36   
37   
38   public LongVector(Growth initGrowth, int initCap) {
39     growth = initGrowth;
40     data = new long[initCap];
41     size = 0;
42   }
43   
44   public LongVector(Growth initGrowth) { this(initGrowth,defaultInitCap); }
45   
46   public LongVector(int initCap) { this(Growth.defaultGrowth, initCap); }
47   
48   public LongVector() { this(Growth.defaultGrowth,defaultInitCap); }
49   
50   
51   public void add(long x) {
52     if (size+1 > data.length) {
53       ensureCapacity(size+1);
54     }
55     data[size] = x;
56     size++;
57   }
58   
59   public long get(int idx) {
60     if (idx >= size) {
61       return 0;
62     } else {
63       return data[idx];
64     }
65   }
66   
67   public void set(int idx, long x) {
68     ensureSize(idx + 1);
69     data[idx] = x;
70   }
71   
72
73   public void squeeze() {
74     while (size > 0 && data[size - 1] == 0) size--;
75   }
76   
77   public void setSize(int sz) {
78     if (sz > size) {
79       ensureCapacity(sz);
80       size = sz;
81     } else {
82       while (size > sz) {
83         size--;
84         data[size] = 0;
85       }
86     }
87   }
88
89   public void clear() { setSize(0); }
90   
91   public int size() { return size; }
92   
93   public void ensureSize(int sz) {
94     if (size < sz) {
95       ensureCapacity(sz);
96       size = sz;
97     }
98   }
99   
100   public void ensureCapacity(int desiredCap) {
101     if (data.length < desiredCap) {
102       long[] newData = new long[growth.grow(data.length, desiredCap)];
103       System.arraycopy(data, 0, newData, 0, size);
104       data = newData;
105     }
106   }
107   
108   public static void copy(LongVector src, int srcPos, LongVector dst, int dstPos, int len) {
109     if (len == 0) return;
110     src.ensureCapacity(srcPos + len);
111     dst.ensureSize(dstPos+len);
112     System.arraycopy(src.data, srcPos, dst.data, dstPos, len);
113   }
114 }