Hash join: Difference between revisions

Content added Content deleted
No edit summary
Line 507: Line 507:
{{28,"Alan"},{"Alan","Zombies"}},
{{28,"Alan"},{"Alan","Zombies"}},
{{28,"Glory"},{"Glory","Buffy"}}]
{{28,"Glory"},{"Glory","Buffy"}}]
</pre>

=={{header|Forth}}==
{{works with|Forth}}
Works with any ANS Forth

Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
<lang forth>
include FMS-SI.f
include FMS-SILib.f

: obj ( addr len -- obj )
heap> string+ dup >r !: r> ;

hash-table r 1 r init
s" Jonah" obj s" Whales" r insert:
s" Jonah" obj s" Spiders" r insert:
s" Alan" obj s" Ghosts" r insert:
s" Alan" obj s" Zombies" r insert:
s" Glory" obj s" Buffy" r insert:

o{ o{ 27 'Jonah' }
o{ 18 'Alan' }
o{ 28 'Glory' }
o{ 18 'Popeye' }
o{ 28 'Alan' } } value s
0 value str
0 value val
0 value cur

: probe ( node -- ) dup val@: str =:
if cur if cr val . dup val@: p: space key@: p: false to cur
else space key@: p:
then
else drop
then ;

: probe ( node -- ) dup val@: str =:
if cur if cr val . dup val@: p: space key@: p: false to cur
else space key@: p:
then exit
then drop ;

\ Step through each element of list s.
\ Probe hash-table r for hits on the Name.
\ For each hit show the Age and Name once, but show every Nemesis
: join
begin
s each:
while
true to cur
dup 0 swap at@: to val 1 swap at: ( obj ) to str ['] probe r iterate:
repeat ;
join
s <free \ free the list memory
r free2: \ free the hash-table memory
</lang>
{{out}}
<pre>
27 Jonah Whales Spiders
18 Alan Zombies Ghosts
28 Glory Buffy
28 Alan Zombies Ghosts
</pre>
</pre>