Changeset 2022
 Timestamp:
 Jul 16, 2006, 12:21:40 AM (14 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

toimperative/trunk/java/org/refal/plus/Expr.java
r2020 r2022 70 70 } 71 71 72 public boolean isEmpty () { 73 return length == 0; 74 } 75 76 /** 77 * The length of expression. 78 */ 79 public int getLen () { 80 return length; 81 } 82 72 83 /** 73 84 * Element at position `i`. … … 82 93 83 94 /** 84 * The length of expression. 85 */ 86 public int getLen () { 87 return length; 95 * Checks whether term is a symbol. 96 * 97 * @param i  98 * the position 99 */ 100 public boolean symbolAt (int i) { 101 assert 0 <= i && i < length; 102 return !(terms[start + i] instanceof Expr); 88 103 } 89 104 … … 303 318 304 319 /** 305 * Creates expression from theObject.320 * Creates expression from an Object. 306 321 */ 307 322 public Expr (Object obj) { … … 314 329 315 330 /** 316 * Creates expression from the array of Objects. 331 * Creates expression from a sequence of chars. 332 */ 333 public static Expr fromSequence (CharSequence seq) { 334 Expr e = new Expr(seq.length()); 335 for (int i = 0, j = e.start; i < e.length; i++, j++) 336 e.terms[j] = new Character(seq.charAt(i)); 337 return e; 338 } 339 340 /** 341 * Creates expression from a subarray of an array of Objects. 342 * This is private constructor because it doesn't copy the array and 343 * doesn't check that there aren't nulls in the array. 344 * For constructing expression from an array use factory methods 345 * fromArray() and (with caution!) fromArrayUnsafe(). 346 * Also this constructor is used by SplitIterator. 347 */ 348 private Expr (Object[] arr, int i, int len) { 349 length = len; 350 terms = arr; 351 start = i; 352 } 353 354 /** 355 * Creates expression from an array of Objects. 356 * Use with caution!!! 317 357 * The returned expression is backed by the array, so the array shouldn't 318 358 * be changed after invocation of this method. 319 359 * Array shouldn't contain nulls. 320 360 */ 361 public static Expr fromArrayUnsafe (Object[] arr) { 362 assert noNulls(arr, 0, arr.length); 363 return new Expr(arr, 0, arr.length); 364 } 365 366 /** 367 * Creates expression from an array of Objects. 368 * Array shouldn't contain nulls. 369 */ 370 public static Expr fromArray (Object[] arr) { 371 assert noNulls(arr, 0, arr.length); 372 Expr e = new Expr(arr.length); 373 System.arraycopy(arr, 0, e.terms, e.start, arr.length); 374 return e; 375 } 376 377 /** 378 * This constructor is needed for making it impossible to erroneously call 379 * Expr (Object obj) constructor with an array as parameter. 380 * It is just a synonim for fromArray (Object[] arr). 381 */ 321 382 public Expr (Object[] arr) { 383 this(arr.length); 322 384 assert noNulls(arr, 0, arr.length); 323 length = arr.length; 324 terms = arr; 325 start = 0; 385 System.arraycopy(arr, 0, terms, start, length); 326 386 } 327 387 328 388 /** 329 389 * Creates expression from a subarray of an array of Objects. 390 * Use with caution!!! 330 391 * The returned expression is backed by the array, so the array shouldn't 331 392 * be changed after invocation of this method (at least its part from `i` … … 333 394 * There shouldn't be nulls in array between i1 and i+len inclusive. 334 395 */ 335 public Expr(Object[] arr, int i, int len) {396 public static Expr fromArrayUnsafe (Object[] arr, int i, int len) { 336 397 assert i >= 0; 337 398 assert len >= 0; 338 399 assert arr.length  len >= i; 339 400 assert noNulls(arr, i  1, len + 1); 340 length = len; 341 terms = arr; 342 start = i; 401 return new Expr(arr, i, len); 402 } 403 404 /** 405 * Creates expression from a subarray of an array of Objects. 406 * There shouldn't be nulls in array between i1 and i+len inclusive. 407 */ 408 public static Expr fromArray (Object[] arr, int i, int len) { 409 assert noNulls(arr, i  1, len + 1); 410 Expr e = new Expr(len); 411 System.arraycopy(arr, i, e.terms, e.start, len); 412 return e; 343 413 } 344 414 … … 348 418 * For use in assertions when creating expressions from arrays. 349 419 */ 350 private boolean noNulls (Object[] arr, int i, int len) {420 private static boolean noNulls (Object[] arr, int i, int len) { 351 421 if (i < 0) 352 422 i = 0; … … 374 444 assert length  len >= what; 375 445 System.arraycopy(terms, start + what, arr, where, len); 376 }377 378 /**379 * Creates expression from a sequence of chars.380 */381 public static Expr fromSequence (CharSequence seq) {382 Expr e = new Expr(seq.length());383 for (int i = 0, j = e.start; i < e.length; i++, j++)384 e.terms[j] = new Character(seq.charAt(i));385 return e;386 }387 388 /**389 * Checks whether term is a symbol.390 *391 * @param i 392 * the position393 */394 public boolean symbolAt (int i) {395 assert 0 <= i && i < length;396 return !(terms[start + i] instanceof Expr);397 }398 399 public boolean isEmpty () {400 return length == 0;401 446 } 402 447 … … 462 507 } 463 508 464 /**465 * Private constructor for creating expression with given parameters.466 * Arguments should be correct, no checks are performed!467 * Used by SplitIterator.468 */469 private Expr (int s, int len, Object[] arr) {470 start = s;471 length = len;472 terms = arr;473 }474 475 509 public SplitIterator leftSplit (int l) { 476 510 return new SplitIterator(this, l); … … 492 526 493 527 public SplitIterator next () { 494 left = new Expr(left. start, left.length + 1, left.terms);495 right = new Expr(right. start + 1, right.length  1, right.terms);528 left = new Expr(left.terms, left.start, left.length + 1); 529 right = new Expr(right.terms, right.start + 1, right.length  1); 496 530 return this; 497 531 } 498 532 499 533 public SplitIterator prev () { 500 left = new Expr(left. start, left.length  1, left.terms);501 right = new Expr(right. start  1, right.length + 1, right.terms);534 left = new Expr(left.terms, left.start, left.length  1); 535 right = new Expr(right.terms, right.start  1, right.length + 1); 502 536 return this; 503 537 }
Note: See TracChangeset
for help on using the changeset viewer.