Of() public static method

Interval objects are used readonly so share all with the same single value a==b up to some max size.
Interval objects are used readonly so share all with the same single value a==b up to some max size. Use an array as a perfect hash. Return shared object for 0..INTERVAL_POOL_MAX_VALUE or a new Interval object with a..a in it. On Java.g4, 218623 IntervalSets have a..a (set with 1 element).
public static Of ( int a, int b ) : Antlr4.Runtime.Misc.Interval
a int
b int
return Antlr4.Runtime.Misc.Interval
Exemplo n.º 1
0
        public virtual void Remove(int el)
        {
            if (@readonly)
            {
                throw new InvalidOperationException("can't alter readonly IntervalSet");
            }
            int n = intervals.Count;

            for (int i = 0; i < n; i++)
            {
                Interval I = intervals[i];
                int      a = I.a;
                int      b = I.b;
                if (el < a)
                {
                    break;
                }
                // list is sorted and el is before this interval; not here
                // if whole interval x..x, rm
                if (el == a && el == b)
                {
                    intervals.RemoveAt(i);
                    break;
                }
                // if on left edge x..b, adjust left
                if (el == a)
                {
                    intervals[i] = Interval.Of(I.a + 1, I.b);
                    break;
                }
                // if on right edge a..x, adjust right
                if (el == b)
                {
                    intervals[i] = Interval.Of(I.a, I.b - 1);
                    break;
                }
                // if in middle a..x..b, split interval
                if (el > a && el < b)
                {
                    // found in this interval
                    int oldb = I.b;
                    intervals[i] = Interval.Of(I.a, el - 1);
                    // [a..x-1]
                    Add(el + 1, oldb);
                }
            }
        }
Exemplo n.º 2
0
 /// <summary>Add interval; i.e., add all integers from a to b to set.</summary>
 /// <remarks>
 /// Add interval; i.e., add all integers from a to b to set.
 /// If b&lt;a, do nothing.
 /// Keep list in sorted order (by left range value).
 /// If overlap, combine ranges.  For example,
 /// If this is {1..5, 10..20}, adding 6..7 yields
 /// {1..5, 6..7, 10..20}.  Adding 4..8 yields {1..8, 10..20}.
 /// </remarks>
 public virtual void Add(int a, int b)
 {
     Add(Interval.Of(a, b));
 }