abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mtrBasic.c
Go to the documentation of this file.
1 /**CFile***********************************************************************
2 
3  FileName [mtrBasic.c]
4 
5  PackageName [mtr]
6 
7  Synopsis [Basic manipulation of multiway branching trees.]
8 
9  Description [External procedures included in this module:
10  <ul>
11  <li> Mtr_AllocNode()
12  <li> Mtr_DeallocNode()
13  <li> Mtr_InitTree()
14  <li> Mtr_FreeTree()
15  <li> Mtr_CopyTree()
16  <li> Mtr_MakeFirstChild()
17  <li> Mtr_MakeLastChild()
18  <li> Mtr_CreateFirstChild()
19  <li> Mtr_CreateLastChild()
20  <li> Mtr_MakeNextSibling()
21  <li> Mtr_PrintTree()
22  </ul>
23  ]
24 
25  SeeAlso [cudd package]
26 
27  Author [Fabio Somenzi]
28 
29  Copyright [Copyright (c) 1995-2004, Regents of the University of Colorado
30 
31  All rights reserved.
32 
33  Redistribution and use in source and binary forms, with or without
34  modification, are permitted provided that the following conditions
35  are met:
36 
37  Redistributions of source code must retain the above copyright
38  notice, this list of conditions and the following disclaimer.
39 
40  Redistributions in binary form must reproduce the above copyright
41  notice, this list of conditions and the following disclaimer in the
42  documentation and/or other materials provided with the distribution.
43 
44  Neither the name of the University of Colorado nor the names of its
45  contributors may be used to endorse or promote products derived from
46  this software without specific prior written permission.
47 
48  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
49  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
50  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
51  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
52  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
55  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59  POSSIBILITY OF SUCH DAMAGE.]
60 
61 ******************************************************************************/
62 
63 #include "misc/util/util_hack.h"
64 #include "mtrInt.h"
65 
67 
68 /*---------------------------------------------------------------------------*/
69 /* Constant declarations */
70 /*---------------------------------------------------------------------------*/
71 
72 /*---------------------------------------------------------------------------*/
73 /* Stucture declarations */
74 /*---------------------------------------------------------------------------*/
75 
76 /*---------------------------------------------------------------------------*/
77 /* Type declarations */
78 /*---------------------------------------------------------------------------*/
79 
80 /*---------------------------------------------------------------------------*/
81 /* Variable declarations */
82 /*---------------------------------------------------------------------------*/
83 
84 #ifndef lint
85 static char rcsid[] MTR_UNUSED = "$Id: mtrBasic.c,v 1.13 2009/02/20 02:03:47 fabio Exp $";
86 #endif
87 
88 /*---------------------------------------------------------------------------*/
89 /* Macro declarations */
90 /*---------------------------------------------------------------------------*/
91 
92 /**AutomaticStart*************************************************************/
93 
94 /*---------------------------------------------------------------------------*/
95 /* Static function prototypes */
96 /*---------------------------------------------------------------------------*/
97 
98 
99 /**AutomaticEnd***************************************************************/
100 
101 
102 /*---------------------------------------------------------------------------*/
103 /* Definition of exported functions */
104 /*---------------------------------------------------------------------------*/
105 
106 /**Function********************************************************************
107 
108  Synopsis [Allocates new tree node.]
109 
110  Description [Allocates new tree node. Returns pointer to node.]
111 
112  SideEffects [None]
113 
114  SeeAlso [Mtr_DeallocNode]
115 
116 ******************************************************************************/
117 MtrNode *
119 {
120  MtrNode *node;
121 
122  node = ABC_ALLOC(MtrNode,1);
123  return node;
124 
125 } /* Mtr_AllocNode */
126 
127 
128 /**Function********************************************************************
129 
130  Synopsis [Deallocates tree node.]
131 
132  Description []
133 
134  SideEffects [None]
135 
136  SeeAlso [Mtr_AllocNode]
137 
138 ******************************************************************************/
139 void
141  MtrNode * node /* node to be deallocated */)
142 {
143  ABC_FREE(node);
144  return;
145 
146 } /* end of Mtr_DeallocNode */
147 
148 
149 /**Function********************************************************************
150 
151  Synopsis [Initializes tree with one node.]
152 
153  Description [Initializes tree with one node. Returns pointer to node.]
154 
155  SideEffects [None]
156 
157  SeeAlso [Mtr_FreeTree Mtr_InitGroupTree]
158 
159 ******************************************************************************/
160 MtrNode *
162 {
163  MtrNode *node;
164 
165  node = Mtr_AllocNode();
166  if (node == NULL) return(NULL);
167 
168  node->parent = node->child = node->elder = node->younger = NULL;
169  node->flags = 0;
170 
171  return(node);
172 
173 } /* end of Mtr_InitTree */
174 
175 
176 /**Function********************************************************************
177 
178  Synopsis [Disposes of tree rooted at node.]
179 
180  Description []
181 
182  SideEffects [None]
183 
184  SeeAlso [Mtr_InitTree]
185 
186 ******************************************************************************/
187 void
189  MtrNode * node)
190 {
191  if (node == NULL) return;
192  if (! MTR_TEST(node,MTR_TERMINAL)) Mtr_FreeTree(node->child);
193  Mtr_FreeTree(node->younger);
194  Mtr_DeallocNode(node);
195  return;
196 
197 } /* end of Mtr_FreeTree */
198 
199 
200 /**Function********************************************************************
201 
202  Synopsis [Makes a copy of tree.]
203 
204  Description [Makes a copy of tree. If parameter expansion is greater
205  than 1, it will expand the tree by that factor. It is an error for
206  expansion to be less than 1. Returns a pointer to the copy if
207  successful; NULL otherwise.]
208 
209  SideEffects [None]
210 
211  SeeAlso [Mtr_InitTree]
212 
213 ******************************************************************************/
214 MtrNode *
216  MtrNode * node,
217  int expansion)
218 {
219  MtrNode *copy;
220 
221  if (node == NULL) return(NULL);
222  if (expansion < 1) return(NULL);
223  copy = Mtr_AllocNode();
224  if (copy == NULL) return(NULL);
225  copy->parent = copy->elder = copy->child = copy->younger = NULL;
226  if (node->child != NULL) {
227  copy->child = Mtr_CopyTree(node->child, expansion);
228  if (copy->child == NULL) {
229  Mtr_DeallocNode(copy);
230  return(NULL);
231  }
232  }
233  if (node->younger != NULL) {
234  copy->younger = Mtr_CopyTree(node->younger, expansion);
235  if (copy->younger == NULL) {
236  Mtr_FreeTree(copy);
237  return(NULL);
238  }
239  }
240  copy->flags = node->flags;
241  copy->low = node->low * expansion;
242  copy->size = node->size * expansion;
243  copy->index = node->index * expansion;
244  if (copy->younger) copy->younger->elder = copy;
245  if (copy->child) {
246  MtrNode *auxnode = copy->child;
247  while (auxnode != NULL) {
248  auxnode->parent = copy;
249  auxnode = auxnode->younger;
250  }
251  }
252  return(copy);
253 
254 } /* end of Mtr_CopyTree */
255 
256 
257 /**Function********************************************************************
258 
259  Synopsis [Makes child the first child of parent.]
260 
261  Description []
262 
263  SideEffects [None]
264 
265  SeeAlso [Mtr_MakeLastChild Mtr_CreateFirstChild]
266 
267 ******************************************************************************/
268 void
270  MtrNode * parent,
271  MtrNode * child)
272 {
273  child->parent = parent;
274  child->younger = parent->child;
275  child->elder = NULL;
276  if (parent->child != NULL) {
277 #ifdef MTR_DEBUG
278  assert(parent->child->elder == NULL);
279 #endif
280  parent->child->elder = child;
281  }
282  parent->child = child;
283  return;
284 
285 } /* end of Mtr_MakeFirstChild */
286 
287 
288 /**Function********************************************************************
289 
290  Synopsis [Makes child the last child of parent.]
291 
292  Description []
293 
294  SideEffects [None]
295 
296  SeeAlso [Mtr_MakeFirstChild Mtr_CreateLastChild]
297 
298 ******************************************************************************/
299 void
301  MtrNode * parent,
302  MtrNode * child)
303 {
304  MtrNode *node;
305 
306  child->younger = NULL;
307 
308  if (parent->child == NULL) {
309  parent->child = child;
310  child->elder = NULL;
311  } else {
312  for (node = parent->child;
313  node->younger != NULL;
314  node = node->younger);
315  node->younger = child;
316  child->elder = node;
317  }
318  child->parent = parent;
319  return;
320 
321 } /* end of Mtr_MakeLastChild */
322 
323 
324 /**Function********************************************************************
325 
326  Synopsis [Creates a new node and makes it the first child of parent.]
327 
328  Description [Creates a new node and makes it the first child of
329  parent. Returns pointer to new child.]
330 
331  SideEffects [None]
332 
333  SeeAlso [Mtr_MakeFirstChild Mtr_CreateLastChild]
334 
335 ******************************************************************************/
336 MtrNode *
338  MtrNode * parent)
339 {
340  MtrNode *child;
341 
342  child = Mtr_AllocNode();
343  if (child == NULL) return(NULL);
344 
345  child->child = child->younger = child-> elder = NULL;
346  child->flags = 0;
347  Mtr_MakeFirstChild(parent,child);
348  return(child);
349 
350 } /* end of Mtr_CreateFirstChild */
351 
352 
353 /**Function********************************************************************
354 
355  Synopsis [Creates a new node and makes it the last child of parent.]
356 
357  Description [Creates a new node and makes it the last child of parent.
358  Returns pointer to new child.]
359 
360  SideEffects [None]
361 
362  SeeAlso [Mtr_MakeLastChild Mtr_CreateFirstChild]
363 
364 ******************************************************************************/
365 MtrNode *
367  MtrNode * parent)
368 {
369  MtrNode *child;
370 
371  child = Mtr_AllocNode();
372  if (child == NULL) return(NULL);
373 
374  child->child = child->younger = child->elder = NULL;
375  child->flags = 0;
376  Mtr_MakeLastChild(parent,child);
377  return(child);
378 
379 } /* end of Mtr_CreateLastChild */
380 
381 
382 /**Function********************************************************************
383 
384  Synopsis [Makes second the next sibling of first.]
385 
386  Description [Makes second the next sibling of first. Second becomes a
387  child of the parent of first.]
388 
389  SideEffects [None]
390 
391  SeeAlso []
392 
393 ******************************************************************************/
394 void
396  MtrNode * first,
397  MtrNode * second)
398 {
399  second->younger = first->younger;
400  if (first->younger != NULL) {
401  first->younger->elder = second;
402  }
403  second->parent = first->parent;
404  first->younger = second;
405  second->elder = first;
406  return;
407 
408 } /* end of Mtr_MakeNextSibling */
409 
410 
411 /**Function********************************************************************
412 
413  Synopsis [Prints a tree, one node per line.]
414 
415  Description []
416 
417  SideEffects [None]
418 
419  SeeAlso [Mtr_PrintGroups]
420 
421 ******************************************************************************/
422 void
424  MtrNode * node)
425 {
426  if (node == NULL) return;
427  (void) fprintf(stdout,
428 #if SIZEOF_VOID_P == 8
429  "N=0x%-8lx C=0x%-8lx Y=0x%-8lx E=0x%-8lx P=0x%-8lx F=%x L=%u S=%u\n",
430  (unsigned long) node, (unsigned long) node->child,
431  (unsigned long) node->younger, (unsigned long) node->elder,
432  (unsigned long) node->parent, node->flags, node->low, node->size);
433 #else
434  "N=0x%-8x C=0x%-8x Y=0x%-8x E=0x%-8x P=0x%-8x F=%x L=%hu S=%hu\n",
435  (unsigned) node, (unsigned) node->child,
436  (unsigned) node->younger, (unsigned) node->elder,
437  (unsigned) node->parent, node->flags, node->low, node->size);
438 #endif
439  if (!MTR_TEST(node,MTR_TERMINAL)) Mtr_PrintTree(node->child);
440  Mtr_PrintTree(node->younger);
441  return;
442 
443 } /* end of Mtr_PrintTree */
444 
445 /*---------------------------------------------------------------------------*/
446 /* Definition of internal functions */
447 /*---------------------------------------------------------------------------*/
448 
449 /*---------------------------------------------------------------------------*/
450 /* Definition of static functions */
451 /*---------------------------------------------------------------------------*/
452 
void Mtr_PrintTree(MtrNode *node)
Definition: mtrBasic.c:423
#define MTR_TERMINAL
Definition: mtr.h:100
MtrHalfWord flags
Definition: mtr.h:132
static void copy(const T &from, T &to)
Definition: Alg.h:61
MtrHalfWord size
Definition: mtr.h:134
MtrNode * Mtr_CreateFirstChild(MtrNode *parent)
Definition: mtrBasic.c:337
MtrNode * Mtr_CopyTree(MtrNode *node, int expansion)
Definition: mtrBasic.c:215
static ABC_NAMESPACE_IMPL_START char rcsid[] MTR_UNUSED
Definition: mtrBasic.c:85
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
struct MtrNode * parent
Definition: mtr.h:136
struct MtrNode * elder
Definition: mtr.h:138
void Mtr_MakeLastChild(MtrNode *parent, MtrNode *child)
Definition: mtrBasic.c:300
#define MTR_TEST(node, flag)
Definition: mtr.h:155
struct MtrNode * younger
Definition: mtr.h:139
void Mtr_MakeNextSibling(MtrNode *first, MtrNode *second)
Definition: mtrBasic.c:395
void Mtr_DeallocNode(MtrNode *node)
Definition: mtrBasic.c:140
MtrHalfWord index
Definition: mtr.h:135
#define SIZEOF_VOID_P
Definition: cudd.h:78
MtrNode * Mtr_AllocNode(void)
Definition: mtrBasic.c:118
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
MtrHalfWord low
Definition: mtr.h:133
Definition: mtr.h:131
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define ABC_FREE(obj)
Definition: abc_global.h:232
MtrNode * Mtr_CreateLastChild(MtrNode *parent)
Definition: mtrBasic.c:366
#define assert(ex)
Definition: util_old.h:213
void Mtr_MakeFirstChild(MtrNode *parent, MtrNode *child)
Definition: mtrBasic.c:269
void Mtr_FreeTree(MtrNode *node)
Definition: mtrBasic.c:188
struct MtrNode * child
Definition: mtr.h:137
MtrNode * Mtr_InitTree(void)
Definition: mtrBasic.c:161