Antlr4.Runtime.Misc.IntervalSet.And C# (CSharp) Method

And() public method

public And ( IIntSet other ) : Antlr4.Runtime.Misc.IntervalSet
other IIntSet
return Antlr4.Runtime.Misc.IntervalSet
        public virtual Antlr4.Runtime.Misc.IntervalSet And(IIntSet other)
        {
            if (other == null)
            {
                //|| !(other instanceof IntervalSet) ) {
                return null;
            }
            // nothing in common with null set
            IList<Interval> myIntervals = this.intervals;
            IList<Interval> theirIntervals = ((Antlr4.Runtime.Misc.IntervalSet)other).intervals;
            Antlr4.Runtime.Misc.IntervalSet intersection = null;
            int mySize = myIntervals.Count;
            int theirSize = theirIntervals.Count;
            int i = 0;
            int j = 0;
            // iterate down both interval lists looking for nondisjoint intervals
            while (i < mySize && j < theirSize)
            {
                Interval mine = myIntervals[i];
                Interval theirs = theirIntervals[j];
                //System.out.println("mine="+mine+" and theirs="+theirs);
                if (mine.StartsBeforeDisjoint(theirs))
                {
                    // move this iterator looking for interval that might overlap
                    i++;
                }
                else
                {
                    if (theirs.StartsBeforeDisjoint(mine))
                    {
                        // move other iterator looking for interval that might overlap
                        j++;
                    }
                    else
                    {
                        if (mine.ProperlyContains(theirs))
                        {
                            // overlap, add intersection, get next theirs
                            if (intersection == null)
                            {
                                intersection = new Antlr4.Runtime.Misc.IntervalSet();
                            }
                            intersection.Add(mine.Intersection(theirs));
                            j++;
                        }
                        else
                        {
                            if (theirs.ProperlyContains(mine))
                            {
                                // overlap, add intersection, get next mine
                                if (intersection == null)
                                {
                                    intersection = new Antlr4.Runtime.Misc.IntervalSet();
                                }
                                intersection.Add(mine.Intersection(theirs));
                                i++;
                            }
                            else
                            {
                                if (!mine.Disjoint(theirs))
                                {
                                    // overlap, add intersection
                                    if (intersection == null)
                                    {
                                        intersection = new Antlr4.Runtime.Misc.IntervalSet();
                                    }
                                    intersection.Add(mine.Intersection(theirs));
                                    // Move the iterator of lower range [a..b], but not
                                    // the upper range as it may contain elements that will collide
                                    // with the next iterator. So, if mine=[0..115] and
                                    // theirs=[115..200], then intersection is 115 and move mine
                                    // but not theirs as theirs may collide with the next range
                                    // in thisIter.
                                    // move both iterators to next ranges
                                    if (mine.StartsAfterNonDisjoint(theirs))
                                    {
                                        j++;
                                    }
                                    else
                                    {
                                        if (theirs.StartsAfterNonDisjoint(mine))
                                        {
                                            i++;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (intersection == null)
            {
                return new Antlr4.Runtime.Misc.IntervalSet();
            }
            return intersection;
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Given the set of possible values (rather than, say UNICODE or MAXINT),
        /// return a new set containing all elements in vocabulary, but not in
        /// this.
        /// </summary>
        /// <remarks>
        /// Given the set of possible values (rather than, say UNICODE or MAXINT),
        /// return a new set containing all elements in vocabulary, but not in
        /// this.  The computation is (vocabulary - this).
        /// 'this' is assumed to be either a subset or equal to vocabulary.
        /// </remarks>
        public virtual Antlr4.Runtime.Misc.IntervalSet Complement(IIntSet vocabulary)
        {
            if (vocabulary == null)
            {
                return(null);
            }
            // nothing in common with null set
            if (!(vocabulary is Antlr4.Runtime.Misc.IntervalSet))
            {
                throw new ArgumentException("can't complement with non IntervalSet (" + vocabulary
                                            .GetType().FullName + ")");
            }
            Antlr4.Runtime.Misc.IntervalSet vocabularyIS = ((Antlr4.Runtime.Misc.IntervalSet)
                                                            vocabulary);
            int maxElement = vocabularyIS.GetMaxElement();

            Antlr4.Runtime.Misc.IntervalSet compl = new Antlr4.Runtime.Misc.IntervalSet();
            int n = intervals.Count;

            if (n == 0)
            {
                return(compl);
            }
            Interval first = intervals[0];

            // add a range from 0 to first.a constrained to vocab
            if (first.a > 0)
            {
                Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(0, first.a
                                                                                       - 1);
                Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS);
                compl.AddAll(a);
            }
            for (int i = 1; i < n; i++)
            {
                // from 2nd interval .. nth
                Interval previous = intervals[i - 1];
                Interval current  = intervals[i];
                Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(previous.b
                                                                                       + 1, current.a - 1);
                Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS);
                compl.AddAll(a);
            }
            Interval last = intervals[n - 1];

            // add a range from last.b to maxElement constrained to vocab
            if (last.b < maxElement)
            {
                Antlr4.Runtime.Misc.IntervalSet s = Antlr4.Runtime.Misc.IntervalSet.Of(last.b + 1
                                                                                       , maxElement);
                Antlr4.Runtime.Misc.IntervalSet a = s.And(vocabularyIS);
                compl.AddAll(a);
            }
            return(compl);
        }