1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package jgroup.util;
20
21 import java.io.Externalizable;
22 import java.io.IOException;
23 import java.io.ObjectInput;
24 import java.io.ObjectOutput;
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public final class IntList
58 implements Externalizable
59 {
60
61
62
63
64
65 private static final long serialVersionUID = -6522311553289690117L;
66
67
68 private int start;
69
70
71 private int stop;
72
73
74 private IntList next;
75
76
77
78
79
80
81
82
83
84 public IntList()
85 {
86 }
87
88
89
90
91
92
93
94 private IntList(int key, IntList next)
95 {
96 this.start = key;
97 this.stop = key;
98 this.next = next;
99 }
100
101
102
103
104
105
106
107 private IntList(int start, int stop)
108 {
109 this.start = start;
110 this.stop = stop;
111 this.next = null;
112 }
113
114
115
116
117
118
119
120
121
122 public int getFirst()
123 {
124 if (next != null)
125 return next.start;
126 else
127 return -1;
128 }
129
130
131
132
133 public int getLast()
134 {
135 IntList scan = this.next;
136 if (scan == null)
137 return -1;
138 while (scan.next != null)
139 scan = scan.next;
140 return scan.stop;
141 }
142
143
144
145
146 public boolean isEmpty()
147 {
148 if (next == null)
149 return true;
150 else
151 return false;
152 }
153
154
155
156
157
158
159
160 public int getMaxConsecutive()
161 {
162 if (next != null)
163 return next.stop;
164 else
165 return -1;
166 }
167
168
169
170
171
172 public boolean contains(int key)
173 {
174
175 IntList scan = this.next;
176 while (scan != null && key >= scan.start) {
177 if (key <= scan.stop)
178 return true;
179 scan = scan.next;
180 }
181 return false;
182 }
183
184
185
186
187 public void insert(int key)
188 {
189
190 IntList scan = this;
191
192
193 while (scan.next != null && key > scan.next.stop + 1)
194 scan = scan.next;
195 if (scan.next == null || key < scan.next.start - 1) {
196
197 scan.next = new IntList(key, scan.next);
198 } else if (key == scan.next.stop + 1) {
199
200 IntList item = scan.next.next;
201 if (item != null && item.start == key + 1) {
202
203 scan.next.stop = item.stop;
204 scan.next.next = item.next;
205 } else {
206
207 scan.next.stop = key;
208 }
209 } else if (key > scan.next.stop + 1) {
210
211 scan.next.next = new IntList(key, scan.next.next);
212 } else if (key == scan.next.start - 1) {
213
214 scan.next.start = key;
215 }
216 }
217
218
219
220
221 public void merge(IntList addend)
222 {
223 if (addend == null)
224 return;
225
226 IntList scan = this;
227 addend = addend.next;
228 while (addend != null) {
229 IntList snext = scan.next;
230 if (snext == null) {
231
232 scan.next = new IntList(addend.start, addend.stop);
233 addend = addend.next;
234 scan = snext;
235 }
236 else if (addend.stop < snext.start - 1) {
237
238
239 IntList temp = snext;
240 scan.next = new IntList(addend.start, addend.stop);
241 snext.next = temp;
242 scan = snext;
243 addend = addend.next;
244 }
245 else if (snext.stop+1 < addend.start) {
246
247 scan = snext;
248 }
249 else {
250
251
252 if (snext.start > addend.start)
253 snext.start = addend.start;
254 if (snext.stop < addend.stop) {
255 snext.stop = addend.stop;
256 IntList temp = snext.next;
257 if (temp != null && temp.start <= addend.stop + 1) {
258
259 snext.next = temp.next;
260 snext.stop = temp.stop;
261 }
262 }
263 addend = addend.next;
264 }
265 }
266 }
267
268
269
270
271
272
273
274
275
276
277 public IntList split(int key)
278 {
279 IntList ret = copy();
280 IntList toRemove = new IntList();
281 toRemove.next = new IntList(key + 1, getLast());
282 ret.remove(toRemove);
283 toRemove.next = new IntList(getFirst(), key);
284 remove(toRemove);
285 return ret;
286 }
287
288
289
290
291
292
293 public void remove(int start, int stop)
294 {
295 IntList toRemove = new IntList();
296 if (stop == 0)
297 stop = getLast();
298 if (getLast() != stop)
299 stop--;
300 toRemove.next = new IntList(start, stop);
301 remove(toRemove);
302 }
303
304
305
306
307
308
309 public int removeFirst()
310 {
311 if (next != null) {
312 int ret = next.start;
313 remove(ret);
314 return ret;
315 } else
316 return -1;
317 }
318
319
320
321
322
323 public void remove(int key)
324 {
325 IntList toRemove = new IntList();
326 toRemove.insert(key);
327 remove(toRemove);
328 }
329
330
331
332
333
334 public void remove(IntList removed)
335 {
336 if (removed == null)
337 return;
338
339 IntList scan = this;
340 removed = removed.next;
341 while (removed != null && scan.next != null) {
342 if (removed.stop < scan.next.start) {
343
344
345
346
347 removed = removed.next;
348 }
349 else if (scan.next.stop < removed.start) {
350
351
352
353
354 scan = scan.next;
355 }
356 else if (removed.start > scan.next.start && removed.stop < scan.next.stop) {
357
358
359
360
361 IntList temp = scan.next.next;
362 scan.next.next = new IntList(removed.stop+1, scan.next.stop);
363 scan.next.stop = removed.start-1;
364 scan.next.next.next = temp;
365 }
366 else {
367
368
369
370
371
372 if (removed.stop >= scan.next.stop)
373 scan.next.stop = removed.start-1;
374 if (removed.start <= scan.next.start)
375 scan.next.start = removed.stop+1;
376 if (scan.next.stop < scan.next.start)
377 scan.next = scan.next.next;
378 }
379 }
380 }
381
382
383
384
385
386 public int[] toIntArray()
387 {
388 IntList scan;
389
390 int size = 0;
391 for (scan = this.next; scan != null; scan = scan.next)
392 size += (scan.stop - scan.start + 1);
393 int[] ret = new int[size];
394
395 int i = 0;
396 for (scan = this.next; scan != null; scan = scan.next)
397 for (int j = scan.start; j <= scan.stop; i++, j++)
398 ret[i] = j;
399 return ret;
400 }
401
402
403
404
405 public void clean()
406 {
407 this.next = null;
408 }
409
410
411
412
413 public IntList copy()
414 {
415 IntList ret = new IntList();
416 IntList scan1 = ret;
417 IntList scan2 = this.next;
418 while (scan2 != null) {
419 scan1.next = new IntList(scan2.start, scan2.stop);
420 scan1 = scan1.next;
421 scan2 = scan2.next;
422 }
423 return ret;
424 }
425
426
427
428
429
430
431
432
433
434
435
436
437
438 public void writeExternal(ObjectOutput out)
439 throws IOException
440 {
441 IntList scan = this.next;
442 while (scan != null) {
443 out.writeInt(scan.start);
444 out.writeInt(scan.stop);
445 scan = scan.next;
446 }
447 out.writeInt(-1);
448 }
449
450
451
452
453
454
455
456 public void readExternal(ObjectInput in)
457 throws IOException, ClassNotFoundException
458 {
459
460 IntList scan = this;
461
462
463 int v1;
464 while ((v1 = in.readInt()) != -1) {
465 scan.next = new IntList(v1, in.readInt());
466 scan = scan.next;
467 }
468 }
469
470
471
472
473
474
475
476
477 public String toString()
478 {
479 StringBuilder buf = new StringBuilder();
480 buf.append("[IntList: {");
481 IntList scan = next;
482 while (scan != null) {
483 buf.append(scan.start);
484 buf.append("-");
485 buf.append(scan.stop);
486 scan = scan.next;
487 if (scan != null)
488 buf.append(", ");
489 }
490 buf.append("}]");
491 return buf.toString();
492 }
493
494 }