/// <summary> /// part 4: implement the Set like HashDictionary /// </summary> public void DictHashMainMethod() { DictHashSet <string> dict = new DictHashSet <string>(); dict.Add("John"); dict.Add("Tolle"); dict.Add("Dale"); dict.Contain("Dale"); dict.Add("Robert"); dict.Add("Noah"); foreach (var item in dict) { Console.Write(item.Value + " "); } Console.WriteLine(dict.Count); Console.WriteLine("Is Dale Removed ? " + (dict.Remove("Dale") ? "Yes!" : "No")); foreach (var item in dict) { Console.Write(item.Value + " "); } Console.WriteLine(dict.Count); DictHashSet <string> dict2 = new DictHashSet <string>(); dict2.Add("Harry"); dict2.Add("Karish"); dict2.Add("Ali"); dict2.Add("Shahzil"); foreach (var item in dict2) { Console.Write(item.Value + " "); } Console.WriteLine(dict.Count); DictHashSet <string> allDict = new DictHashSet <string>(); allDict.UnionWith(dict); allDict.UnionWith(dict2); Console.Write("Union: "); foreach (var item in allDict) { Console.Write(item.Value + " "); } Console.WriteLine(allDict.Count); DictHashSet <string> sameDict = new DictHashSet <string>(dict); sameDict.IntersectWith(dict2); Console.Write("Intersect: "); foreach (KeyValuePairSet <string> item in sameDict) { Console.Write(item.Value + " "); } Console.WriteLine(sameDict.Count > 0 ? sameDict.Count.ToString() : "false"); }
/// <summary> /// Put element together with is common in any two table /// </summary> /// <param name="other">other table you want to compare with</param> public void IntersectWith(DictHashSet <T> other) { if (this.size > 0) // if set table have element { List <T> objList = new List <T>(); for (int i = 0; i < other.setTable.Length; i++) { List <KeyValuePairSet <T> > otherObj = other.setTable[i]; if (otherObj != null) { foreach (KeyValuePairSet <T> otherEntry in otherObj) { T value = otherEntry.Value; if (this.Contain(value)) // place same element in the objlist { objList.Add(value); } } } } this.Clear(); // clear every other element in the table foreach (var value in objList) { this.Add(value); // add element in the current table } } else { Console.WriteLine("Error! Set is Empty. Please insert Set table in Object"); } }
/// <summary> /// put each elements together of any two table /// </summary> /// <param name="other">other table you want to union with</param> public void UnionWith(DictHashSet <T> other) { for (int i = 0; i < other.setTable.Length; i++) { List <KeyValuePairSet <T> > otherObj = other.setTable[i]; if (otherObj != null) { foreach (KeyValuePairSet <T> otherEntry in otherObj) { T value = otherEntry.Value; if (!this.Contain(value)) { this.Add(value); } } } } }
//For intersection purpose public DictHashSet(DictHashSet <T> other) { this.setTable = other.setTable; this.initialCapacity = other.initialCapacity; this.size = other.size; }