| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.z; |
| 9 |
|
| 10 |
|
| 11 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 12 |
|
| 13 |
import java.util.Arrays; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public class IntArraySet implements IntSet { |
| 28 |
|
| 29 |
public static final int[] EMPTY_INT_ARRAY = new int[0]; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
private int[] contents; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
private int hashCode = -1; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public IntArraySet() { |
| 47 |
contents = EMPTY_INT_ARRAY; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public IntArraySet(IntHashSet input) { |
| 57 |
|
| 58 |
contents = input.getValues(); |
| 59 |
|
| 60 |
Arrays.sort(contents); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public IntArraySet(IntArraySet input) { |
| 70 |
contents = new int[input.contents.length]; |
| 71 |
System.arraycopy(input.contents, 0, contents, 0, contents.length); |
| 72 |
} |
| 73 |
|
| 74 |
public IntSet copy() { |
| 75 |
IntArraySet i2 = new IntArraySet(); |
| 76 |
i2.contents = new int[contents.length]; |
| 77 |
System.arraycopy(contents, 0, i2.contents, 0, contents.length); |
| 78 |
|
| 79 |
return i2; |
| 80 |
} |
| 81 |
|
| 82 |
public IntSet mutableCopy() { |
| 83 |
return copy(); |
| 84 |
} |
| 85 |
|
| 86 |
public void clear() { |
| 87 |
contents = EMPTY_INT_ARRAY; |
| 88 |
hashCode = -1; |
| 89 |
} |
| 90 |
|
| 91 |
public int size() { |
| 92 |
return contents.length; |
| 93 |
} |
| 94 |
|
| 95 |
public boolean isEmpty() { |
| 96 |
return contents.length == 0; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public int[] getValues() { |
| 106 |
return contents; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
public boolean contains(int value) { |
| 111 |
return Arrays.binarySearch(contents, value) >= 0; |
| 112 |
} |
| 113 |
|
| 114 |
public boolean remove(int value) { |
| 115 |
hashCode = -1; |
| 116 |
int pos = Arrays.binarySearch(contents, value); |
| 117 |
if (pos < 0) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
int[] newArray = new int[contents.length - 1]; |
| 121 |
if (pos > 0) { |
| 122 |
|
| 123 |
System.arraycopy(contents, 0, newArray, 0, pos); |
| 124 |
} |
| 125 |
if (pos < newArray.length) { |
| 126 |
|
| 127 |
System.arraycopy(contents, pos + 1, newArray, pos, contents.length - pos); |
| 128 |
} |
| 129 |
contents = newArray; |
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public boolean add(int value) { |
| 141 |
hashCode = -1; |
| 142 |
if (contents.length == 0) { |
| 143 |
contents = new int[]{value}; |
| 144 |
return true; |
| 145 |
} |
| 146 |
int pos = Arrays.binarySearch(contents, value); |
| 147 |
if (pos >= 0) { |
| 148 |
return false; |
| 149 |
} |
| 150 |
pos = -pos - 1; |
| 151 |
int[] newArray = new int[contents.length + 1]; |
| 152 |
if (pos > 0) { |
| 153 |
|
| 154 |
System.arraycopy(contents, 0, newArray, 0, pos); |
| 155 |
} |
| 156 |
newArray[pos] = value; |
| 157 |
if (pos < contents.length) { |
| 158 |
|
| 159 |
System.arraycopy(contents, pos, newArray, pos + 1, newArray.length - pos); |
| 160 |
} |
| 161 |
contents = newArray; |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public int getFirst() { |
| 173 |
return contents[0]; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public IntIterator iterator() { |
| 183 |
return new IntArrayIterator(contents, contents.length); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public IntSet union(IntSet other) { |
| 194 |
|
| 195 |
if (size() == 0) { |
| 196 |
return other.copy(); |
| 197 |
} else if (other.isEmpty()) { |
| 198 |
return copy(); |
| 199 |
} else if (other == IntUniversalSet.getInstance()) { |
| 200 |
return other; |
| 201 |
} else if (other instanceof IntComplementSet) { |
| 202 |
return other.union(this); |
| 203 |
} |
| 204 |
if (equals(other)) { |
| 205 |
return copy(); |
| 206 |
} |
| 207 |
if (other instanceof IntArraySet) { |
| 208 |
|
| 209 |
int[] merged = new int[size() + other.size()]; |
| 210 |
int[] a = contents; |
| 211 |
int[] b = ((IntArraySet) other).contents; |
| 212 |
int m = a.length, n = b.length; |
| 213 |
int o = 0, i = 0, j = 0; |
| 214 |
while (true) { |
| 215 |
if (a[i] < b[j]) { |
| 216 |
merged[o++] = a[i++]; |
| 217 |
} else if (b[j] < a[i]) { |
| 218 |
merged[o++] = b[j++]; |
| 219 |
} else { |
| 220 |
merged[o++] = a[i++]; |
| 221 |
j++; |
| 222 |
} |
| 223 |
if (i == m) { |
| 224 |
System.arraycopy(b, j, merged, o, n - j); |
| 225 |
o += (n - j); |
| 226 |
return make(merged, o); |
| 227 |
} else if (j == n) { |
| 228 |
System.arraycopy(a, i, merged, o, m - i); |
| 229 |
o += (m - i); |
| 230 |
return make(merged, o); |
| 231 |
} |
| 232 |
} |
| 233 |
} else { |
| 234 |
return IntSet.super.union(other); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public static IntArraySet make(int[] in, int size) { |
| 247 |
int[] out; |
| 248 |
if (in.length == size) { |
| 249 |
out = in; |
| 250 |
} else { |
| 251 |
out = new int[size]; |
| 252 |
System.arraycopy(in, 0, out, 0, size); |
| 253 |
} |
| 254 |
return new IntArraySet(out); |
| 255 |
} |
| 256 |
|
| 257 |
private IntArraySet(int[] content) { |
| 258 |
contents = content; |
| 259 |
} |
| 260 |
|
| 261 |
public String toString() { |
| 262 |
FastStringBuffer sb = new FastStringBuffer(contents.length * 4); |
| 263 |
for (int i = 0; i < contents.length; i++) { |
| 264 |
if (i == contents.length - 1) { |
| 265 |
sb.append(contents[i] + ""); |
| 266 |
} else if (contents[i] + 1 != contents[i + 1]) { |
| 267 |
sb.append(contents[i] + ","); |
| 268 |
} else { |
| 269 |
int j = i + 1; |
| 270 |
while (contents[j] == contents[j - 1] + 1) { |
| 271 |
j++; |
| 272 |
if (j == contents.length) { |
| 273 |
break; |
| 274 |
} |
| 275 |
} |
| 276 |
sb.append(contents[i] + "-" + contents[j - 1] + ","); |
| 277 |
i = j - 1; |
| 278 |
} |
| 279 |
} |
| 280 |
return sb.toString(); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public boolean equals(Object other) { |
| 303 |
if (other instanceof IntArraySet) { |
| 304 |
IntArraySet s = (IntArraySet) other; |
| 305 |
return hashCode() == other.hashCode() && Arrays.equals(contents, s.contents); |
| 306 |
} else |
| 307 |
return other instanceof IntSet && |
| 308 |
contents.length == ((IntSet) other).size() && |
| 309 |
containsAll((IntSet) other); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public int hashCode() { |
| 317 |
|
| 318 |
if (hashCode == -1) { |
| 319 |
int h = 936247625; |
| 320 |
IntIterator it = iterator(); |
| 321 |
while (it.hasNext()) { |
| 322 |
h += it.next(); |
| 323 |
} |
| 324 |
hashCode = h; |
| 325 |
} |
| 326 |
return hashCode; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public static class IntArrayIterator implements IntIterator { |
| 334 |
|
| 335 |
private int[] contents; |
| 336 |
private int limit; |
| 337 |
private int i = 0; |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public IntArrayIterator(int[] contents, int limit) { |
| 348 |
i = 0; |
| 349 |
this.contents = contents; |
| 350 |
this.limit = limit; |
| 351 |
} |
| 352 |
|
| 353 |
public boolean hasNext() { |
| 354 |
return i < limit; |
| 355 |
} |
| 356 |
|
| 357 |
public int next() { |
| 358 |
return contents[i++]; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
} |
| 364 |
|