utte.numberset
2.0.1
dotnet add package utte.numberset --version 2.0.1
NuGet\Install-Package utte.numberset -Version 2.0.1
<PackageReference Include="utte.numberset" Version="2.0.1" />
<PackageVersion Include="utte.numberset" Version="2.0.1" />
<PackageReference Include="utte.numberset" />
paket add utte.numberset --version 2.0.1
#r "nuget: utte.numberset, 2.0.1"
#addin nuget:?package=utte.numberset&version=2.0.1
#tool nuget:?package=utte.numberset&version=2.0.1
NumberSet
This package handles generic sets for numbers or numberlike objects. The generic parameter T is a non-discreet number (for example double or float) or a number-like class. Number-like means that it implements certain interfaces (see below). The sets track if they contain their endpoints and thus wether they are Open, Closed or neither. They also has a measure, i.e. how large they are. The sets implement the methods Union, Intersection, Difference, and Contains. A short description of the interfaces can be found below.
Ex: [1.6, 2.3) Assuming T is double, this is a set with lower bound 1.6, upper bound 2.3 that contains its lower bound but not its upper bound. Moreover, it is not closed (if it contained 2.3 it would be), it is not open (if it didn't contain 1.6 it would be), it is not empty and it has a measure of 0.7.
Content
The package contains interfaces and classes for sets. The sets are immutable and every operation on them creates new sets.
Three interfaces are included. T has to implement IAdditionOperators<T, T, T>, ISubtractionOperators<T, T, T>, IComparisonOperators<T, T, bool> in the interfaces.
IBoundedSet<T> - Interface for sets of numberlike objects of type T. The structure of the sets are INumberSet<T> which contains a collection of INumberSetElement<T>. Both INumberSet<T> and INumberSetElement<T> implements IBoundedSet<T>. A single T instance is considered a point
INumberSetElement<T> - Interface for elements of a set which is a set in itself. It implements IBoundedSet<T>. The interface is used as part of INumberSet<T>.
INumberSet<T> - Interface for sets of numberlike objects of type T. It implements IBoundedSet<T>. It consists of a collection of INumberSetElement<T>. Every INumberSetElement<T> in the collection should be disjunct and sorted in order.
Two classes are included. T has to implement IAdditionOperators<T, T, T>, ISubtractionOperators<T, T, T>, IComparisonOperators<T, T, bool>, IParsable<T>, IFormattable in the classes.
NumberSetElement<T> - Implementation of INumberSetElement<T>.
NumberSet<T> - Implementation of INumberSet<T>.
Usage
Here are explanations and examples of how to use the package.
Create set and get data
Sets can be created by casts, parse string or the Create method. At creation it should be specified wether each NumberSetElement should contain its boundaries. A ( or ) means the boundary is not included while a [ or ] means it is for casts/parsing. In the Create method it is set using a boolean.
var element = NumberSetElement<double>.Create(5.1, 9.2, false, false);
var numberSet = NumberSet<double>.Create(element);
Console.WriteLine("LowerBound: " + numberSet.LowerBound.ToString());
Console.WriteLine("UpperBound: " + numberSet.UpperBound.ToString());
Console.WriteLine("IsClosed: " + numberSet.IsClosed.ToString());
Console.WriteLine("IsOpen: " + numberSet.IsOpen.ToString());
Console.WriteLine("IsEmpty: " + numberSet.IsEmpty.ToString());
Console.WriteLine("Measure: " + numberSet.Measure.ToString());
When run, this code prints to the console:
LowerBound: 5.1
UpperBound: 9.2
IsClosed: False
IsOpen: True
IsEmpty: False
Measure: 4.1
The NumberSet could have been produced through a cast (NumberSet<double> numberSet = "(5.1, 9.2)";
) or Parsed (var numberSet = NumberSet<double>.Parse("(5.1, 9.2)");
) instead of through the Create method.
Use of the methods
NumberSet<double> set1 = "(0.3, 2.1]";
NumberSet<double> set2 = "(1.8, 3.6]";
Console.WriteLine("Set1: " + set1.ToString());
Console.WriteLine("Set2: " + set2.ToString());
var unionSet = set1.Union(set2);
Console.WriteLine("Union: " + unionSet.ToString());
var intersects = set1.Intersects(set2);
Console.WriteLine("Intersects: " + intersects.ToString());
var intersectionSet = set1.Intersection(set2);
Console.WriteLine("Intersection: " + intersectionSet.ToString());
var differenceSet = set1.Difference(set2);
Console.WriteLine("Difference: " + differenceSet.ToString());
var containsSet = set1.Contains(set2);
Console.WriteLine("Contains: " + containsSet.ToString());
var containsPoint = set1.Contains(1.5);
Console.WriteLine("Contains point: " + containsPoint.ToString());
This code will print the following to the console:
Set1: (0.3, 2.1]
Set2: (1.8, 3.6]
Union: (0.3, 3.6]
Intersects: True
Intersection: (1.8, 2.1]
Difference: (0.3, 1.8]
Contains: False
Contains point: True
Advanced Union example
NumberSet<double> testSet1 = "(5.4, 6.7]; [1.1, 1.9); [0.1, 1); [3.1, 5.1]";
NumberSet<double> testSet2 = "(1.9, 2); [4.9, 5.4]";
var resultSet = testSet1.Union(testSet2);
Console.WriteLine("Union: " + resultSet);
Observe that the elements of the sets does not have to be entered in order (i.e. (5.4, 6.7] is before [1.1, 1.9) in the string. The code print the following to the console:
Union: [0.1, 1); [1.1, 1.9); (1.9, 2); [3.1, 6.7]
Advanced Intersection example
NumberSet<double> testSet1 = "(0.0, 1.3]; [1.4, 1.9); [3.2, 3.5]";
NumberSet<double> testSet2 = "[1.3, 1.4); (0.1, 0.2); [1.7, 2.5)";
var resultSet = testSet1.Intersection(testSet2);
Console.WriteLine("Intersection: " + resultSet);
This code results in the print out:
Intersection: (0.1, 0.2); [1.3, 1.3]; [1.7, 1.9)
Notice the results at the boundaries, 1.3 is included since it is included in both sets while 1.4 is not included since it is only included in one set.
Advanced Difference example
NumberSet<double> testSet1 = "(0.2, 3.3]; [5.1, 9.3)";
NumberSet<double> testSet2 = "[3.1, 5.2); [0.4, 0.4]; (6.0, 7.5); [9.4, 9.5]";
var resultSet = testSet1.Difference(testSet2);
Console.WriteLine("Difference: " + resultSet);
The code prints:
Difference: (0.2, 0.4); (0.4, 3.1); [5.2, 6]; [7.5, 9.3)
Additional documentation
The code can be found here: NumberSet repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- 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.
Improved documentation, organize code better