The System provides default implementations of the design pattern described in section 3. The goal of this implementation is to be the definitive source for the “correct” implementation of the design pattern (ObjectList, etc) and provide a useful set of List classes for doing common operations.
The Collection classes offers a rich set of utility classes (List, ObjectList, etc) and a default implementation of the List design pattern (BaseList).
The BaseList class is the default implementation of the List design pattern.
public class ObjectList : IList, ICollection { public BaseList(); public BaseList(ICollection collection); //Properites Object this[int index] {get;set;}; public int Count {get;}; public Object SyncRoot {get;}; public boolean IsSynchronized {get;}; //Methods static ObjectList Synchronized (ObjectList list); public int Add(Object value); //adds at end public void Insert(int position, Object value); //insert public void CopyTo(Array array, int index); //from IList public void CopyTo(Object[] array, int index); public boolean Contains (Object value); public void Clear(); public IEnumerator GetEnumerator(); public Object[] ToArray (); public static List FromArray (Object [] array); public void RemoveAt(int position); public void Remove(Object value); //shrink the collection }
public class ArrayList: ICollection, ICloneable { ObjectList(); ObjectList(int capacity); ObjectList(ICollection collection); public static readonly List Empty; public static List Synchronized(List list); public static List FixedSize(List list); public static List ReadOnly(List list); public static List Repeat(object value, int count); // ICollection void CopyTo(Array array, int index); public IEnumerator GetEnumerator(); public int Count {get;}; public Object SyncRoot{get;}; public boolean IsSynchronized{get;}; public boolean Contains (Object value); // IList public Object this[int index]; //(read-write) public int Add (Object value); public void Clear(); public int IndexOf (Object value); // Uses Object.Equals method public void Insert(int index, Object value); public void RemoveAt(int index); public virtual void AddRange(ICollection c); public virtual int BinarySearch(int index, int count, object value, IComparer comparer); public virtual int BinarySearch(object value); (not implemented in the current release) public virtual int BinarySearch(object value, IComparer comparer); (not implemented in the current release) public virtual bool Contains(object value); public virtual void CopyTo(Array array); public virtual void CopyTo(Array array, int arrayIndex); public virtual void CopyTo(int index, Array array, int arrayIndex, int count); public virtual IEnumerator GetEnumerator(bool allowRemove); public virtual IEnumerator GetEnumerator(int index, int count, bool allowRemove); public virtual List GetRange(int index, int count); public virtual int IndexOf(object value); public virtual int IndexOf(object value, int startIndex); public virtual int IndexOf(object value, int startIndex, int endIndex); public virtual void Insert(int index, object value); public virtual void InsertRange(int index, ICollection c); public virtual int LastIndexOf(object value); public virtual int LastIndexOf(object value, int startIndex); public virtual int LastIndexOf(object value, int startIndex, int endIndex); public virtual void Remove(object value); public virtual void RemoveRange(int index, int count); public virtual void Reverse(); public virtual void Reverse(int index, int count); public virtual void SetRange(int index, ICollection c); public virtual void Sort(); public virtual void Sort(IComparer comparer); public virtual void Sort(int index, int count, IComparer comparer); }
Below are the reference implementations of the Dictionary design pattern.
The Dictionary class is the default implementation of the Dictionary design pattern.
public class Dictionary : IDictionary, ICollection { public Dictionary (); public Dictionary(int capacity) public Dictionary(int capacity, float loadFactor) public Dictionary(IDictionary dictionary) //Private Implementations IEnumerator ICollection.GetEnumerator(); //Properites public int Count {get;}; public Object SyncRoot {get;}; public boolean IsSynchronized {get;}; public ICollection Keys; public ICollection Values; public Object this[Object key] {get; set;} // Methods static Dictionary Synchronized (Dictionary dictionary); public void CopyTo(DictionaryEntry[] array, int index); public void CopyTo(Array array, int index); //from ICollection public boolean Contains (Object value); public IDictionaryEnumerator GetEnumerator(); public virtual void Add (Object key, Object value); public virtual void Clear (); public virtual void Remove (Object key); }
The Hashtable class represents a dictionary of associated keys and values, implemented as a hashtable.
Objects used as keys in a hashtable must implement the GetHashCode and Equals methods (or they can rely on the default implementations inherited from Object if key equality is simply based on referential identity). Furthermore, the GetHashCode and Equals methods of a key object must produce the same results given the same parameters for the entire time the key is present in the hashtable. In practical terms, this means that key objects should be immutable, at least for the time they are used as keys in a hashtable.
When entries are added to a hashtable, they are placed into buckets based on the hashcode of their keys. Subsequent lookups of keys will use the hashcode of the keys to search a particular bucket, thus substantially reducing the number of key comparisons required to find an entry. A hashtable's maximum load factor, which can be specified when the hashtable is instantiated, determines the maximum ratio of hashtable entries to hashtable buckets. Smaller load factors cause faster average lookup times at the cost of increased memory consumption. The default maximum load factor of 1.0 generally provides the best balance between speed and size. As entries are added to a hashtable, the hashtable's actual load factor increases, and when the actual load factor reaches the maximum load factor value, the number of buckets in the hashtable is automatically increased by approximately a factor of two (To be precise, the number of hashtable buckets is increased to the smallest prime number that is larger than twice the current number of hashtable buckets).
class Hashtable : public IDictionary, public ICollection, ICloneable { public: //Constructors Hashtable (); Hashtable (IDictionary d, float loadFactor); Hashtable (IDictionary d); Hashtable (int capacity, float loadFactor); Hashtable (int capacity); Hashtable (IHashCodeProvider hashCodeProvider, IComparer comparer) //Methods static IDictionary ReadOnly (IDictionary dictionary); static IDictionary Synchronized (IDictionary dictionary); // ICollection void CopyTo(Array array, int index); IEnumerator GetEnumerator(); //The return value must be castable to IDictionaryEnumerator //Used directly, the IEnumerator enumerates over the values. property int Count {get;}; property Object SyncRoot {get;}; property boolean IsSynchronized {get;}; boolean Contains (Object value); // IDictionary property ICollection Keys; {get;} property ICollection Values {get;}; virtual void Add (Object key, Object value); virtual void Clear (); virtual void Remove (Object key); IDictionayEnumerator GetEnumerator(); //ICloneable virtual Object Clone (); //Dictionary property ICollection Keys {get;}; property ICollection Values {get;}; property Object Item [Object key]; // default index (set,get) virtual bool ContainsKey (Object key); virtual bool ContainsValue (Object value); //This method is real slow public bool Equals (Object o); };
This class implements a sorted list of keys and values. Entries in a sorted list are sorted by their keys and are accessible both by key and by index. The keys of a sorted list can be ordered either according to a specific IComparer implementation given when the sorted list is instantiated, or according to the IComparable implementation provided by the keys themselves. In either case, a sorted list does not allow entries with duplicate keys.
A sorted list internally maintains two arrays that store the keys and values of the entries. The capacity of a sorted list is the allocated length of these internal arrays. As elements are added to a sorted list, the capacity of the sorted list is automatically increased as required by reallocating the internal arrays. The capacity is never automatically decreased, but users can call either TrimToSize or Capacity explicitly.
The GetKeyList and GetValueList methods of a sorted list provides access to the keys and values of the sorted list in the form of List implementations. The List objects returned by these methods are aliases for the underlying sorted list, so modifications made to those lists are directly reflected in the sorted list, and vice versa.
The SortedList class provides a convenient way to create a sorted copy of another dictionary, such as a Hashtable. For example:
Hashtable h = new Hashtable(); h.Set(...); h.Set(...); ... SortedList s = new SortedList(h);
The last line above creates a sorted list that contains a copy of the keys and values stored in the hashtable. In this particular example, the keys will be ordered according to the IComparable interface, which they all must implement. To impose a different ordering, SortedList also has a constructor that allows a specific IComparer implementation to be specified.
class SortedDictionary : Dictionary { public: //Constructors SortedDictionary (); SortedDictionary (IDictionary d, IComparer comparer); SortedDictionary (IDictionary d); SortedDictionary (IComparer comparer, int capacity); SortedDictionary (IComparer comparer); //Methods static IDictionary ReadOnly (IDictionary dictionary); static IDictionary Synchronized (IDictionary dictionary); // ICollection void CopyTo(Array array, int index); IEnumerator ICollection.GetEnumerator(); //The return value must be castable to IDictionaryEnumerator //Used directly, the IEnumerator enumerates over the values. property int Count {get;}; property Object SyncRoot {get;}; property boolean IsSynchronized {get;}; boolean Contains (Object key); // IDictionary property ICollection Keys {get;}; property ICollection Values {get;}; virtual void Add (Object key, Object value); virtual void Clear (); virtual void Remove (Object key); IDictionayEnumerator GetEnumerator(); //Dictionary property ICollection Keys {get;}; property ICollection Values {get;}; property Object Item [Object key]; // default index (set,get) //ICloneable virtual Object Clone (); virtual bool ContainsKey (Object key); virtual bool ContainsValue (Object value); //This method is realslow sealed IEnumerator GetEnumerator (); virtual IList GetValueList (); virtual IList GetKeyList (); virtual Object GetKey (int index); virtual Object GetByIndex (int index); virtual int IndexOfKey (Object key); virtual int IndexOfValue (Object value); virtual void RemoveRange (int index, int count); virtual void RemoveAt (int index); virtual void SetByIndex (int index, Object value); virtual void TrimToSize ();