Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -14,4 +14,4 @@
|
|||
(loop for (i r) in *table-B* do
|
||||
(let ((val (car (gethash i *hash-table*))))
|
||||
(loop for (a b) in val do
|
||||
(format t "{~a ~a} {~a ~a}~%" a b i r))))
|
||||
(format t "{~a ~a} {~a ~a}~%" a b i r))))
|
||||
|
|
|
|||
|
|
@ -9,10 +9,10 @@ RightRec := RECORD
|
|||
STRING6 Name;
|
||||
STRING7 Nemesis;
|
||||
END;
|
||||
|
||||
|
||||
RightFile := DATASET([{'Jonah','Whales'},{'Jonah','Spiders'},{'Alan','Ghosts'},{'Alan','Zombies'},{'Glory','Buffy'}],
|
||||
RightRec);
|
||||
|
||||
|
||||
HashJoin := JOIN(LeftFile,RightFile,Left.Name = RIGHT.Name,HASH);
|
||||
|
||||
HashJoin;
|
||||
|
|
|
|||
|
|
@ -8,9 +8,9 @@
|
|||
(define (table-hash table source key-proc )
|
||||
(local-make-store table)
|
||||
(for ((r source))
|
||||
(local-put-value
|
||||
(key-proc r)
|
||||
(append (list r) (local-get-value (key-proc r) table)) table)))
|
||||
(local-put-value
|
||||
(key-proc r)
|
||||
(append (list r) (local-get-value (key-proc r) table)) table)))
|
||||
|
||||
;; build the two tables
|
||||
(define-syntax-rule (second record) (cadr record))
|
||||
|
|
@ -22,6 +22,6 @@
|
|||
|
||||
;; join
|
||||
(for* ((k (local-keys 'AGES))
|
||||
(a (local-get-value k 'AGES))
|
||||
(n (local-get-value k 'NEMESIS)))
|
||||
(writeln a n))
|
||||
(a (local-get-value k 'AGES))
|
||||
(n (local-get-value k 'NEMESIS)))
|
||||
(writeln a n))
|
||||
|
|
|
|||
34
Task/Hash-join/Emacs-Lisp/hash-join.el
Normal file
34
Task/Hash-join/Emacs-Lisp/hash-join.el
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
(defun make-multi-map (rows)
|
||||
(let ((multi-map nil))
|
||||
(cl-loop for row in rows do
|
||||
(let* ((name (car row))
|
||||
(name-list (assoc name multi-map)))
|
||||
(if name-list
|
||||
(nconc name-list (list row))
|
||||
(progn
|
||||
(add-to-list 'multi-map (list name row) 't) ) ) ) )
|
||||
multi-map) )
|
||||
|
||||
(defun join-tables (table1 table2)
|
||||
(let ((multi-map (make-multi-map table2))
|
||||
(result-table '()))
|
||||
(cl-loop for row in table1 do
|
||||
(let ((multi-rc (assoc (cdr row) multi-map)))
|
||||
(when multi-rc
|
||||
(cl-loop for multi-line in (cdr multi-rc) do
|
||||
(add-to-list 'result-table
|
||||
(list (car row) (cdr row) (car multi-line) (cdr multi-line))
|
||||
't)))))
|
||||
result-table))
|
||||
|
||||
(let ((table1 '((27 . "Jonah")
|
||||
(18 . "Alan")
|
||||
(28 . "Glory")
|
||||
(18 . "Popeye")
|
||||
(28 . "Alan")))
|
||||
(table2 '(("Jonah" . "Whales")
|
||||
("Jonah" . "Spiders")
|
||||
("Alan" . "Ghosts")
|
||||
("Alan" . "Zombies")
|
||||
("Glory" . "Buffy"))))
|
||||
(message "%s" (join-tables table1 table2)) )
|
||||
|
|
@ -14,4 +14,4 @@ dict_append( {Key, Value}, Acc ) -> dict:append( Value, {Key, Value}, Acc ).
|
|||
dict_find( {Key, Value}, Dict ) -> dict_find( dict:find(Key, Dict), Key, Value ).
|
||||
|
||||
dict_find( error, _Key, _Value ) -> [];
|
||||
dict_find( {ok, Values}, Key, Value ) -> [{X, {Key, Value}} || X <- Values].
|
||||
dict_find( {ok, Values}, Key, Value ) -> [{X, {Key, Value}} || X <- Values].
|
||||
|
|
|
|||
|
|
@ -31,5 +31,5 @@ def hashJoin(table1; key1; table2; key2):
|
|||
($row[key2]|h) as $key
|
||||
| if $hash|has($key) then
|
||||
reduce $hash[$key][] as $r (.; . + [ $row + $r ] )
|
||||
else . end)
|
||||
else . end)
|
||||
;
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ def hashJoinArrays(table1; index1; table2; index2):
|
|||
($row[index2]|h) as $key
|
||||
| if $hash|has($key) then
|
||||
reduce $hash[$key][] as $r
|
||||
(.;
|
||||
. + [ $r + $row[0:index2] + $row[index2+1:] ] )
|
||||
else . end)
|
||||
(.;
|
||||
. + [ $r + $row[0:index2] + $row[index2+1:] ] )
|
||||
else . end)
|
||||
;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@ using DataFrames
|
|||
A = DataFrame(Age = [27, 18, 28, 18, 28], Name = ["Jonah", "Alan", "Glory", "Popeye", "Alan"])
|
||||
B = DataFrame(Name = ["Jonah", "Jonah", "Alan", "Alan", "Glory"],
|
||||
Nemesis = ["Whales", "Spiders", "Ghosts", "Zombies", "Buffy"])
|
||||
AB = join(A, B, on = :Name)
|
||||
AB = innerjoin(A, B, on = :Name)
|
||||
|
||||
@show A B AB
|
||||
|
|
|
|||
|
|
@ -5,8 +5,8 @@ function hashJoin($table1, $index1, $table2, $index2) {
|
|||
$h[$s[$index1]][] = $s;
|
||||
// join phase
|
||||
foreach ($table2 as $r)
|
||||
foreach ($h[$r[$index2]] as $s)
|
||||
$result[] = array($s, $r);
|
||||
foreach ($h[$r[$index2]] as $s)
|
||||
$result[] = array($s, $r);
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,11 +5,11 @@ sub hashJoin {
|
|||
my %h;
|
||||
# hash phase
|
||||
foreach my $s (@$table1) {
|
||||
push @{ $h{$s->[$index1]} }, $s;
|
||||
push @{ $h{$s->[$index1]} }, $s;
|
||||
}
|
||||
# join phase
|
||||
map { my $r = $_;
|
||||
map [$_, $r], @{ $h{$r->[$index2]} }
|
||||
map [$_, $r], @{ $h{$r->[$index2]} }
|
||||
} @$table2;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,17 +4,17 @@
|
|||
\def\tabB{Jonah:Whales,Jonah:Spiders,Alan:Ghosts,Alan:Zombies,Glory:Buffy}
|
||||
\def\mergejoin{\tabjoin{}\expandafter\mergejoini\tabA,\quark:\quark,}
|
||||
\def\mergejoini#1:#2,{%
|
||||
\ifx\quark#1\the\tabjoin
|
||||
\else
|
||||
\def\mergejoinii##1,#2:##2,{%
|
||||
\ifx\quark##2\else
|
||||
\tabjoin\expandafter{\the\tabjoin#1 : #2 : ##2\par}%
|
||||
\expandafter\mergejoinii\expandafter,%
|
||||
\fi
|
||||
}%
|
||||
\expandafter\mergejoinii\expandafter,\tabB,#2:\quark,%
|
||||
\expandafter\mergejoini
|
||||
\fi
|
||||
\ifx\quark#1\the\tabjoin
|
||||
\else
|
||||
\def\mergejoinii##1,#2:##2,{%
|
||||
\ifx\quark##2\else
|
||||
\tabjoin\expandafter{\the\tabjoin#1 : #2 : ##2\par}%
|
||||
\expandafter\mergejoinii\expandafter,%
|
||||
\fi
|
||||
}%
|
||||
\expandafter\mergejoinii\expandafter,\tabB,#2:\quark,%
|
||||
\expandafter\mergejoini
|
||||
\fi
|
||||
}
|
||||
\mergejoin
|
||||
\bye
|
||||
|
|
|
|||
45
Task/Hash-join/Pluto/hash-join.pluto
Normal file
45
Task/Hash-join/Pluto/hash-join.pluto
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
local fmt = require "fmt"
|
||||
|
||||
class A
|
||||
function __construct(public age, public name)
|
||||
end
|
||||
end
|
||||
|
||||
class B
|
||||
function __construct(public character, public nemesis)
|
||||
end
|
||||
end
|
||||
|
||||
local tableA = {
|
||||
new A(27, "Jonah"), new A(18, "Alan"), new A(28, "Glory"),
|
||||
new A(18, "Popeye"), new A(28, "Alan")
|
||||
}
|
||||
|
||||
local tableB = {
|
||||
new B("Jonah", "Whales"), new B("Jonah", "Spiders"), new B("Alan", "Ghosts"),
|
||||
new B("Alan", "Zombies"), new B("Glory", "Buffy")
|
||||
}
|
||||
|
||||
local h = {}
|
||||
local i = 1
|
||||
for tableA as a do
|
||||
local n = h[a.name]
|
||||
if n then
|
||||
n:insert(i)
|
||||
else
|
||||
h[a.name] = {i}
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
|
||||
print("Age Name Character Nemesis")
|
||||
print("--- ----- --------- -------")
|
||||
for tableB as b do
|
||||
local c = h[b.character]
|
||||
if c then
|
||||
for c as j do
|
||||
local t = tableA[j]
|
||||
fmt.print("%3d %-5s %-9s %s", t.age, t.name, b.character, b.nemesis)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
% Name/Age
|
||||
person_age('Jonah', 27).
|
||||
person_age('Alan', 18).
|
||||
person_age('Glory', 28).
|
||||
person_age('Popeye', 18).
|
||||
person_age('Alan', 28).
|
||||
% Name/Age
|
||||
person_age('Jonah', 27).
|
||||
person_age('Alan', 18).
|
||||
person_age('Glory', 28).
|
||||
person_age('Popeye', 18).
|
||||
person_age('Alan', 28).
|
||||
|
||||
% Character/Nemesis
|
||||
character_nemisis('Jonah', 'Whales').
|
||||
|
|
@ -13,8 +13,8 @@ character_nemisis('Alan', 'Zombies').
|
|||
character_nemisis('Glory', 'Buffy').
|
||||
|
||||
join_and_print :-
|
||||
format('Age\tName\tCharacter\tNemisis\n\n'),
|
||||
forall(
|
||||
(person_age(Person, Age), character_nemisis(Person, Nemesis)),
|
||||
format('~w\t~w\t~w\t\t~w\n', [Age, Person, Person, Nemesis])
|
||||
).
|
||||
format('Age\tName\tCharacter\tNemisis\n\n'),
|
||||
forall(
|
||||
(person_age(Person, Age), character_nemisis(Person, Nemesis)),
|
||||
format('~w\t~w\t~w\t\t~w\n', [Age, Person, Person, Nemesis])
|
||||
).
|
||||
|
|
|
|||
35
Task/Hash-join/Rebol/hash-join.rebol
Normal file
35
Task/Hash-join/Rebol/hash-join.rebol
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: Hash join"
|
||||
file: %Hash_join.r3
|
||||
url: https://rosettacode.org/wiki/Hash_join
|
||||
]
|
||||
|
||||
hash-join: function [t1 t2] [
|
||||
result: copy []
|
||||
foreach [age name] t1 [
|
||||
pos: t2
|
||||
while [pos: find/skip pos name 2][
|
||||
repend result [age name pos/1 pos/2]
|
||||
pos: skip pos 2
|
||||
]
|
||||
]
|
||||
new-line/skip result true 4
|
||||
]
|
||||
|
||||
table1: [
|
||||
27 "Jonah"
|
||||
18 "Alan"
|
||||
28 "Glory"
|
||||
18 "Popeye"
|
||||
28 "Alan"
|
||||
]
|
||||
|
||||
table2: [
|
||||
"Jonah" "Whales"
|
||||
"Jonah" "Spiders"
|
||||
"Alan" "Ghosts"
|
||||
"Alan" "Zombies"
|
||||
"Glory" "Buffy"
|
||||
]
|
||||
|
||||
print hash-join table1 table2
|
||||
|
|
@ -4,17 +4,17 @@ hTable = []
|
|||
Qtable = []
|
||||
|
||||
for a in table1
|
||||
h = hashing(a[2])
|
||||
add(htable,[h , a])
|
||||
h = hashing(a[2])
|
||||
add(htable,[h , a])
|
||||
next
|
||||
|
||||
for b in table2
|
||||
h = hashing(b[1])
|
||||
for sh in htable
|
||||
if sh[1] = h
|
||||
add(qtable, sh[2] + b[2])
|
||||
ok
|
||||
next
|
||||
h = hashing(b[1])
|
||||
for sh in htable
|
||||
if sh[1] = h
|
||||
add(qtable, sh[2] + b[2])
|
||||
ok
|
||||
next
|
||||
next
|
||||
|
||||
print(qtable)
|
||||
|
|
@ -23,20 +23,20 @@ print(qtable)
|
|||
|
||||
func print lst
|
||||
see "---------------------------------------------------
|
||||
Age | Name || Name | Nemesis
|
||||
Age | Name || Name | Nemesis
|
||||
---------------------------------------------------
|
||||
"
|
||||
for l in lst
|
||||
see string(l[1]) + char(9) + "| " + l[2] + copy(char(9),2) + "|| " + l[2] + " " + char(9) + "| " + l[3] + nl
|
||||
see string(l[1]) + char(9) + "| " + l[2] + copy(char(9),2) + "|| " + l[2] + " " + char(9) + "| " + l[3] + nl
|
||||
next
|
||||
|
||||
func Hashing str
|
||||
r = 0
|
||||
if len(str) > 4
|
||||
r = (ascii(str[1]) + ascii(str[len(str)]) + ascii(str[ceil(len(str) * 0.25)]) + ascii(str[ceil(len(str) * 0.75)]))
|
||||
r = (ascii(str[1]) + ascii(str[len(str)]) + ascii(str[ceil(len(str) * 0.25)]) + ascii(str[ceil(len(str) * 0.75)]))
|
||||
else
|
||||
for s in str
|
||||
r += ascii(s)
|
||||
next
|
||||
for s in str
|
||||
r += ascii(s)
|
||||
next
|
||||
ok
|
||||
return r
|
||||
|
|
|
|||
|
|
@ -17,9 +17,9 @@ sqliteconnect #mem, ":memory:"
|
|||
|
||||
#mem execute("SELECT *,t_age.name FROM t_age LEFT JOIN t_name ON t_name.name = t_age.name")
|
||||
WHILE #mem hasanswer()
|
||||
#row = #mem #nextrow()
|
||||
age = #row age()
|
||||
name$ = #row name$()
|
||||
nemesis$ = #row nemesis$()
|
||||
#row = #mem #nextrow()
|
||||
age = #row age()
|
||||
name$ = #row name$()
|
||||
nemesis$ = #row nemesis$()
|
||||
print age;" ";name$;" ";nemesis$
|
||||
WEND
|
||||
|
|
|
|||
|
|
@ -4,24 +4,24 @@ package require Tcl 8.6
|
|||
proc joinTables {tableA a tableB b} {
|
||||
# Optimisation: if the first table is longer, do in reverse order
|
||||
if {[llength $tableB] < [llength $tableA]} {
|
||||
return [lmap pair [joinTables $tableB $b $tableA $a] {
|
||||
lreverse $pair
|
||||
}]
|
||||
return [lmap pair [joinTables $tableB $b $tableA $a] {
|
||||
lreverse $pair
|
||||
}]
|
||||
}
|
||||
|
||||
foreach value $tableA {
|
||||
lappend hashmap([lindex $value $a]) [lreplace $value $a $a]
|
||||
#dict version# dict lappend hashmap [lindex $value $a] [lreplace $value $a $a]
|
||||
lappend hashmap([lindex $value $a]) [lreplace $value $a $a]
|
||||
#dict version# dict lappend hashmap [lindex $value $a] [lreplace $value $a $a]
|
||||
}
|
||||
set result {}
|
||||
foreach value $tableB {
|
||||
set key [lindex $value $b]
|
||||
if {![info exists hashmap($key)]} continue
|
||||
#dict version# if {![dict exists $hashmap $key]} continue
|
||||
foreach first $hashmap($key) {
|
||||
#dict version# foreach first [dict get $hashmap $key]
|
||||
lappend result [list {*}$first $key {*}[lreplace $value $b $b]]
|
||||
}
|
||||
set key [lindex $value $b]
|
||||
if {![info exists hashmap($key)]} continue
|
||||
#dict version# if {![dict exists $hashmap $key]} continue
|
||||
foreach first $hashmap($key) {
|
||||
#dict version# foreach first [dict get $hashmap $key]
|
||||
lappend result [list {*}$first $key {*}[lreplace $value $b $b]]
|
||||
}
|
||||
}
|
||||
return $result
|
||||
}
|
||||
|
|
|
|||
31
Task/Hash-join/V-(Vlang)/hash-join.v
Normal file
31
Task/Hash-join/V-(Vlang)/hash-join.v
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
struct Table_a {
|
||||
value int
|
||||
key string
|
||||
}
|
||||
|
||||
struct Table_b {
|
||||
key string
|
||||
value string
|
||||
}
|
||||
|
||||
fn main() {
|
||||
table_a := [Table_a{27, "Jonah"}, Table_a{18, "Alan"}, Table_a{28, "Glory"},
|
||||
Table_a{18, "Popeye"}, Table_a{28, "Alan"}]
|
||||
|
||||
table_b := [Table_b{"Jonah", "Whales"}, Table_b{"Jonah", "Spiders"}, Table_b{"Alan", "Ghosts"},
|
||||
Table_b{"Alan", "Zombies"}, Table_b{"Glory", "Buffy"}]
|
||||
|
||||
mut h := map[string][]int{}
|
||||
|
||||
// hash phase
|
||||
for i, r in table_a {
|
||||
h[r.key] << i
|
||||
}
|
||||
|
||||
// join phase
|
||||
for x in table_b {
|
||||
for a in h[x.key] {
|
||||
println("${table_a[a].value} ${table_a[a].key}, ${x.key} ${x.value}")
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Task/Hash-join/VBScript/hash-join.vbs
Normal file
30
Task/Hash-join/VBScript/hash-join.vbs
Normal 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
|
||||
|
|
@ -13,7 +13,7 @@ n = 100
|
|||
FOR i = 1 TO n
|
||||
INSERT INTO people_ids (id) VALUES (i)
|
||||
INSERT INTO nem_ids (id) VALUES (i)
|
||||
ENDFOR
|
||||
ENDFOR
|
||||
|
||||
CREATE CURSOR people (age I, name V(16), id I)
|
||||
INDEX ON id TAG id COLLATE "Machine"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ nameNemesis:=T("Jonah","Whales", "Jonah","Spiders", "Alan","Ghosts",
|
|||
fcn addAN(age,name,d){ // keys are names, values are ( (age,...),() )
|
||||
if (r:=d.find(name)) d[name] = T(r[0].append(age),r[1]);
|
||||
else d.add(name,T(T(age),T));
|
||||
d // return d so pump will use that as result for assignment
|
||||
d // return d so pump will use that as result for assignment
|
||||
}
|
||||
fcn addNN(name,nemesis,d){ // values-->( (age,age...), (nemesis,...) )
|
||||
if (r:=d.find(name)){
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue