public static List <string[]> LeftJoin(HashTable hashTableA, HashTable hashTableB) { List <string[]> results = new List <string[]>(); foreach (var bucket in hashTableA.Contents) { if (bucket != null) { foreach (var kvPair in bucket) { string[] result = new string[] { kvPair.Key, kvPair.Value, null }; results.Add(result); if (hashTableB.Contains(kvPair.Key)) { result[2] = hashTableB.Get(kvPair.Key); } } } } return(results); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); HashTable synonyms = new HashTable(5); synonyms.Add("fond", "enamored"); synonyms.Add("wrath", "anger"); synonyms.Add("diligent", "employed"); HashTable antonyms = new HashTable(5); antonyms.Add("fond", "averse"); antonyms.Add("wrath", "delight"); antonyms.Add("guide", "follow"); string[][] joined = LeftJoin(synonyms, antonyms); for (int i = 0; i < joined.Length; i++) { Console.WriteLine(joined[i][0] + joined[i][1] + joined[i][2]); } }
/// <summary> /// Call LeftJoinHashTable method of the HashTable class to try out solution /// </summary> public static void TryLeftJoin() { //instantiate new Hashtable ht1 and add contents HashTable ht1 = new HashTable(); ht1.Add("fond", "enamored"); ht1.Add("wrath", "anger"); ht1.Add("diligent", "employed"); ht1.Add("outfit", "garb"); ht1.Add("guide", "usher"); //instantiate new Hashtable ht2 and add contents HashTable ht2 = new HashTable(); ht2.Add("fond", "averse"); ht2.Add("wrath", "delight"); ht2.Add("diligent", "idle"); ht2.Add("guide", "follow"); ht2.Add("flow", "jam"); //pass the 2 hashtables to the method to left join them HashTable ht3 = ht1.LeftJoinHashTable(ht1, ht2); //log to the view to prove it was returned for (int i = 0; i < ht3.HashArray.Length; i++) { if (ht3.HashArray[i] != null) { Console.WriteLine($"{ht3.HashArray[i].Key} : {ht3.HashArray[i].Value}"); if (ht3.HashArray[i].Next != null) { //checks if there is another node at that key //if there is another node, output that //this means that there was another value for that key on the 2nd table Console.WriteLine($"{ht3.HashArray[i].Next.Value}"); } } } }
/// <summary> /// Left joins two HashTable<string> objects. /// </summary> /// <param name="leftTable"> /// HashTable<string>: the HashTable<string> to be used as the left table in the left join /// </param> /// <param name="rightTable"> /// HashTable<string>: the HashTable<string> to be used as the right table in the left join /// </param> /// <returns> /// HashTable<string>: a HashTable<LeftJoinNode>, containing the keys and values from leftTable and rightTable, left joined together /// </returns> public static HashTable <LeftJoinNode> LeftJoinHashTables(HashTable <string> leftTable, HashTable <string> rightTable) { HashTable <LeftJoinNode> leftJoin = new HashTable <LeftJoinNode>(); for (int i = 0; i < leftTable.HashMap.Length; i++) { if (leftTable.HashMap[i] != null) { var currNode = leftTable.HashMap[i].First; while (currNode != null) { LeftJoinNode newLeftJoinNode = new LeftJoinNode(currNode.Value.Value); if (rightTable.Contains(currNode.Value.Key)) { newLeftJoinNode.RightValue = rightTable.Get(currNode.Value.Key); } leftJoin.Add(currNode.Value.Key, newLeftJoinNode); currNode = currNode.Next; } } } return(leftJoin); }