/** 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. */ public virtual IIntSet Complement(IIntSet vocabulary) { if (vocabulary == null) { return(null); // nothing in common with null set } if (!(vocabulary is IntervalSet)) { throw new ArgumentException("can't complement with non IntervalSet (" + vocabulary.GetType().Name + ")"); } IntervalSet vocabularyIS = ((IntervalSet)vocabulary); int maxElement = vocabularyIS.GetMaxElement(); IntervalSet compl = new IntervalSet(); int n = intervals.Count; if (n == 0) { return(compl); } Interval first = (Interval)intervals[0]; // add a range from 0 to first.a constrained to vocab if (first.a > 0) { IntervalSet s = IntervalSet.Of(0, first.a - 1); IntervalSet a = (IntervalSet)s.And(vocabularyIS); compl.AddAll(a); } for (int i = 1; i < n; i++) { // from 2nd interval .. nth Interval previous = (Interval)intervals[i - 1]; Interval current = (Interval)intervals[i]; IntervalSet s = IntervalSet.Of(previous.b + 1, current.a - 1); IntervalSet a = (IntervalSet)s.And(vocabularyIS); compl.AddAll(a); } Interval last = (Interval)intervals[n - 1]; // add a range from last.b to maxElement constrained to vocab if (last.b < maxElement) { IntervalSet s = IntervalSet.Of(last.b + 1, maxElement); IntervalSet a = (IntervalSet)s.And(vocabularyIS); compl.AddAll(a); } return(compl); }
/** 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. */ public virtual IIntSet Complement(Interval vocabulary) { if (vocabulary.b < MinElement || vocabulary.a > MaxElement) { // nothing in common with this set return(null); } int n = intervals.Count; if (n == 0) { return(IntervalSet.Of(vocabulary)); } IntervalSet compl = new IntervalSet(); Interval first = intervals[0]; // add a range from 0 to first.a constrained to vocab if (first.a > vocabulary.a) { compl.Intervals.Add(Interval.FromBounds(vocabulary.a, first.a - 1)); } for (int i = 1; i < n; i++) { if (intervals[i - 1].b >= vocabulary.b) { break; } if (intervals[i].a <= vocabulary.a) { continue; } if (intervals[i - 1].b == intervals[i].a - 1) { continue; } compl.Intervals.Add(Interval.FromBounds(Math.Max(vocabulary.a, intervals[i - 1].b + 1), Math.Min(vocabulary.b, intervals[i].a - 1))); //// from 2nd interval .. nth //Interval previous = intervals[i - 1]; //Interval current = intervals[i]; //IntervalSet s = IntervalSet.Of( previous.b + 1, current.a - 1 ); //IntervalSet a = (IntervalSet)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 < vocabulary.b) { compl.Intervals.Add(Interval.FromBounds(last.b + 1, vocabulary.b)); //IntervalSet s = IntervalSet.Of( last.b + 1, maxElement ); //IntervalSet a = (IntervalSet)s.And( vocabularyIS ); //compl.AddAll( a ); } return(compl); }
public virtual IIntSet Complement(int minElement, int maxElement) { return(this.Complement(IntervalSet.Of(minElement, maxElement))); }