And() public method

public And ( IIntSet other ) : Antlr4.Runtime.Misc.IntervalSet
other IIntSet
return Antlr4.Runtime.Misc.IntervalSet
Ejemplo n.º 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);
        }