Beruflich Dokumente
Kultur Dokumente
RangeHelper_Integer.zip - 5.6 KB
Introduction
By default in C#, we have Enumerable.Range(Int32, Int32) which generates a sequence of integral numbers within a specified range.
Here is this article we are going to explore a few more options to extend the range related operations.
Background
What are we going to do?
Define a range.
Check if an item inside the range
Check if a range inside the range
Check if a range overlapping the range
Range Model
Here is our range model
/*https://stackoverflow.com/questions/325933/determine-whether-two-date-ranges-overlap*/
public bool Overlappes(IntegerRangeModel range)
{
bool includes = false;
includes = StartFrom <= range.EndTo && EndTo >= range.StartFrom; /*(StartA <= EndB)
and(EndA >= StartB)*/
//includes = Includes(range.StartFrom) || Includes(range.EndTo); /*can also use this
one*/
return includes;
}
}
Utility Class
Using the range model in the utility class
using System;
using System.Collections.Generic;
using System.Linq;
/*
* missing, overlapping
* https://stackoverflow.com/questions/7024051/find-missing-and-overlapping-numbers-in-
sequences
*/
public static IEnumerable<int> Overlappings(IEnumerable<int> expectedRangeItems,
IEnumerable<IntegerRangeModel> sourceRanges)
{
IEnumerable<int> overlapping = expectedRangeItems.Where(i => sourceRanges.Count(t =>
t.StartFrom <= i && t.EndTo >= i) > 1);
return overlapping;
}
https://www.codeproject.com/Tips/5061316/Csharp-Integer-Range-Helper?display=Print 2/6
02/06/2019 C#: Integer Range Helper - CodeProject
IEnumerable<IntegerRangeModel> sourceRanges)
{
IEnumerable<int> missing = expectedRangeItems.Where(i => sourceRanges.All(t =>
t.StartFrom > i || t.EndTo < i));
return missing;
}
/*
* https://stackoverflow.com/questions/19576504/find-available-numbers-from-a-list-of-
numbers-in-a-particular-range
* https://stackoverflow.com/questions/4936876/grouping-into-ranges-of-continuous-
integers/4937283#4937283
*/
public static IEnumerable<List<T>> ToContiguousSequences<T>(IEnumerable<T> sequence,
Func<T, T> next)
{
sequence = sequence.OrderBy(x => x);
var e = sequence.GetEnumerator();
if (!e.MoveNext())
{
throw new InvalidOperationException("Sequence is empty.");
}
var currentList = new List<T> { e.Current };
while (e.MoveNext())
{
T current = e.Current;
if (current.Equals(next(currentList.Last())))
{
currentList.Add(current);
}
else
{
yield return currentList;
currentList = new List<T> { current };
}
}
yield return currentList;
}
https://www.codeproject.com/Tips/5061316/Csharp-Integer-Range-Helper?display=Print 3/6
02/06/2019 C#: Integer Range Helper - CodeProject
https://www.codeproject.com/Tips/5061316/Csharp-Integer-Range-Helper?display=Print 4/6
02/06/2019 C#: Integer Range Helper - CodeProject
List to sequence range string list specifying the start and end item
Future Improvements
1. Move range population option to the model class and use lazyloading.
2. Creating a generic utility class to manage different types like DateTime, double or any user-defined type.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
https://www.codeproject.com/Tips/5061316/Csharp-Integer-Range-Helper?display=Print 5/6
02/06/2019 C#: Integer Range Helper - CodeProject
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile Article Copyright 2019 by DiponRoy
Web06 | 2.8.190530.2 | Last Updated 2 Jun 2019 Everything else Copyright © CodeProject, 1999-2019
https://www.codeproject.com/Tips/5061316/Csharp-Integer-Range-Helper?display=Print 6/6