aiso.spc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Module: Arbitrary-In Sorted-Out (AISO)
  3. Author: [email protected] (Dick Grune @ Vrije Universiteit, Amsterdam)
  4. Version: Tue Aug 23 12:54:22 1988
  5. Description:
  6. This is the specification of a generic module that builds an
  7. arbitrary-in sorted-out data structure, to be used as a heap, a
  8. priority queue, etc. Elements can be inserted, the first element
  9. extracted and the set scanned at any moment.
  10. Instantiation:
  11. The module is instantiated as follows.
  12. Create a file M.h for some M, which contains at least:
  13. - a definition of AISO_TYPE, the type of the object to be stored
  14. - a possible definition of AISO_EXTRACTOR; see below
  15. - a possible definition of AISO_ITERATOR; see below
  16. - #include "aiso.spc"
  17. This file M.h is to be included in all files that use the aiso
  18. package.
  19. Create a file M.c which contains at least:
  20. - #include "M.h"
  21. - a definition of a routine
  22. int AISO_BEFORE(AISO_TYPE v, AISO_TYPE w)
  23. which yields non-zero if v is to be sorted before w
  24. - #include "aiso.bdy"
  25. This file compiles into the module object.
  26. Specification:
  27. The module always supplies:
  28. int InsertAiso(AISO_TYPE value)
  29. inserts value in its proper place; fails if out of memory
  30. If AISO_EXTRACTOR is defined, the module will also supply:
  31. int ExtractAiso(AISO_TYPE *value)
  32. yields the first value in the aiso and removes it;
  33. fails if empty
  34. If AISO_ITERATOR is defined, the module also supplies a type AisoIter
  35. which declares an iterator, i.e., a structure that records a position
  36. in the ordered set, plus routines for manipulating the iterator, thus
  37. enabling the user to scan the ordered set. The iterator should be
  38. declared as:
  39. AisoIter iter;
  40. and is manipulated by the following commands:
  41. void OpenIter(AisoIter *iter)
  42. opens the iterator for scanning the existing set in order
  43. int GetAisoItem(AisoIter *iter, AISO_TYPE *value)
  44. yields the next value in the iterator; fails if exhausted
  45. void CloseIter(AisoIter *iter)
  46. closes the iterator
  47. If AISO_DEBUG is defined the module will also supply:
  48. void PrintAisoTree(void)
  49. prints the AISO tree; requires AISO_FORMAT, to be set to
  50. a format suitable to print a value of type AISO_TYPE
  51. Implementation:
  52. The AISO implementation is based on a self-adjusting binary tree.
  53. Degenerate behaviour of the tree is avoided by shaking the tree
  54. every 'ln aiso_size' node accesses. This guarantees ln aiso_size
  55. behaviour in the long run, though it is possible for a single
  56. operation to take aiso_size node accesses.
  57. The iterator is implemented as an additional linear linked list
  58. through the tree. This is simpler than and at least as efficient as
  59. clever tree-wiring.
  60. Restrictions:
  61. Due to built-in fixed names, there can only be one AISO per program.
  62. */
  63. struct aiso_node {
  64. struct aiso_node *an_left;
  65. struct aiso_node *an_right;
  66. #ifdef AISO_ITERATOR
  67. struct aiso_node *an_next;
  68. #endif /* AISO_ITERATOR */
  69. AISO_TYPE an_value;
  70. };
  71. extern int InsertAiso(AISO_TYPE value);
  72. #ifdef AISO_EXTRACTOR
  73. extern int ExtractAiso(AISO_TYPE *value);
  74. #endif /* AISO_EXTRACTOR */
  75. #ifdef AISO_ITERATOR
  76. typedef struct aiso_node *AisoIter;
  77. extern void OpenIter(AisoIter *iter);
  78. extern int GetAisoItem(AisoIter *iter, AISO_TYPE *value);
  79. extern void CloseIter(AisoIter *iter);
  80. #endif /* AISO_ITERATOR */
  81. #ifdef AISO_DEBUG
  82. extern void PrintAisoTree(void);
  83. #endif /* AISO_ITERATOR */