Just some simple primitives that are used by other parts of the framework
Tuple
A tuple is a immutable structure able to contain a small amount of data with no operations on it.
In functional languages it's usual to return a sequence of values into a tuple, instead of defining a custom class or passing parameters by reference (or out).
Currently we have two-element and three-element tuples in Signum Utilities. Both have Equals, ToString and GetHashCode properly defined.
When Signum Framework will move to .Net 4.0 this structure will disappear in favor of Microsoft one.
[Serializable]
public struct Tuple<F, S> : IEquatable<Tuple<F, S>>
{
public readonly F First;
public readonly S Second;
public Tuple(F first, S second){...}
public bool Equals(Tuple<F, S> value){...}
public override bool Equals(object obj){...}
public override int GetHashCode(){...}
public override string ToString(){...}
}
[Serializable]
public struct Tuple<F, S, T> : IEquatable<Tuple<F, S, T>>
{
public F First;
public S Second;
public T Third;
public Tuple(F first, S second, T third){...}
public bool Equals(Tuple<F, S, T> value){...}
public override bool Equals(object obj){...}
public override int GetHashCode(){...}
public override string ToString(){...}
}
Also, in order to make it simpler using type inference:
public static class Tuple
{
public static Tuple<P, S> New<P, S>(P first, S second){...}
public static Tuple<F, S, T> New<F, S, T>(F first, S second, T third){...}
}
Example:
var tuples = new List<Tuple<int,string>>
{
Tuple.New(1,"hello"),
Tuple.New(2,"bye"),
};
MinMax
MinMax is a immutable structure. Is just a Tuple of elements of the same type where Min is considered the minimum and Max the maximum in any given order. It's different from Interval because the elements per-se are not comparables, but usually have a member that is. It is returned by
WithMinMaxPair function.
[Serializable]
public struct MinMax<T> : IEquatable<MinMax<T>>
{
public readonly T Min;
public readonly T Max;
(..)
}
Example
countries.WithMinMaxPair(c=>c.Population).ToString();
Interval
Interval is a immutable structure of an ordered pair. The elements have to be comparable and you can rely on Min being always lesser or equals than Max.
public struct Interval<T> : IEquatable<Interval<T>>, IComparable<Interval<T>>
where T: struct, IComparable<T>, IEquatable<T>
{
public readonly T min;
public readonly T max;
public T Min { get { return min; } }
public T Max { get { return max; } }
public Interval(T min, T max)
public bool Contains(T value)
public bool Overlap(Interval<T> other)
public Interval<T>? Intersection(Interval<T> other)
public Interval<T>? Union(Interval<T> other)
public bool Subset(Interval<T> other)
public IEnumerable<T> Elements()
public int CompareTo(Interval<T> other)
public override string ToString()
public bool Equals(Interval<T> other)
public override bool Equals(object obj)
public override int GetHashCode()
}
Example:
new Interval<int>(1,3).Intersect(new Interval<int>(0,2)).ToString();
Gruping
Out implementation of LINQ's IGrouping<K,T>, and the one that uses Signum.Engine LINQ Provider.
It inherits from List<T> so if you feel like hacking with undocumented APIs, you can downcast and add some elements more if you want :)
public class Grouping<K, T> : List<T>, IGrouping<K, T>
{
public Grouping(K key)
public Grouping(K key, IEnumerable<T> values)
public K Key {get;}
(...)
}