home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tile-forth-2.1-base.tgz / tile-forth-2.1-base.tar / fsf / tile-forth / tst / tree-sort.tst < prev   
Text File  |  1991-09-14  |  3KB  |  121 lines

  1. .( Loading Tree Sort benchmark...) cr
  2.  
  3. \ A classical benchmark of an O(log n) algorithm; Tree Sort
  4. \
  5. \ Part of the programs gathered by John Hennessy for the MIPS
  6. \ RISC project at Stanford. Translated to forth by Marty Fraeman,
  7. \ Johns Hopkins University/Applied Physics Laboratory.
  8.  
  9. : exchange ( x y -- ) dup @ rot dup @ >r ! r> swap ! ;
  10.  
  11. variable seed
  12.  
  13. : initiate-seed ( -- )  74755 seed ! ;
  14. : random  ( -- n )  seed @ 1309 * 13849 + 65535 and dup seed ! ;
  15.  
  16. \ These structure access words were originally developed by
  17. \ at JHU/APL by Ben Ballard and John Hayes
  18. \ Structure access words
  19. \ Examples of use:
  20. \ structure foo  \ declare a structure named foo
  21. \       wrd: .thing1 \ with a one word field named .thing1
  22. \    2 wrds: .thing2 \ and a two word field named .thing2
  23. \ endstructure
  24. \
  25. \ structure foobar \ another structure
  26. \           wrd: .thing
  27. \    foo struct: .blah \ nested structure
  28. \ endstructure
  29. \
  30. \ foobar makestruct test \ allocate space for a structure instance
  31. \ 1234 test .blah .thing1 ! \ access structure
  32.  
  33. : structure ( --- structure offset0)
  34.   create here 0 , 0
  35. does> ( structure -- size)
  36.   @
  37. ;
  38.  
  39. : struct: ( offset1 size --- offset2)
  40.   create over , +
  41. does> ( structure field -- field-addr)
  42.   @ +
  43.  
  44. : wrds: ( offset1 size --- offset2)  cells struct: ;
  45. : wrd: ( offset1 --- offset2)  cell struct: ;
  46. : endstructure ( structure size --- ) swap ! ;
  47. : makestruct ( size --- )  create allot ;
  48. : malloc  ( structure -- instance)  here swap allot ;
  49.  
  50. \ The Tree Sort definitions:
  51.  
  52. structure node ( -- )
  53.  wrd: .left
  54.  wrd: .right
  55.  wrd: .val
  56. endstructure
  57.  
  58. 5000 constant tree-size
  59. variable tree
  60.  
  61. : create-node ( n t -- )
  62.   node malloc dup >r swap !
  63.   r@ .val !
  64.   nil r@ .left !
  65.   nil r> .right !
  66. ;
  67.  
  68. : insert-node ( n t -- )
  69.   2dup .val @ >
  70.   if dup .left @ nil =
  71.     if 2dup .left create-node
  72.     else
  73.       2dup .left @ recurse
  74.     then
  75.   else 2dup .val @ <
  76.     if dup .right @ nil =
  77.       if 2dup .right create-node
  78.       else
  79.     2dup .right @ recurse
  80.       then
  81.     then
  82.   then
  83.   2drop
  84. ;
  85.  
  86. : verify-tree ( t -- f)
  87.   true >r dup .left @ nil = not
  88.   if dup .left @ .val @ over .val @ > not
  89.     if r> drop false >r 
  90.     else dup .left @ recurse r> and >r then
  91.   then 
  92.   dup .right @ nil = not
  93.   if dup .right @ .val @ over .val @ < not
  94.     if r> drop false >r
  95.     else dup .right @ recurse r> and >r then
  96.  then
  97.  drop r>
  98. ;
  99.  
  100. : dump-tree ( t -- )
  101.   dup nil = not
  102.   if dup .right @ recurse
  103.     dup .val @ .
  104.     dup .left @ recurse
  105.   then
  106.   drop
  107. ;
  108.  
  109. : tree-sort   ( -- )
  110.   initiate-seed
  111.   random tree create-node
  112.   tree @
  113.   tree-size 0 do
  114.     random over insert-node
  115.   loop
  116.   verify-tree not abort" trees: wrong result"
  117. ;
  118.  
  119. forth only
  120.