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.

450 lines
13 KiB

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