/// <summary> /// Checks if hashtable2 has the hashtable1 key /// if it exists, add the value of that key from HashTable2 and Hashtabel1 to Hashtable3 /// if it doesn't exist, add only the key value pair from Hashtable1 to Hashtable3 along /// with the string "null" to indicate that Hashtable2 did not have that key /// </summary> /// <param name="ht1">HashTable1</param> /// <param name="ht2">HashTable2</param> /// <returns>HashTable3 with a left join</returns> public HashTable LeftJoinHashTable(HashTable ht1, HashTable ht2) { //instantiate a new hashtable to store the left join table HashTable ht3 = new HashTable(); for (int i = 0; i < ht1.HashArray.Length; i++) { if (ht1.HashArray[i] != null) { Console.WriteLine($"checking {ht1.HashArray[i].Key}"); if (ht2.Contains(ht1.HashArray[i].Key)) { //ht2 has ht1 key, so add it to ht3 ht3.Add(ht1.HashArray[i].Key, ht1.HashArray[i].Value); //add ht2's value to the same key ht3.Add(ht1.HashArray[i].Key, ht2.HashArray[i].Value); Console.WriteLine($"found a match for {ht1.HashArray[i].Key}"); } else if (!ht2.Contains(ht1.HashArray[i].Key)) { Console.WriteLine($"didn't find a match for {ht1.HashArray[i].Key}"); //ht2 does not have ht1 key, so add ht1's current key value pair ht3.Add(ht1.HashArray[i].Key, ht1.HashArray[i].Value); //also add ht1's key and a "null" to represent that ht2 did not have ht1's key ht3.Add(ht1.HashArray[i].Key, "null"); } } } return(ht3); }
static public string[][] LeftJoin(HashTable synonyms, HashTable antonyms) { List <string[]> result = new List <string[]>(); foreach (var b in synonyms.Buckets) { if (b != null) { LinkedListNode <string[]> current = b.First; while (current != null) { result.Add(new string[] { current.Value[0], current.Value[1], "NULL" }); current = current.Next; } } } foreach (var k in result) { if (antonyms.Contains(k[0])) { k[2] = antonyms.Get(k[0]); } } return(result.ToArray()); }
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); }
/// <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); }