forked from arminbiere/satch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.h
60 lines (43 loc) · 1.96 KB
/
queue.h
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
#ifndef _queue_h_INCLUDED
#define _queue_h_INCLUDED
/*------------------------------------------------------------------------*/
// This is a simple almost generic implementation of a queue with enqueue
// and dequeue operations. It is only 'almost' generic since it requires
// too much memory in case the number of elements in the queue remains much
// smaller than the number of enqueued elements. For such a use case you
// should not use this queue. See also the comment below before 'DEQUEUE'.
/*------------------------------------------------------------------------*/
#include "stack.h"
struct int_queue
{
struct int_stack stack;
int *head;
};
/*------------------------------------------------------------------------*/
#define EMPTY_QUEUE(Q) ((Q).stack.end == (Q).head)
#define SIZE_QUEUE(Q) ((size_t)((Q).stack.end - (Q).head))
#define INIT_QUEUE(Q) \
do { INIT_STACK ((Q).stack); (Q).head = (Q).stack.begin; } while (0)
#define RELEASE_QUEUE(Q) RELEASE_STACK ((Q).stack)
/*------------------------------------------------------------------------*/
// Enqueuing an elements amounts to push it on the stack and adapt the
// 'head' pointer in case the stack has to be enlarged (which we do here
// explicitly but borrowing the 'ENLARGE_STACK' operation from stacks).
#define ENQUEUE(Q,E) \
do { \
if (FULL_STACK ((Q).stack)) \
{ \
size_t OFFSET = (Q).head - (Q).stack.begin; \
ENLARGE_STACK ((Q).stack); \
(Q).head = (Q).stack.begin + OFFSET; \
} \
*(Q).stack.end++ = (E); \
} while (0)
/*------------------------------------------------------------------------*/
// This is too simplistic in general, since we will allocate as much stack
// memory as there are 'ENQUEUE' operations. If for instance the stack size
// is twice as big as the queue size, we might want to shrink everything.
#define DEQUEUE(Q) \
(assert (!EMPTY_QUEUE (Q)), *(Q).head++)
/*------------------------------------------------------------------------*/
#endif