Hash join: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Swift)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 467:
(27.Jonah.Whales)
(27.Jonah.Spiders)</pre>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
 
using tab_t = std::vector<std::vector<std::string>>;
tab_t tab1 {
// Age Name
{"27", "Jonah"}
, {"18", "Alan"}
, {"28", "Glory"}
, {"18", "Popeye"}
, {"28", "Alan"}
};
 
tab_t tab2 {
// Character Nemesis
{"Jonah", "Whales"}
, {"Jonah", "Spiders"}
, {"Alan", "Ghosts"}
, {"Alan", "Zombies"}
, {"Glory", "Buffy"}
};
 
std::ostream& operator<<(std::ostream& o, const tab_t& t) {
for(size_t i = 0; i < t.size(); ++i) {
o << i << ":";
for(const auto& e : t[i])
o << '\t' << e;
o << std::endl;
}
return o;
}
 
tab_t Join(const tab_t& a, size_t columna, const tab_t& b, size_t columnb) {
std::unordered_multimap<std::string, size_t> hashmap;
// hash
for(size_t i = 0; i < a.size(); ++i) {
hashmap.insert(std::make_pair(a[i][columna], i));
}
// map
tab_t result;
for(size_t i = 0; i < b.size(); ++i) {
auto range = hashmap.equal_range(b[i][columnb]);
for(auto it = range.first; it != range.second; ++it) {
tab_t::value_type row;
row.insert(row.end() , a[it->second].begin() , a[it->second].end());
row.insert(row.end() , b[i].begin() , b[i].end());
result.push_back(std::move(row));
}
}
return result;
}
 
int main(int argc, char const *argv[])
{
using namespace std;
int ret = 0;
cout << "Table A: " << endl << tab1 << endl;
cout << "Table B: " << endl << tab2 << endl;
auto tab3 = Join(tab1, 1, tab2, 0);
cout << "Joined tables: " << endl << tab3 << endl;
return ret;
}
 
</lang>
{{out}}
<pre>Table A:
0: 27 Jonah
1: 18 Alan
2: 28 Glory
3: 18 Popeye
4: 28 Alan
 
Table B:
0: Jonah Whales
1: Jonah Spiders
2: Alan Ghosts
3: Alan Zombies
4: Glory Buffy
 
Joined tables:
0: 27 Jonah Jonah Whales
1: 27 Jonah Jonah Spiders
2: 28 Alan Alan Ghosts
3: 18 Alan Alan Ghosts
4: 28 Alan Alan Zombies
5: 18 Alan Alan Zombies
6: 28 Glory Glory Buffy
</pre>
 
 
=={{header|C sharp}}==
Line 673 ⟶ 580:
Age: 28, Name: Alan, Nemesis: Ghosts
Age: 28, Name: Alan, Nemesis: Zombies
</pre>
 
=={{header|C++}}==
<lang cpp>#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
 
using tab_t = std::vector<std::vector<std::string>>;
tab_t tab1 {
// Age Name
{"27", "Jonah"}
, {"18", "Alan"}
, {"28", "Glory"}
, {"18", "Popeye"}
, {"28", "Alan"}
};
 
tab_t tab2 {
// Character Nemesis
{"Jonah", "Whales"}
, {"Jonah", "Spiders"}
, {"Alan", "Ghosts"}
, {"Alan", "Zombies"}
, {"Glory", "Buffy"}
};
 
std::ostream& operator<<(std::ostream& o, const tab_t& t) {
for(size_t i = 0; i < t.size(); ++i) {
o << i << ":";
for(const auto& e : t[i])
o << '\t' << e;
o << std::endl;
}
return o;
}
 
tab_t Join(const tab_t& a, size_t columna, const tab_t& b, size_t columnb) {
std::unordered_multimap<std::string, size_t> hashmap;
// hash
for(size_t i = 0; i < a.size(); ++i) {
hashmap.insert(std::make_pair(a[i][columna], i));
}
// map
tab_t result;
for(size_t i = 0; i < b.size(); ++i) {
auto range = hashmap.equal_range(b[i][columnb]);
for(auto it = range.first; it != range.second; ++it) {
tab_t::value_type row;
row.insert(row.end() , a[it->second].begin() , a[it->second].end());
row.insert(row.end() , b[i].begin() , b[i].end());
result.push_back(std::move(row));
}
}
return result;
}
 
