Debian Codegraph @

net/sf/saxon/z/MonotonicIntSet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
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 * Set of int values. This implementation requires that new entries are added in monotonically
17 * increasing order; any attempt to add a value out of sequence, or to remove a value, results
18 * is an UnsupportedOperationException
19 */
20
21 public class MonotonicIntSet implements IntSet {
22
23 /**
24 * The array of integers, which will always be sorted
25 */
26
27 private int[] contents;
28
29 /**
30 * The portion of the array currently in use
31 */
32
33 private int used = 0;
34
35 /**
36 * Create an empty set
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 * Add an integer to the set
79 *
80 * @param value the integer to be added (which must be greater than or equal to the
81 * largest integer currently in the set)
82 * @return true if the integer was added, false if it was already present
83 * @throws UnsupportedOperationException if the set already contains an integer larger
84 * than the supplied value
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 * Get an iterator over the values
105 *
106 * @return an iterator over the values, which will be delivered in sorted order
107 */
108
109 public IntIterator iterator() {
110 return new IntArraySet.IntArrayIterator(contents, used);
111 }
112
113 /**
114 * Form a new set that is the union of this set with another set.
115 *
116 * @param other the other set
117 * @return the union of the two sets
118 */
119
120 @Override
121 public IntSet union(IntSet other) {
122 // Look for special cases: one set empty, or both sets equal
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 // Form the union by a merge of the two sorted arrays
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 * Factory method to construct a set from an array of integers
169 *
170 * @param in the array of integers, which must be in ascending order
171 * @param size the number of elements in the array that are significant
172 * @return the constructed set
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 * Test whether this set has exactly the same members as another set
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 * Construct a hash key that supports the equals() test
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