using System;
using System.Collections.Generic;
using System.Text;
namespace OcclusionTest
{
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;
Period y = periodList.Find
(
delegate(Period p)
{
return (p.StartDate <= time && time < p.EndDate);
}
)
;
if (y != null) return y;
return null;
}
}
}