1 /* Pdeq --- double ended queue of generic pointers.
2 Copyright (C) 1995, 1996 Christian von Roques */
31 /* # of data items in block */
32 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
35 # define VRFY(dq) ((void)0)
38 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
46 pdeq *l_end, *r_end; /* left and right ends of the deque */
47 pdeq *l, *r; /* left and right neighbour */
53 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
54 /* +1 for compilers that can't grok empty arrays */
55 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
56 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
60 free_pdeq_block (pdeq *p)
63 p->cookie = 0xbadf00d1;
65 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
66 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
67 pdeq_block_cache[pdeqs_cached++] = p;
69 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
76 alloc_pdeq_block (void)
79 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
80 p = pdeq_block_cache[--pdeqs_cached];
81 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
83 p = xmalloc (PREF_MALLOC_SIZE);
84 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
96 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
99 && (dq->cookie == PDEQ_COOKIE1)
100 && (dq->l_end && dq->r_end));
103 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
104 && ((q == dq->l_end) ^ (q->l!=0))
105 && ((q == dq->r_end) ^ (q->r!=0))
106 && (!q->l || (q == q->l->r))
107 && ((q->n>=0) && (q->n<=NDATA))
108 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
109 && ((q->p>=0) && (q->p<NDATA)));
121 dq = alloc_pdeq_block();
124 dq->cookie = PDEQ_COOKIE1;
126 dq->l_end = dq->r_end = dq;
127 dq->l = dq->r = NULL;
131 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
137 new_pdeq1 (const void *x)
139 return pdeq_putr (new_pdeq(), x);
150 q = dq->l_end; /* left end of chain */
151 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
152 if (dq->n == 0 && dq->l_end != dq ) {
153 free_pdeq_block (dq);
156 /* Free all blocks in the pdeq chain */
162 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
167 pdeq_empty (pdeq *dq)
170 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
171 return dq->l_end->n == 0;
190 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
196 pdeq_putr (pdeq *dq, const void *x)
205 if (rdq->n >= NDATA) { /* tailblock full */
208 ndq = dq; /* try to reuse trunk, but ... */
209 if (dq->n) { /* ... if trunk used */
210 /* allocate and init new block */
211 ndq = alloc_pdeq_block ();
212 pr = d_ (df_pdeq, 2);
214 ndq->cookie = PDEQ_COOKIE2;
216 ndq->l_end = ndq->r_end = NULL;
220 ndq->l = rdq; rdq->r = ndq;
221 ndq->n = 0; ndq->p = 0;
226 n = rdq->n++ + rdq->p;
227 if (n >= NDATA) n -= NDATA;
232 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
238 pdeq_putl (pdeq *dq, const void *x)
247 if (ldq->n >= NDATA) { /* headblock full */
250 ndq = dq; /* try to reuse trunk, but ... */
251 if (dq->n) { /* ... if trunk used */
252 /* allocate and init new block */
253 ndq = alloc_pdeq_block ();
254 pr = d_ (df_pdeq, 2);
256 ndq->cookie = PDEQ_COOKIE2;
258 ndq->l_end = ndq->r_end = NULL;
262 ndq->r = ldq; ldq->l = ndq;
263 ndq->n = 0; ndq->p = 0;
270 if (p < 0) p += NDATA;
276 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
290 assert (dq->l_end->n);
293 n = rdq->p + --rdq->n;
294 if (n>=NDATA) n -= NDATA;
303 dq->r_end = dq->l_end = dq;
306 free_pdeq_block (rdq);
307 pr = d_ (df_pdeq, 2);
312 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
326 assert (dq->l_end->n);
331 if (++p >= NDATA) p = 0;
340 dq->l_end = dq->r_end = dq;
343 free_pdeq_block (ldq);
344 pr = d_ (df_pdeq, 2);
349 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
355 pdeq_contains (pdeq *dq, const void *x)
365 p = q->p; ep = p + q->n;
368 do if (q->data[p] == x) goto found; while (++p < NDATA);
373 while (p < ep) if (q->data[p++] == x) goto found;
378 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
381 found: /* The two gotos can be optimized away, if !DEBUG */
382 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
388 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
399 p = q->p; ep = p + q->n;
402 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
407 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
412 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
415 found: /* The two gotos can be optimized away, if !DEBUG */
416 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
417 dq, cmp, key, q->data[p]);
418 return (void *)q->data[p-1];
423 pdeq_copyl (pdeq *dq, const void **dst)
426 const void **d = dst;
438 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
442 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
447 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
453 pdeq_copyr (pdeq *dq, const void **dst)
456 const void **d = dst;
464 p = q->p; i = q->n + p - 1;
467 do *d++ = q->data[i]; while (--i >= 0);
471 do *d++ = q->data[i]; while (--i >= p);
476 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);