1 5 package org.h2.util; 6 7 import org.h2.engine.Constants; 8 import org.h2.message.Message; 9 10 13 public class IntArray { 14 15 private int[] data; 16 private int size; 17 private int hash; 18 19 public IntArray() { 20 data = new int[10]; 21 } 22 23 public IntArray(int[] data) { 24 this.data = data; 25 size = data.length; 26 } 27 28 public static int[] clone(int[] array) { 29 if(array == null) { 30 return null; 31 } 32 int[] copy = new int[array.length]; 33 System.arraycopy(array, 0, copy, 0, array.length); 34 return copy; 35 } 36 37 public static boolean equals(int[] a, int[] b) { 38 if(a == null || b == null) { 39 return a == b; 40 } 41 if(a.length != b.length) { 42 return false; 43 } 44 for(int i=0; i<a.length; i++) { 45 if(a[i] != b[i]) { 46 return false; 47 } 48 } 49 return true; 50 } 51 52 public void add(int value) { 53 checkCapacity(); 54 data[size++] = value; 55 } 56 57 public int get(int i) { 58 if (Constants.CHECK && i >= size) { 59 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 60 } 61 return data[i]; 62 } 63 64 public int remove(int i) { 65 if (Constants.CHECK && i >= size) { 66 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 67 } 68 int value = data[i]; 69 System.arraycopy(data, i + 1, data, i, size - i - 1); 70 size--; 71 return value; 72 } 73 74 private void checkCapacity() { 75 if (size >= data.length) { 76 int[] d = new int[data.length * 2]; 77 System.arraycopy(data, 0, d, 0, data.length); 78 data = d; 79 } 80 } 81 82 public void add(int i, int value) { 83 if (Constants.CHECK && i > size) { 84 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 85 } 86 checkCapacity(); 87 if (i == size) { 88 add(value); 89 } else { 90 System.arraycopy(data, i, data, i + 1, size - i); 91 data[i] = value; 92 size++; 93 } 94 } 95 96 public void set(int i, int value) { 97 if (Constants.CHECK && i >= size) { 98 throw new ArrayIndexOutOfBoundsException ("i=" + i + " size=" + size); 99 } 100 data[i] = value; 101 } 102 103 public boolean equals(Object obj) { 104 if(!(obj instanceof IntArray)) { 105 return false; 106 } 107 IntArray other = (IntArray) obj; 108 if(hashCode() != other.hashCode() || size != other.size) { 109 return false; 110 } 111 for(int i=0; i<size; i++) { 112 if(data[i] != other.data[i]) { 113 return false; 114 } 115 } 116 return true; 117 } 118 119 public int hashCode() { 120 if (hash != 0) { 121 return hash; 122 } 123 int h = size + 1; 124 for(int i=0; i<size; i++) { 125 h = h * 31 + data[i]; 126 } 127 hash = h; 128 return h; 129 } 130 131 public int size() { 132 return size; 133 } 134 135 public void addValueSorted(int value) { 136 int l = 0, r = size; 137 while(l < r) { 138 int i = (l + r) >>> 1; 139 int d = data[i]; 140 if(d == value) { 141 return; 142 } else if(d > value) { 143 r = i; 144 } else { 145 l = i + 1; 146 } 147 } 148 add(l, value); 149 } 150 151 167 public void removeValue(int value) { 168 for(int i=0; i<size; i++) { 169 if(data[i] == value) { 170 remove(i); 171 return; 172 } 173 } 174 throw Message.getInternalError(); 175 } 176 177 public int findNextValueIndex(int value) { 178 int l = 0, r = size; 179 while(l < r) { 180 int i = (l + r) >>> 1; 181 int d = data[i]; 182 if(d >= value) { 183 r = i; 184 } else { 185 l = i + 1; 186 } 187 } 188 return l; 189 190 } 197 198 public void sort() { 199 for (int i = 1, j; i < size(); i++) { 200 int t = get(i); 201 for (j = i - 1; j >= 0 && (get(j) > t); j--) { 202 set(j + 1, get(j)); 203 } 204 set(j + 1, t); 205 } 206 } 207 208 public void toArray(int[] array) { 209 System.arraycopy(data, 0, array, 0, size); 210 } 211 212 247 } 248 | Popular Tags |