Example #1
0
        public DateRangeCollection Union(DateRangeCollection rhs)
        {
            /*
             * Simple case, since Normalize basically creates a union.
             * Hence, we don't really need any implementation, beyond
             * whatever logic the CTOR of the class contains, besides
             * from making sure we pass in both sides' content to the CTOR.
             */
            var list = new List <DateRange>(_content);

            list.AddRange(rhs);
            return(new DateRangeCollection(Normalize(list)));
        }
Example #2
0
        public DateRangeCollection Intersection(DateRangeCollection rhs)
        {
            // Buffer used to hold our result.
            var list = new List <DateRange>();

            /*
             * The iterator for the this instance, which is used to enumerate
             * all the instances in the this object.
             */
            var selfIterator = GetEnumerator();

            /*
             * The iterator for the rhs (argument) collection.
             * Notice, these iterators are incremented independently,
             * in such a way that each relevant instance in each
             * collection is compared for intersection towards the other
             * party's relevant instance(s).
             *
             * This algorithm assumes that both sides are sorted lists,
             * and normalized, which shouldn't be a problem, since creating
             * an instance of a DateTimeCollection is impossible without
             * also making sure it's normalized.
             */
            var otherIterator = rhs.GetEnumerator();

            /*
             * Verifying both sides have instances, since if not, the
             * obvious result would be an empty collection
             */
            if (selfIterator.MoveNext() && otherIterator.MoveNext())
            {
                /*
                 * The body of our algorithm, which increments the correct
                 * iterator, to make sure we only compare relevant instances.
                 */
                while (true)
                {
                    var selfCurrent  = selfIterator.Current;
                    var otherCurrent = otherIterator.Current;

                    // Checking for intersection.
                    if (selfCurrent.Intersects(otherCurrent))
                    {
                        list.Add(selfCurrent.Intersection(otherCurrent));
                    }

                    /*
                     * Which iterator we advance depends upon which item
                     * has the largest End date, to make sure consecutive relevant
                     * instances are compared towards all relevant instances
                     * in the other collection.
                     *
                     * If any of the iterators yields no more results, we are
                     * done, and no more additions are possible.
                     */
                    if (selfCurrent.End > otherCurrent.End)
                    {
                        if (!otherIterator.MoveNext())
                        {
                            break;
                        }
                    }
                    else
                    {
                        if (!selfIterator.MoveNext())
                        {
                            break;
                        }
                    }
                }
            }

            /*
             * Our end result, passed into our private CTOR, which assumes
             * the items are already normalized.
             */
            return(new DateRangeCollection(list));
        }