xstorage.red 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. module xstorage;
  2. % Storage and retrieval of critical pairs and polynomials.
  3. % Author: David Hartley
  4. Comment. Critical pairs and polynomials are stored in a search tree,
  5. called an xset here:
  6. xset ::= empty_xset | item . xset
  7. empty_xset ::= any . nil
  8. item ::= any
  9. All changes to xset are made destructively as side-effects.
  10. endcomment;
  11. symbolic smacro procedure xset_ptrs c;
  12. cdr c;
  13. symbolic smacro procedure left_xset c;
  14. cadr c;
  15. symbolic smacro procedure right_xset c;
  16. cddr c;
  17. symbolic procedure find_item(pr,c);
  18. % pr:item, c:xset -> find_item:xset|nil
  19. % if pr in c, returns pointer to pr, otherwise nil
  20. if empty_xsetp c then nil
  21. else find_item(pr,left_xset c) or
  22. (if xset_item c = pr then c) or
  23. find_item(pr,right_xset c);
  24. symbolic procedure add_item(pr,c);
  25. % pr:item, c:xset -> add_item:nil
  26. % add new item pr to structure c as side-effect
  27. % goes left iff xkey pr < xkey xset_item c
  28. if empty_xsetp c then
  29. <<xset_item c := pr;
  30. xset_ptrs c := empty_xset() . empty_xset();>>
  31. else if monordp(xkey xset_item c,xkey pr) then
  32. add_item(pr,left_xset c)
  33. else
  34. add_item(pr,right_xset c);
  35. symbolic procedure remove_item(pr,c);
  36. % pr:item, c:xset -> remove_item:item or nil
  37. % deletes pr, if present, from c as side-effect
  38. if c := find_item(pr,c) then remove_root_item c;
  39. symbolic procedure remove_least_item c;
  40. % c:xset -> remove_least_item:item
  41. % returns "least" item in structure and deletes it as side-effect
  42. if empty_xsetp c then rederr "How did we get here?"
  43. else if empty_xsetp left_xset c then remove_root_item c
  44. else remove_least_item left_xset c;
  45. symbolic procedure remove_root_item c;
  46. % c:xset -> remove_root_item:item
  47. % deletes first item in c, which is not empty
  48. begin scalar x,y;
  49. x := left_xset c; y := xset_item c;
  50. xset_item c := xset_item right_xset c;
  51. xset_ptrs c := xset_ptrs right_xset c;
  52. if not empty_xsetp x then % graft x onto the left-most part of c
  53. <<while not empty_xsetp c do c := left_xset c;
  54. xset_item c := xset_item x;
  55. xset_ptrs c := xset_ptrs x>>;
  56. return y;
  57. end;
  58. symbolic procedure remove_items(c,u);
  59. % c:xset of lists, u:list -> remove_items:nil
  60. % removes all items containing elements of u from c
  61. begin
  62. if empty_xsetp c then return;
  63. remove_items(left_xset c,u);
  64. remove_items(right_xset c,u);
  65. if xnp(u,xset_item c) then remove_root_item c;
  66. end;
  67. endmodule;
  68. end;