00001
00036 #include "util_hack.h"
00037 #include "mtrInt.h"
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #ifndef lint
00057 static char rcsid[] MTR_UNUSED = "$Id: mtrBasic.c,v 1.1.1.1 2003/02/24 22:24:02 wjiang Exp $";
00058 #endif
00059
00060
00061
00062
00063
00066
00067
00068
00069
00070
00074
00075
00076
00077
00089 MtrNode *
00090 Mtr_AllocNode(
00091 )
00092 {
00093 MtrNode *node;
00094
00095 node = ALLOC(MtrNode,1);
00096 return node;
00097
00098 }
00099
00100
00112 void
00113 Mtr_DeallocNode(
00114 MtrNode * node )
00115 {
00116 FREE(node);
00117 return;
00118
00119 }
00120
00121
00133 MtrNode *
00134 Mtr_InitTree(
00135 )
00136 {
00137 MtrNode *node;
00138
00139 node = Mtr_AllocNode();
00140 if (node == NULL) return(NULL);
00141
00142 node->parent = node->child = node->elder = node->younger = NULL;
00143 node->flags = 0;
00144
00145 return(node);
00146
00147 }
00148
00149
00161 void
00162 Mtr_FreeTree(
00163 MtrNode * node)
00164 {
00165 if (node == NULL) return;
00166 if (! MTR_TEST(node,MTR_TERMINAL)) Mtr_FreeTree(node->child);
00167 Mtr_FreeTree(node->younger);
00168 Mtr_DeallocNode(node);
00169 return;
00170
00171 }
00172
00173
00188 MtrNode *
00189 Mtr_CopyTree(
00190 MtrNode * node,
00191 int expansion)
00192 {
00193 MtrNode *copy;
00194
00195 if (node == NULL) return(NULL);
00196 if (expansion < 1) return(NULL);
00197 copy = Mtr_AllocNode();
00198 if (copy == NULL) return(NULL);
00199 copy->parent = copy->elder = copy->child = copy->younger = NULL;
00200 if (node->child != NULL) {
00201 copy->child = Mtr_CopyTree(node->child, expansion);
00202 if (copy->child == NULL) {
00203 Mtr_DeallocNode(copy);
00204 return(NULL);
00205 }
00206 }
00207 if (node->younger != NULL) {
00208 copy->younger = Mtr_CopyTree(node->younger, expansion);
00209 if (copy->younger == NULL) {
00210 Mtr_FreeTree(copy);
00211 return(NULL);
00212 }
00213 }
00214 copy->flags = node->flags;
00215 copy->low = node->low * expansion;
00216 copy->size = node->size * expansion;
00217 copy->index = node->index * expansion;
00218 if (copy->younger) copy->younger->elder = copy;
00219 if (copy->child) {
00220 MtrNode *auxnode = copy->child;
00221 while (auxnode != NULL) {
00222 auxnode->parent = copy;
00223 auxnode = auxnode->younger;
00224 }
00225 }
00226 return(copy);
00227
00228 }
00229
00230
00242 void
00243 Mtr_MakeFirstChild(
00244 MtrNode * parent,
00245 MtrNode * child)
00246 {
00247 child->parent = parent;
00248 child->younger = parent->child;
00249 child->elder = NULL;
00250 if (parent->child != NULL) {
00251 #ifdef MTR_DEBUG
00252 assert(parent->child->elder == NULL);
00253 #endif
00254 parent->child->elder = child;
00255 }
00256 parent->child = child;
00257 return;
00258
00259 }
00260
00261
00273 void
00274 Mtr_MakeLastChild(
00275 MtrNode * parent,
00276 MtrNode * child)
00277 {
00278 MtrNode *node;
00279
00280 child->younger = NULL;
00281
00282 if (parent->child == NULL) {
00283 parent->child = child;
00284 child->elder = NULL;
00285 } else {
00286 for (node = parent->child;
00287 node->younger != NULL;
00288 node = node->younger);
00289 node->younger = child;
00290 child->elder = node;
00291 }
00292 child->parent = parent;
00293 return;
00294
00295 }
00296
00297
00310 MtrNode *
00311 Mtr_CreateFirstChild(
00312 MtrNode * parent)
00313 {
00314 MtrNode *child;
00315
00316 child = Mtr_AllocNode();
00317 if (child == NULL) return(NULL);
00318
00319 child->child = child->younger = child-> elder = NULL;
00320 child->flags = 0;
00321 Mtr_MakeFirstChild(parent,child);
00322 return(child);
00323
00324 }
00325
00326
00339 MtrNode *
00340 Mtr_CreateLastChild(
00341 MtrNode * parent)
00342 {
00343 MtrNode *child;
00344
00345 child = Mtr_AllocNode();
00346 if (child == NULL) return(NULL);
00347
00348 child->child = child->younger = child->elder = NULL;
00349 child->flags = 0;
00350 Mtr_MakeLastChild(parent,child);
00351 return(child);
00352
00353 }
00354
00355
00368 void
00369 Mtr_MakeNextSibling(
00370 MtrNode * first,
00371 MtrNode * second)
00372 {
00373 second->younger = first->younger;
00374 if (first->younger != NULL) {
00375 first->younger->elder = second;
00376 }
00377 second->parent = first->parent;
00378 first->younger = second;
00379 second->elder = first;
00380 return;
00381
00382 }
00383
00384
00396 void
00397 Mtr_PrintTree(
00398 MtrNode * node)
00399 {
00400 if (node == NULL) return;
00401 (void) fprintf(stdout,
00402 #if SIZEOF_VOID_P == 8
00403 "N=0x%-8lx C=0x%-8lx Y=0x%-8lx E=0x%-8lx P=0x%-8lx F=%x L=%d S=%d\n",
00404 (unsigned long) node, (unsigned long) node->child,
00405 (unsigned long) node->younger, (unsigned long) node->elder,
00406 (unsigned long) node->parent, node->flags, node->low, node->size);
00407 #else
00408 "N=0x%-8x C=0x%-8x Y=0x%-8x E=0x%-8x P=0x%-8x F=%x L=%d S=%d\n",
00409 (unsigned) node, (unsigned) node->child,
00410 (unsigned) node->younger, (unsigned) node->elder,
00411 (unsigned) node->parent, node->flags, node->low, node->size);
00412 #endif
00413 if (!MTR_TEST(node,MTR_TERMINAL)) Mtr_PrintTree(node->child);
00414 Mtr_PrintTree(node->younger);
00415 return;
00416
00417 }
00418
00419
00420
00421
00422
00423
00424
00425
00426