| 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 |
public class IntRangeSet implements IntSet { |
| 22 |
|
| 23 |
|
| 24 |
private int[] startPoints; |
| 25 |
|
| 26 |
|
| 27 |
private int[] endPoints; |
| 28 |
|
| 29 |
|
| 30 |
private int used = 0; |
| 31 |
|
| 32 |
|
| 33 |
private int hashCode = -1; |
| 34 |
|
| 35 |
|
| 36 |
private int size = 0; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public IntRangeSet() { |
| 42 |
startPoints = new int[4]; |
| 43 |
endPoints = new int[4]; |
| 44 |
used = 0; |
| 45 |
size = 0; |
| 46 |
hashCode = -1; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public IntRangeSet(IntRangeSet input) { |
| 56 |
startPoints = new int[input.used]; |
| 57 |
endPoints = new int[input.used]; |
| 58 |
used = input.used; |
| 59 |
System.arraycopy(input.startPoints, 0, startPoints, 0, used); |
| 60 |
System.arraycopy(input.endPoints, 0, endPoints, 0, used); |
| 61 |
hashCode = input.hashCode; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public IntRangeSet(int[] startPoints, int[] endPoints) { |
| 76 |
if (startPoints.length != endPoints.length) { |
| 77 |
throw new IllegalArgumentException("Array lengths differ"); |
| 78 |
} |
| 79 |
this.startPoints = startPoints; |
| 80 |
this.endPoints = endPoints; |
| 81 |
used = startPoints.length; |
| 82 |
for (int i = 0; i < used; i++) { |
| 83 |
size += (endPoints[i] - startPoints[i] + 1); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public void clear() { |
| 88 |
startPoints = new int[4]; |
| 89 |
endPoints = new int[4]; |
| 90 |
used = 0; |
| 91 |
hashCode = -1; |
| 92 |
} |
| 93 |
|
| 94 |
public IntSet copy() { |
| 95 |
IntRangeSet s = new IntRangeSet(); |
| 96 |
s.startPoints = new int[startPoints.length]; |
| 97 |
System.arraycopy(startPoints, 0, s.startPoints, 0, startPoints.length); |
| 98 |
|
| 99 |
s.endPoints = new int[endPoints.length]; |
| 100 |
System.arraycopy(endPoints, 0, s.endPoints, 0, endPoints.length); |
| 101 |
|
| 102 |
s.used = used; |
| 103 |
s.size = size; |
| 104 |
return s; |
| 105 |
} |
| 106 |
|
| 107 |
public IntSet mutableCopy() { |
| 108 |
return copy(); |
| 109 |
} |
| 110 |
|
| 111 |
public int size() { |
| 112 |
return size; |
| 113 |
} |
| 114 |
|
| 115 |
public boolean isEmpty() { |
| 116 |
return size == 0; |
| 117 |
} |
| 118 |
|
| 119 |
public boolean contains(int value) { |
| 120 |
if (used == 0) { |
| 121 |
return false; |
| 122 |
} |
| 123 |
if (value > endPoints[used - 1]) { |
| 124 |
return false; |
| 125 |
} |
| 126 |
if (value < startPoints[0]) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
int i = 0; |
| 130 |
int j = used; |
| 131 |
do { |
| 132 |
int mid = i + (j - i) / 2; |
| 133 |
if (endPoints[mid] < value) { |
| 134 |
i = Math.max(mid, i + 1); |
| 135 |
} else if (startPoints[mid] > value) { |
| 136 |
j = Math.min(mid, j - 1); |
| 137 |
} else { |
| 138 |
return true; |
| 139 |
} |
| 140 |
} while (i != j); |
| 141 |
return false; |
| 142 |
} |
| 143 |
|
| 144 |
public boolean remove(int value) { |
| 145 |
throw new UnsupportedOperationException("remove"); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public boolean add(int value) { |
| 156 |
hashCode = -1; |
| 157 |
if (used == 0) { |
| 158 |
ensureCapacity(1); |
| 159 |
startPoints[used - 1] = value; |
| 160 |
endPoints[used - 1] = value; |
| 161 |
size++; |
| 162 |
return true; |
| 163 |
} |
| 164 |
if (value > endPoints[used - 1]) { |
| 165 |
if (value == endPoints[used - 1] + 1) { |
| 166 |
endPoints[used - 1]++; |
| 167 |
} else { |
| 168 |
ensureCapacity(used + 1); |
| 169 |
startPoints[used - 1] = value; |
| 170 |
endPoints[used - 1] = value; |
| 171 |
} |
| 172 |
size++; |
| 173 |
return true; |
| 174 |
} |
| 175 |
if (value < startPoints[0]) { |
| 176 |
if (value == startPoints[0] - 1) { |
| 177 |
startPoints[0]--; |
| 178 |
} else { |
| 179 |
ensureCapacity(used + 1); |
| 180 |
System.arraycopy(startPoints, 0, startPoints, 1, used - 1); |
| 181 |
System.arraycopy(endPoints, 0, endPoints, 1, used - 1); |
| 182 |
startPoints[0] = value; |
| 183 |
endPoints[0] = value; |
| 184 |
} |
| 185 |
size++; |
| 186 |
return true; |
| 187 |
} |
| 188 |
int i = 0; |
| 189 |
int j = used; |
| 190 |
do { |
| 191 |
int mid = i + (j - i) / 2; |
| 192 |
if (endPoints[mid] < value) { |
| 193 |
i = Math.max(mid, i + 1); |
| 194 |
} else if (startPoints[mid] > value) { |
| 195 |
j = Math.min(mid, j - 1); |
| 196 |
} else { |
| 197 |
return false; |
| 198 |
} |
| 199 |
} while (i != j); |
| 200 |
if (i > 0 && endPoints[i - 1] + 1 == value) { |
| 201 |
i--; |
| 202 |
} else if (i < used - 1 && startPoints[i + 1] - 1 == value) { |
| 203 |
i++; |
| 204 |
} |
| 205 |
if (endPoints[i] + 1 == value) { |
| 206 |
if (value == startPoints[i + 1] - 1) { |
| 207 |
|
| 208 |
endPoints[i] = endPoints[i + 1]; |
| 209 |
System.arraycopy(startPoints, i + 2, startPoints, i + 1, used - i - 2); |
| 210 |
System.arraycopy(endPoints, i + 2, endPoints, i + 1, used - i - 2); |
| 211 |
used--; |
| 212 |
} else { |
| 213 |
endPoints[i]++; |
| 214 |
} |
| 215 |
size++; |
| 216 |
return true; |
| 217 |
} else if (startPoints[i] - 1 == value) { |
| 218 |
if (value == endPoints[i - 1] + 1) { |
| 219 |
|
| 220 |
endPoints[i - 1] = endPoints[i]; |
| 221 |
System.arraycopy(startPoints, i + 1, startPoints, i, used - i - 1); |
| 222 |
System.arraycopy(endPoints, i + 1, endPoints, i, used - i - 1); |
| 223 |
used--; |
| 224 |
} else { |
| 225 |
startPoints[i]--; |
| 226 |
} |
| 227 |
size++; |
| 228 |
return true; |
| 229 |
} else { |
| 230 |
if (value > endPoints[i]) { |
| 231 |
i++; |
| 232 |
} |
| 233 |
ensureCapacity(used + 1); |
| 234 |
try { |
| 235 |
System.arraycopy(startPoints, i, startPoints, i + 1, used - i - 1); |
| 236 |
System.arraycopy(endPoints, i, endPoints, i + 1, used - i - 1); |
| 237 |
} catch (Exception err) { |
| 238 |
err.printStackTrace(); |
| 239 |
} |
| 240 |
startPoints[i] = value; |
| 241 |
endPoints[i] = value; |
| 242 |
size++; |
| 243 |
return true; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
private void ensureCapacity(int n) { |
| 248 |
if (startPoints.length < n) { |
| 249 |
int[] s = new int[startPoints.length * 2]; |
| 250 |
int[] e = new int[startPoints.length * 2]; |
| 251 |
System.arraycopy(startPoints, 0, s, 0, used); |
| 252 |
System.arraycopy(endPoints, 0, e, 0, used); |
| 253 |
startPoints = s; |
| 254 |
endPoints = e; |
| 255 |
} |
| 256 |
used = n; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public IntIterator iterator() { |
| 265 |
return new IntRangeSetIterator(); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
public String toString() { |
| 270 |
FastStringBuffer sb = new FastStringBuffer(used * 8); |
| 271 |
for (int i = 0; i < used; i++) { |
| 272 |
sb.append(startPoints[i] + "-" + endPoints[i] + ","); |
| 273 |
} |
| 274 |
return sb.toString(); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
public boolean equals(Object other) { |
| 283 |
if (other instanceof IntSet) { |
| 284 |
if (other instanceof IntRangeSet) { |
| 285 |
return used == ((IntRangeSet) other).used && |
| 286 |
Arrays.equals(startPoints, ((IntRangeSet) other).startPoints) && |
| 287 |
Arrays.equals(endPoints, ((IntRangeSet) other).endPoints); |
| 288 |
} else { |
| 289 |
return containsAll((IntSet) other); |
| 290 |
} |
| 291 |
} else { |
| 292 |
return false; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
public int hashCode() { |
| 301 |
|
| 302 |
if (hashCode == -1) { |
| 303 |
int h = 0x836a89f1; |
| 304 |
for (int i = 0; i < used; i++) { |
| 305 |
h ^= startPoints[i] + (endPoints[i] << 3); |
| 306 |
} |
| 307 |
hashCode = h; |
| 308 |
} |
| 309 |
return hashCode; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public void addRange(int low, int high) { |
| 322 |
if (low == high) { |
| 323 |
add(low); |
| 324 |
return; |
| 325 |
} |
| 326 |
hashCode = -1; |
| 327 |
if (used == 0) { |
| 328 |
ensureCapacity(1); |
| 329 |
startPoints[used - 1] = low; |
| 330 |
endPoints[used - 1] = high; |
| 331 |
size += (high - low + 1); |
| 332 |
} else if (low > endPoints[used - 1]) { |
| 333 |
if (low == endPoints[used - 1] + 1) { |
| 334 |
endPoints[used - 1] = high; |
| 335 |
} else { |
| 336 |
ensureCapacity(used + 1); |
| 337 |
startPoints[used - 1] = low; |
| 338 |
endPoints[used - 1] = high; |
| 339 |
} |
| 340 |
size += (high - low + 1); |
| 341 |
} else if (high < startPoints[0]) { |
| 342 |
ensureCapacity(used + 1); |
| 343 |
System.arraycopy(startPoints, 0, startPoints, 1, used - 1); |
| 344 |
System.arraycopy(endPoints, 0, endPoints, 1, used - 1); |
| 345 |
startPoints[0] = low; |
| 346 |
endPoints[0] = high; |
| 347 |
} else { |
| 348 |
for (int i = 1; i < used; i++) { |
| 349 |
if (startPoints[i] > high && endPoints[i - 1] < low) { |
| 350 |
ensureCapacity(used + 1); |
| 351 |
System.arraycopy(startPoints, i, startPoints, i + 1, used - i - 1); |
| 352 |
System.arraycopy(endPoints, i, endPoints, i + 1, used - i - 1); |
| 353 |
startPoints[i] = low; |
| 354 |
endPoints[i] = high; |
| 355 |
return; |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
for (int i = low; i <= high; i++) { |
| 360 |
add(i); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
public int[] getStartPoints() { |
| 370 |
return startPoints; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
public int[] getEndPoints() { |
| 378 |
return endPoints; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public int getNumberOfRanges() { |
| 386 |
return used; |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
private class IntRangeSetIterator implements IntIterator { |
| 394 |
|
| 395 |
private int i = 0; |
| 396 |
private int current = 0; |
| 397 |
|
| 398 |
public IntRangeSetIterator() { |
| 399 |
i = -1; |
| 400 |
current = Integer.MIN_VALUE; |
| 401 |
} |
| 402 |
|
| 403 |
public boolean hasNext() { |
| 404 |
if (i < 0) { |
| 405 |
return size > 0; |
| 406 |
} else { |
| 407 |
return current < endPoints[used - 1]; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
public int next() { |
| 412 |
if (i < 0) { |
| 413 |
i = 0; |
| 414 |
current = startPoints[0]; |
| 415 |
return current; |
| 416 |
} |
| 417 |
if (current == endPoints[i]) { |
| 418 |
current = startPoints[++i]; |
| 419 |
return current; |
| 420 |
} else { |
| 421 |
return ++current; |
| 422 |
} |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
} |
| 427 |
|