Introduction
Shared resources are often limited by their usage count, like jobs per person or beds per room. This tip demonstrates based on the Time Period Library for .NET, how you can calculate the available time of such a resource.
Algorithm
The calculation of the resource occupation is based on the TimeLineMomentCollection:
Iterating through the time line moments, we detect the period Start
when the cumulated BalanceCount
exceeds the required usage count. The period End
is arrived, when the balance counter falls below the usage count:
public ITimePeriodCollection GetOccupiedPeriods<t>( ITimePeriodCollection periods, int usageCount )
where T : ITimeRange, new()
{
TimePeriodCollection occupiedPeriods = new TimePeriodCollection();
if ( periods.Count > 0)
{
TimeLineMomentCollection timeLineMoments = new TimeLineMomentCollection( periods );
DateTime? start = null;
int balance = 0;
foreach ( ITimeLineMoment timeLineMoment in timeLineMoments )
{
balance += timeLineMoment.BalanceCount;
if ( !start.HasValue )
{
if ( balance >= usageCount )
{
start = timeLineMoment.Moment;
}
}
else if ( balance < usageCount )
{
T period = new T();
period.Setup( start.Value, timeLineMoment.Moment );
occupiedPeriods.Add( period );
start = null;
}
}
}
return occupiedPeriods;
}
Using the Code
The following example calculates the occupation periods (step one) of a resource using GetOccupiedPeriods
. Based on the occupied periods, the library tool TimeGapCalculator
(step two) calculates the available time periods of the resource:
public void FreePeriodFinder()
{
ITimePeriodCollection resourcePeriods = new TimePeriodCollection();
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 08, 15, 0 ), new DateTime( 2014, 7, 19, 14, 00, 0 ) ) );
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 12, 45, 0 ), new DateTime( 2014, 7, 19, 20, 30, 0 ) ) );
resourcePeriods.Add(
new TimeRange( new DateTime( 2014, 7, 19, 18, 00, 0 ), new DateTime( 2014, 7, 19, 23, 00, 0 ) ) );
ITimePeriodCollection occupiedPeriods = GetOccupiedPeriods<TimeRange>( resourcePeriods, 2 );
foreach ( ITimePeriod occupiedPeriod in occupiedPeriods )
{
Console.WriteLine( "occupied: {0}", occupiedPeriod );
}
ITimePeriod freePeriodLimits = resourcePeriods;
ITimePeriodCollection freePeriods = new TimeGapCalculator<TimeRange>().
GetGaps( occupiedPeriods, freePeriodLimits );
foreach ( ITimePeriod freePeriod in freePeriods )
{
Console.WriteLine( "free: {0}", freePeriod );
}
}