Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/graphs/planarity/stack.c
4057 views
1
/*
2
Planarity-Related Graph Algorithms Project
3
Copyright (c) 1997-2010, John M. Boyer
4
All rights reserved. Includes a reference implementation of the following:
5
6
* John M. Boyer. "Simplified O(n) Algorithms for Planar Graph Embedding,
7
Kuratowski Subgraph Isolation, and Related Problems". Ph.D. Dissertation,
8
University of Victoria, 2001.
9
10
* John M. Boyer and Wendy J. Myrvold. "On the Cutting Edge: Simplified O(n)
11
Planarity by Edge Addition". Journal of Graph Algorithms and Applications,
12
Vol. 8, No. 3, pp. 241-273, 2004.
13
14
* John M. Boyer. "A New Method for Efficiently Generating Planar Graph
15
Visibility Representations". In P. Eades and P. Healy, editors,
16
Proceedings of the 13th International Conference on Graph Drawing 2005,
17
Lecture Notes Comput. Sci., Volume 3843, pp. 508-511, Springer-Verlag, 2006.
18
19
Redistribution and use in source and binary forms, with or without modification,
20
are permitted provided that the following conditions are met:
21
22
* Redistributions of source code must retain the above copyright notice, this
23
list of conditions and the following disclaimer.
24
25
* Redistributions in binary form must reproduce the above copyright notice, this
26
list of conditions and the following disclaimer in the documentation and/or
27
other materials provided with the distribution.
28
29
* Neither the name of the Planarity-Related Graph Algorithms Project nor the names
30
of its contributors may be used to endorse or promote products derived from this
31
software without specific prior written permission.
32
33
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
*/
44
45
#include "appconst.h"
46
#include "stack.h"
47
#include <stdlib.h>
48
49
stackP sp_New(int capacity)
50
{
51
stackP theStack;
52
53
theStack = (stackP) malloc(sizeof(stack));
54
55
if (theStack != NULL)
56
{
57
theStack->S = (int *) malloc(capacity*sizeof(int));
58
if (theStack->S == NULL)
59
{
60
free(theStack);
61
theStack = NULL;
62
}
63
}
64
65
if (theStack != NULL)
66
{
67
theStack->capacity = capacity;
68
sp_ClearStack(theStack);
69
}
70
71
return theStack;
72
}
73
74
void sp_Free(stackP *pStack)
75
{
76
if (pStack == NULL || *pStack == NULL) return;
77
78
(*pStack)->capacity = (*pStack)->size = 0;
79
80
if ((*pStack)->S != NULL)
81
free((*pStack)->S);
82
(*pStack)->S = NULL;
83
free(*pStack);
84
85
*pStack = NULL;
86
}
87
88
int sp_CopyContent(stackP stackDst, stackP stackSrc)
89
{
90
if (stackDst->capacity < stackSrc->size)
91
return NOTOK;
92
93
if (stackSrc->size > 0)
94
memcpy(stackDst->S, stackSrc->S, stackSrc->size*sizeof(int));
95
96
stackDst->size = stackSrc->size;
97
return OK;
98
}
99
100
stackP sp_Duplicate(stackP theStack)
101
{
102
stackP newStack = sp_New(theStack->capacity);
103
104
if (newStack == NULL)
105
return NULL;
106
107
if (theStack->size > 0)
108
memcpy(newStack->S, theStack->S, theStack->size*sizeof(int));
109
110
return newStack;
111
}
112
113
int sp_Copy(stackP stackDst, stackP stackSrc)
114
{
115
if (sp_CopyContent(stackDst, stackSrc) != OK)
116
{
117
stackP newStack = sp_Duplicate(stackSrc);
118
int *p;
119
120
if (newStack == NULL)
121
return NOTOK;
122
123
p = stackDst->S;
124
stackDst->S = newStack->S;
125
newStack->S = p;
126
newStack->capacity = stackDst->capacity;
127
sp_Free(&newStack);
128
129
stackDst->size = stackSrc->size;
130
stackDst->capacity = stackSrc->capacity;
131
}
132
133
return OK;
134
}
135
136
#ifndef SPEED_MACROS
137
138
int sp_ClearStack(stackP theStack)
139
{
140
theStack->size = 0;
141
return OK;
142
}
143
144
int sp_GetCurrentSize(stackP theStack)
145
{
146
return theStack->size;
147
}
148
149
int sp_SetCurrentSize(stackP theStack, int size)
150
{
151
return size > theStack->capacity ? NOTOK : (theStack->size = size, OK);
152
}
153
154
int sp_IsEmpty(stackP theStack)
155
{
156
return !theStack->size;
157
}
158
159
int sp_NonEmpty(stackP theStack)
160
{
161
return theStack->size;
162
}
163
164
int sp__Push(stackP theStack, int a)
165
{
166
if (theStack->size >= theStack->capacity)
167
return NOTOK;
168
169
theStack->S[theStack->size++] = a;
170
return OK;
171
}
172
173
int sp__Push2(stackP theStack, int a, int b)
174
{
175
if (theStack->size + 1 >= theStack->capacity)
176
return NOTOK;
177
178
theStack->S[theStack->size++] = a;
179
theStack->S[theStack->size++] = b;
180
return OK;
181
}
182
183
int sp__Pop(stackP theStack, int *pA)
184
{
185
if (theStack->size <= 0)
186
return NOTOK;
187
188
*pA = theStack->S[--theStack->size];
189
return OK;
190
}
191
192
int sp__Pop2(stackP theStack, int *pA, int *pB)
193
{
194
if (theStack->size <= 1)
195
return NOTOK;
196
197
*pB = theStack->S[--theStack->size];
198
*pA = theStack->S[--theStack->size];
199
200
return OK;
201
}
202
203
int sp_Top(stackP theStack)
204
{
205
return theStack->size ? theStack->S[theStack->size-1] : NIL;
206
}
207
208
int sp_Get(stackP theStack, int pos)
209
{
210
if (theStack == NULL || pos < 0 || pos >= theStack->size)
211
return NOTOK;
212
213
return (theStack->S[pos]);
214
}
215
216
int sp_Set(stackP theStack, int pos, int val)
217
{
218
if (theStack == NULL || pos < 0 || pos >= theStack->size)
219
return NOTOK;
220
221
return (theStack->S[pos] = val);
222
}
223
224
#endif // not defined SPEED_MACROS
225
226