Hash join: Difference between revisions

no edit summary
m (→‎{{header|Sidef}}: modified code to work with Sidef 2.10)
No edit summary
Line 1,508:
[[28, "Alan"], ["Alan", "Zombies"]]
[[28, "Glory"], ["Glory", "Buffy"]]
</pre>
 
=={{header|Rust}}==
<lang rust>
use std::collections::HashMap;
 
fn main() {
let table_a = vec![
(27, "Jonah"), (18, "Alan"), (28, "Glory"),
(18, "Popeye"), (28, "Alan")
];
let table_b = vec![
("Jonah", "Whales"), ("Jonah", "Spiders"), ("Alan", "Ghosts"),
("Alan", "Zombies"), ("Glory", "Buffy")
];
// hash phase
let mut h = HashMap::new();
for (i, a) in table_a.iter().enumerate() {
h.entry(a.1).or_insert(vec![]).push(i);
}
// join phase
for b in table_b {
for i in h.get(b.0).unwrap_or(&vec![]) {
let a = table_a.get(*i).unwrap();
println!("{:?} {:?}", a, b);
}
}
}
</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>
 
Anonymous user