Ako.Interval
1.0.0
dotnet add package Ako.Interval --version 1.0.0
NuGet\Install-Package Ako.Interval -Version 1.0.0
<PackageReference Include="Ako.Interval" Version="1.0.0" />
paket add Ako.Interval --version 1.0.0
#r "nuget: Ako.Interval, 1.0.0"
// Install Ako.Interval as a Cake Addin
#addin nuget:?package=Ako.Interval&version=1.0.0
// Install Ako.Interval as a Cake Tool
#tool nuget:?package=Ako.Interval&version=1.0.0
Interval
In mathematics, a interval is a set of real numbers that contains all real numbers lying between any two numbers of the set. We brought interval into ASP.Net so that you simply can manage overlaps, unions, intersections and subtractions of intervals. Unlike mathematics, an interval can contains a range of any type which its values are comparable.
Installation
Simply add Ako.Interval
nuget package using dotnet CLI
dotnet add package Ako.Interval --version 1.0.0
Documentation
1- Constructing Interval
An standard interval contains two edges (endpoints). Each edge can be included withing the range or excluded. Also start edge can be minus infinity and end edge can be infinity. Every value (including edges) withing the range of interval must be comparable. Therefore you can simply construct an interval like below:
using Ako.IntervalCore;
using System;
namespace Program
{
public class IntervalApp
{
public void Main(){
var interval1 = new Interval<int>(1, 10);
// An interval of type int from 1 to 10
// Including both start and end edge.
// Standard notation: [1, 10]
var interval2 = new Interval<int>(1, 5, false);
// Start edge is excluded
// Standard notation: (1, 5]
var interval3 = new Interval<int>(1, 5, false, false);
// Both edges are excluded
// Standard notation: (1, 5)
var interval3 = new Interval<int>(null, 5);
// Start edge is infinity
// Standard notation: (-∞, 5]
var interval4 = new Interval<int>(0, null, false);
// End edge is infinity
// Standard notation: (0, ∞)
var interval5 = new Interval<int>(null, null);
// The infinite interval of integers
// Standard notation: (-∞, ∞)
/* Intervals can also take different types; Unless the given type is not comparable. */
var interval6 = new Interval<DateTime>(DateTime.Today, DateTime.Now);
// Standard notation: [2022/28/4-00:00:00, 2022/28/4-16:35:00]
}
}
}
Interval can also be constructed using standard notation string.
An standard interval notation is an string having two values separated by a comma. It should be starting with [
(open-bracket) as if start edge is included or (
(open-parenthesis) as if start edge is excluded. It also should be ending with ]
(closed-bracket) or )
(closed-parenthesis). As of the matter of infinity, ∞
symbol or the word infinity
can be used as value.
Note that the values of interval is casted into the given type using System.Convert
var interval1 = Interval.Parse<int>("[1, 10]");
var interval2 = Interval.Parse<int>("(1, 10]");
var interval3 = Interval.Parse<int>("(0, ∞)");
var interval4 = Interval.Parse<int>("(0, infinity)");
var interval5 = Interval.Parse<int>("(infinity, infinity)");
var interval6 = Interval.Parse<int>("(-∞, infinity)");
Intervals can also be constructed using extension methods of namespace Ako.IntervalCore.Extensions
var interval1 = 1.IntervalUntil(5);
// Standard notation: [1, 5]
var interval2 = 10.IntervalFrom(0, false);
// Standard notation: (0, 10]
2- Containing of a value
You can simply check if a value is inside an intervals range or not.
var interval1 = new Interval<int>(0, 100, false); // (0, 100]
interval1.Contains(10); // True
interval1.Contains(100); // True; End edge is included.
interval1.Contains(110); // False
interval1.Contains(0); // False; Start edge is excluded.
var interval2 = new Interval<int>(null, null); // (-∞, ∞)
interval2.Contains(10); // True
interval2.Contains(100); // True
interval2.Contains(1100); // True
3- Overlaps check
var interval1 = new Interval<int>(0, 10, false); // (0, 10]
var interval2 = new Interval<int>(10, 20); // [10, 20]
var interval3 = new Interval<int>(10, 20, false, false); // (10, 20)
var interval4 = new Interval<int>(null, null); // (-∞, ∞)
Interval.HasOverlap(interval1, interval2); // True
Interval.HasOverlap(interval1, interval3); // False
Interval.HasOverlap(interval2, interval3); // True
Interval.HasOverlap(interval2, interval4); // True
4- Union
var interval1 = new Interval<int>(0, 10, false, false); // (0, 10)
var interval2 = new Interval<int>(5, 15); // [5, 15]
var interval3 = new Interval<int>(null, null); // (-∞, ∞)
Interval.Union(interval1, interval2); // (0, 15]
Interval.Union(interval1, interval3); // (-∞, ∞)
Above methods are also available in nn-static form. operator +
calls the Union method.
5- Intersection
var interval1 = new Interval<int>(0, 10, false, false); // (0, 10)
var interval2 = new Interval<int>(5, 15); // [5, 15]
var interval3 = new Interval<int>(null, null); // (-∞, ∞)
Interval.Intersection(interval1, interval2); // [5, 10)
Interval.Intersection(interval1, interval3); // (0, 10)
Above methods are also available in nn-static form.
6- Subtraction
The picture below indicates intervalB - intervalA
var interval1 = new Interval<int>(0, 10, false, false); // (0, 10)
var interval2 = new Interval<int>(5, 15); // [5, 15]
Interval.Subtraction(interval1, interval2); // (0, 5)
Interval.Subtraction(interval2, interval1); // [10, 15]
Above methods are also available in nn-static form. operator -
calls the Subtraction method.
6- Equality
Two intervals are equal when both has the same type, edges are equal and inclusion of edges are the same.
var interval1 = new Interval<int>(0, 10); // [0, 10]
var interval2 = new Interval<int>(0, 10); // [0, 10]
var interval3 = new Interval<int>(0, 15); // [0, 15]
interval1.Equals(interval2); // True
interval1.Equals(interval3); // False
Operators ==
and !=
calls the Equality method.
Author
Farhad Rad; Ako Team ©
License
Repository
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 232 | 4/28/2022 |
Initial Release