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.

398 lines
9.3 KiB

  1. /**
  2. @file
  3. @ingroup mtr
  4. @brief Basic manipulation of multiway branching trees.
  5. @see cudd package
  6. @author Fabio Somenzi
  7. @copyright@parblock
  8. Copyright (c) 1995-2015, Regents of the University of Colorado
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. Redistributions of source code must retain the above copyright
  14. notice, this list of conditions and the following disclaimer.
  15. Redistributions in binary form must reproduce the above copyright
  16. notice, this list of conditions and the following disclaimer in the
  17. documentation and/or other materials provided with the distribution.
  18. Neither the name of the University of Colorado nor the names of its
  19. contributors may be used to endorse or promote products derived from
  20. this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. POSSIBILITY OF SUCH DAMAGE.
  33. @endparblock
  34. */
  35. #include "util.h"
  36. #include "mtrInt.h"
  37. /*---------------------------------------------------------------------------*/
  38. /* Constant declarations */
  39. /*---------------------------------------------------------------------------*/
  40. /*---------------------------------------------------------------------------*/
  41. /* Stucture declarations */
  42. /*---------------------------------------------------------------------------*/
  43. /*---------------------------------------------------------------------------*/
  44. /* Type declarations */
  45. /*---------------------------------------------------------------------------*/
  46. /*---------------------------------------------------------------------------*/
  47. /* Variable declarations */
  48. /*---------------------------------------------------------------------------*/
  49. /*---------------------------------------------------------------------------*/
  50. /* Macro declarations */
  51. /*---------------------------------------------------------------------------*/
  52. /** \cond */
  53. /*---------------------------------------------------------------------------*/
  54. /* Static function prototypes */
  55. /*---------------------------------------------------------------------------*/
  56. /** \endcond */
  57. /*---------------------------------------------------------------------------*/
  58. /* Definition of exported functions */
  59. /*---------------------------------------------------------------------------*/
  60. /**
  61. @brief Allocates new tree node.
  62. @return pointer to node.
  63. @sideeffect None
  64. @see Mtr_DeallocNode
  65. */
  66. MtrNode *
  67. Mtr_AllocNode(void)
  68. {
  69. MtrNode *node;
  70. node = ALLOC(MtrNode,1);
  71. node->flags = node->low = node->size = node->index = 0;
  72. return node;
  73. } /* Mtr_AllocNode */
  74. /**
  75. @brief Deallocates tree node.
  76. @sideeffect None
  77. @see Mtr_AllocNode
  78. */
  79. void
  80. Mtr_DeallocNode(
  81. MtrNode * node /**< node to be deallocated */)
  82. {
  83. FREE(node);
  84. return;
  85. } /* end of Mtr_DeallocNode */
  86. /**
  87. @brief Initializes tree with one node.
  88. @return pointer to node.
  89. @sideeffect None
  90. @see Mtr_FreeTree Mtr_InitGroupTree
  91. */
  92. MtrNode *
  93. Mtr_InitTree(void)
  94. {
  95. MtrNode *node;
  96. node = Mtr_AllocNode();
  97. if (node == NULL) return(NULL);
  98. node->parent = node->child = node->elder = node->younger = NULL;
  99. return(node);
  100. } /* end of Mtr_InitTree */
  101. /**
  102. @brief Disposes of tree rooted at node.
  103. @sideeffect None
  104. @see Mtr_InitTree
  105. */
  106. void
  107. Mtr_FreeTree(
  108. MtrNode * node)
  109. {
  110. if (node == NULL) return;
  111. if (! MTR_TEST(node,MTR_TERMINAL)) Mtr_FreeTree(node->child);
  112. Mtr_FreeTree(node->younger);
  113. Mtr_DeallocNode(node);
  114. return;
  115. } /* end of Mtr_FreeTree */
  116. /**
  117. @brief Makes a copy of tree.
  118. @details If parameter expansion is greater than 1, it will expand
  119. the tree by that factor. It is an error for expansion to be less
  120. than 1.
  121. @return a pointer to the copy if successful; NULL otherwise.
  122. @sideeffect None
  123. @see Mtr_InitTree
  124. */
  125. MtrNode *
  126. Mtr_CopyTree(
  127. MtrNode const * node,
  128. int expansion)
  129. {
  130. MtrNode *copy;
  131. if (node == NULL) return(NULL);
  132. if (expansion < 1) return(NULL);
  133. copy = Mtr_AllocNode();
  134. if (copy == NULL) return(NULL);
  135. copy->parent = copy->elder = copy->child = copy->younger = NULL;
  136. if (node->child != NULL) {
  137. copy->child = Mtr_CopyTree(node->child, expansion);
  138. if (copy->child == NULL) {
  139. Mtr_DeallocNode(copy);
  140. return(NULL);
  141. }
  142. }
  143. if (node->younger != NULL) {
  144. copy->younger = Mtr_CopyTree(node->younger, expansion);
  145. if (copy->younger == NULL) {
  146. Mtr_FreeTree(copy);
  147. return(NULL);
  148. }
  149. }
  150. copy->flags = node->flags;
  151. copy->low = node->low * expansion;
  152. copy->size = node->size * expansion;
  153. copy->index = node->index * expansion;
  154. if (copy->younger) copy->younger->elder = copy;
  155. if (copy->child) {
  156. MtrNode *auxnode = copy->child;
  157. while (auxnode != NULL) {
  158. auxnode->parent = copy;
  159. auxnode = auxnode->younger;
  160. }
  161. }
  162. return(copy);
  163. } /* end of Mtr_CopyTree */
  164. /**
  165. @brief Makes child the first child of parent.
  166. @sideeffect None
  167. @see Mtr_MakeLastChild Mtr_CreateFirstChild
  168. */
  169. void
  170. Mtr_MakeFirstChild(
  171. MtrNode * parent,
  172. MtrNode * child)
  173. {
  174. child->parent = parent;
  175. child->younger = parent->child;
  176. child->elder = NULL;
  177. if (parent->child != NULL) {
  178. #ifdef MTR_DEBUG
  179. assert(parent->child->elder == NULL);
  180. #endif
  181. parent->child->elder = child;
  182. }
  183. parent->child = child;
  184. return;
  185. } /* end of Mtr_MakeFirstChild */
  186. /**
  187. @brief Makes child the last child of parent.
  188. @sideeffect None
  189. @see Mtr_MakeFirstChild Mtr_CreateLastChild
  190. */
  191. void
  192. Mtr_MakeLastChild(
  193. MtrNode * parent,
  194. MtrNode * child)
  195. {
  196. MtrNode *node;
  197. child->younger = NULL;
  198. if (parent->child == NULL) {
  199. parent->child = child;
  200. child->elder = NULL;
  201. } else {
  202. for (node = parent->child;
  203. node->younger != NULL;
  204. node = node->younger);
  205. node->younger = child;
  206. child->elder = node;
  207. }
  208. child->parent = parent;
  209. return;
  210. } /* end of Mtr_MakeLastChild */
  211. /**
  212. @brief Creates a new node and makes it the first child of parent.
  213. @return pointer to new child.
  214. @sideeffect None
  215. @see Mtr_MakeFirstChild Mtr_CreateLastChild
  216. */
  217. MtrNode *
  218. Mtr_CreateFirstChild(
  219. MtrNode * parent)
  220. {
  221. MtrNode *child;
  222. child = Mtr_AllocNode();
  223. if (child == NULL) return(NULL);
  224. child->child = NULL;
  225. Mtr_MakeFirstChild(parent,child);
  226. return(child);
  227. } /* end of Mtr_CreateFirstChild */
  228. /**
  229. @brief Creates a new node and makes it the last child of parent.
  230. @return pointer to new child.
  231. @sideeffect None
  232. @see Mtr_MakeLastChild Mtr_CreateFirstChild
  233. */
  234. MtrNode *
  235. Mtr_CreateLastChild(
  236. MtrNode * parent)
  237. {
  238. MtrNode *child;
  239. child = Mtr_AllocNode();
  240. if (child == NULL) return(NULL);
  241. child->child = NULL;
  242. Mtr_MakeLastChild(parent,child);
  243. return(child);
  244. } /* end of Mtr_CreateLastChild */
  245. /**
  246. @brief Makes second the next sibling of first.
  247. @details Second becomes a child of the parent of first.
  248. @sideeffect None
  249. */
  250. void
  251. Mtr_MakeNextSibling(
  252. MtrNode * first,
  253. MtrNode * second)
  254. {
  255. second->parent = first->parent;
  256. second->elder = first;
  257. second->younger = first->younger;
  258. if (first->younger != NULL) {
  259. first->younger->elder = second;
  260. }
  261. first->younger = second;
  262. return;
  263. } /* end of Mtr_MakeNextSibling */
  264. /**
  265. @brief Prints a tree, one node per line.
  266. @sideeffect None
  267. @see Mtr_PrintGroups
  268. */
  269. void
  270. Mtr_PrintTree(
  271. MtrNode const * node)
  272. {
  273. if (node == NULL) return;
  274. (void) fprintf(stdout,
  275. "N=0x%-8" PRIxPTR " C=0x%-8" PRIxPTR " Y=0x%-8" PRIxPTR
  276. " E=0x%-8" PRIxPTR " P=0x%-8" PRIxPTR " F=%x L=%u S=%u\n",
  277. (uintptr_t) node, (uintptr_t) node->child,
  278. (uintptr_t) node->younger, (uintptr_t) node->elder,
  279. (uintptr_t) node->parent, node->flags, node->low, node->size);
  280. if (!MTR_TEST(node,MTR_TERMINAL)) Mtr_PrintTree(node->child);
  281. Mtr_PrintTree(node->younger);
  282. return;
  283. } /* end of Mtr_PrintTree */
  284. /*---------------------------------------------------------------------------*/
  285. /* Definition of internal functions */
  286. /*---------------------------------------------------------------------------*/
  287. /*---------------------------------------------------------------------------*/
  288. /* Definition of static functions */
  289. /*---------------------------------------------------------------------------*/