Changeset 2019
 Timestamp:
 Jul 13, 2006, 8:06:44 PM (14 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

toimperative/trunk/java/org/refal/plus/Expr.java
r2018 r2019 16 16 public static final Expr empty = new Expr(0); 17 17 18 private static final Object border = new Object ();19 20 18 private final Object[] terms; 21 19 … … 51 49 terms = new Object[len*2]; 52 50 start = len/2; 53 if (start > 0)54 terms[start  1] = border;55 if (start + length < terms.length)56 terms[start + length] = border;57 51 } 58 52 … … 235 229 } 236 230 int e1_end = e1.start + e1.length; 237 if (e1.terms.length  e1_end >= e2.length && 238 e1.terms[e1_end] == border) 231 if (e1.terms.length  e1_end >= e2.length && e1.terms[e1_end] == null) 239 232 { 240 233 terms = e1.terms; 241 234 start = e1.start; 242 if (start + length < terms.length)243 terms[start + length] = border;244 235 System.arraycopy(e2.terms, e2.start, terms, e1_end, e2.length); 245 236 right_copy++; 246 237 return; 247 238 } 248 if (e1.length <= e2.start && e2.terms[e2.start  1] == border) {239 if (e1.length <= e2.start && e2.terms[e2.start  1] == null) { 249 240 terms = e2.terms; 250 241 start = e2.start  e1.length; 251 if (start > 0)252 terms[start  1] = border;253 242 System.arraycopy(e1.terms, e1.start, terms, start, e1.length); 254 243 left_copy++; … … 257 246 terms = new Object[length*2]; 258 247 start = length/2; 259 terms[start  1] = border;260 terms[start + length] = border;261 248 System.arraycopy(e1.terms, e1.start, terms, start, e1.length); 262 249 System.arraycopy(e2.terms, e2.start, terms, start + e1.length, e2.length); … … 270 257 length = e1.length + 1; 271 258 int e1_end = e1.start + e1.length; 272 if (e1.terms.length  e1_end > 0 && e1.terms[e1_end] == border) {259 if (e1.terms.length  e1_end > 0 && e1.terms[e1_end] == null) { 273 260 start = e1.start; 274 261 terms = e1.terms; 275 262 terms[e1_end] = t2; 276 if (start + length < terms.length)277 terms[start + length] = border;278 263 right_copy++; 279 264 return; … … 281 266 terms = new Object[length*2]; 282 267 start = 0; 283 terms[start + length] = border;284 268 System.arraycopy(e1.terms, e1.start, terms, start, e1.length); 285 269 terms[start + e1.length] = t2; … … 322 306 */ 323 307 public Expr (Object obj) { 308 assert obj != null; 324 309 length = 1; 325 310 terms = new Object[2]; 326 311 terms[0] = obj; 327 terms[1] = border;328 312 start = 0; 329 313 } … … 333 317 * The returned expression is backed by the array, so the array shouldn't 334 318 * be changed after invocation of this method. 319 * Array shouldn't contain nulls. 335 320 */ 336 321 public Expr (Object[] arr) { 322 assert noNulls(arr, 0, arr.length); 337 323 length = arr.length; 338 324 terms = arr; … … 345 331 * be changed after invocation of this method (at least its part from `i` 346 332 * to `i + len` shouldn't be changed). 333 * There shouldn't be nulls in array between i1 and i+len inclusive. 347 334 */ 348 335 public Expr (Object[] arr, int i, int len) { … … 350 337 assert len >= 0; 351 338 assert arr.length  len >= i; 339 assert noNulls(arr, i  1, len + 1); 352 340 length = len; 353 341 terms = arr; 354 342 start = i; 343 } 344 345 /** 346 * Auxiliary method that checks there aren't nulls in a given part of 347 * array. 348 * For use in assertions when creating expressions from arrays. 349 */ 350 private boolean noNulls (Object[] arr, int i, int len) { 351 if (i < 0) 352 i = 0; 353 if (len > arr.length) 354 len = arr.length; 355 while (i < len) 356 if (arr[i++] == null) 357 return false; 358 return true; 355 359 } 356 360 … … 458 462 } 459 463 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 460 475 public SplitIterator leftSplit (int l) { 461 476 return new SplitIterator(this, l); … … 477 492 478 493 public SplitIterator next () { 479 left = new Expr(left.terms, left.start, left.length + 1); 480 right = 481 right.length > 0 ? new Expr(right, 1, right.length  1) : null; 494 left = new Expr(left.start, left.length + 1, left.terms); 495 right = new Expr(right.start + 1, right.length  1, right.terms); 482 496 return this; 483 497 } 484 498 485 499 public SplitIterator prev () { 486 left = left.length > 0 ? new Expr(left, 0, left.length  1) : null;487 right = new Expr(right. terms, right.start  1, right.length + 1);500 left = new Expr(left.start, left.length  1, left.terms); 501 right = new Expr(right.start  1, right.length + 1, right.terms); 488 502 return this; 489 503 } 490 504 491 505 public boolean isValid () { 492 return right != null && left != null;506 return right.length >= 0 && left.length >= 0; 493 507 } 494 508
Note: See TracChangeset
for help on using the changeset viewer.