int main(int argc, char const *argv[])
{
using namespace std;
int ret = 0;
cout << "Table A: " << endl << tab1 << endl;
cout << "Table B: " << endl << tab2 << endl;
auto tab3 = Join(tab1, 1, tab2, 0);
cout << "Joined tables: " << endl << tab3 << endl;
return ret;
}
 
</lang>
{{out}}
<pre>Table A:
0: 27 Jonah
1: 18 Alan
2: 28 Glory
3: 18 Popeye
4: 28 Alan
 
Table B:
0: Jonah Whales
1: Jonah Spiders
2: Alan Ghosts
3: Alan Zombies
4: Glory Buffy
 
Joined tables:
0: 27 Jonah Jonah Whales
1: 27 Jonah Jonah Spiders
2: 28 Alan Alan Ghosts
3: 18 Alan Alan Ghosts
4: 28 Alan Alan Zombies
5: 18 Alan Alan Zombies
6: 28 Glory Glory Buffy
</pre>
 
Line 1,376 ⟶ 1,375:
[[28, Alan], [Alan, Zombies]]
[[28, Glory], [Glory, Buffy]]</pre>
 
 
=={{header|JavaScript}}==
Line 1,772 ⟶ 1,770:
((#(age 28) #(name "Glory") #(nemesis "Buffy"))))
</lang>
 
 
=={{header|M2000 Interpreter}}==
Line 2,121 ⟶ 2,118:
$VAR1 = [[28,'Glory'],['Glory','Buffy']];
</pre>
 
=={{header|Perl 6}}==
 
The <tt>.classify</tt> method returns a multimap represented as a <tt>Hash</tt> whose values are <tt>Array</tt>s.
 
{{works with|Rakudo|2016.07}}
<lang perl6>sub hash-join(@a, &a, @b, &b) {
my %hash := @b.classify(&b);
@a.map: -> $a {
|(%hash{a $a} // next).map: -> $b { [$a, $b] }
}
}
 
# Testing:
 
my @A =
[27, "Jonah"],
[18, "Alan"],
[28, "Glory"],
[18, "Popeye"],
[28, "Alan"],
;
 
my @B =
["Jonah", "Whales"],
["Jonah", "Spiders"],
["Alan", "Ghosts"],
["Alan", "Zombies"],
["Glory", "Buffy"],
;
 
.say for hash-join @A, *[1], @B, *[0];</lang>
 
{{out}}
<pre>[[27 Jonah] [Jonah Whales]]
[[27 Jonah] [Jonah Spiders]]
[[18 Alan] [Alan Ghosts]]
[[18 Alan] [Alan Zombies]]
[[28 Glory] [Glory Buffy]]
[[28 Alan] [Alan Ghosts]]
[[28 Alan] [Alan Zombies]]</pre>
 
=={{header|Phix}}==
Line 2,380 ⟶ 2,335:
28 Alan Alan Zombies
28 Alan Alan Ghosts</pre>
 
 
=={{header|plainTeX}}==
Line 2,595 ⟶ 2,549:
#(struct:AB "Alan" 28 "Zombies")
#(struct:AB "Glory" 28 "Buffy"))</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
 
The <tt>.classify</tt> method returns a multimap represented as a <tt>Hash</tt> whose values are <tt>Array</tt>s.
 
{{works with|Rakudo|2016.07}}
<lang perl6>sub hash-join(@a, &a, @b, &b) {
my %hash := @b.classify(&b);
@a.map: -> $a {
|(%hash{a $a} // next).map: -> $b { [$a, $b] }
}
}
 
# Testing:
 
my @A =
[27, "Jonah"],
[18, "Alan"],
[28, "Glory"],
[18, "Popeye"],
[28, "Alan"],
;
 
my @B =
["Jonah", "Whales"],
["Jonah", "Spiders"],
["Alan", "Ghosts"],
["Alan", "Zombies"],
["Glory", "Buffy"],
;
 
.say for hash-join @A, *[1], @B, *[0];</lang>
 
{{out}}
<pre>[[27 Jonah] [Jonah Whales]]
[[27 Jonah] [Jonah Spiders]]
[[18 Alan] [Alan Ghosts]]
[[18 Alan] [Alan Zombies]]
[[28 Glory] [Glory Buffy]]
[[28 Alan] [Alan Ghosts]]
[[28 Alan] [Alan Zombies]]</pre>
 
=={{header|REXX}}==
10,333

edits