Sinbadsoft Advanced .NET Collections

Library of specialized containers : dynamic multi-dimensional arrays, bijective (reversible) dictionaries etc.

Examples

Reversible Dictionary

// Declare a reversible dictionary and insert some elements
IReversibleDictionary<int, string> rdict = new ReversibleDictionary<int, string>();
rdict[1] = "S1";
rdict[2] = "S2";
rdict[3] = "S3";
// Values are accessed using the indexer as usual
Console.WriteLine(rdict[1]);
Console.WriteLine(rdict[2]);
// The reversible dictionary gives access to keys from values with O(1) constant cost
Console.WriteLine(rdict.Reverse["S1"]);
Console.WriteLine(rdict.Reverse["S2"]);
// Values can be inserted in the reverse dictionary ...
rdict.Reverse["S4"] = 4;
// ... and then retrieved back using the corresponding key.
string s4;
if(rdict.TryGetValue(4, out s4)) Console.WriteLine(s4);
// The reverse dictionary remains enumerable
foreach (KeyValuePair<string, int> pair in rdict.Reverse)
{
    Console.WriteLine(pair);
}
Output:
S1
S2
1
2
S4
[S1, 1]
[S2, 2]
[S3, 3]
[S4, 4]

Dynamic Array

// Create a dynamic array with two dimensions
DynamicArray<string> darray = new DynamicArray<string>(2);
// Count is intially zero. No preallocation is made.
Console.WriteLine("Rank={0} Count={1}", darray.Rank, darray.Count);
// Fill some values in the array. The dynamic array will 
// grow automatically to hold the new elements.
for (int i = 0; i < 2; i++)
{
    for (int j = 0; j < 3; j++)
    {
        darray[i, j] = string.Format("({0},{1})", i, j);
    }
}
// The dynamic array count is automatically adjusted
Console.WriteLine("Count={0}", darray.Count);
// Dynamic arrays are enumerable
foreach (string elt in darray)
{
    Console.Write("{0} ", elt);
}
Console.WriteLine();
// Elements could be inserted at any position
darray[3, 2] = "(3,2)";
// As for regular arrays, elements are retrieved using the indexer.
for (int i = 0; i < darray.GetCount(0); i++)
{
    for (int j = 0; j < darray.GetCount(1); j++)
    {
        Console.Write("{0} ", darray[i, j] ?? "-nil-");
    }
    Console.WriteLine();
}
// A regular array could be extracted at any time
string[,] rarray = (string[,])darray;
Output:
Rank=2 Count=0
Count=6
(0,0) (0,1) (0,2) (1,0) (1,1) (1,2)
(0,0) (0,1) (0,2)
(1,0) (1,1) (1,2)
-nil- -nil- -nil-
-nil- -nil- (3,2)

License

Sinbadsoft Advanced .NET Collections is distributed under the terms of the GNU Library General Public License (LGPL)
Version 2.1. Please refer to the License section for more details.

Last edited Apr 5, 2010 at 2:21 PM by nakhli, version 10