Hash join: Difference between revisions
Content added Content deleted
(added php) |
(+ D entry) |
||
Line 182: | Line 182: | ||
{28 Glory} {Glory Buffy} |
{28 Glory} {Glory Buffy} |
||
</pre> |
</pre> |
||
=={{header|D}}== |
|||
{{trans|Python}} |
|||
<lang d>import std.stdio, std.typecons; |
|||
auto hashJoin(size_t index1, size_t index2, T1, T2) |
|||
(in T1[] table1, in T2[] table2) pure /*nothrow*/ |
|||
if (is(typeof(T1[index1]) == typeof(T2[index2]))) { |
|||
T1[][typeof(T1[index1])] h; |
|||
// Hash phase. |
|||
foreach (s; table1) |
|||
h[s[index1]] ~= s; |
|||
// Join phase. |
|||
Tuple!(T1, const T2)[] result; |
|||
foreach (r; table2) |
|||
foreach (s; h.get(r[index2], [])) |
|||
result ~= tuple(s, r); |
|||
return result; |
|||
} |
|||
void main() { |
|||
alias T = tuple; |
|||
immutable table1 = [T(27, "Jonah"), |
|||
T(18, "Alan"), |
|||
T(28, "Glory"), |
|||
T(18, "Popeye"), |
|||
T(28, "Alan")]; |
|||
immutable table2 = [T("Jonah", "Whales"), |
|||
T("Jonah", "Spiders"), |
|||
T("Alan", "Ghosts"), |
|||
T("Alan", "Zombies"), |
|||
T("Glory", "Buffy")]; |
|||
foreach (row; hashJoin!(1, 0)(table1, table2)) |
|||
writefln("(%s, %5s) (%5s, %7s)", row[0][], row[1][]); |
|||
}</lang> |
|||
{{out}} |
|||
<pre>(27, Jonah) (Jonah, Whales) |
|||
(27, Jonah) (Jonah, Spiders) |
|||
(18, Alan) ( Alan, Ghosts) |
|||
(28, Alan) ( Alan, Ghosts) |
|||
(18, Alan) ( Alan, Zombies) |
|||
(28, Alan) ( Alan, Zombies) |
|||
(28, Glory) (Glory, Buffy)</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |