You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
7.4 KiB

  1. The mtr package
  2. Multiway-branch tree manipulation
  3. Fabio Somenzi
  4. **********************************************************************
  5. Mtr_AllocNode() Allocates new tree node.
  6. Mtr_CopyTree() Makes a copy of tree.
  7. Mtr_CreateFirstChild() Creates a new node and makes it the first child
  8. of parent.
  9. Mtr_CreateLastChild() Creates a new node and makes it the last child
  10. of parent.
  11. Mtr_DeallocNode() Deallocates tree node.
  12. Mtr_DissolveGroup() Merges the children of `group' with the
  13. children of its parent.
  14. Mtr_FindGroup() Finds a group with size leaves starting at low,
  15. if it exists.
  16. Mtr_FreeTree() Disposes of tree rooted at node.
  17. Mtr_InitGroupTree() Allocate new tree.
  18. Mtr_InitTree() Initializes tree with one node.
  19. Mtr_MakeFirstChild() Makes child the first child of parent.
  20. Mtr_MakeGroup() Makes a new group with size leaves starting at
  21. low.
  22. Mtr_MakeLastChild() Makes child the last child of parent.
  23. Mtr_MakeNextSibling() Makes second the next sibling of first.
  24. Mtr_PrintGroups() Prints the groups as a parenthesized list.
  25. Mtr_PrintTree() Prints a tree, one node per line.
  26. Mtr_ReadGroups() Reads groups from a file and creates a group
  27. tree.
  28. Mtr_SwapGroups() Swaps two children of a tree node.
  29. **********************************************************************
  30. This package provides two layers of functions. Functions of the lower level
  31. manipulate multiway-branch trees, implemented according to the classical
  32. scheme whereby each node points to its first child and its previous and
  33. next siblings. These functions are collected in mtrBasic.c. Functions
  34. of the upper layer deal with group trees, that is the trees used by group
  35. sifting to represent the grouping of variables. These functions are
  36. collected in mtrGroup.c.
  37. MtrNode *
  38. Mtr_AllocNode(
  39. )
  40. Allocates new tree node. Returns pointer to node.
  41. Side Effects: None
  42. MtrNode *
  43. Mtr_CopyTree(
  44. MtrNode * node,
  45. int expansion
  46. )
  47. Makes a copy of tree. If parameter expansion is greater than 1, it will
  48. expand the tree by that factor. It is an error for expansion to be less than
  49. 1. Returns a pointer to the copy if successful; NULL otherwise.
  50. Side Effects: None
  51. MtrNode *
  52. Mtr_CreateFirstChild(
  53. MtrNode * parent
  54. )
  55. Creates a new node and makes it the first child of parent. Returns pointer
  56. to new child.
  57. Side Effects: None
  58. MtrNode *
  59. Mtr_CreateLastChild(
  60. MtrNode * parent
  61. )
  62. Creates a new node and makes it the last child of parent. Returns pointer to
  63. new child.
  64. Side Effects: None
  65. void
  66. Mtr_DeallocNode(
  67. MtrNode * node node to be deallocated
  68. )
  69. Deallocates tree node.
  70. Side Effects: None
  71. MtrNode *
  72. Mtr_DissolveGroup(
  73. MtrNode * group group to be dissolved
  74. )
  75. Merges the children of `group' with the children of its parent. Disposes of
  76. the node pointed by group. If group is the root of the group tree, this
  77. procedure leaves the tree unchanged. Returns the pointer to the parent of
  78. `group' upon successful termination; NULL otherwise.
  79. Side Effects: None
  80. MtrNode *
  81. Mtr_FindGroup(
  82. MtrNode * root, root of the group tree
  83. unsigned int low, lower bound of the group
  84. unsigned int size upper bound of the group
  85. )
  86. Finds a group with size leaves starting at low, if it exists. This procedure
  87. relies on the low and size fields of each node. It also assumes that the
  88. children of each node are sorted in order of increasing low. Returns the
  89. pointer to the root of the group upon successful termination; NULL
  90. otherwise.
  91. Side Effects: None
  92. void
  93. Mtr_FreeTree(
  94. MtrNode * node
  95. )
  96. Disposes of tree rooted at node.
  97. Side Effects: None
  98. MtrNode *
  99. Mtr_InitGroupTree(
  100. int lower,
  101. int size
  102. )
  103. Allocate new tree with one node, whose low and size fields are specified by
  104. the lower and size parameters. Returns pointer to tree root.
  105. Side Effects: None
  106. MtrNode *
  107. Mtr_InitTree(
  108. )
  109. Initializes tree with one node. Returns pointer to node.
  110. Side Effects: None
  111. void
  112. Mtr_MakeFirstChild(
  113. MtrNode * parent,
  114. MtrNode * child
  115. )
  116. Makes child the first child of parent.
  117. Side Effects: None
  118. MtrNode *
  119. Mtr_MakeGroup(
  120. MtrNode * root, root of the group tree
  121. unsigned int low, lower bound of the group
  122. unsigned int size, upper bound of the group
  123. unsigned int flags flags for the new group
  124. )
  125. Makes a new group with size leaves starting at low. If the new group
  126. intersects an existing group, it must either contain it or be contained by
  127. it. This procedure relies on the low and size fields of each node. It also
  128. assumes that the children of each node are sorted in order of increasing
  129. low. In case of a valid request, the flags of the new group are set to the
  130. value passed in `flags.' This can also be used to change the flags of an
  131. existing group. Returns the pointer to the root of the new group upon
  132. successful termination; NULL otherwise. If the group already exists, the
  133. pointer to its root is returned.
  134. Side Effects: None
  135. void
  136. Mtr_MakeLastChild(
  137. MtrNode * parent,
  138. MtrNode * child
  139. )
  140. Makes child the last child of parent.
  141. Side Effects: None
  142. void
  143. Mtr_MakeNextSibling(
  144. MtrNode * first,
  145. MtrNode * second
  146. )
  147. Makes second the next sibling of first. Second becomes a child of the parent
  148. of first.
  149. Side Effects: None
  150. void
  151. Mtr_PrintGroups(
  152. MtrNode * root, root of the group tree
  153. int silent flag to check tree syntax only
  154. )
  155. Prints the groups as a parenthesized list. After each group, the group's
  156. flag are printed, preceded by a `|'. For each flag (except MTR_TERMINAL) a
  157. character is printed. F: MTR_FIXED N: MTR_NEWNODE S:
  158. MTR_SOFT The second argument, silent, if different from 0, causes
  159. Mtr_PrintGroups to only check the syntax of the group tree.
  160. Side Effects: None
  161. void
  162. Mtr_PrintTree(
  163. MtrNode * node
  164. )
  165. Prints a tree, one node per line.
  166. Side Effects: None
  167. MtrNode *
  168. Mtr_ReadGroups(
  169. FILE * fp, file pointer
  170. int nleaves number of leaves of the new tree
  171. )
  172. Reads groups from a file and creates a group tree. Each group is specified
  173. by three fields: low size flags. Low and size are (short)
  174. integers. Flags is a string composed of the following characters (with
  175. associated translation): D: MTR_DEFAULT F: MTR_FIXED N:
  176. MTR_NEWNODE S: MTR_SOFT T: MTR_TERMINAL Normally, the only
  177. flags that are needed are D and F. Groups and fields are separated by white
  178. space (spaces, tabs, and newlines). Returns a pointer to the group tree if
  179. successful; NULL otherwise.
  180. Side Effects: None
  181. int
  182. Mtr_SwapGroups(
  183. MtrNode * first, first node to be swapped
  184. MtrNode * second second node to be swapped
  185. )
  186. Swaps two children of a tree node. Adjusts the high and low fields of the
  187. two nodes and their descendants. The two children must be adjacent. However,
  188. first may be the younger sibling of second. Returns 1 in case of success; 0
  189. otherwise.
  190. Side Effects: None