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 /* Pointer Double Ended Queue */
31 #define PDEQ_MAGIC1 FOURCC('P','D','E','1')
32 #define PDEQ_MAGIC2 FOURCC('P','D','E','2')
34 /** Size of pdeq block cache. */
35 #define TUNE_NSAVED_PDEQS 16
38 * Maximal number of data items in a pdeq chunk.
40 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
43 # define VRFY(dq) ((void)0)
45 # define VRFY(dq) assert((dq) && ((dq)->magic == PDEQ_MAGIC1))
49 * A pointer double ended queue.
50 * This structure is used as a list chunk either.
54 unsigned magic; /**< debug magic */
56 pdeq *l_end, *r_end; /**< left and right ends of the deque */
57 pdeq *l, *r; /**< left and right neighbour */
58 int n; /**< number of elements in the current chunk */
59 int p; /**< the read/write pointer */
60 const void *data[1]; /**< storage for elements */
65 * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
66 * +1 for compilers that can't grok empty arrays
68 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
71 * Number of pdeqs in pdeq_store.
73 unsigned pdeqs_cached;
76 * Free a pdeq chunk, put in into the cache if possible.
78 * @param p The pdeq chunk.
80 static INLINE void free_pdeq_block (pdeq *p)
83 p->magic = 0xbadf00d1;
85 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
86 pdeq_block_cache[pdeqs_cached++] = p;
93 * Allocate a new pdeq chunk, get it from the cache if possible.
95 * @return A new pdeq chunk.
97 static INLINE pdeq *alloc_pdeq_block (void)
100 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
101 p = pdeq_block_cache[--pdeqs_cached];
103 p = xmalloc (PREF_MALLOC_SIZE);
111 * Verify a double ended list, assert if failure.
113 * @param dq The list to verify.
115 void _pdeq_vrfy(pdeq *dq)
121 && (dq->magic == PDEQ_MAGIC1)
122 && (dq->l_end && dq->r_end));
125 assert ( ((q == dq) || (q->magic == PDEQ_MAGIC2))
126 && ((q == dq->l_end) ^ (q->l != NULL))
127 && ((q == dq->r_end) ^ (q->r != NULL))
128 && (!q->l || (q == q->l->r))
129 && ((q->n >= 0) && (q->n <= NDATA))
130 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
131 && ((q->p >= 0) && (q->p < NDATA)));
137 /* Creates a new double ended pointer list. */
142 dq = alloc_pdeq_block();
145 dq->magic = PDEQ_MAGIC1;
147 dq->l_end = dq->r_end = dq;
148 dq->l = dq->r = NULL;
155 /* Creates a new double ended pointer list and puts an initial pointer element in. */
156 pdeq *new_pdeq1(const void *x)
158 return pdeq_putr(new_pdeq(), x);
161 /* Delete a double ended pointer list. */
162 void del_pdeq(pdeq *dq)
168 q = dq->l_end; /* left end of chain */
169 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
170 if (dq->n == 0 && dq->l_end != dq ) {
174 /* Free all blocks in the pdeq chain */
182 /* Checks if a list is empty. */
183 int pdeq_empty(pdeq *dq)
186 return dq->l_end->n == 0;
189 /* Returns the length of a double ended pointer list. */
190 int pdeq_len(pdeq *dq)
207 /* Add a pointer to the right site of a double ended pointer list. */
208 pdeq * pdeq_putr(pdeq *dq, const void *x)
216 if (rdq->n >= NDATA) { /* tailblock full */
219 ndq = dq; /* try to reuse trunk, but ... */
220 if (dq->n) { /* ... if trunk used */
221 /* allocate and init new block */
222 ndq = alloc_pdeq_block();
224 ndq->magic = PDEQ_MAGIC2;
226 ndq->l_end = ndq->r_end = NULL;
230 ndq->l = rdq; rdq->r = ndq;
231 ndq->n = 0; ndq->p = 0;
236 n = rdq->n++ + rdq->p;
237 if (n >= NDATA) n -= NDATA;
245 /* Add a pointer to the left site of a double ended pointer list. */
246 pdeq *pdeq_putl(pdeq *dq, const void *x)
254 if (ldq->n >= NDATA) { /* headblock full */
257 ndq = dq; /* try to reuse trunk, but ... */
258 if (dq->n) { /* ... if trunk used */
259 /* allocate and init new block */
260 ndq = alloc_pdeq_block();
262 ndq->magic = PDEQ_MAGIC2;
264 ndq->l_end = ndq->r_end = NULL;
268 ndq->r = ldq; ldq->l = ndq;
269 ndq->n = 0; ndq->p = 0;
276 if (p < 0) p += NDATA;
285 /* Retrieve a pointer from the right site of a double ended pointer list. */
286 void *pdeq_getr(pdeq *dq)
293 assert(dq->l_end->n);
296 n = rdq->p + --rdq->n;
297 if (n >= NDATA) n -= NDATA;
306 dq->r_end = dq->l_end = dq;
309 free_pdeq_block(rdq);
317 /* Retrieve a pointer from the left site of a double ended pointer list. */
318 void *pdeq_getl(pdeq *dq)
325 assert(dq->l_end->n);
330 if (++p >= NDATA) p = 0;
339 dq->l_end = dq->r_end = dq;
342 free_pdeq_block(ldq);
351 * Returns non-zero if a double ended pointer list
352 * contains a pointer x.
354 int pdeq_contains(pdeq *dq, const void *x)
364 p = q->p; ep = p + q->n;
368 if (q->data[p] == x) return 1;
369 } while (++p < NDATA);
375 if (q->data[p++] == x) return 1;
385 * Search a key in a double ended pointer list, the search
386 * is controlled by a compare function.
387 * An element is found, if the compare function returns 0.
388 * The search is started from the left site of the list.
390 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
401 p = q->p; ep = p + q->n;
405 if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
406 } while (++p < NDATA);
412 if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
422 * Convert the double ended pointer list into a linear array beginning from
423 * left, the first element in the linear array will be the left one.
425 void **pdeq_copyl(pdeq *dq, const void **dst)
428 const void **d = dst;
440 memcpy(d, &q->data[p], nn * sizeof(void *)); d += nn;
444 memcpy(d, &q->data[p], n * sizeof(void *)); d += n;
453 * Convert the double ended pointer list into a linear array beginning from
454 * right, the first element in the linear array will be the right one.
456 void **pdeq_copyr(pdeq *dq, const void **dst)
459 const void **d = dst;
467 p = q->p; i = q->n + p - 1;
470 do *d++ = q->data[i]; while (--i >= 0);
474 do *d++ = q->data[i]; while (--i >= p);