/**Function******************************************************************** Synopsis [Finds a group with size leaves starting at low, if it exists.] Description [Finds a group with size leaves starting at low, if it exists. 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. Returns the pointer to the root of the group upon successful termination; NULL otherwise.] SideEffects [None] SeeAlso [] ******************************************************************************/ MtrNode * Mtr_FindGroup( MtrNode * root /* root of the group tree */, unsigned int low /* lower bound of the group */, unsigned int size /* upper bound of the group */) { MtrNode *node; #ifdef MTR_DEBUG /* We cannot have a non-empty proper subgroup of a singleton set. */ assert(!MTR_TEST(root,MTR_TERMINAL)); #endif /* Sanity check. */ if (size < 1) return(NULL); /* Check whether current group includes the group sought. 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); if (root->size == size && root->low == low) return(root); if (root->child == NULL) return(NULL); /* 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. */ node = root->child; while (low >= (unsigned int) (node->low + node->size)) { node = node->younger; } if (low + size <= (unsigned int) (node->low + node->size)) { /* The group is contained in the group of node. */ node = Mtr_FindGroup(node, low, size); return(node); } else { return(NULL); } } /* end of Mtr_FindGroup */
/**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 */