-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intervals): add new methods for intervals
Add methods to get intersection and union of intervals. Add methods to merge intervals in minimum intervals count. BREAKING CHANGE: Intersect method renamed in IntersectWith
- Loading branch information
Showing
7 changed files
with
135 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright (c) Stéphane ANDRE. All Right Reserved. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using MyNet.Utilities.Sequences; | ||
|
||
namespace MyNet.Utilities | ||
{ | ||
public static class IntervalExtensions | ||
{ | ||
public static IEnumerable<TClass> Merge<T, TClass>(this IEnumerable<TClass> intervals) | ||
where T : struct, IComparable | ||
where TClass : Interval<T, TClass> | ||
{ | ||
if (intervals.Count() <= 1) return intervals; | ||
|
||
var result = new List<TClass>(); | ||
|
||
TClass? previousInterval = null; | ||
foreach (var item in intervals.OrderBy(x => x.Start).ToList()) | ||
{ | ||
if (previousInterval is not null) | ||
{ | ||
if (previousInterval.Union(item) is TClass interval) | ||
{ | ||
result.Add(interval); | ||
previousInterval = interval; | ||
} | ||
else | ||
{ | ||
result.Add(previousInterval); | ||
previousInterval = item; | ||
} | ||
} | ||
else | ||
previousInterval = item; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters