Exemple #1
0
/**Function********************************************************************

  Synopsis    [Creates a new ZDD variable group.]

  Description [Creates a new ZDD variable group. The group starts at
  variable and contains size variables. The parameter low is the index
  of the first variable. If the variable already exists, its current
  position in the order is known to the manager. If the variable does
  not exist yet, the position is assumed to be the same as the index.
  The group tree is created if it does not exist yet.
  Returns a pointer to the group if successful; NULL otherwise.]

  SideEffects [The ZDD variable tree is changed.]

  SeeAlso     [Cudd_MakeTreeNode]

******************************************************************************/
MtrNode *
Cudd_MakeZddTreeNode(
  DdManager * dd /* manager */,
  unsigned int  low /* index of the first group variable */,
  unsigned int  size /* number of variables in the group */,
  unsigned int  type /* MTR_DEFAULT or MTR_FIXED */)
{
    MtrNode *group;
    MtrNode *tree;
    unsigned int level;

    /* If the variable does not exist yet, the position is assumed to be
    ** the same as the index. Therefore, applications that rely on
    ** Cudd_bddNewVarAtLevel or Cudd_addNewVarAtLevel to create new
    ** variables have to create the variables before they group them.
    */
    level = (low < (unsigned int) dd->sizeZ) ? dd->permZ[low] : low;

    if (level + size - 1> (int) MTR_MAXHIGH)
	return(NULL);

    /* If the tree does not exist yet, create it. */
    tree = dd->treeZ;
    if (tree == NULL) {
	dd->treeZ = tree = Mtr_InitGroupTree(0, dd->sizeZ);
	if (tree == NULL)
	    return(NULL);
	tree->index = dd->invpermZ[0];
    }

    /* Extend the upper bound of the tree if necessary. This allows the
    ** application to create groups even before the variables are created.
    */
    tree->size = ddMax(tree->size, level + size);

    /* Create the group. */
    group = Mtr_MakeGroup(tree, level, size, type);
    if (group == NULL)
	return(NULL);

    /* Initialize the index field to the index of the variable currently
    ** in position low. This field will be updated by the reordering
    ** procedure to provide a handle to the group once it has been moved.
    */
    group->index = (MtrHalfWord) low;

    return(group);

} /* end of Cudd_MakeZddTreeNode */
Exemple #2
0
/**Function********************************************************************

  Synopsis    [Reads groups from a file and creates a group tree.]

  Description [Reads groups from a file and creates a group tree.
  Each group is specified by three fields:
  <xmp>
       low size flags.
  </xmp>
  Low and size are (short) integers. Flags is a string composed of the
  following characters (with associated translation):
  <ul>
  <li>D: MTR_DEFAULT
  <li>F: MTR_FIXED
  <li>N: MTR_NEWNODE
  <li>S: MTR_SOFT
  <li>T: MTR_TERMINAL
  </ul>
  Normally, the only flags that are needed are D and F.  Groups and
  fields are separated by white space (spaces, tabs, and newlines).
  Returns a pointer to the group tree if successful; NULL otherwise.]

  SideEffects [None]

  SeeAlso     [Mtr_InitGroupTree Mtr_MakeGroup]

******************************************************************************/
MtrNode *
Mtr_ReadGroups(
  FILE * fp /* file pointer */,
  int  nleaves /* number of leaves of the new tree */)
{
    int low;
    int size;
    int err;
    unsigned int flags;
    MtrNode *root;
    MtrNode *node;
    char attrib[8*sizeof(unsigned int)+1];
    char *c;

    root = Mtr_InitGroupTree(0,nleaves);
    if (root == NULL) return NULL;

    while (! feof(fp)) {
	/* Read a triple and check for consistency. */
	err = fscanf(fp, "%d %d %s", &low, &size, attrib);
	if (err == EOF) {
	    break;
	} else if (err != 3) {
	    return(NULL);
	} else if (low < 0 || low+size > nleaves || size < 1) {
	    return(NULL);
	} else if (strlen(attrib) > 8 * sizeof(MtrHalfWord)) {
	    /* Not enough bits in the flags word to store these many
	    ** attributes. */
	    return(NULL);
	}

	/* Parse the flag string. Currently all flags are permitted,
	** to make debugging easier. Normally, specifying NEWNODE
	** wouldn't be allowed. */
	flags = MTR_DEFAULT;
	for (c=attrib; *c != 0; c++) {
	    switch (*c) {
	    case 'D':
		break;
	    case 'F':
		flags |= MTR_FIXED;
		break;
	    case 'N':
		flags |= MTR_NEWNODE;
		break;
	    case 'S':
		flags |= MTR_SOFT;
		break;
	    case 'T':
		flags |= MTR_TERMINAL;
		break;
	    default:
		return NULL;
	    }
	}
	node = Mtr_MakeGroup(root, (MtrHalfWord) low, (MtrHalfWord) size,
			     flags);
	if (node == NULL) return(NULL);
    }

    return(root);

} /* end of Mtr_ReadGroups */
Exemple #3
0
/**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 */
Exemple #4
0
/**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 */
Exemple #5
0
/**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 */