All pastes #254612 Raw Edit

Untitled

public text v1 · immutable
#254612 ·published 2006-11-22 01:32 UTC
rendered paste body
    public class PeriodList : List<Period>
    {

        public PeriodList ProjectOnto(PeriodList periodList)
        {
            
            List<DateTime> slicePoints = new List<DateTime>();

            foreach (Period p in this)
            {
                if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate);
                if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate);
            }

            foreach (Period p in periodList)
            {
                if (!slicePoints.Contains(p.StartDate)) slicePoints.Add(p.StartDate);
                if (!slicePoints.Contains(p.EndDate)) slicePoints.Add(p.EndDate);
            }

            slicePoints.Sort();


            PeriodList results = new PeriodList();

            Period current = null;

            foreach (DateTime i in slicePoints)
            {
                Period best = SelectBest(i, periodList);

                if (current != null)
                {
                    if (best == null || best != current)
                    {
                        current.EndDate = i;
                        results.Add(current);
                        current = null;
                    }
                }

                // no else here
                if (current == null)
                {
                    if (best != null)
                    {
                        current = best; // or copy $ or whatever
                        current.StartDate = i;
                    }
                }

            }
            
            return results;
        }

        public Period SelectBest(DateTime time, PeriodList periodList)
        {

            Period x = this.Find
                    (
                        delegate(Period p)
                        {
                            return (p.StartDate <= time && time < p.EndDate);
                        }
                    )
                    ;

            if (x != null) return x.Copy();

            Period y = periodList.Find
        (
            delegate(Period p)
            {
                return (p.StartDate <= time &&  time < p.EndDate);
            }
        )
        ;

            if (y != null) return y.Copy();
            else return null;
        }
    }


}