1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/**************************************************************/
/* ********************************************************** */
/* * * */
/* * RANDOM ACCESS STACK * */
/* * * */
/* * $Module: RAS * */
/* * * */
/* * Copyright (C) 1999, 2000, 2001 MPI fuer Informatik * */
/* * * */
/* * This program is free software; you can redistribute * */
/* * it and/or modify it under the terms of the FreeBSD * */
/* * Licence. * */
/* * * */
/* * This program is distributed in the hope that it will * */
/* * be useful, but WITHOUT ANY WARRANTY; without even * */
/* * the implied warranty of MERCHANTABILITY or FITNESS * */
/* * FOR A PARTICULAR PURPOSE. See the LICENCE file * */
/* * for more details. * */
/* * * */
/* * * */
/* $Revision: 1.2 $ * */
/* $State: Exp $ * */
/* $Date: 2010-02-22 14:09:58 $ * */
/* $Author: weidenb $ * */
/* * * */
/* * Contact: * */
/* * Christoph Weidenbach * */
/* * MPI fuer Informatik * */
/* * Stuhlsatzenhausweg 85 * */
/* * 66123 Saarbruecken * */
/* * Email: spass@mpi-inf.mpg.de * */
/* * Germany * */
/* * * */
/* ********************************************************** */
/**************************************************************/
#ifndef _RAS_
#define _RAS_
/**************************************************************/
/* Includes */
/**************************************************************/
#include "misc.h"
#include "memory.h"
/**************************************************************/
/* Constants and types */
/**************************************************************/
#define ras_alloc -1 /* index of size of allocated space */
#define ras_top -2 /* index of next free element */
#define ras_head 2 /* size of stack head for management purposes */
#define ras_stdsize 16 /* standard stack size */
typedef POINTER *RAS;
/* A RAS (Random Access Stack) is a pointer to an array of elements */
/* where the actual size of the stack and its current top pointer */
/* are stored one and two cells before the array pointer. */
/**************************************************************/
/* Functions */
/**************************************************************/
RAS ras_InitWithSize(RAS ras, int size);
/**************************************************************/
/* Inline Functions */
/**************************************************************/
static __inline__ RAS ras_CreateWithSize(int size)
/****************************************************************
INPUT: The maximal expected size of the stack to create.
RETURNS: A new empty stack.
*****************************************************************/
{
RAS result;
#ifdef CHECK
if (size <= 0) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_CreateWithSize: size not positive.");
misc_FinishErrorReport();
}
#endif
result = (RAS) memory_Malloc((size + ras_head) * sizeof(POINTER));
result = result + ras_head; /* leave space for head */
result[ras_alloc] = (POINTER) size;
result[ras_top] = (POINTER) 0;
return result;
}
static __inline__ RAS ras_Create(void)
{
return ras_CreateWithSize(ras_stdsize);
}
static __inline__ void ras_Free(RAS ras)
{
if (ras != NULL) {
memory_Free (
ras - ras_head,
(ras_head + (int) ras[ras_alloc]) * sizeof(POINTER)
);
}
}
static __inline__ RAS ras_Init(RAS ras)
/****************************************************************
INPUT: A random access stack.
RETURNS: The initialized and potentially new stack.
CAUTION: Because it potentially frees the old stack this
function must be called inside an assignment like:
stack = ras_InitWithSize(stack, ...)
*****************************************************************/
{
return ras_InitWithSize(ras, ras_stdsize);
}
static __inline__ int ras_Size(RAS ras)
{
return (int) ras[ras_top];
}
static __inline__ RAS ras_FastPush(RAS ras, POINTER entry)
/*********************************************************
INPUT: A random access stack and an element to push.
RETURNS: The modified stack.
CAUTION: The function does not care about stack overflow!
**********************************************************/
{
int top;
#ifdef CHECK
if (ras_Size(ras) == (int) ras[ras_alloc]) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_FastPush: stack overflow.");
misc_FinishErrorReport();
}
#endif
top = ras_Size(ras);
ras[top++] = entry;
ras[ras_top] = (POINTER) top;
return ras;
}
static __inline__ RAS ras_Push(RAS ras, POINTER entry)
/*********************************************************
INPUT: A random access stack and an element to push.
RETURNS: The modified and potentially new stack.
SUMMARY: Before the push the stack is checked for overflow
and in case of overflow its size is doubled while
elements are copied to the (new) stack.
CAUTION: Must be called inside an assignment:
stack = ras_Push(stack, ...)
**********************************************************/
{
RAS old;
int oldsize;
POINTER *oldscan, *scan;
/* if not enough space allocated, double it: */
if (ras_Size(ras) == (int) ras[ras_alloc]) {
old = ras;
oldsize = (int) old[ras_alloc];
ras = ras_CreateWithSize(oldsize * 2);
ras[ras_top] = (POINTER) oldsize;
/* copy entries: */
for (oldscan = old + oldsize - 1,scan = ras + oldsize - 1; oldscan >= old;
oldscan--, scan--)
*scan = *oldscan;
ras_Free(old);
}
return ras_FastPush(ras, entry);
}
static __inline__ BOOL ras_LegalIndex(RAS ras, int stack_index)
{
return 0 <= stack_index && stack_index < ras_Size(ras);
}
static __inline__ POINTER ras_Get(RAS ras, int stack_index)
{
#ifdef CHECK
if (!ras_LegalIndex(ras, stack_index)) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_Get: illegal stack index.");
misc_FinishErrorReport();
}
#endif
return ras[stack_index];
}
static __inline__ RAS ras_Set(RAS ras, int stack_index, POINTER entry)
{
#ifdef CHECK
if (!ras_LegalIndex(ras, stack_index)) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_Set: illegal stack index.");
misc_FinishErrorReport();
}
#endif
ras[stack_index] = entry;
return ras;
}
static __inline__ BOOL ras_Empty(RAS ras)
{
return ras_Size(ras) == 0;
}
static __inline__ POINTER ras_Pop(RAS ras)
{
int top;
#ifdef CHECK
if (ras_Empty(ras)) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_Pop: empty stack.");
misc_FinishErrorReport();
}
#endif
top = ras_Size(ras) - 1;
ras[ras_top] = (POINTER) top;
return ras[top];
}
static __inline__ POINTER ras_Top(RAS ras)
{
#ifdef CHECK
if (ras_Empty(ras)) {
misc_StartErrorReport();
misc_ErrorReport("\n In ras_Top: empty stack.");
misc_FinishErrorReport();
}
#endif
return ras[ras_Size(ras) - 1];
}
#endif
|