Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00041 package utils;
00042
00043 import java.io.*;
00044 import java.util.*;
00045
00046
00047 public class SortedIntList extends LinkedList {
00048
00049
00050 public Object clone() {
00051 return super.clone();
00052 }
00053
00054
00055
00056
00057 protected boolean isSorted() {
00058 int lastItem = Integer.MIN_VALUE;
00059
00060 Iterator it = iterator();
00061 while (it.hasNext()) {
00062 Integer newItem = (Integer)it.next();
00063 if (newItem.intValue() < lastItem) return false;
00064 lastItem = newItem.intValue();
00065 }
00066
00067 return true;
00068 }
00069
00070
00071
00072
00073 protected boolean uniqueElements() {
00074 Iterator it = iterator();
00075
00076 Integer lastItem;
00077
00078 if (it.hasNext()) {
00079 lastItem = (Integer)it.next();
00080 } else return true;
00081
00082 while (it.hasNext()) {
00083 Integer nextItem = (Integer)it.next();
00084
00085 if (nextItem.equals(lastItem)) return false;
00086 }
00087
00088 return true;
00089 }
00090
00091
00092 protected boolean invariant() {
00093 return (isSorted() && uniqueElements());
00094 }
00095
00100 public boolean addSorted(int item) {
00101
00102
00103 assert(invariant());
00104
00105
00106
00107 ListIterator it = listIterator(0);
00108 while (it.hasNext()) {
00109 Integer currentItem = (Integer)it.next();
00110 int current_n = currentItem.intValue();
00111
00112 if (item == current_n) return false;
00113 else if (item < current_n) {
00114 it.previous();
00115 it.add(new Integer(item));
00116 assert(invariant() && contains(new Integer(item)));
00117 return true;
00118 }
00119 }
00120
00121
00122
00123 it.add(new Integer(item));
00124
00125
00126 assert(invariant() && contains(new Integer(item)));
00127 return true;
00128 }
00129
00130 public int getFirstInt() {
00131 assert(size() > 0);
00132
00133 return ((Integer)getFirst()).intValue();
00134 }
00135
00136 public int getLastInt() {
00137 assert(size() > 0);
00138
00139 return ((Integer)getLast()).intValue();
00140 }
00141
00142 public boolean contains(int n) {
00143
00144 if (size() == 0) return false;
00145 else {
00146 int last = ((Integer)getLast()).intValue();
00147 if (n > last) return false;
00148 else {
00149 Iterator it = iterator();
00150 while(it.hasNext()) {
00151 int item = ((Integer)it.next()).intValue();
00152 if (n < item) return false;
00153 else if (n == item) return true;
00154 }
00155 assert(false);
00156 return false;
00157 }
00158 }
00159 }
00160
00161 public boolean contains(Object o) {
00162 assert(o instanceof Integer);
00163
00164 int n = ((Integer)o).intValue();
00165 return contains(n);
00166 }
00167
00171 public boolean equals(Object anObject) {
00172 if (anObject != null) {
00173 if (anObject instanceof SortedIntList) {
00174 SortedIntList other = (SortedIntList)anObject;
00175
00176 if (size() == other.size()) {
00177 Iterator it = iterator();
00178 Iterator ot = other.iterator();
00179
00180
00181 while(it.hasNext()) {
00182 Integer n = (Integer)it.next();
00183 Integer o = (Integer)ot.next();
00184 if (!n.equals(o)) {
00185
00186 assert(!this.subsetOf(other) || !other.subsetOf(this));
00187 return false;
00188 }
00189 }
00190
00191 assert(this.subsetOf(other) && other.subsetOf(this));
00192 return true;
00193
00194 } else return false;
00195
00196 } else return false;
00197 } else return false;
00198 }
00199
00200
00205 public SortedIntList subtract(SortedIntList other) {
00206
00207 SortedIntList result;
00208
00209 if (size() == 0) result = new SortedIntList();
00210 else if (other.size() == 0) result = (SortedIntList)this.clone();
00211 else {
00212
00213 result = new SortedIntList();
00214
00215 Iterator itThis = iterator();
00216 Iterator itOther = other.iterator();
00217
00218 int o = ((Integer)itOther.next()).intValue();
00219
00220
00221 while(itThis.hasNext()) {
00222
00223 Integer nobj = (Integer)itThis.next();
00224 int n = nobj.intValue();
00225
00226 if (n < o) result.add(nobj);
00227 else {
00228 if (itOther.hasNext()) o = ((Integer)itOther.next()).intValue();
00229 else {
00230 while(itThis.hasNext()) {
00231 nobj = (Integer)itThis.next();
00232 result.add(nobj);
00233 }
00234 }
00235 }
00236 }
00237
00238 }
00239
00240
00241
00242
00243 assert(result.invariant() && !result.intersects(other) && result.join(other).equals(this));
00244
00245 return result;
00246 }
00247
00251 public SortedIntList join(SortedIntList other) {
00252 SortedIntList result = (SortedIntList)this.clone();
00253
00254 Iterator itOther = other.iterator();
00255 while(itOther.hasNext()) {
00256 Integer obj = (Integer)itOther.next();
00257 result.addSorted(obj.intValue());
00258 }
00259
00260
00261 assert(result.invariant() && this.subsetOf(result) && other.subsetOf(result));
00262
00263 return result;
00264 }
00265
00270 public boolean subsetOf(SortedIntList other) {
00271
00272
00273 assert(invariant());
00274
00275 int sz = size();
00276
00277
00278 if (sz == 0) return true;
00279 else if (sz > other.size()) return false;
00280 else {
00281 int nlast = ((Integer)getLast()).intValue();
00282 int olast = ((Integer)other.getLast()).intValue();
00283 if (nlast > olast) return false;
00284 }
00285
00286 Iterator it = iterator();
00287 Iterator ot = other.iterator();
00288
00289
00290
00291 while (it.hasNext()) {
00292
00293 int ni = ((Integer)it.next()).intValue();
00294
00295 if (!ot.hasNext()) {
00296 assert(postcond_subsetOf(other, false));
00297 return false;
00298 }
00299 int no = ((Integer)ot.next()).intValue();
00300 while (no < ni) {
00301 if (!ot.hasNext()) {
00302 assert(postcond_subsetOf(other, false));
00303 return false;
00304 }
00305 no = ((Integer)ot.next()).intValue();
00306 }
00307
00308 if (no > ni) {
00309 assert(postcond_subsetOf(other, false));
00310 return false;
00311 }
00312 else assert(no == ni);
00313
00314 }
00315
00316 assert(postcond_subsetOf(other, true));
00317 return true;
00318
00319 }
00320
00321
00326 public boolean properSubsetOf(SortedIntList other) {
00327
00328
00329 assert(invariant());
00330
00331 int sz = size();
00332
00333
00334 if (sz == 0) return (other.size() > 0);
00335 else if (sz >= other.size()) return false;
00336 else {
00337 int nlast = ((Integer)getLast()).intValue();
00338 int olast = ((Integer)other.getLast()).intValue();
00339 if (nlast > olast) return false;
00340 }
00341
00342 Iterator it = iterator();
00343 Iterator ot = other.iterator();
00344
00345
00346
00347 while (it.hasNext()) {
00348
00349 int ni = ((Integer)it.next()).intValue();
00350
00351 if (!ot.hasNext()) {
00352 assert(postcond_properSubsetOf(other, false));
00353 return false;
00354 }
00355 int no = ((Integer)ot.next()).intValue();
00356 while (no < ni) {
00357 if (!ot.hasNext()) {
00358 assert(postcond_properSubsetOf(other, false));
00359 return false;
00360 }
00361 no = ((Integer)ot.next()).intValue();
00362 }
00363
00364 if (no > ni) {
00365 assert(postcond_properSubsetOf(other, false));
00366 return false;
00367 }
00368 else assert(no == ni);
00369
00370 }
00371
00372 assert(postcond_properSubsetOf(other, true));
00373 return true;
00374
00375 }
00376
00381 public boolean intersection(SortedIntList other, SortedIntList intersectionSet) {
00382
00383
00384 assert((intersectionSet != null) && (intersectionSet.size() == 0));
00385 assert(invariant());
00386
00387 if ((size() == 0) || (other.size() == 0)) return false;
00388
00389 int nfirst = ((Integer)getFirst()).intValue();
00390 int nlast = ((Integer)getLast()).intValue();
00391 int ofirst = ((Integer)other.getFirst()).intValue();
00392 int olast = ((Integer)other.getLast()).intValue();
00393
00394
00395
00396 if ((nlast < ofirst) || (olast < nfirst)) {
00397 assert(postcond_intersection(other, false, intersectionSet));
00398 return false;
00399 }
00400 else {
00401
00402 boolean result = false;
00403 Iterator itThis = iterator();
00404 Iterator itOther = other.iterator();
00405
00406 int n = ((Integer)itThis.next()).intValue();
00407 int o = ((Integer)itOther.next()).intValue();
00408 int index = 0;
00409
00410 while(true) {
00411
00412 if (n == o) {
00413
00414 result = true;
00415 intersectionSet.addSorted(n);
00416
00417 if (!itThis.hasNext() || !itOther.hasNext()) break;
00418 n = ((Integer)itThis.next()).intValue();
00419 ++index;
00420 o = ((Integer)itOther.next()).intValue();
00421
00422 } else if (n < o) {
00423 if (!itThis.hasNext()) break;
00424
00425 n = ((Integer)itThis.next()).intValue();
00426 ++index;
00427 }
00428 else {
00429 if (!itOther.hasNext()) break;
00430 o = ((Integer)itOther.next()).intValue();
00431 }
00432 }
00433
00434 assert(postcond_intersection(other, result, intersectionSet));
00435 return result;
00436
00437 }
00438
00439 }
00440
00441
00446 public boolean intersects(SortedIntList other) {
00447
00448 assert((size() > 0) || (other.size() > 0));
00449 assert(invariant());
00450
00451 if ((size() == 0) || (other.size() == 0)) return false;
00452
00453 int nfirst = ((Integer)getFirst()).intValue();
00454 int nlast = ((Integer)getLast()).intValue();
00455 int ofirst = ((Integer)other.getFirst()).intValue();
00456 int olast = ((Integer)other.getLast()).intValue();
00457
00458
00459
00460 if ((nlast < ofirst) || (olast < nfirst)) {
00461 assert(postcond_intersects(other, false));
00462 return false;
00463 }
00464 else {
00465
00466 Iterator itThis = iterator();
00467 Iterator itOther = other.iterator();
00468
00469 int n = ((Integer)itThis.next()).intValue();
00470 int o = ((Integer)itOther.next()).intValue();
00471
00472
00473
00474
00475 while(true) {
00476
00477 if (n == o) {
00478 assert(postcond_intersects(other, true));
00479 return true;
00480 } else if (n < o) {
00481 if (!itThis.hasNext()) {
00482 assert(postcond_intersects(other, false));
00483 return false;
00484 }
00485 n = ((Integer)itThis.next()).intValue();
00486 }
00487 else {
00488 if (!itOther.hasNext()) {
00489 assert(postcond_intersects(other, false));
00490 return false;
00491 }
00492 o = ((Integer)itOther.next()).intValue();
00493 }
00494 }
00495
00496 }
00497
00498 }
00499
00500
00501 protected boolean postcond_intersects(SortedIntList other, boolean result) {
00502 boolean proper_res = false;
00503
00504 Iterator it = iterator();
00505 while (it.hasNext()) {
00506 if (other.contains(it.next())) {
00507 proper_res = true;
00508 break;
00509 }
00510 }
00511
00512 return (result == proper_res);
00513 }
00514
00515
00516 protected boolean postcond_intersection(SortedIntList other, boolean result,
00517 SortedIntList intersectionSet) {
00518
00519 assert(intersectionSet.invariant());
00520
00521 boolean proper_res = false;
00522
00523 Iterator it = iterator();
00524 while (it.hasNext()) {
00525 if (other.contains(it.next())) {
00526 proper_res = true;
00527 break;
00528 }
00529 }
00530
00531 if (result != proper_res) return false;
00532 else {
00533 if (!result) return (intersectionSet.size() == 0);
00534 else {
00535 Iterator itIS = intersectionSet.iterator();
00536 while (itIS.hasNext()) {
00537 Integer no = (Integer)itIS.next();
00538 if (!contains(no) || !other.contains(no)) return false;
00539 }
00540 return true;
00541 }
00542 }
00543 }
00544
00545
00546 protected boolean postcond_subsetOf(SortedIntList other, boolean result) {
00547 boolean proper_res = true;
00548
00549 Iterator it = iterator();
00550 while (it.hasNext()) {
00551 if (!other.contains(it.next())) {
00552 proper_res = false;
00553 break;
00554 }
00555 }
00556
00557 return (result == proper_res);
00558 }
00559
00560
00561 protected boolean postcond_properSubsetOf(SortedIntList other, boolean result) {
00562 boolean trueRes = true;
00563
00564 Iterator it = iterator();
00565 while (it.hasNext()) {
00566 if (!other.contains(it.next())) {
00567 trueRes = false;
00568 break;
00569 }
00570 }
00571
00572 trueRes = (trueRes && (size() < other.size()));
00573
00574 return (result == trueRes);
00575 }
00576
00577 public String toString() {
00578 String result = "";
00579
00580 Iterator it = iterator();
00581 while(it.hasNext()) {
00582 int n = ((Integer)it.next()).intValue();
00583 result = result + n;
00584 if (it.hasNext()) result = result + ",";
00585 }
00586
00587 return result;
00588 }
00589
00590 }