// Function to show the dictionary. public static void ShowDic(MyDictionary <int, AbstrState> 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, AbstrState> 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 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]; } for (int i = 0; i < dic.Keys.Length; i++) { this.Keys[i] = dic.Keys[i]; } 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; }
// 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); } }