Graphviz 2.29.20120208.0545
lib/circogen/stack.c
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 
00015 #include        "stack.h"
00016 #include        "circular.h"
00017 #include        <assert.h>
00018 
00019 nstack_t *mkStack()
00020 {
00021     nstack_t *s;
00022 
00023     s = NEW(nstack_t);
00024 
00025     s->top = NULL;
00026     s->sz = 0;
00027     return s;
00028 }
00029 
00030 void freeStack(nstack_t * s)
00031 {
00032     free(s);
00033 }
00034 
00035 void stackPush(nstack_t * s, Agnode_t * n)
00036 {
00037     SET_ONSTACK(n);
00038     NEXT(n) = s->top;
00039     s->top = n;
00040     s->sz += 1;
00041 }
00042 
00043 Agnode_t *stackPop(nstack_t * s)
00044 {
00045     Agnode_t *top = s->top;
00046 
00047     if (top) {
00048         assert(s->sz > 0);
00049         UNSET_ONSTACK(top);
00050         s->top = NEXT(top);
00051         s->sz -= 1;
00052     } else {
00053         assert(0);
00054     }
00055 
00056     return top;
00057 }
00058 
00059 int stackSize(nstack_t * s)
00060 {
00061     return s->sz;
00062 }
00063 
00064 /* stackCheck:
00065  * Return true if n in on the stack.
00066  */
00067 int stackCheck(nstack_t * s, Agnode_t * n)
00068 {
00069     return ONSTACK(n);
00070 #ifdef OLD
00071     stackitem_t *top = s->top;
00072     Agnode_t *node;
00073 
00074     while (top != NULL) {
00075         node = top->data;
00076         if (node == n)
00077             return 1;
00078         top = top->next;
00079     }
00080 
00081     return 0;
00082 #endif
00083 }
00084 
00085 #ifdef DEBUG
00086 void printStack(nstack_t * s)
00087 {
00088     Agnode_t *n;
00089     for (n = s->top; n; n = NEXT(n))
00090         fprintf(stderr, " %s", n->name);
00091     fprintf(stderr, "\n");
00092 
00093 }
00094 #endif