Changeset 2022


Ignore:
Timestamp:
Jul 16, 2006, 12:21:40 AM (14 years ago)
Author:
orlov
Message:
  • fromArray() factory methods.
File:
1 edited

Legend:

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

    r2020 r2022  
    7070    }
    7171
     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
    7283    /**
    7384     * Element at position `i`.
     
    8293
    8394    /**
    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);
    88103    }
    89104
     
    303318
    304319    /**
    305      * Creates expression from the Object.
     320     * Creates expression from an Object.
    306321     */
    307322    public Expr (Object obj) {
     
    314329
    315330    /**
    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!!!
    317357     * The returned expression is backed by the array, so the array shouldn't
    318358     * be changed after invocation of this method.
    319359     * Array shouldn't contain nulls.
    320360     */
     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     */
    321382    public Expr (Object[] arr) {
     383        this(arr.length);
    322384        assert noNulls(arr, 0, arr.length);
    323         length = arr.length;
    324         terms = arr;
    325         start = 0;
     385        System.arraycopy(arr, 0, terms, start, length);
    326386    }
    327387
    328388    /**
    329389     * Creates expression from a subarray of an array of Objects.
     390     * Use with caution!!!
    330391     * The returned expression is backed by the array, so the array shouldn't
    331392     * be changed after invocation of this method (at least its part from `i`
     
    333394     * There shouldn't be nulls in array between i-1 and i+len inclusive.
    334395     */
    335     public Expr (Object[] arr, int i, int len) {
     396    public static Expr fromArrayUnsafe (Object[] arr, int i, int len) {
    336397        assert i >= 0;
    337398        assert len >= 0;
    338399        assert arr.length - len >= i;
    339400        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 i-1 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;
    343413    }
    344414
     
    348418     * For use in assertions when creating expressions from arrays.
    349419     */
    350     private boolean noNulls (Object[] arr, int i, int len) {
     420    private static boolean noNulls (Object[] arr, int i, int len) {
    351421        if (i < 0)
    352422            i = 0;
     
    374444        assert length - len >= what;
    375445        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 position
    393      */
    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;
    401446    }
    402447
     
    462507    }
    463508
    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 
    475509    public SplitIterator leftSplit (int l) {
    476510        return new SplitIterator(this, l);
     
    492526
    493527        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);
    496530            return this;
    497531        }
    498532
    499533        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);
    502536            return this;
    503537        }
Note: See TracChangeset for help on using the changeset viewer.