// Function to show the dictionary. public static void ShowDic(MyDictionary <int, State> myDictionary) { Console.Clear(); if (myDictionary.Count != 0) { for (int i = 0; i < myDictionary.Capacity; i++) { Console.WriteLine("INDEX: {0}", i); if (myDictionary.Table[i] != null && myDictionary.Table[i].value != null) { DicPoint <int, State> curr = myDictionary.Table[i]; curr.value.Show(); while (curr.next != null) { curr.next.value.Show(); curr = curr.next; } } } } else { Console.WriteLine("The dictionary is empty"); } Console.WriteLine("Press ENTER to continue"); Console.ReadLine(); }
// Clear the dictionary. public void Clear() { Table = new DicPoint <K, T> [100]; Capacity = 100; Count = 0; Keys = null; Values = null; }
// Constructor with a capacity parameter. public MyDictionary(int capacity) { Table = new DicPoint <K, T> [capacity]; for (int i = 0; i < Table.Length; i++) { Table[i] = new DicPoint <K, T>(); } this.Capacity = capacity; Count = 0; Keys = null; Values = null; }
// Redefinition of the Equals method. public override bool Equals(object obj) { DicPoint <K, T> buf = (DicPoint <K, T>)obj; if (value.Equals(buf.value) && key.Equals(buf.key)) { return(true); } else { return(false); } }
// Constructor without parameters. public MyDictionary() { // Initital capacity is 100. Table = new DicPoint <K, T> [100]; for (int i = 0; i < Table.Length; i++) { Table[i] = new DicPoint <K, T>(); } Capacity = 100; Count = 0; Keys = null; Values = null; }
// Redefinition of the GetEnumerator method. public IEnumerator GetEnumerator() { for (int i = 0; i < Capacity; i++) { DicPoint <K, T> curr = Table[i]; while (curr != null) { yield return(curr); curr = curr.next; } } }
// Adds an element to the dictionary. public virtual bool Add(object key, object value) { // Creating an object with wanted key and value. DicPoint <K, T> dicPointBuffer = new DicPoint <K, T>((K)key, (T)value); // Getting index. int index = GetIndex(key); // If there are no objects with this index. if (Table[index] == null || Table[index].value == null) { DicPoint <K, T> buf = new DicPoint <K, T>((K)key, (T)value); Table[index] = buf; } else { // The first object in the list of this index. DicPoint <K, T> current = Table[index]; // If there is the same object. if (current.Equals(dicPointBuffer)) { return(false); } // Finding the end of the list or the same object. while (current.next != null) { if (current.next.Equals(dicPointBuffer)) { return(false); } current = current.next; } // Adding the element to the table. current.next = dicPointBuffer; } // Adding the key and the value to the arrays and increasing the count. AddKey((K)key); AddValue((T)value); Count++; return(true); }
// Constructor with a MyDictionary object parameter. public MyDictionary(MyDictionary <K, T> dic) { Table = new DicPoint <K, T> [dic.Table.Length]; for (int i = 0; i < dic.Table.Length; i++) { this.Table[i] = dic.Table[i]; } this.Keys = new K[dic.Keys.Length]; for (int i = 0; i < dic.Keys.Length; i++) { this.Keys[i] = dic.Keys[i]; } this.Values = new T[dic.Values.Length]; for (int i = 0; i < dic.Values.Length; i++) { this.Values[i] = dic.Values[i]; } Capacity = dic.capacity; Count = dic.count; }
// Constructor with a T parameter. public DicPoint(K key, T ob) { value = ob; this.key = key; next = null; }
// Constructor without parameters public DicPoint() { value = default(T); key = default(K); next = null; }
public static void EventsDemo() { Console.Clear(); // The first collection. MyNewDictionary <int, State> dic1 = new MyNewDictionary <int, State>(); MyDictionary <int, State> buf = new MyDictionary <int, State>(4); MyDicDemo.FillDic(ref buf); dic1 = new MyNewDictionary <int, State>(buf); dic1.Name = "dic1"; // The second collection. MyNewDictionary <int, State> dic2 = new MyNewDictionary <int, State>(4); buf = new MyDictionary <int, State>(4); MyDicDemo.FillDic(ref buf); dic2 = new MyNewDictionary <int, State>(buf); dic2.Name = "dic2"; // The first journal. Journal journal1 = new Journal(); // The second journal. Journal journal2 = new Journal(); // Subscription of the first journal. dic1.CollectionCountChanged += new CollectionHandler(journal1.CollectionCountChanged); dic1.CollectionReferenceChanged += new CollectionHandler(journal1.CollectionReferenceChanged); // Subscription of the second journal. dic1.CollectionReferenceChanged += new CollectionHandler(journal2.CollectionReferenceChanged); dic2.CollectionReferenceChanged += new CollectionHandler(journal2.CollectionReferenceChanged); // Generating a random state object. State rnd = MyDicDemo.RandomState(); // Adding the elem to the 1st collection. dic1.Add(rnd.Name.Length, rnd); // Removing an object from the 1st collection. dic1.Remove(0); // Changing a value int the 1st collection. dic1.Remove(rnd); DicPoint <int, State> dicPointBuf = new DicPoint <int, State>(rnd.Name.Length, rnd); dic1[0] = dicPointBuf; // Adding the elem to the 2nd collection. dic2.Add(rnd.Name.Length, rnd); // Removing an object from the 2nd collection. dic2.Remove(0); // Changing a value int the 2nd collection. dic2.Remove(rnd); dicPointBuf = new DicPoint <int, State>(rnd.Name.Length, rnd); dic2[0] = dicPointBuf; Console.WriteLine(journal1.ToString()); Console.WriteLine(journal2.ToString()); Console.WriteLine("Press ENTER to continue"); Console.ReadLine(); }
// Removing an object with the wanted value (parameter). public bool Remove(object value) { // Index of the element. int ind = -1; if (Values != null) { // Getting the index. for (int i = 0; i < Values.Length; i++) { if (Values[i].Equals((T)value)) { ind = i; break; } } } // If the elemet exists. if (ind > -1) { // Getting the key of the object. object key = Keys[ind]; // Getting the index og the object. int index = GetIndex(key); // Deleting it from the Keys. DelFromKeys(ind); // Deleting it from the Values. DelFromValues(ind); DicPoint <K, T> current = Table[index]; // IF it's the first in the list. if (current.value.Equals((T)value)) { Table[index] = null; Count--; } else { // Looking till the end of the list. while (current.next != null) { // If found. if (current.next.value.Equals((T)value)) { current.next = current.next.next; Count--; return(true); } current = current.next; } } return(true); } else { return(false); } }