2 /* -----------------------------------------------------------------------------
3 * $Id: QueueTemplate.h,v 1.4 1999/04/27 10:07:19 sewardj Exp $
5 * (c) The GHC Team, 1998
7 * Template for generating queues of various types
9 * #define Queue and Type before #including this file
10 * to define the following:
12 * typedef { Type* elems; nat used; nat size } Queue;
13 * static void insertQueue( Queue* q, Type i );
14 * static void initQueue ( Queue* q );
15 * static void setQueue ( Queue* q, nat i, Type x );
16 * static void freeQueue ( Queue* q );
18 * $RCSfile: QueueTemplate.h,v $
20 * $Date: 1999/04/27 10:07:19 $
22 * ------------------------------------------------------------------------*/
24 /* These macros are rather delicate - read a good ANSI C book carefully
28 #define mycat(x,y) x##y
29 #define mycat2(x,y) mycat(x,y)
30 #define mycat3(x,y,z) mycat2(x,mycat2(y,z))
35 nat len; /* always <= size */
40 static void mycat2(init,Queue)( Queue* q )
44 q->elems = malloc(q->size * sizeof(Type));
45 if (q->elems == NULL) {
46 barf("Out of memory: can't allocate initial " mystr(Queue) " space");
51 static void mycat2(free,Queue)( Queue* q )
58 static void mycat2(insert,Queue)( Queue* q, Type x )
61 if (q->len == q->size) {
62 Type* elems2 = malloc(2 * q->size * sizeof(Type));
64 barf("Out of memory: can't resize " mystr(Queue) " space");
66 for (i = 0; i < q->len; i++)
67 elems2[i] = q->elems[i];
77 void mycat2(set,Queue)( Queue* q, nat i, Type x )
85 /* evaluate a statement s once for every element in a queue q.
86 * i and x are usually free in s
87 * queueTy and eltTy are the types of the container and element respectively
89 #define mapQueue(queueTy,eltTy,q,s) \
93 while( i < (q).len ) { \
100 /* --------------------------------------------------------------------------
101 * End of Queue template
102 * ------------------------------------------------------------------------*/