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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*-----------------------------------------------------------------------
File : clb_pqueue.h
Author: Stephan Schulz
Contents
Functions for LIFO-lists.
Copyright 1998, 1999 by the author.
This code is released under the GNU General Public Licence and
the GNU Lesser General Public License.
See the file COPYING in the main E directory for details..
Run "eprover -h" for contact information.
Changes
<1> Tue Jun 30 17:14:42 MET DST 1998
New
-----------------------------------------------------------------------*/
#ifndef CLB_PQUEUE
#define CLB_PQUEUE
#include <clb_memory.h>
/*---------------------------------------------------------------------*/
/* Data type declarations */
/*---------------------------------------------------------------------*/
#define PQUEUE_DEFAULT_SIZE 128 /* Queues grow exponentially (and never
shrink unless explicitly freed) -
take care */
typedef struct pqueuecell
{
long size; /* Of allocateted memory */
long head; /* Where the next element will be put */
long tail; /* Where the next element will come from */
IntOrP *queue; /* Memory */
}PQueueCell, *PQueue_p;
/*---------------------------------------------------------------------*/
/* Exported Functions and Variables */
/*---------------------------------------------------------------------*/
#define PQueueCellAlloc() (PQueueCell*)SizeMalloc(sizeof(PQueueCell))
#define PQueueCellFree(junk) SizeFree(junk, sizeof(PQueueCell))
static inline PQueue_p PQueueAlloc(void);
static inline void PQueueFree(PQueue_p junk);
void PQueueGrow(PQueue_p queue);
#define PQueueEmpty(queue) ((queue)->head == (queue)->tail)
static inline void PQueueReset(PQueue_p queue);
static inline void PQueueStoreInt(PQueue_p queue, long val);
static inline void PQueueStoreP(PQueue_p queue, void* val);
static inline void PQueueBuryInt(PQueue_p queue, long val);
static inline void PQueueBuryP(PQueue_p queue, void* val);
static inline IntOrP PQueueGetNext(PQueue_p queue);
#define PQueueGetNextInt(Queue) (PQueueGetNext(Queue).i_val)
#define PQueueGetNextP(Queue) (PQueueGetNext(Queue).p_val)
static inline IntOrP PQueueGetLast(PQueue_p queue);
#define PQueueGetLastInt(Queue) (PQueueGetLast(Queue).i_val)
#define PQueueGetLastP(Queue) (PQueueGetLast(Queue).p_val)
static inline IntOrP PQueueLook(PQueue_p queue);
#define PQueueLookInt(Queue) (PQueueLook(Queue).i_val)
#define PQueueLookP(Queue) (PQueueLook(Queue).p_val)
static inline IntOrP PQueueLookLast(PQueue_p queue);
#define PQueueLookLastInt(Queue) (PQueueLookLast(Queue).i_val)
#define PQueueLookLastP(Queue) (PQueueLookLast(Queue).p_val)
long PQueueCardinality(PQueue_p queue);
IntOrP PQueueElement(PQueue_p queue, long index);
#define PQueueElementInt(Queue, index) (PQueueElement(Queue,index).i_val)
#define PQueueElementP(Queue, index) (PQueueElement(Queue,index).p_val)
long PQueueTailIndex(PQueue_p queue);
long PQueueIncIndex(PQueue_p queue, long index);
/*---------------------------------------------------------------------*/
/* Inline-Functions */
/*---------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
//
// Function: pqueue_store()
//
// Put an element in the queue.
//
// Global Variables: -
//
// Side Effects : memory operations, changes queue
//
/----------------------------------------------------------------------*/
static inline void pqueue_store(PQueue_p queue, IntOrP val)
{
queue->queue[queue->head] = val;
queue->head++;
if(queue->head == queue->size)
{
queue->head = 0;
}
if(queue->head == queue->tail)
{
PQueueGrow(queue);
}
}
/*-----------------------------------------------------------------------
//
// Function: pqueue_bury()
//
// Put an element at the front of the queue (i.e. "bury" it under
// all the other elements in a stack-view of the queue).
//
// Global Variables: -
//
// Side Effects : memory operations, changes queue
//
/----------------------------------------------------------------------*/
static inline void pqueue_bury(PQueue_p queue, IntOrP val)
{
queue->tail = queue->tail? (queue->tail-1):queue->size-1;
queue->queue[queue->tail] = val;
if(queue->head == queue->tail)
{
PQueueGrow(queue);
}
}
/*-----------------------------------------------------------------------
//
// Function: PQueueAlloc()
//
// Allocate an empty, initialized Queue.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
static inline PQueue_p PQueueAlloc(void)
{
PQueue_p handle = PQueueCellAlloc();
handle->size = PQUEUE_DEFAULT_SIZE;
handle->head = 0;
handle->tail = 0;
handle->queue = SizeMalloc(PQUEUE_DEFAULT_SIZE*sizeof(IntOrP));
return handle;
}
/*-----------------------------------------------------------------------
//
// Function: PQueueFree()
//
// Free a Queue.
//
// Global Variables: -
//
// Side Effects : Memory operations
//
/----------------------------------------------------------------------*/
static inline void PQueueFree(PQueue_p junk)
{
SizeFree(junk->queue, junk->size*sizeof(IntOrP));
PQueueCellFree(junk);
}
/*-----------------------------------------------------------------------
//
// Function: PQueueReset()
//
// Reset a queue to empty state.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void PQueueReset(PQueue_p queue)
{
queue->head = 0;
queue->tail = 0;
}
/*-----------------------------------------------------------------------
//
// Function: PQueueStoreInt()
//
// Store an integer in the queue.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void PQueueStoreInt(PQueue_p queue, long val)
{
IntOrP handle;
handle.i_val = val;
pqueue_store(queue, handle);
}
/*-----------------------------------------------------------------------
//
// Function: PQueueStoreP()
//
// Store a pointer in the queue.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void PQueueStoreP(PQueue_p queue, void* val)
{
IntOrP handle;
handle.p_val = val;
pqueue_store(queue, handle);
}
/*-----------------------------------------------------------------------
//
// Function: PQueueBuryInt()
//
// Store an integer at the front of the the queue.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void PQueueBuryInt(PQueue_p queue, long val)
{
IntOrP handle;
handle.i_val = val;
pqueue_bury(queue, handle);
}
/*-----------------------------------------------------------------------
//
// Function: PQueueBuryP()
//
// Store a pointer at the front of the queue.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline void PQueueBuryP(PQueue_p queue, void* val)
{
IntOrP handle;
handle.p_val = val;
pqueue_bury(queue, handle);
}
/*-----------------------------------------------------------------------
//
// Function: PQueueGetNext()
//
// Extract the next value from the queue and return it.
//
// Global Variables: -
//
// Side Effects : Changes queue.
//
/----------------------------------------------------------------------*/
static inline IntOrP PQueueGetNext(PQueue_p queue)
{
IntOrP res;
assert(!PQueueEmpty(queue));
res = queue->queue[queue->tail];
queue->tail++;
if(queue->tail == queue->size)
{
queue->tail = 0;
}
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PQueueGetLast()
//
// Extract the last value from the queue (i.e. pop from the queue
// viewed as a stack) and return it.
//
// Global Variables: -
//
// Side Effects : Changes queue.
//
/----------------------------------------------------------------------*/
static inline IntOrP PQueueGetLast(PQueue_p queue)
{
IntOrP res;
assert(!PQueueEmpty(queue));
queue->head = queue->head? (queue->head-1):queue->size-1;
res = queue->queue[queue->head];
return res;
}
/*-----------------------------------------------------------------------
//
// Function: PQueueLook()
//
// Return the next element from the queue without changing the
// queue.
//
// Global Variables: -
//
// Side Effects : -
//
/----------------------------------------------------------------------*/
static inline IntOrP PQueueLook(PQueue_p queue)
{
assert(!PQueueEmpty(queue));
return queue->queue[queue->tail];
}
/*-----------------------------------------------------------------------
//
// Function: PQueueLookLast()
//
// Return the last (youngest) value from the queue without modifyin
// the queue.
//
// Global Variables: -
//
// Side Effects : Changes queue.
//
/----------------------------------------------------------------------*/
static inline IntOrP PQueueLookLast(PQueue_p queue)
{
IntOrP res;
long index;
assert(!PQueueEmpty(queue));
index = queue->head? (queue->head-1):queue->size-1;
res = queue->queue[index];
return res;
}
#endif
/*---------------------------------------------------------------------*/
/* End of File */
/*---------------------------------------------------------------------*/
|