/*1Planarity-Related Graph Algorithms Project2Copyright (c) 1997-2010, John M. Boyer3All rights reserved. Includes a reference implementation of the following:45* John M. Boyer. "Simplified O(n) Algorithms for Planar Graph Embedding,6Kuratowski Subgraph Isolation, and Related Problems". Ph.D. Dissertation,7University of Victoria, 2001.89* John M. Boyer and Wendy J. Myrvold. "On the Cutting Edge: Simplified O(n)10Planarity by Edge Addition". Journal of Graph Algorithms and Applications,11Vol. 8, No. 3, pp. 241-273, 2004.1213* John M. Boyer. "A New Method for Efficiently Generating Planar Graph14Visibility Representations". In P. Eades and P. Healy, editors,15Proceedings of the 13th International Conference on Graph Drawing 2005,16Lecture Notes Comput. Sci., Volume 3843, pp. 508-511, Springer-Verlag, 2006.1718Redistribution and use in source and binary forms, with or without modification,19are permitted provided that the following conditions are met:2021* Redistributions of source code must retain the above copyright notice, this22list of conditions and the following disclaimer.2324* Redistributions in binary form must reproduce the above copyright notice, this25list of conditions and the following disclaimer in the documentation and/or26other materials provided with the distribution.2728* Neither the name of the Planarity-Related Graph Algorithms Project nor the names29of its contributors may be used to endorse or promote products derived from this30software without specific prior written permission.3132THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"33AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE34IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE35DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR36ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES37(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;38LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON39ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT40(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS41SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.42*/4344#include "appconst.h"45#include "stack.h"46#include <stdlib.h>4748stackP sp_New(int capacity)49{50stackP theStack;5152theStack = (stackP) malloc(sizeof(stack));5354if (theStack != NULL)55{56theStack->S = (int *) malloc(capacity*sizeof(int));57if (theStack->S == NULL)58{59free(theStack);60theStack = NULL;61}62}6364if (theStack != NULL)65{66theStack->capacity = capacity;67sp_ClearStack(theStack);68}6970return theStack;71}7273void sp_Free(stackP *pStack)74{75if (pStack == NULL || *pStack == NULL) return;7677(*pStack)->capacity = (*pStack)->size = 0;7879if ((*pStack)->S != NULL)80free((*pStack)->S);81(*pStack)->S = NULL;82free(*pStack);8384*pStack = NULL;85}8687int sp_CopyContent(stackP stackDst, stackP stackSrc)88{89if (stackDst->capacity < stackSrc->size)90return NOTOK;9192if (stackSrc->size > 0)93memcpy(stackDst->S, stackSrc->S, stackSrc->size*sizeof(int));9495stackDst->size = stackSrc->size;96return OK;97}9899stackP sp_Duplicate(stackP theStack)100{101stackP newStack = sp_New(theStack->capacity);102103if (newStack == NULL)104return NULL;105106if (theStack->size > 0)107memcpy(newStack->S, theStack->S, theStack->size*sizeof(int));108109return newStack;110}111112int sp_Copy(stackP stackDst, stackP stackSrc)113{114if (sp_CopyContent(stackDst, stackSrc) != OK)115{116stackP newStack = sp_Duplicate(stackSrc);117int *p;118119if (newStack == NULL)120return NOTOK;121122p = stackDst->S;123stackDst->S = newStack->S;124newStack->S = p;125newStack->capacity = stackDst->capacity;126sp_Free(&newStack);127128stackDst->size = stackSrc->size;129stackDst->capacity = stackSrc->capacity;130}131132return OK;133}134135#ifndef SPEED_MACROS136137int sp_ClearStack(stackP theStack)138{139theStack->size = 0;140return OK;141}142143int sp_GetCurrentSize(stackP theStack)144{145return theStack->size;146}147148int sp_SetCurrentSize(stackP theStack, int size)149{150return size > theStack->capacity ? NOTOK : (theStack->size = size, OK);151}152153int sp_IsEmpty(stackP theStack)154{155return !theStack->size;156}157158int sp_NonEmpty(stackP theStack)159{160return theStack->size;161}162163int sp__Push(stackP theStack, int a)164{165if (theStack->size >= theStack->capacity)166return NOTOK;167168theStack->S[theStack->size++] = a;169return OK;170}171172int sp__Push2(stackP theStack, int a, int b)173{174if (theStack->size + 1 >= theStack->capacity)175return NOTOK;176177theStack->S[theStack->size++] = a;178theStack->S[theStack->size++] = b;179return OK;180}181182int sp__Pop(stackP theStack, int *pA)183{184if (theStack->size <= 0)185return NOTOK;186187*pA = theStack->S[--theStack->size];188return OK;189}190191int sp__Pop2(stackP theStack, int *pA, int *pB)192{193if (theStack->size <= 1)194return NOTOK;195196*pB = theStack->S[--theStack->size];197*pA = theStack->S[--theStack->size];198199return OK;200}201202int sp_Top(stackP theStack)203{204return theStack->size ? theStack->S[theStack->size-1] : NIL;205}206207int sp_Get(stackP theStack, int pos)208{209if (theStack == NULL || pos < 0 || pos >= theStack->size)210return NOTOK;211212return (theStack->S[pos]);213}214215int sp_Set(stackP theStack, int pos, int val)216{217if (theStack == NULL || pos < 0 || pos >= theStack->size)218return NOTOK;219220return (theStack->S[pos] = val);221}222223#endif // not defined SPEED_MACROS224225226