| 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 MonotonicIntSet implements IntSet { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private int[] contents; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private int used = 0; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public MonotonicIntSet() { |
| 39 |
contents = new int[4]; |
| 40 |
used = 0; |
| 41 |
} |
| 42 |
|
| 43 |
public IntSet copy() { |
| 44 |
MonotonicIntSet i2 = new MonotonicIntSet(); |
| 45 |
i2.contents = Arrays.copyOf(contents, used); |
| 46 |
i2.used = used; |
| 47 |
return i2; |
| 48 |
} |
| 49 |
|
| 50 |
public IntSet mutableCopy() { |
| 51 |
return copy(); |
| 52 |
} |
| 53 |
|
| 54 |
public void clear() { |
| 55 |
if (contents.length > used + 20) { |
| 56 |
contents = new int[4]; |
| 57 |
} |
| 58 |
used = 0; |
| 59 |
} |
| 60 |
|
| 61 |
public int size() { |
| 62 |
return used; |
| 63 |
} |
| 64 |
|
| 65 |
public boolean isEmpty() { |
| 66 |
return used == 0; |
| 67 |
} |
| 68 |
|
| 69 |
public boolean contains(int value) { |
| 70 |
return Arrays.binarySearch(contents, 0, used, value) >= 0; |
| 71 |
} |
| 72 |
|
| 73 |
public boolean remove(int value) { |
| 74 |
throw new UnsupportedOperationException(); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public boolean add(int value) { |
| 88 |
if (used > 0) { |
| 89 |
int last = contents[used - 1]; |
| 90 |
if (value == last) { |
| 91 |
return false; |
| 92 |
} else if (value < last) { |
| 93 |
throw new UnsupportedOperationException("Values must be added in monotonic order"); |
| 94 |
} |
| 95 |
} |
| 96 |
if (used == contents.length) { |
| 97 |
contents = Arrays.copyOf(contents, used==0 ? 4 : used*2); |
| 98 |
} |
| 99 |
contents[used++] = value; |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public IntIterator iterator() { |
| 110 |
return new IntArraySet.IntArrayIterator(contents, used); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
@Override |
| 121 |
public IntSet union(IntSet other) { |
| 122 |
|
| 123 |
if (size() == 0) { |
| 124 |
return other.copy(); |
| 125 |
} else if (other.isEmpty()) { |
| 126 |
return copy(); |
| 127 |
} else if (other == IntUniversalSet.getInstance()) { |
| 128 |
return other; |
| 129 |
} else if (other instanceof IntComplementSet) { |
| 130 |
return other.union(this); |
| 131 |
} |
| 132 |
if (equals(other)) { |
| 133 |
return copy(); |
| 134 |
} |
| 135 |
if (other instanceof MonotonicIntSet) { |
| 136 |
|
| 137 |
int[] merged = new int[size() + other.size()]; |
| 138 |
int[] a = contents; |
| 139 |
int[] b = ((MonotonicIntSet) other).contents; |
| 140 |
int m = used; |
| 141 |
int n = ((MonotonicIntSet) other).used; |
| 142 |
int o = 0, i = 0, j = 0; |
| 143 |
while (true) { |
| 144 |
if (a[i] < b[j]) { |
| 145 |
merged[o++] = a[i++]; |
| 146 |
} else if (b[j] < a[i]) { |
| 147 |
merged[o++] = b[j++]; |
| 148 |
} else { |
| 149 |
merged[o++] = a[i++]; |
| 150 |
j++; |
| 151 |
} |
| 152 |
if (i == m) { |
| 153 |
System.arraycopy(b, j, merged, o, n - j); |
| 154 |
o += (n - j); |
| 155 |
return make(merged, o); |
| 156 |
} else if (j == n) { |
| 157 |
System.arraycopy(a, i, merged, o, m - i); |
| 158 |
o += (m - i); |
| 159 |
return make(merged, o); |
| 160 |
} |
| 161 |
} |
| 162 |
} else { |
| 163 |
return IntSet.super.union(other); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public static MonotonicIntSet make(int[] in, int size) { |
| 176 |
return new MonotonicIntSet(in, size); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
private MonotonicIntSet(int[] content, int used) { |
| 181 |
this.contents = content; |
| 182 |
this.used = used; |
| 183 |
} |
| 184 |
|
| 185 |
public String toString() { |
| 186 |
FastStringBuffer sb = new FastStringBuffer(used * 4); |
| 187 |
for (int i = 0; i < used; i++) { |
| 188 |
if (i == used - 1) { |
| 189 |
sb.append(contents[i] + ""); |
| 190 |
} else if (contents[i] + 1 != contents[i + 1]) { |
| 191 |
sb.append(contents[i] + ","); |
| 192 |
} else { |
| 193 |
int j = i + 1; |
| 194 |
while (contents[j] == contents[j - 1] + 1) { |
| 195 |
j++; |
| 196 |
if (j == used) { |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
sb.append(contents[i] + "-" + contents[j - 1] + ","); |
| 201 |
i = j; |
| 202 |
} |
| 203 |
} |
| 204 |
return sb.toString(); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public boolean equals(Object other) { |
| 212 |
if (other instanceof MonotonicIntSet) { |
| 213 |
MonotonicIntSet s = (MonotonicIntSet) other; |
| 214 |
if (used != s.used) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
for (int i=0; i<used; i++) { |
| 218 |
if (contents[i] != s.contents[i]) { |
| 219 |
return false; |
| 220 |
} |
| 221 |
} |
| 222 |
return true; |
| 223 |
} else |
| 224 |
return other instanceof IntSet && |
| 225 |
used == ((IntSet) other).size() && |
| 226 |
containsAll((IntSet) other); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public int hashCode() { |
| 234 |
int h = 936247625; |
| 235 |
IntIterator it = iterator(); |
| 236 |
while (it.hasNext()) { |
| 237 |
h += it.next(); |
| 238 |
} |
| 239 |
return h; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
} |
| 244 |
|