Wintellect’s Power Collection Library
First of all a very happy and prosperous new year wishes to everyone ……… :)
CLR via C# by Jeffery Richter is one book which I really enjoyed reading.Any .Net developer whether experienced or a beginner will immediately benefit from the content provided in this book and will also help him/her to refine its overall programming with C# language.
The book covers all major topics of C# programming language and explains them through the point of view of CLR i.e. how CLR interprets our code which we write in regular C# language.After explaining “Generics” in chapter 16 , author mentions briefly about “Power Collection Library”.
This library is a set of collection classes just like List<> , Dictionary<> etc but with major differences.Not all but I have listed all the important collections of Power Collection Library in the following section.Also , I have provided a detailed official description for each collection class which is provided by the library itself.You can download the ‘Power Collection Library” from the following URL :
Download Link for the Power Collection Library
Various Collection Classes and their Definitions
Collection | Description |
Bag<T> | Bag is a collection that contains items of type T. Unlike a Set, duplicate items (items that compare equal to each other) are allowed in an Bag. RemarksThe items are compared in one of two ways. If T implements IComparable then the Equals method of that interface will be used to compare items, otherwise the Equals method from Object will be used. Alternatively, an instance of IComparer can be passed to the constructor to use to compare items. Bag is implemented as a hash table. Inserting, deleting, and looking up an an element all are done in approximately constant time, regardless of the number of items in the bag. When multiple equal items are stored in the bag, they are stored as a representative item and a count. If equal items can be distinguished, this may be noticeable. For example, if a case-insensitive comparer is used with a Bag, and both "hello", and "HELLO" are added to the bag, then the bag will appear to contain two copies of "hello" (the representative item). OrderedBag<> is similar, but uses comparison instead of hashing, maintain the items in sorted order, and stores distinct copies of items that compare equal. |
BigList<T> | BigList provides a list of items, in order, with indices of the items ranging from 0 to one less than the count of items in the collection. BigList is optimized for efficient operations on large (>100 items) lists, especially for insertions, deletions, copies, and concatenations. Type Parameters
RemarksBigList class is similar in functionality to the standard List class. Both classes provide a collection that stores an set of items in order, with indices of the items ranging from 0 to one less than the count of items in the collection. Both classes provide the ability to add and remove items from any index, and the get or set the item at any index. BigList differs significantly from List in the performance of various operations, especially when the lists become large (several hundred items or more). With List, inserting or removing elements from anywhere in a large list except the end is very inefficient -- every item after the point of inserting or deletion has to be moved in the list. The BigList class, however, allows for fast insertions and deletions anywhere in the list. Furthermore, BigList allows copies of a list, sub-parts of a list, and concatenations of two lists to be very fast. When a copy is made of part or all of a BigList, two lists shared storage for the parts of the lists that are the same. Only when one of the lists is changed is additional memory allocated to store the distinct parts of the lists. Of course, there is a small price to pay for this extra flexibility. Although still quite efficient, using an index to get or change one element of a BigList, while still reasonably efficient, is significantly slower than using a plain List. Because of this, if you want to process every element of a BigList, using a foreach loop is a lot more efficient than using a for loop and indexing the list. In general, use a List when the only operations you are using are Add (to the end), foreach, or indexing, or you are very sure the list will always remain small (less than 100 items). For large (>100 items) lists that do insertions, removals, copies, concatenations, or sub-ranges, BigList will be more efficient than List. In almost all cases, BigList is more efficient and easier to use than LinkedList. |
Deque<T> | The Deque class implements a type of list known as a Double Ended Queue. A Deque is quite similar to a List, in that items have indices (starting at 0), and the item at any index can be efficiently retrieved. The difference between a List and a Deque lies in the efficiency of inserting elements at the beginning. In a List, items can be efficiently added to the end, but inserting an item at the beginning of the List is slow, taking time proportional to the size of the List. In a Deque, items can be added to the beginning or end equally efficiently, regardless of the number of items in the Deque. As a trade-off for this increased flexibility, Deque is somewhat slower than List (but still constant time) when being indexed to get or retrieve elements. Type Parameters
RemarksThe Deque class can also be used as a more flexible alternative to the Queue and Stack classes. Deque is as efficient as Queue and Stack for adding or removing items, but is more flexible: it allows access to all items in the queue, and allows adding or removing from either end. Deque is implemented as a ring buffer, which is grown as necessary. The size of the buffer is doubled whenever the existing capacity is too small to hold all the elements. |
MultiDictionary<TKey,TValue> | The MultiDictionary class that associates values with a key. Unlike an Dictionary, each key can have multiple values associated with it. When indexing an MultiDictionary, instead of a single value associated with a key, you retrieve an enumeration of values. When constructed, you can chose to allow the same value to be associated with a key multiple times, or only one time. Type Parameters
|
OrderedBag<T> | OrderedBag is a collection that contains items of type T. The item are maintained in a sorted order. Unlike a OrderedSet, duplicate items (items that compare equal to each other) are allowed in an OrderedBag. RemarksThe items are compared in one of three ways. If T implements IComparable or IComparable, then the CompareTo method of that interface will be used to compare items. Alternatively, a comparison function can be passed in either as a delegate, or as an instance of IComparer. OrderedBag is implemented as a balanced binary tree. Inserting, deleting, and looking up an an element all are done in log(N) + M time, where N is the number of keys in the tree, and M is the current number of copies of the element being handled. Bag<> is similar, but uses hashing instead of comparison, and does not maintain the keys in sorted order. |
OrderedDictionary<TKey, TValue> | OrderedDictionary is a collection that maps keys of type TKey to values of type TValue. The keys are maintained in a sorted order, and at most one value is permitted for each key. RemarksThe keys are compared in one of three ways. If TKey implements IComparable or IComparable, then the CompareTo method of that interface will be used to compare elements. Alternatively, a comparison function can be passed in either as a delegate, or as an instance of IComparer. OrderedDictionary is implemented as a balanced binary tree. Inserting, deleting, and looking up an an element all are done in log(N) type, where N is the number of keys in the tree. Dictionary<,> is similar, but uses hashing instead of comparison, and does not maintain the keys in sorted order. |
OrderedMultiDictionary<TKey, TValue> | The OrderedMultiDictionary class that associates values with a key. Unlike an OrderedDictionary, each key can have multiple values associated with it. When indexing an OrderedMultidictionary, instead of a single value associated with a key, you retrieve an enumeration of values. All of the key are stored in sorted order. Also, the values associated with a given key are kept in sorted order as well. When constructed, you can chose to allow the same value to be associated with a key multiple times, or only one time. Type Parameters
|
OrderedSet<T> | OrderedSet is a collection that contains items of type T. The item are maintained in a sorted order, and duplicate items are not allowed. Each item has an index in the set: the smallest item has index 0, the next smallest item has index 1, and so forth. RemarksThe items are compared in one of three ways. If T implements IComparable or IComparable, then the CompareTo method of that interface will be used to compare items. Alternatively, a comparison function can be passed in either as a delegate, or as an instance of IComparer. OrderedSet is implemented as a balanced binary tree. Inserting, deleting, and looking up an an element all are done in log(N) type, where N is the number of keys in the tree. Set<> is similar, but uses hashing instead of comparison, and does not maintain the items in sorted order. |
Set<T> | Set is a collection that contains items of type T. The item are maintained in a haphazard, unpredictable order, and duplicate items are not allowed. RemarksThe items are compared in one of two ways. If T implements IComparable then the Equals method of that interface will be used to compare items, otherwise the Equals method from Object will be used. Alternatively, an instance of IComparer can be passed to the constructor to use to compare items. Set is implemented as a hash table. Inserting, deleting, and looking up an an element all are done in approximately constant time, regardless of the number of items in the Set. OrderedSet<> is similar, but uses comparison instead of hashing, and does maintains the items in sorted order. |
Code Example
In the following code snippet , I have highlighted some of the collection classes and their corresponding implementations.Also , other interesting thing is that , I have written the following code on VS 2010 Beta 2 with .Net 3.5 framework as the target framework.So it means , all the extension methods like FindAll<> , Skip<> , Take<> etc which are provided for .Net framework collection classes like List<> , Dictionary<> etc will also work with “Power Collection Library” collection classes.The code mentioned below is fairly simple and you can copy paste it and start playing with it as per your comfort.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wintellect.PowerCollections;
namespace SamplePowerCollectionLibrary
{
class Program
{
static void Main(string[] args)
{
#region Bag<T>
Console.WriteLine("-------- Start Of Bag<T> ----------");
Bag<string> strBag = new Bag<string>();
strBag.Add("India");
strBag.Add("US");
strBag.Add("UK");
strBag.Add("India");
strBag.Add("Japan");
strBag.ChangeNumberOfCopies("US", 3);
Console.WriteLine(strBag.CountWhere(a => a.StartsWith("I")));
foreach (string str in strBag)
{
Console.WriteLine(str);
}
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("-------- End Of Bag<T> ----------");
#endregion
#region Deque<T>
Console.WriteLine("-------- Start Of Deque<T> ----------");
Console.WriteLine(System.Environment.NewLine);
Deque<string> deque = new Deque<string>();
deque.Add("India");
deque.Add("USA");
deque.AddManyToBack(new List<string>() { "Brazil", "UK", "Australia" });
deque.AddToFront("Earth");
foreach (string str in deque)
Console.WriteLine(str);
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("-------- End Of Deque<T> ----------");
#endregion
#region MultiDictionary<TKey,TValue>
Console.WriteLine("-------- Start Of MultiDictionary<TKey,TValue> ----------");
Console.WriteLine(System.Environment.NewLine);
MultiDictionary<string, string> multiDictionary = new MultiDictionary<string, string>(true);
multiDictionary.Add("India", "Delhi");
multiDictionary.Add("India", "Mumbai");
multiDictionary.Add("India", "Delhi");
foreach (KeyValuePair<string, string> item in multiDictionary.KeyValuePairs)
{
Console.WriteLine("Key : {0} , Value : {1} ", item.Key, item.Value);
}
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("-------- End Of MultiDictionary<TKey,TValue> ----------");
#endregion
#region OrderedBag<T>
Console.WriteLine("-------- Start Of OrderedBag<T> ----------");
Console.WriteLine(System.Environment.NewLine);
OrderedBag<string> orderedBag = new OrderedBag<string>();
orderedBag.Add("India");
orderedBag.Add("USA");
orderedBag.Add("Australia");
orderedBag.Add("France");
foreach (string str in orderedBag)
Console.WriteLine("{0}", str);
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("-------- End Of OrderedBag<T> ----------");
#endregion
#region OrderedDictionary<TKey, TValue>
Console.WriteLine("-------- Start Of OrderedMultiDictionary<TKey, TValue> ----------");
OrderedMultiDictionary<string, string> orderedDictionary = new OrderedMultiDictionary<string, string>(true);
orderedDictionary.Add("India", "Delhi");
orderedDictionary.Add("India", "Bangalore");
orderedDictionary.Add("India", "Chennai");
orderedDictionary.Add("USA", "Newyork");
orderedDictionary.Add("USA", "Chicago");
orderedDictionary.Add("Australia", "Sydney");
orderedDictionary.Add("Australia", "Melbourn");
foreach (KeyValuePair<string, string> item in orderedDictionary.KeyValuePairs)
Console.WriteLine("Key : {0} , Value : {1}", item.Key, item.Value);
Console.WriteLine(System.Environment.NewLine);
Console.WriteLine("-------- End Of OrderedMultiDictionary<TKey, TValue> ----------");
#endregion
Console.ReadLine();
}
}
}
Output
Following is the output from the successful execution of above mentioned code snippet.You can use it to verify on how various collection class behave.