home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
pd6.lzh
/
TST
/
tree_sort.tst
< prev
Wrap
Text File
|
1989-12-21
|
3KB
|
119 lines
.( Loading Tree Sort benchmark...) cr
\ A classical benchmark of an O(log n) algorithm; Tree Sort
\
\ Part of the programs gathered by John Hennessy for the MIPS
\ RISC project at Stanford. Translated to forth by Matin Freamen,
\ Johns Hopkins University/Applied Physics Laboratory.
4 constant cell
: cells ( n -- m) cell * ;
: align ( -- ) here cell 1- and allot ;
: exchange ( x y -- ) dup @ rot dup @ >r ! r> swap ! ;
variable seed
: initiate-seed ( -- ) 74755 seed ! ;
: random ( -- n ) seed @ 1309 * 13849 + 65535 and dup seed ! ;
\ These structure access words were originally developed by
\ at JHU/APL by Ben Ballard and John Hayes
\ Structure access words
\ Examples of use:
\ structure foo \ declare a structure named foo
\ wrd: .thing1 \ with a one word field named .thing1
\ 2 wrds: .thing2 \ and a two word field named .thing2
\ endstructure
\
\ structure foobar \ another structure
\ wrd: .thing
\ foo struct: .blah \ nested structure
\ endstructure
\
\ foobar makestruct test \ allocate space for a structure instance
\ 1234 test .blah .thing1 ! \ access structure
: structure ( --- structure offset0)
create
here 0 , 0
does> ( structure -- size)
@ ;
: struct: ( offset1 size --- offset2)
create
over , +
does> ( structure field -- field-addr)
@ + ;
: wrds: ( offset1 size --- offset2) cells struct: ;
: wrd: ( offset1 --- offset2) cell struct: ;
: endstructure ( structure size --- ) swap ! ;
: makestruct ( size --- ) create allot ;
: malloc ( structure -- instance) here swap allot ;
\ The Tree Sort definitions:
structure node ( -- )
wrd: .left
wrd: .right
wrd: .val
endstructure
5000 constant tree-size
variable tree
: create-node ( n t -- )
node malloc dup >r swap !
r@ .val !
nil r@ .left !
nil r> .right ! ;
: insert-node ( n t -- )
over over .val @ >
if dup .left @ nil =
if over over .left create-node
else over over .left @ recurse then
else over over .val @ <
if dup .right @ nil =
if over over .right create-node
else over over .right @ recurse then
then
then
drop drop ;
: verify-tree ( t -- f)
true >r dup .left @ nil = not
if dup .left @ .val @ over .val @ > not
if r> drop false >r
else dup .left @ recurse r> and >r then
then
dup .right @ nil = not
if dup .right @ .val @ over .val @ < not
if r> drop false >r
else dup .right @ recurse r> and >r then
then
drop r> ;
: dump-tree ( t -- )
dup nil = not
if dup .right @ recurse
dup .val @ .
dup .left @ recurse
then
drop ;
: tree-sort ( -- )
initiate-seed
random tree create-node
tree @
tree-size 0 do
random over insert-node
loop
verify-tree not abort" trees: wrong result" ;
forth only