Changeset 2019


Ignore:
Timestamp:
Jul 13, 2006, 8:06:44 PM (14 years ago)
Author:
orlov
Message:
  • Expr.border field is removed. Holes in Expr are denoted by nulls.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • to-imperative/trunk/java/org/refal/plus/Expr.java

    r2018 r2019  
    1616    public static final Expr empty = new Expr(0);
    1717
    18     private static final Object border = new Object ();
    19 
    2018    private final Object[] terms;
    2119
     
    5149        terms = new Object[len*2];
    5250        start = len/2;
    53         if (start > 0)
    54             terms[start - 1] = border;
    55         if (start + length < terms.length)
    56             terms[start + length] = border;
    5751    }
    5852
     
    235229        }
    236230        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)
    239232        {
    240233            terms = e1.terms;
    241234            start = e1.start;
    242             if (start + length < terms.length)
    243                 terms[start + length] = border;
    244235            System.arraycopy(e2.terms, e2.start, terms, e1_end, e2.length);
    245236            right_copy++;
    246237            return;
    247238        }
    248         if (e1.length <= e2.start && e2.terms[e2.start - 1] == border) {
     239        if (e1.length <= e2.start && e2.terms[e2.start - 1] == null) {
    249240            terms = e2.terms;
    250241            start = e2.start - e1.length;
    251             if (start > 0)
    252                 terms[start - 1] = border;
    253242            System.arraycopy(e1.terms, e1.start, terms, start, e1.length);
    254243            left_copy++;
     
    257246        terms = new Object[length*2];
    258247        start = length/2;
    259         terms[start - 1] = border;
    260         terms[start + length] = border;
    261248        System.arraycopy(e1.terms, e1.start, terms, start, e1.length);
    262249        System.arraycopy(e2.terms, e2.start, terms, start + e1.length, e2.length);
     
    270257        length = e1.length + 1;
    271258        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) {
    273260            start = e1.start;
    274261            terms = e1.terms;
    275262            terms[e1_end] = t2;
    276             if (start + length < terms.length)
    277                 terms[start + length] = border;
    278263            right_copy++;
    279264            return;
     
    281266        terms = new Object[length*2];
    282267        start = 0;
    283         terms[start + length] = border;
    284268        System.arraycopy(e1.terms, e1.start, terms, start, e1.length);
    285269        terms[start + e1.length] = t2;
     
    322306     */
    323307    public Expr (Object obj) {
     308        assert obj != null;
    324309        length = 1;
    325310        terms = new Object[2];
    326311        terms[0] = obj;
    327         terms[1] = border;
    328312        start = 0;
    329313    }
     
    333317     * The returned expression is backed by the array, so the array shouldn't
    334318     * be changed after invocation of this method.
     319     * Array shouldn't contain nulls.
    335320     */
    336321    public Expr (Object[] arr) {
     322        assert noNulls(arr, 0, arr.length);
    337323        length = arr.length;
    338324        terms = arr;
     
    345331     * be changed after invocation of this method (at least its part from `i`
    346332     * to `i + len` shouldn't be changed).
     333     * There shouldn't be nulls in array between i-1 and i+len inclusive.
    347334     */
    348335    public Expr (Object[] arr, int i, int len) {
     
    350337        assert len >= 0;
    351338        assert arr.length - len >= i;
     339        assert noNulls(arr, i - 1, len + 1);
    352340        length = len;
    353341        terms = arr;
    354342        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;
    355359    }
    356360
     
    458462    }
    459463
     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
    460475    public SplitIterator leftSplit (int l) {
    461476        return new SplitIterator(this, l);
     
    477492
    478493        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);
    482496            return this;
    483497        }
    484498
    485499        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);
    488502            return this;
    489503        }
    490504
    491505        public boolean isValid () {
    492             return right != null && left != null;
     506            return right.length >= 0 && left.length >= 0;
    493507        }
    494508
Note: See TracChangeset for help on using the changeset viewer.