Changeset 3975
 Timestamp:
 Oct 17, 2008, 5:12:09 PM (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

toimperative/branches/java_expr_without_holes/Expr.java
r2889 r3975 18 18 */ 19 19 20 public final class Expr implements Comparable<Expr>, Iterable<Comparable >, RandomAccess {20 public final class Expr implements Comparable<Expr>, Iterable<Comparable<?>>, RandomAccess { 21 21 public static final Expr empty = new Expr(new Comparable[0], 0, 0); 22 22 … … 30 30 public static int both_copy = 0; 31 31 32 private static Comparable [] terms_pool = new Comparable[128];32 private static Comparable<?>[] terms_pool = new Comparable[8]; 33 33 private static int pool_index = 0; 34 34 35 private final Comparable [] terms;35 private final Comparable<?>[] terms; 36 36 private final int start; 37 37 private final int length; 38 38 39 39 /** 40 * Constructs expression from a subarray of an array of Comparable objects.40 * Constructs expression from a subarray of an array of Comparable<?> objects. 41 41 * This is private constructor because it doesn't copy the array and doesn't 42 42 * check that there aren't nulls in the array. 43 43 */ 44 private Expr (Comparable [] ts, int st, int len) {44 private Expr (Comparable<?>[] ts, int st, int len) { 45 45 assert ts != null : "ts="+ts; 46 46 assert 0 <= st && 0 <= len && st <= ts.length  len : "ts.length="+ts.length+", st="+st+", len="+len; … … 51 51 52 52 /** 53 * Constructs empty expression.54 */55 public Expr () {56 terms = empty.terms;57 start = 0;58 length = 0;59 }60 61 /**62 * Constructor from Comparable object63 */64 public Expr (Comparable obj) {65 assert obj != null : "obj="+obj;66 if (pool_index >= terms_pool.length) {67 terms_pool = new Comparable[8];68 pool_index = 0;69 }70 terms = terms_pool;71 start = pool_index;72 length = 1;73 terms_pool[pool_index++] = obj;74 }75 76 /**77 53 * Tests if this expression is empty. 78 54 */ … … 94 70 * position of the term 95 71 */ 96 public Comparable at (int i) {72 public Comparable<?> at (int i) { 97 73 assert 0 <= i && i < length : "length="+length+", i="+i; 98 74 return terms[start + i]; … … 158 134 assert expr != null : "expr="+expr; 159 135 assert 0 <= j && j <= expr.length  length : "length="+length+", expr.length="+expr.length+", j="+j; 160 Comparable [] terms1 = expr.terms;136 Comparable<?>[] terms1 = expr.terms; 161 137 int k1 = expr.start + j; 162 138 int tslen = terms.length; … … 206 182 } else if (length == expr.length) 207 183 compare_len = 0; 208 Comparable [] terms1 = expr.terms;184 Comparable<?>[] terms1 = expr.terms; 209 185 int tslen1 = terms1.length; 210 186 int k1 = expr.start; … … 212 188 int k = start; 213 189 for ( ; min_len > 0; min_len, k++, k1++) { 214 Comparable term = terms[k];215 Comparable term1 = terms1[k1];216 int res = RefalRuntime.c ompare(term.getClass(), term1.getClass());190 Comparable<?> term = terms[k]; 191 Comparable<?> term1 = terms1[k1]; 192 int res = RefalRuntime.classCompare(term.getClass(), term1.getClass()); 217 193 if (res == 0) 218 res = term.compareTo(term1);194 res = ((Comparable<Comparable<?>>) term).compareTo(term1); // May be CheckCastException 219 195 if (res != 0) 220 196 return res; … … 241 217 return expr1; 242 218 } 243 Comparable [] ts1 = expr1.terms;244 Comparable [] ts2 = expr2.terms;219 Comparable<?>[] ts1 = expr1.terms; 220 Comparable<?>[] ts2 = expr2.terms; 245 221 if (ts1 == ts2 && expr1.start + len1 == expr2.start) { 246 222 no_copy++; … … 248 224 } 249 225 both_copy++; 250 Comparable [] terms = new Comparable[len];226 Comparable<?>[] terms = new Comparable[len]; 251 227 System.arraycopy(expr1.terms, expr1.start, terms, 0, len1); 252 228 System.arraycopy(expr2.terms, expr2.start, terms, len1, len2); … … 257 233 * Concatenates two terms. 258 234 */ 259 public static Expr concat (Comparable obj1, Comparableobj2) {235 public static Expr concat (Comparable<?> obj1, Comparable<?> obj2) { 260 236 assert obj1 != null && obj2 != null : "obj1="+obj1+", obj2="+obj2; 261 237 if (pool_index >= terms_pool.length  1) { … … 271 247 * Concatenates expression and term. 272 248 */ 273 public static Expr concat (Expr expr, Comparable obj) {249 public static Expr concat (Expr expr, Comparable<?> obj) { 274 250 assert expr != null && obj != null : "expr="+expr+", obj="+obj; 275 251 if (expr.length == 0) 276 return new Expr(obj); 252 return create(obj); 253 int len = expr.length + 1; 254 if (expr.start + expr.length < expr.terms.length && expr.terms[expr.start + expr.length] == obj) { 255 no_copy++; 256 return new Expr(expr.terms, expr.start, len); 257 } 277 258 both_copy++; 278 int len = expr.length + 1; 279 Comparable[] terms = new Comparable[len]; 259 Comparable<?>[] terms = new Comparable[len]; 280 260 System.arraycopy(expr.terms, expr.start, terms, 0, expr.length); 281 261 terms[expr.length] = obj; … … 286 266 * Concatenates term and expression. 287 267 */ 288 public static Expr concat (Comparable obj, Expr expr) {268 public static Expr concat (Comparable<?> obj, Expr expr) { 289 269 assert obj != null && expr != null : "obj="+obj+", expr="+expr; 290 270 if (expr.length == 0) 291 return new Expr(obj); 271 return create(obj); 272 int len = expr.length + 1; 273 if (expr.start > 0 && expr.terms[expr.start  1] == obj) { 274 no_copy++; 275 return new Expr(expr.terms, expr.start  1, len); 276 } 292 277 both_copy++; 293 int len = expr.length + 1; 294 Comparable[] terms = new Comparable[len]; 278 Comparable<?>[] terms = new Comparable[len]; 295 279 terms[0] = obj; 296 280 System.arraycopy(expr.terms, expr.start, terms, 1, expr.length); … … 299 283 300 284 /** 285 * Creates expression with one term. 286 */ 287 public static Expr create (Comparable<?> term) { 288 assert term != null : "term=" + term; 289 if (pool_index >= terms_pool.length) { 290 terms_pool = new Comparable[8]; 291 pool_index = 0; 292 } 293 terms_pool[pool_index] = term; 294 return new Expr(terms_pool, pool_index++, 1); 295 } 296 297 /** 301 298 * Creates expression from a sequence of chars. 302 299 */ … … 304 301 assert seq != null : "seq="+seq; 305 302 int len = seq.length(); 306 Comparable [] terms = new Comparable[len];303 Comparable<?>[] terms = new Comparable[len]; 307 304 for (int i = 0; i < len; i++) 308 terms[i] = new Character(seq.charAt(i));305 terms[i] = seq.charAt(i); 309 306 return new Expr(terms, 0, len); 310 307 } 311 308 312 309 /** 313 * Creates expression from an array of Comparable objects. 314 * Array shouldn't contain nulls. 315 */ 316 public static Expr fromArray (Comparable[] arr) { 317 assert noNulls(arr) : "arr="+arr; 318 int len = arr.length; 319 Comparable[] terms = new Comparable[len]; 320 System.arraycopy(arr, 0, terms, 0, len); 321 return new Expr(terms, 0, len); 322 } 323 324 /** 325 * Auxiliary method that checks there aren't nulls in a given part of array. 326 * For use in assertions when creating expressions from arrays. 327 */ 328 private static boolean noNulls (Comparable[] arr) { 329 if (arr == null) 330 return false; 331 for (Comparable obj : arr) 332 if (obj == null) 333 return false; 334 return true; 310 * Returns the contents of this expr. 311 */ 312 public Object[] toArray () { 313 Comparable<?>[] arr = new Comparable[length]; 314 System.arraycopy(terms, start, arr, 0, length); 315 return arr; 335 316 } 336 317 337 318 public String toString () { 338 return new String(toStringBuffer()); 339 } 340 341 public StringBuffer toStringBuffer () { 342 StringBuffer str = new StringBuffer(length); 343 for (Comparable term : this) 344 if (term instanceof Expr) 345 str.append('(').append(((Expr) term).toStringBuffer()).append(')'); 346 else 347 str.append(term.toString()); 348 return str; 349 } 350 351 public String formattedRepresentation () { 352 if (length == 0) 353 return ""; 354 StringBuffer str = new StringBuffer(2 * length); 319 StringBuffer strBuf = new StringBuffer(); 320 toString(strBuf); 321 return strBuf.toString(); 322 } 323 324 private void toString (StringBuffer strBuf) { 355 325 boolean chars = false; 356 for (Comparable term : this) { 326 boolean isNotFirst = false; 327 for (Comparable<?> term : this) { 357 328 if (term instanceof Character) { 358 329 if (!chars) { 330 if (isNotFirst) 331 strBuf.append(' '); 332 strBuf.append('\''); 359 333 chars = true; 360 str.append(' ');361 str.append('\'');362 334 } 363 335 switch (((Character) term).charValue()) { 364 case '\t': str .append("\\t"); continue;365 case '\r': str .append("\\r"); continue;366 case '\n': str .append("\\n"); continue;367 case '\\': str .append("\\\\"); continue;368 case '\'': str .append("\\\'"); continue;369 case '\"': str .append("\\\""); continue;370 default: str .append(term); continue;336 case '\t': strBuf.append("\\t"); continue; 337 case '\r': strBuf.append("\\r"); continue; 338 case '\n': strBuf.append("\\n"); continue; 339 case '\\': strBuf.append("\\\\"); continue; 340 case '\'': strBuf.append("\\\'"); continue; 341 case '\"': strBuf.append("\\\""); continue; 342 default: strBuf.append(term); continue; 371 343 } 372 344 } 373 345 if (chars) { 374 346 chars = false; 375 str .append('\'');347 strBuf.append('\''); 376 348 } 377 str.append(' '); 378 if (term instanceof Expr) 379 str.append('(').append(((Expr) term).formattedRepresentation()).append(')'); 380 else if (term instanceof String) { 349 if (isNotFirst) 350 strBuf.append(' '); 351 if (term instanceof Expr) { 352 strBuf.append('('); 353 ((Expr) term).toString(strBuf); 354 strBuf.append(')'); 355 } else if (term instanceof String) { 381 356 String w = (String) term; 382 357 if (isIdentifier(w)) 383 str .append(w);358 strBuf.append(w); 384 359 else 385 str .append('"').append(w).append('"');360 strBuf.append('"').append(w).append('"'); 386 361 } else 387 str.append(term); 362 strBuf.append(term.toString()); 363 isNotFirst = true; 388 364 } 389 365 if (chars) 390 str.append('\''); 391 return str.substring(1); 366 strBuf.append('\''); 367 } 368 369 public String toChars () { 370 StringBuffer strBuf = new StringBuffer(); 371 toChars(strBuf); 372 return strBuf.toString(); 373 } 374 375 private void toChars (StringBuffer strBuf) { 376 for (Comparable<?> term : this) 377 if (term instanceof Expr) { 378 strBuf.append('('); 379 ((Expr) term).toChars(strBuf); 380 strBuf.append(')'); 381 } else if (term instanceof Reference && ((Reference) term).object instanceof org.refal.plus.objects.String) 382 ((org.refal.plus.objects.String) ((Reference) term).object).toExpr().toChars(strBuf); 383 else 384 strBuf.append(term.toString()); 392 385 } 393 386 … … 400 393 for (int i = 1; i < str.length(); i++) { 401 394 c = str.charAt(i); 402 if (!Character.isLetterOrDigit(c) && c != '_' )395 if (!Character.isLetterOrDigit(c) && c != '_' && c != '.') 403 396 return false; 404 397 } … … 448 441 } 449 442 450 public class Iterator implements ListIterator<Comparable > {443 public class Iterator implements ListIterator<Comparable<?>> { 451 444 int idx; 452 445 … … 463 456 } 464 457 465 public Comparable next () {458 public Comparable<?> next () { 466 459 if (idx < 0  length <= idx) 467 460 throw new NoSuchElementException(); … … 473 466 } 474 467 475 public Comparable previous () {468 public Comparable<?> previous () { 476 469 idx; 477 470 if (idx < 0  length <= idx) … … 484 477 } 485 478 486 public void add (Comparable obj) {479 public void add (Comparable<?> obj) { 487 480 throw new UnsupportedOperationException(); 488 481 } … … 492 485 } 493 486 494 public void set (Comparable obj) {487 public void set (Comparable<?> obj) { 495 488 throw new UnsupportedOperationException(); 496 489 } … … 506 499 507 500 public interface Concatenator { 508 public Concatenator toRight (Comparable obj);501 public Concatenator toRight (Comparable<?> obj); 509 502 510 503 public Concatenator toRight (Expr expr); … … 518 511 519 512 private static final class ParenthesesMarker { 520 public final Comparable object;521 522 public ParenthesesMarker (Comparableobj) {513 public final Comparable<?> object; 514 515 public ParenthesesMarker (Comparable<?> obj) { 523 516 object = obj; 524 517 } … … 529 522 } 530 523 531 public ListConcatenator toRight (Comparable obj) {524 public ListConcatenator toRight (Comparable<?> obj) { 532 525 list.add(new ParenthesesMarker(obj)); 533 526 length++; … … 536 529 537 530 public ListConcatenator toRight (Expr expr) { 531 if (expr.length == 0) 532 return this; 538 533 list.add(expr); 539 534 length += expr.length; … … 544 539 if (length == 0) 545 540 return Expr.empty; 546 Comparable [] terms = new Comparable[length];541 Comparable<?>[] terms = new Comparable[length]; 547 542 int i = 0; 548 543 for (Object obj : list) … … 558 553 559 554 private static final class InplaceConcatenator implements Concatenator { 560 private Comparable [] terms;555 private Comparable<?>[] terms; 561 556 private int length; 562 557 … … 578 573 } 579 574 580 public InplaceConcatenator toRight (Comparable obj) {575 public InplaceConcatenator toRight (Comparable<?> obj) { 581 576 assert obj != null; 582 577 if (terms.length <= length) { 583 Comparable [] ts = new Comparable[length * 2];578 Comparable<?>[] ts = new Comparable[length * 2]; 584 579 System.arraycopy(terms, 0, ts, 0, length); 585 580 terms = ts; … … 591 586 public InplaceConcatenator toRight (Expr expr) { 592 587 assert expr != null; 588 if (expr.length == 0) 589 return this; 593 590 int newLength = length + expr.length; 594 591 if (terms.length < newLength) { 595 Comparable [] ts = new Comparable[newLength * 2];592 Comparable<?>[] ts = new Comparable[newLength * 2]; 596 593 System.arraycopy(terms, 0, ts, 0, length); 597 594 terms = ts; … … 610 607 } 611 608 } 612 613 /**614 * Unused methods615 */616 {617 ///**618 // * Creates zerolength expression with given initial capacity.619 // */620 //public Expr (int initialCapacity) {621 // assert 0 <= initialCapacity : "initialCapacity="+initialCapacity;622 // terms = new Comparable[initialCapacity * 2 + 4];623 // start = 0;624 // length = 0;625 //}626 627 ///**628 // * Constructs subexpression  remove it.629 // *630 // * @param expr631 // * source expression632 // * @param i633 // * index where subexpression starts634 // * @param len635 // * subexpression length636 // */637 //public Expr (Expr expr, int i, int len) {638 // assert expr != null;639 // assert 0 <= i && 0 <= len && i <= expr.length  len : expr.length + ", " + i + ", " + len;640 // terms = expr.terms;641 // start = (expr.start + i) % terms.length;642 // length = len;643 //}644 645 ///**646 // * Concatenates all expressions from the given array.647 // */648 //public static Expr concat (Expr[] arr) {649 // assert arr != null;650 // int length = 0;651 // for (Expr expr : arr) {652 // assert expr != null;653 // length += expr.length;654 // }655 // if (length == 0)656 // return empty;657 // Expr.Concatenator ec = concatenator.reinit(length);658 // for (Expr expr : arr)659 // ec.toRight(expr);660 // return ec.yield();661 //}662 663 ///**664 // * Creates expression from Comparable object.665 // */666 //public static Expr fromComparable (Comparable obj) {667 // assert obj != null;668 // return new Expr(obj);669 //}670 671 ///**672 // * Creates expression from Object.673 // */674 //public static Expr fromObject (Object obj) {675 // assert obj != null;676 // Expr e = new Expr(1, 1);677 // e.terms[e.start] = new Reference(obj);678 // return e;679 //}680 681 ///**682 //* Creates expression from an array of Objects.683 //*/684 //public static Expr fromObjectArray (Object[] arr) {685 // return fromObjectArray(arr, 0, arr.length);686 //}687 688 ///**689 // * Creates expression from a subarray of an array of Comparable objects.690 // * There shouldn't be nulls in array between st and st+len.691 // */692 //public static Expr fromComparableArray (Comparable[] arr, int st, int len) {693 // assert noNulls(arr, st, len);694 // Comparable[] terms = new Comparable[len * 2 + 4];695 // System.arraycopy(arr, st, terms, 0, len);696 // return new Expr(terms, 0, len);697 //}698 699 ///**700 //* Creates expression from a subarray of an array of Objects.701 //*/702 //public static Expr fromObjectArray (Object[] arr, int i, int len) {703 // Expr e = new Expr(len, len);704 // for (int k = 0, j = e.start; k < len; k++)705 // e.terms[j++] = new Reference(arr[i++]);706 // return e;707 //}708 709 ///**710 //* Returns the contents of this expr.711 //*/712 //public Object[] toArray() {713 // Object[] arr = new Object[length];714 // System.arraycopy(terms, start, arr, 0, length);715 // return arr;716 //}717 718 ///**719 // * Writes the contents of this expr to the given array.720 // */721 //public void toArray (Object[] arr, int where, int what, int len) {722 // assert length  len >= what;723 // System.arraycopy(terms, start + what, arr, where, len);724 //}725 }726 609 }
Note: See TracChangeset
for help on using the changeset viewer.