3 * File name: ir/adt/pdeq.c
4 * Purpose: Pdeq --- double ended queue of generic pointers.
5 * Author: Christian von Roques
7 * Created: 1999 by getting from fiasco
9 * Copyright: (c) 1995, 1996 Christian von Roques
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
33 /* Pointer Double Ended Queue */
34 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
35 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
37 /** Size of pdeq block cache. */
38 #define TUNE_NSAVED_PDEQS 16
41 * Maximal number of data items in a pdeq chunk.
43 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
46 # define VRFY(dq) ((void)0)
48 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
52 * A pointer double ended queue.
53 * This structure is used as a list chunk either.
57 unsigned magic; /**< debug magic */
59 pdeq *l_end, *r_end; /**< left and right ends of the queue */
60 pdeq *l, *r; /**< left and right neighbor */
61 int n; /**< number of elements in the current chunk */
62 int p; /**< the read/write pointer */
63 const void *data[1]; /**< storage for elements */
68 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
69 * +1 for compilers that can't grok empty arrays
71 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
74 * Number of pdeqs in pdeq_store.
76 static unsigned pdeqs_cached;
79 * Free a pdeq chunk, put in into the cache if possible.
81 * @param p The pdeq chunk.
83 static INLINE void free_pdeq_block (pdeq *p)
86 p->magic = 0xbadf00d1;
88 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
89 pdeq_block_cache[pdeqs_cached++] = p;
96 * Allocate a new pdeq chunk, get it from the cache if possible.
98 * @return A new pdeq chunk.
100 static INLINE pdeq *alloc_pdeq_block (void)
103 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
104 p = pdeq_block_cache[--pdeqs_cached];
106 p = xmalloc(PREF_MALLOC_SIZE);
114 * Verify a double ended list, assert if failure.
116 * @param dq The list to verify.
118 void _pdeq_vrfy(pdeq *dq)
123 && (dq->magic == PDEQ_MAGIC1)
124 && (dq->l_end && dq->r_end));
127 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
128 && ((q == dq->l_end) ^ (q->l != NULL))
129 && ((q == dq->r_end) ^ (q->r != NULL))
130 && (!q->l || (q == q->l->r))
131 && ((q->n >= 0) && (q->n <= NDATA))
132 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
133 && ((q->p >= 0) && (q->p < NDATA)));
139 /* Creates a new double ended pointer list. */
144 dq = alloc_pdeq_block();
147 dq->magic = PDEQ_MAGIC1;
149 dq->l_end = dq->r_end = dq;
150 dq->l = dq->r = NULL;
157 /* Creates a new double ended pointer list and puts an initial pointer element in. */
158 pdeq *new_pdeq1(const void *x)
160 return pdeq_putr(new_pdeq(), x);
163 /* Delete a double ended pointer list. */
164 void del_pdeq(pdeq *dq)
170 q = dq->l_end; /* left end of chain */
171 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
172 if (dq->n == 0 && dq->l_end != dq ) {
176 /* Free all blocks in the pdeq chain */
184 /* Checks if a list is empty. */
185 int pdeq_empty(pdeq *dq)
188 return dq->l_end->n == 0;
191 /* Returns the length of a double ended pointer list. */
192 int pdeq_len(pdeq *dq)
209 /* Add a pointer to the right site of a double ended pointer list. */
210 pdeq *pdeq_putr(pdeq *dq, const void *x)
218 if (rdq->n >= NDATA) { /* tailblock full */
221 ndq = dq; /* try to reuse trunk, but ... */
222 if (dq->n) { /* ... if trunk used */
223 /* allocate and init new block */
224 ndq = alloc_pdeq_block();
226 ndq->magic = PDEQ_MAGIC2;
228 ndq->l_end = ndq->r_end = NULL;
232 ndq->l = rdq; rdq->r = ndq;
233 ndq->n = 0; ndq->p = 0;
238 n = rdq->n++ + rdq->p;
239 if (n >= NDATA) n -= NDATA;
247 /* Add a pointer to the left site of a double ended pointer list. */
248 pdeq *pdeq_putl(pdeq *dq, const void *x)
256 if (ldq->n >= NDATA) { /* headblock full */
259 ndq = dq; /* try to reuse trunk, but ... */
260 if (dq->n) { /* ... if trunk used */
261 /* allocate and init new block */
262 ndq = alloc_pdeq_block();
264 ndq->magic = PDEQ_MAGIC2;
266 ndq->l_end = ndq->r_end = NULL;
270 ndq->r = ldq; ldq->l = ndq;
271 ndq->n = 0; ndq->p = 0;
278 if (p < 0) p += NDATA;
287 /* Retrieve a pointer from the right site of a double ended pointer list. */
288 void *pdeq_getr(pdeq *dq)
295 assert(dq->l_end->n);
298 n = rdq->p + --rdq->n;
299 if (n >= NDATA) n -= NDATA;
308 dq->r_end = dq->l_end = dq;
311 free_pdeq_block(rdq);
319 /* Retrieve a pointer from the left site of a double ended pointer list. */
320 void *pdeq_getl(pdeq *dq)
327 assert(dq->l_end->n);
332 if (++p >= NDATA) p = 0;
341 dq->l_end = dq->r_end = dq;
344 free_pdeq_block(ldq);
353 * Returns non-zero if a double ended pointer list
354 * contains a pointer x.
356 int pdeq_contains(pdeq *dq, const void *x)
366 p = q->p; ep = p + q->n;
370 if (q->data[p] == x) return 1;
371 } while (++p < NDATA);
377 if (q->data[p++] == x) return 1;
387 * Search a key in a double ended pointer list, the search
388 * is controlled by a compare function.
389 * An element is found, if the compare function returns 0.
390 * The search is started from the left site of the list.
392 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
403 p = q->p; ep = p + q->n;
407 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
408 } while (++p < NDATA);
414 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
424 * Convert the double ended pointer list into a linear array beginning from
425 * left, the first element in the linear array will be the left one.
427 void **pdeq_copyl(pdeq *dq, const void **dst)
430 const void **d = dst;
442 memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
446 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
455 * Convert the double ended pointer list into a linear array beginning from
456 * right, the first element in the linear array will be the right one.
458 void **pdeq_copyr(pdeq *dq, const void **dst)
461 const void **d = dst;
469 p = q->p; i = q->n + p - 1;
472 do *d++ = q->data[i]; while (--i >= 0);
476 do *d++ = q->data[i]; while (--i >= p);