Signum Framework Logo
"Open framework that encourages convention over configuration, using C# code,
not XML files, to model at the right level of abstraction and achieve deadlines.
...but also has a full Linq provider, and syncs the schema for you!"
Login
RSS

Search

»



Main
Index
Map
Videos
Download
Source code
Tutorials
Forum
FAQ



Image



PoweredBy

DataStructures - Simple

RSS
Modified on 2010/01/02 17:15 by olmo Categorized as Uncategorized
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.

//Two element Tuple
[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(){...} }

//Three element Tuple
[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(); //Returns [Min = Pitcaim Islands, Max = China]

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) //Constructor public bool Contains(T value) //Returns true if val is inside of the interval public bool Overlap(Interval<T> other) //Returns true if the intervals overlap

public Interval<T>? Intersection(Interval<T> other) //Returns the intersection of intervals, if any public Interval<T>? Union(Interval<T> other) //Returns the union of intervals public bool Subset(Interval<T> other) //returns true if other is a subset of the current instance

public IEnumerable<T> Elements() //returns Min and Max as an IEnumerable<T> public int CompareTo(Interval<T> other) //compares two intervals by comparing their Min value 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(); //Returns: [1,2]

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;}
    (...)
}

Creative Commons License Signum Framework Site by Signum Software is licensed under a Creative Commons Attribution 3.0 License.
Powered by ScrewTurn Wiki version 3.0.5.600.