/**Function******************************************************************** Synopsis [Initializes tree with one node.] Description [Initializes tree with one node. Returns pointer to node.] SideEffects [None] SeeAlso [Mtr_FreeTree Mtr_InitGroupTree] ******************************************************************************/ MtrNode * Mtr_InitTree(void) { MtrNode *node; node = Mtr_AllocNode(); if (node == NULL) return(NULL); node->parent = node->child = node->elder = node->younger = NULL; node->flags = 0; return(node); } /* end of Mtr_InitTree */
/**Function******************************************************************** Synopsis [Creates a new node and makes it the last child of parent.] Description [Creates a new node and makes it the last child of parent. Returns pointer to new child.] SideEffects [None] SeeAlso [Mtr_MakeLastChild Mtr_CreateFirstChild] ******************************************************************************/ MtrNode * Mtr_CreateLastChild( MtrNode * parent) { MtrNode *child; child = Mtr_AllocNode(); if (child == NULL) return(NULL); child->child = NULL; child->flags = 0; Mtr_MakeLastChild(parent,child); return(child); } /* end of Mtr_CreateLastChild */
/**Function******************************************************************** Synopsis [Makes a copy of tree.] Description [Makes a copy of tree. If parameter expansion is greater than 1, it will expand the tree by that factor. It is an error for expansion to be less than 1. Returns a pointer to the copy if successful; NULL otherwise.] SideEffects [None] SeeAlso [Mtr_InitTree] ******************************************************************************/ MtrNode * Mtr_CopyTree( MtrNode * node, int expansion) { MtrNode *copy; if (node == NULL) return(NULL); if (expansion < 1) return(NULL); copy = Mtr_AllocNode(); if (copy == NULL) return(NULL); copy->parent = copy->elder = copy->child = copy->younger = NULL; if (node->child != NULL) { copy->child = Mtr_CopyTree(node->child, expansion); if (copy->child == NULL) { Mtr_DeallocNode(copy); return(NULL); } } if (node->younger != NULL) { copy->younger = Mtr_CopyTree(node->younger, expansion); if (copy->younger == NULL) { Mtr_FreeTree(copy); return(NULL); } } copy->flags = node->flags; copy->low = node->low * expansion; copy->size = node->size * expansion; copy->index = node->index * expansion; if (copy->younger) copy->younger->elder = copy; if (copy->child) { MtrNode *auxnode = copy->child; while (auxnode != NULL) { auxnode->parent = copy; auxnode = auxnode->younger; } } return(copy); } /* end of Mtr_CopyTree */
/**Function******************************************************************** Synopsis [Makes a new group with size leaves starting at low.] Description [Makes a new group with size leaves starting at low. If the new group intersects an existing group, it must either contain it or be contained by it. This procedure relies on the low and size fields of each node. It also assumes that the children of each node are sorted in order of increasing low. In case of a valid request, the flags of the new group are set to the value passed in `flags.' This can also be used to change the flags of an existing group. Returns the pointer to the root of the new group upon successful termination; NULL otherwise. If the group already exists, the pointer to its root is returned.] SideEffects [None] SeeAlso [Mtr_DissolveGroup Mtr_ReadGroups Mtr_FindGroup] ******************************************************************************/ MtrNode * Mtr_MakeGroup( MtrNode * root /* root of the group tree */, unsigned int low /* lower bound of the group */, unsigned int size /* upper bound of the group */, unsigned int flags /* flags for the new group */) { MtrNode *node, *first, *last, *previous, *newn; /* Sanity check. */ if (size == 0) return(NULL); /* Check whether current group includes new group. This check is ** necessary at the top-level call. In the subsequent calls it is ** redundant. */ if (low < (unsigned int) root->low || low + size > (unsigned int) (root->low + root->size)) return(NULL); /* Trying to create an existing group has the effect of updating ** the flags. */ if (root->size == size && root->low == low) { root->flags = flags; return(root); } /* At this point we know that the new group is properly contained ** in the group of root. We have two possible cases here: - root ** is a terminal node; - root has children. */ /* Root has no children: create a new group. */ if (root->child == NULL) { newn = Mtr_AllocNode(); if (newn == NULL) return(NULL); /* out of memory */ newn->low = low; newn->size = size; newn->flags = flags; newn->parent = root; newn->elder = newn->younger = newn->child = NULL; root->child = newn; return(newn); } /* Root has children: Find all chidren of root that are included ** in the new group. If the group of any child entirely contains ** the new group, call Mtr_MakeGroup recursively. */ previous = NULL; first = root->child; /* guaranteed to be non-NULL */ while (first != NULL && low >= (unsigned int) (first->low + first->size)) { previous = first; first = first->younger; } if (first == NULL) { /* We have scanned the entire list and we need to append a new ** child at the end of it. Previous points to the last child ** of root. */ newn = Mtr_AllocNode(); if (newn == NULL) return(NULL); /* out of memory */ newn->low = low; newn->size = size; newn->flags = flags; newn->parent = root; newn->elder = previous; previous->younger = newn; newn->younger = newn->child = NULL; return(newn); } /* Here first is non-NULL and low < first->low + first->size. */ if (low >= (unsigned int) first->low && low + size <= (unsigned int) (first->low + first->size)) { /* The new group is contained in the group of first. */ newn = Mtr_MakeGroup(first, low, size, flags); return(newn); } else if (low + size <= first->low) { /* The new group is entirely contained in the gap between ** previous and first. */ newn = Mtr_AllocNode(); if (newn == NULL) return(NULL); /* out of memory */ newn->low = low; newn->size = size; newn->flags = flags; newn->child = NULL; newn->parent = root; newn->elder = previous; newn->younger = first; first->elder = newn; if (previous != NULL) { previous->younger = newn; } else { root->child = newn; } return(newn); } else if (low < (unsigned int) first->low && low + size < (unsigned int) (first->low + first->size)) { /* Trying to cut an existing group: not allowed. */ return(NULL); } else if (low > first->low) { /* The new group neither is contained in the group of first ** (this was tested above) nor contains it. It is therefore ** trying to cut an existing group: not allowed. */ return(NULL); } /* First holds the pointer to the first child contained in the new ** group. Here low <= first->low and low + size >= first->low + ** first->size. One of the two inequalities is strict. */ last = first->younger; while (last != NULL && (unsigned int) (last->low + last->size) < low + size) { last = last->younger; } if (last == NULL) { /* All the chilren of root from first onward become children ** of the new group. */ newn = Mtr_AllocNode(); if (newn == NULL) return(NULL); /* out of memory */ newn->low = low; newn->size = size; newn->flags = flags; newn->child = first; newn->parent = root; newn->elder = previous; newn->younger = NULL; first->elder = NULL; if (previous != NULL) { previous->younger = newn; } else { root->child = newn; } last = first; while (last != NULL) { last->parent = newn; last = last->younger; } return(newn); } /* Here last != NULL and low + size <= last->low + last->size. */ if (low + size - 1 >= (unsigned int) last->low && low + size <= (unsigned int) (last->low + last->size)) { /* Trying to cut an existing group: not allowed. */ return(NULL); } /* First and last point to the first and last of the children of ** root that are included in the new group. Allocate a new node ** and make all children of root between first and last chidren of ** the new node. Previous points to the child of root immediately ** preceeding first. If it is NULL, then first is the first child ** of root. */ newn = Mtr_AllocNode(); if (newn == NULL) return(NULL); /* out of memory */ newn->low = low; newn->size = size; newn->flags = flags; newn->child = first; newn->parent = root; if (previous == NULL) { root->child = newn; } else { previous->younger = newn; } newn->elder = previous; newn->younger = last->younger; if (last->younger != NULL) { last->younger->elder = newn; } last->younger = NULL; first->elder = NULL; for (node = first; node != NULL; node = node->younger) { node->parent = newn; } return(newn); } /* end of Mtr_MakeGroup */
/**Function******************************************************************** Synopsis [Main program for testmtr.] Description [Main program for testmtr. Performs initialization. Reads command line options and network(s). Builds some simple trees and prints them out.] SideEffects [None] SeeAlso [] ******************************************************************************/ int main( int argc, char ** argv) { MtrNode *root, *node; int i, c, pr = 0; FILE *fp; const char *file = NULL; while ((c = getopt(argc, argv, "Mhp:")) != EOF) { switch(c) { case 'M': break; case 'p': pr = atoi(optarg); break; case 'h': default: printHeader(argc, argv); usage(argv[0]); break; } } if (argc - optind == 0) { file = "-"; } else if (argc - optind == 1) { file = argv[optind]; } else { printHeader(argc, argv); usage(argv[0]); } if (pr > 0) printHeader(argc, argv); /* Create and print a simple tree. */ root = Mtr_InitTree(); root->flags = 0; node = Mtr_CreateFirstChild(root); node->flags = 1; node = Mtr_CreateLastChild(root); node->flags = 2; node = Mtr_CreateFirstChild(root); node->flags = 3; node = Mtr_AllocNode(); node->child = NULL; node->flags = 4; Mtr_MakeNextSibling(root->child,node); if (pr > 0) { Mtr_PrintTree(root); (void) printf("#------------------------\n"); } Mtr_FreeTree(root); /* Create an initial tree in which all variables belong to one group. */ root = Mtr_InitGroupTree(0,12); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } node = Mtr_MakeGroup(root,0,6,MTR_DEFAULT); node = Mtr_MakeGroup(root,6,6,MTR_DEFAULT); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } for (i = 0; i < 6; i+=2) { node = Mtr_MakeGroup(root,(unsigned) i,(unsigned) 2,MTR_DEFAULT); } node = Mtr_MakeGroup(root,0,12,MTR_FIXED); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); /* Print a partial tree. */ (void) printf("# "); Mtr_PrintGroups(root->child,pr == 0); (void) printf("\n"); } node = Mtr_FindGroup(root,0,6); node = Mtr_DissolveGroup(node); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } node = Mtr_FindGroup(root,4,2); if (!Mtr_SwapGroups(node,node->younger)) { (void) printf("error in Mtr_SwapGroups\n"); return 3; } if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("#------------------------\n"); } Mtr_FreeTree(root); /* Create a group tree with fixed subgroups. */ root = Mtr_InitGroupTree(0,4); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } node = Mtr_MakeGroup(root,0,2,MTR_FIXED); node = Mtr_MakeGroup(root,2,2,MTR_FIXED); if (pr > 0) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } Mtr_FreeTree(root); if (pr > 0) { (void) printf("#------------------------\n"); } /* Open input file. */ fp = open_file(file, "r"); root = Mtr_ReadGroups(fp,12); fclose(fp); if (pr > 0) { if (root) { Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); } else { (void) printf("error in group file\n"); } } Mtr_FreeTree(root); return 0; } /* end of main */
/**Function******************************************************************** Synopsis [Main program for testmtr.] Description [Main program for testmtr. Performs initialization. Reads command line options and network(s). Builds some simple trees and prints them out.] SideEffects [None] SeeAlso [] ******************************************************************************/ int main( int argc, char ** argv) { MtrNode *root, *node; int i, c, pr = 0; FILE *fp; char *file = NULL; (void) printf("# %s\n", TESTMTR_VERSION); /* Echo command line and arguments. */ (void) printf("#"); for(i = 0; i < argc; i++) { (void) printf(" %s", argv[i]); } (void) printf("\n"); (void) fflush(stdout); while ((c = getopt(argc, argv, "Mhp:")) != EOF) { switch(c) { case 'M': #ifdef MNEMOSYNE (void) mnem_setrecording(0); #endif break; case 'p': pr = atoi(optarg); break; case 'h': default: usage(argv[0]); break; } } if (argc - optind == 0) { file = "-"; } else if (argc - optind == 1) { file = argv[optind]; } else { usage(argv[0]); } /* Create and print a simple tree. */ root = Mtr_InitTree(); root->flags = 0; node = Mtr_CreateFirstChild(root); node->flags = 1; node = Mtr_CreateLastChild(root); node->flags = 2; node = Mtr_CreateFirstChild(root); node->flags = 3; node = Mtr_AllocNode(); node->flags = 4; Mtr_MakeNextSibling(root->child,node); Mtr_PrintTree(root); Mtr_FreeTree(root); (void) printf("#------------------------\n"); /* Create an initial tree in which all variables belong to one group. */ root = Mtr_InitGroupTree(0,12); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); node = Mtr_MakeGroup(root,0,6,MTR_DEFAULT); node = Mtr_MakeGroup(root,6,6,MTR_DEFAULT); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); for (i = 0; i < 6; i+=2) { node = Mtr_MakeGroup(root,(unsigned) i,(unsigned) 2,MTR_DEFAULT); } node = Mtr_MakeGroup(root,0,12,MTR_FIXED); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); /* Print a partial tree. */ (void) printf("# "); Mtr_PrintGroups(root->child,pr == 0); (void) printf("\n"); node = Mtr_FindGroup(root,0,6); node = Mtr_DissolveGroup(node); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); node = Mtr_FindGroup(root,4,2); if (!Mtr_SwapGroups(node,node->younger)) { (void) printf("error in Mtr_SwapGroups\n"); exit(3); } Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); Mtr_FreeTree(root); (void) printf("#------------------------\n"); /* Create a group tree with fixed subgroups. */ root = Mtr_InitGroupTree(0,4); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); node = Mtr_MakeGroup(root,0,2,MTR_FIXED); node = Mtr_MakeGroup(root,2,2,MTR_FIXED); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); Mtr_FreeTree(root); (void) printf("#------------------------\n"); /* Open input file. */ fp = open_file(file, "r"); root = Mtr_ReadGroups(fp,12); Mtr_PrintTree(root); (void) printf("# "); Mtr_PrintGroups(root,pr == 0); (void) printf("\n"); Mtr_FreeTree(root); #ifdef MNEMOSYNE mnem_writestats(); #endif exit(0); /* NOTREACHED */ } /* end of main */