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.
30 /** Size of pdeq block cache. */
31 #define TUNE_NSAVED_PDEQS 16
34 * Maximal number of data items in a pdeq chunk.
36 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
39 # define VRFY(dq) ((void)0)
41 # define VRFY(dq) assert((dq) && ((dq)->cookie == PDEQ_COOKIE1))
45 * A pointer double ended queue.
46 * This structure is used as a list chunk either.
50 unsigned cookie; /**< debug cookie */
52 pdeq *l_end, *r_end; /**< left and right ends of the deque */
53 pdeq *l, *r; /**< left and right neighbour */
54 int n; /**< number of elements in the current chunk */
55 int p; /**< the read/write pointer */
56 const void *data[1]; /**< storage for elements */
61 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
62 * +1 for compilers that can't grok empty arrays
64 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
67 * Number of pdeqs in pdeq_store.
69 unsigned pdeqs_cached;
72 * Free a pdeq chunk, put in into the cache if possible.
74 * @param p The pdeq chunk.
76 static INLINE void free_pdeq_block (pdeq *p)
79 p->cookie = 0xbadf00d1;
81 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
82 pdeq_block_cache[pdeqs_cached++] = p;
89 * Allocate a new pdeq chunk, get it from the cache if possible.
91 * @return A new pdeq chunk.
93 static INLINE pdeq *alloc_pdeq_block (void)
96 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
97 p = pdeq_block_cache[--pdeqs_cached];
99 p = xmalloc (PREF_MALLOC_SIZE);
107 * Verify a double ended list, assert if failure.
109 * @param dq The list to verify.
111 void _pdeq_vrfy(pdeq *dq)
117 && (dq->cookie == PDEQ_COOKIE1)
118 && (dq->l_end && dq->r_end));
121 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
122 && ((q == dq->l_end) ^ (q->l != NULL))
123 && ((q == dq->r_end) ^ (q->r != NULL))
124 && (!q->l || (q == q->l->r))
125 && ((q->n >= 0) && (q->n <= NDATA))
126 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
127 && ((q->p >= 0) && (q->p < NDATA)));
133 /* Creates a new double ended pointer list. */
138 dq = alloc_pdeq_block();
141 dq->cookie = PDEQ_COOKIE1;
143 dq->l_end = dq->r_end = dq;
144 dq->l = dq->r = NULL;
151 /* Creates a new double ended pointer list and puts an initial pointer element in. */
152 pdeq *new_pdeq1(const void *x)
154 return pdeq_putr(new_pdeq(), x);
157 /* Delete a double ended pointer list. */
158 void del_pdeq(pdeq *dq)
164 q = dq->l_end; /* left end of chain */
165 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
166 if (dq->n == 0 && dq->l_end != dq ) {
170 /* Free all blocks in the pdeq chain */
178 /* Checks if a list is empty. */
179 int pdeq_empty(pdeq *dq)
182 return dq->l_end->n == 0;
185 /* Returns the lenght of a double ended pointer list. */
186 int pdeq_len(pdeq *dq)
203 /* Add a pointer to the right site of a double ended pointer list. */
204 pdeq * pdeq_putr(pdeq *dq, const void *x)
212 if (rdq->n >= NDATA) { /* tailblock full */
215 ndq = dq; /* try to reuse trunk, but ... */
216 if (dq->n) { /* ... if trunk used */
217 /* allocate and init new block */
218 ndq = alloc_pdeq_block();
220 ndq->cookie = PDEQ_COOKIE2;
222 ndq->l_end = ndq->r_end = NULL;
226 ndq->l = rdq; rdq->r = ndq;
227 ndq->n = 0; ndq->p = 0;
232 n = rdq->n++ + rdq->p;
233 if (n >= NDATA) n -= NDATA;
241 /* Add a pointer to the left site of a double ended pointer list. */
242 pdeq *pdeq_putl(pdeq *dq, const void *x)
250 if (ldq->n >= NDATA) { /* headblock full */
253 ndq = dq; /* try to reuse trunk, but ... */
254 if (dq->n) { /* ... if trunk used */
255 /* allocate and init new block */
256 ndq = alloc_pdeq_block();
258 ndq->cookie = PDEQ_COOKIE2;
260 ndq->l_end = ndq->r_end = NULL;
264 ndq->r = ldq; ldq->l = ndq;
265 ndq->n = 0; ndq->p = 0;
272 if (p < 0) p += NDATA;
281 /* Retrieve a pointer from the right site of a double ended pointer list. */
282 void *pdeq_getr(pdeq *dq)
289 assert(dq->l_end->n);
292 n = rdq->p + --rdq->n;
293 if (n >= NDATA) n -= NDATA;
302 dq->r_end = dq->l_end = dq;
305 free_pdeq_block(rdq);
313 /* Retrieve a pointer from the left site of a double ended pointer list. */
314 void *pdeq_getl(pdeq *dq)
321 assert(dq->l_end->n);
326 if (++p >= NDATA) p = 0;
335 dq->l_end = dq->r_end = dq;
338 free_pdeq_block(ldq);
347 * Returns non-zero if a double ended pointer list
348 * contains a pointer x.
350 int pdeq_contains(pdeq *dq, const void *x)
360 p = q->p; ep = p + q->n;
364 if (q->data[p] == x) return 1;
365 } while (++p < NDATA);
371 if (q->data[p++] == x) return 1;
381 * Search a key in a double ended pointer list, the search
382 * is controlled by a compare function.
383 * An element is found, if the compare function returns 0.
384 * The search is started from the left site of the list.
386 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
397 p = q->p; ep = p + q->n;
401 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
402 } while (++p < NDATA);
408 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
418 * Convert the double ended pointer list into a linear array beginning from
419 * left, the first element in the linear array will be the left one.
421 void **pdeq_copyl(pdeq *dq, const void **dst)
424 const void **d = dst;
436 memcpy(d, &q->data[p], nn * sizeof(void *)); d += nn;
440 memcpy(d, &q->data[p], n * sizeof(void *)); d += n;
449 * Convert the double ended pointer list into a linear array beginning from
450 * right, the first element in the linear array will be the right one.
452 void **pdeq_copyr(pdeq *dq, const void **dst)
455 const void **d = dst;
463 p = q->p; i = q->n + p - 1;
466 do *d++ = q->data[i]; while (--i >= 0);
470 do *d++ = q->data[i]; while (--i >= p);