Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,20 @@
defmodule Hash do
def join(table1, index1, table2, index2) do
h = Enum.group_by(table1, fn s -> elem(s, index1) end)
Enum.flat_map(table2, fn r ->
Enum.map(h[elem(r, index2)], fn s -> {s, r} end)
end)
end
end
table1 = [{27, "Jonah"},
{18, "Alan"},
{28, "Glory"},
{18, "Popeye"},
{28, "Alan"}]
table2 = [{"Jonah", "Whales"},
{"Jonah", "Spiders"},
{"Alan", "Ghosts"},
{"Alan", "Zombies"},
{"Glory", "Buffy"}]
Hash.join(table1, 1, table2, 0) |> Enum.each(&IO.inspect &1)

View file

@ -1,33 +1,32 @@
/*REXX pgm demonstrates the classic hash join algorithm for 2 relations.*/
S. = ; R. =
S.1 = 27 'Jonah' ; R.1 = 'Jonah Whales'
S.2 = 18 'Alan' ; R.2 = 'Jonah Spiders'
S.3 = 28 'Glory' ; R.3 = 'Alan Ghosts'
S.4 = 18 'Popeye' ; R.4 = 'Alan Zombies'
S.5 = 28 'Alan' ; R.5 = 'Glory Buffy'
hash.= /*initialize the hash table. */
do #=1 while S.#\==''; parse var S.# age name /*extract info*/
hash.name=hash.name # /*build a hash table entry. */
end /*#*/ /* [↑] REXX does the heavy work.*/
#=#-1 /*adjust for DO loop (#) overage.*/
do j=1 while R.j\=='' /*process a nemesis for a name. */
parse var R.j x nemesis /*extract name and it's nemesis. */
if hash.x=='' then do /*Not in hash? Then a new name.*/
#=#+1 /*bump the number of S entries. */
S.#=',' x /*add new name to the S table. */
hash.x=# /*add new name to the hash table.*/
end /* [↑] this DO isn't used today.*/
do k=1 for words(hash.x); _=word(hash.x,k) /*get pointer.*/
S._=S._ nemesis /*add nemesis──► applicable hash.*/
/*REXX program demonstrates the classic hash join algorithm for two relations.*/
S. = ; R. =
S.1 = 27 'Jonah' ; R.1 = 'Jonah Whales'
S.2 = 18 'Alan' ; R.2 = 'Jonah Spiders'
S.3 = 28 'Glory' ; R.3 = 'Alan Ghosts'
S.4 = 18 'Popeye' ; R.4 = 'Alan Zombies'
S.5 = 28 'Alan' ; R.5 = 'Glory Buffy'
hash.= /*initialize the hash table (array). */
do #=1 while S.#\==''; parse var S.# age name /*extract information*/
hash.name=hash.name # /*build a hash table entry with its idx*/
end /*#*/ /* [↑] REXX does the heavy work here. */
#=#-1 /*adjust for the DO loop (#) overage.*/
do j=1 while R.j\=='' /*process a nemesis for a name element.*/
parse var R.j x nemesis /*extract the name and its nemesis. */
if hash.x=='' then do; #=#+1 /*Not in hash? Then a new name; bump #*/
S.#=',' x /*add a new name to the S table. */
hash.x=# /* " " " " " " hash " */
end /* [↑] this DO isn't used today. */
do k=1 for words(hash.x); _=word(hash.x,k) /*get the pointer.*/
S._=S._ nemesis /*add the nemesis ──► applicable hash. */
end /*k*/
end /*j*/
_='' /*character used for separater. */
pad=left('',6-2) /*spacing used in hdr/sep/output.*/
say pad center('age',3) pad center('name',20) pad center('nemesis',30)
say pad center('',3) pad center('' ,20,_) pad center('' ,30,_)
_='' /*the character used for the separator.*/
pad=left('',6-2) /*spacing used in header and the output*/
say pad center('age',3) pad center('name',20 } pad center('nemesis',30 )
say pad center('',3) pad center('' ,20,_) pad center('' ,30,_)
do n=1 for #; parse var S.n age name nems /*get info. */
if nems=='' then iterate /*if no nemesis, then don't show.*/
say pad right(age,3) pad center(name,20) pad nems /*show an S.*/
do n=1 for #; parse var S.n age name nems /*get information.*/
if nems=='' then iterate /*No nemesis? Skip*/
say pad right(age,3) pad center(name,20) pad nems /*display an S. */
end /*n*/
/*stick a fork in it, we're done.*/
/*stick a fork in it, we're all done. */

View file

@ -0,0 +1,24 @@
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);
}
}
}

View file

@ -1,20 +1,19 @@
@(do
(defvar age-name '((27 Jonah)
(18 Alan)
(28 Glory)
(18 Popeye)
(28 Alan)))
(defvar age-name '((27 Jonah)
(18 Alan)
(28 Glory)
(18 Popeye)
(28 Alan)))
(defvar nemesis-name '((Jonah Whales)
(Jonah Spiders)
(Alan Ghosts)
(Alan Zombies)
(Glory Buffy)))
(defvar nemesis-name '((Jonah Whales)
(Jonah Spiders)
(Alan Ghosts)
(Alan Zombies)
(Glory Buffy)))
(defun hash-join (left left-key right right-key)
(let ((join-hash [group-by left-key left])) ;; hash phase
(append-each ((r-entry right)) ;; join phase
(collect-each ((l-entry [join-hash [right-key r-entry]]))
^(,l-entry ,r-entry)))))
(defun hash-join (left left-key right right-key)
(let ((join-hash [group-by left-key left])) ;; hash phase
(append-each ((r-entry right)) ;; join phase
(collect-each ((l-entry [join-hash [right-key r-entry]]))
^(,l-entry ,r-entry)))))
(format t "~s\n" [hash-join age-name second nemesis-name first]))
(format t "~s\n" [hash-join age-name second nemesis-name first])

View file

@ -0,0 +1,30 @@
Dim t_age(4,1)
t_age(0,0) = 27 : t_age(0,1) = "Jonah"
t_age(1,0) = 18 : t_age(1,1) = "Alan"
t_age(2,0) = 28 : t_age(2,1) = "Glory"
t_age(3,0) = 18 : t_age(3,1) = "Popeye"
t_age(4,0) = 28 : t_age(4,1) = "Alan"
Dim t_nemesis(4,1)
t_nemesis(0,0) = "Jonah" : t_nemesis(0,1) = "Whales"
t_nemesis(1,0) = "Jonah" : t_nemesis(1,1) = "Spiders"
t_nemesis(2,0) = "Alan" : t_nemesis(2,1) = "Ghosts"
t_nemesis(3,0) = "Alan" : t_nemesis(3,1) = "Zombies"
t_nemesis(4,0) = "Glory" : t_nemesis(4,1) = "Buffy"
Call hash_join(t_age,1,t_nemesis,0)
Sub hash_join(table_1,index_1,table_2,index_2)
Set hash = CreateObject("Scripting.Dictionary")
For i = 0 To UBound(table_1)
hash.Add i,Array(table_1(i,0),table_1(i,1))
Next
For j = 0 To UBound(table_2)
For Each key In hash.Keys
If hash(key)(index_1) = table_2(j,index_2) Then
WScript.StdOut.WriteLine hash(key)(0) & "," & hash(key)(1) &_
" = " & table_2(j,0) & "," & table_2(j,1)
End If
Next
Next
End Sub