Example #1
0
        // copy on write so we can cache a..a intervals and sets of that
        protected virtual void Add(Interval addition)
        {
            //[email protected]("add "+addition+" to "+intervals.toString());
            if (addition.b < addition.a)
            {
                return;
            }
            // find position in list
            // Use iterators as we modify list in place
            //for ( ListIterator iter = intervals.listIterator(); iter.hasNext(); )
            for (int i = 0; i < intervals.Count; i++)
            {
                //Interval r = (Interval)iter.next();
                Interval r = intervals[i];
                if (addition.Equals(r))
                {
                    return;
                }
                if (addition.Adjacent(r) || !addition.Disjoint(r))
                {
                    // next to each other, make a single larger interval
                    Interval bigger = addition.Union(r);
                    intervals[i] = bigger;
                    // make sure we didn't just create an interval that
                    // should be merged with next interval in list
                    while (i < intervals.Count - 1)
                    {
                        i++;
                        Interval next = intervals[i];
                        //Interval next = (Interval)iter.next();
                        if (!bigger.Adjacent(next) && bigger.Disjoint(next))
                        {
                            break;
                        }

                        // if we bump up against or overlap next, merge
                        //iter.remove();   // remove this one
                        intervals.RemoveAt(i);
                        //iter.previous(); // move backwards to what we just set
                        i--;
                        //iter.set( bigger.union( next ) ); // set to 3 merged ones
                        intervals[i] = bigger.Union(next);
                    }
                    return;
                }
                if (addition.StartsBeforeDisjoint(r))
                {
                    // insert before r
                    //iter.previous();
                    //iter.add( addition );
                    intervals.Insert(i, addition);
                    return;
                }
                // if disjoint and after r, a future iteration will handle it
            }
            // ok, must be after last interval (and disjoint from last interval)
            // just add it
            intervals.Add(addition);
        }
Example #2
0
        protected virtual void Add(Interval addition)
        {
            //[email protected]("add "+addition+" to "+intervals.toString());
            if (addition.b < addition.a)
            {
                return;
            }
            // find position in list
            //for (ListIterator iter = intervals.listIterator(); iter.hasNext();) {
            int n = intervals.Count;

            for (int i = 0; i < n; i++)
            {
                Interval r = (Interval)intervals[i];
                if (addition.Equals(r))
                {
                    return;
                }
                if (addition.adjacent(r) || !addition.disjoint(r))
                {
                    // next to each other, make a single larger interval
                    Interval bigger = addition.union(r);
                    intervals[i] = bigger;
                    // make sure we didn't just create an interval that
                    // should be merged with next interval in list
                    if ((i + 1) < n)
                    {
                        i++;
                        Interval next = (Interval)intervals[i];
                        if (bigger.adjacent(next) || !bigger.disjoint(next))
                        {
                            // if we bump up against or overlap next, merge
                            intervals.RemoveAt(i);             // remove next one
                            i--;
                            intervals[i] = bigger.union(next); // set to 3 merged ones
                        }
                    }
                    return;
                }
                if (addition.startsBeforeDisjoint(r))
                {
                    // insert before r
                    intervals.Insert(i, addition);
                    return;
                }
                // if disjoint and after r, a future iteration will handle it
            }
            // ok, must be after last interval (and disjoint from last interval)
            // just add it
            intervals.Add(addition);
        }