public static DateRange[] CoalesceRanges(DateRange[] ranges) { //coalesces/simplifies a given set of date ranges to the simplest form. For example: /* a * |----| * b * |----| c * |----| * */ //will return an array of two Dateranges: /* a * |-----| * b * |----| * * */ //this is the "simplest" reduction of what the previous ranges were. List <DateRange> userange = ranges.ToList(); //userange.Sort((w,p)=>w.StartTime.CompareTo(p.StartTime)); List <DateRange> workalist = userange; List <DateRange> result = new List <DateRange>(); //algorithm //set a flag indicating a coalesce was discovered to true. //iterate while said flag is true. //set flag to false as loop begins. //iterate through every possible pair X and Y in the List. skip iterations where X==Y. //for each possible pairing: //coalesce the two DateRanges. If the results coalesce (the return value array has a length of 1) //mark x and y for removal, set the new Array to be AddRanged() to the List, and break out of both loops, and set the flag saying that //a coalesce was discovered. bool coalescefound = true; while (coalescefound) { List <DateRange> removalmarked = new List <DateRange>(); List <DateRange> addmarked = new List <DateRange>(); bool breakouter = false; coalescefound = false; foreach (DateRange x in workalist) { foreach (DateRange y in workalist) { if (x != y) { DateRange[] coalresult = DateRange.Coalesce(x, y); if (coalresult.Length == 1) { coalescefound = true; //add new array to be added... addmarked.AddRange(coalresult); //remove both x and y. We can't remove them here since we are iterating... removalmarked.Add(x); removalmarked.Add(y); breakouter = true; break; } } } if (breakouter) { break; //break out of outer iterator if flag set. } } //add and remove the marked items. foreach (var removeme in removalmarked) { workalist.Remove(removeme); } foreach (var addme in addmarked) { workalist.Add(addme); } } return(workalist.ToArray()); }
public DateRange[] Coalesce(DateRange Otherdaterange) { return(DateRange.Coalesce(this, Otherdaterange)); }