Esempio n. 1
0
        /// <summary>
        /// Joins adjacent intervals.
        /// </summary>
        /// <returns> new set with the minimum amount of intervals</returns>
        public static IDisjointIntervalSet Consolidate(this IDisjointIntervalSet set)
        {
            var result = new DisjointIntervalSet();

            var orderedList = set.OrderBy(x => x.Start);

            var cachedItem = orderedList.FirstOrDefault();

            foreach (var item in orderedList.Skip(1))
            {
                if (cachedItem.IsContiguouslyFollowedBy(item))
                {
                    cachedItem = new Interval(cachedItem.Start, item.End, cachedItem.StartIncluded, item.EndIncluded);
                }
                else
                {
                    result.Add(cachedItem);
                    cachedItem = item;
                }
            }

            result.Add(cachedItem);

            return(result);
        }