1 /* Pdeq --- double ended queue of generic pointers.
2 Copyright (C) 1995, 1996 Christian von Roques */
20 /* # of data items in block */
21 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
24 # define VRFY(dq) ((void)0)
27 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
35 pdeq *l_end, *r_end; /* left and right ends of the deque */
36 pdeq *l, *r; /* left and right neighbour */
42 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
43 /* +1 for compilers that can't grok empty arrays */
44 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
45 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
49 free_pdeq_block (pdeq *p)
52 p->cookie = 0xbadf00d1;
54 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
55 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
56 pdeq_block_cache[pdeqs_cached++] = p;
58 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
65 alloc_pdeq_block (void)
68 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
69 p = pdeq_block_cache[--pdeqs_cached];
70 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
72 p = xmalloc (PREF_MALLOC_SIZE);
73 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
85 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
88 && (dq->cookie == PDEQ_COOKIE1)
89 && (dq->l_end && dq->r_end));
92 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
93 && ((q == dq->l_end) ^ (q->l!=0))
94 && ((q == dq->r_end) ^ (q->r!=0))
95 && (!q->l || (q == q->l->r))
96 && ((q->n>=0) && (q->n<=NDATA))
97 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
98 && ((q->p>=0) && (q->p<NDATA)));
110 dq = alloc_pdeq_block();
113 dq->cookie = PDEQ_COOKIE1;
115 dq->l_end = dq->r_end = dq;
116 dq->l = dq->r = NULL;
120 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
126 new_pdeq1 (const void *x)
128 return pdeq_putr (new_pdeq(), x);
139 q = dq->l_end; /* left end of chain */
140 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
141 if (dq->n == 0 && dq->l_end != dq ) {
142 free_pdeq_block (dq);
145 /* Free all blocks in the pdeq chain */
151 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
156 pdeq_empty (pdeq *dq)
159 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
160 return dq->l_end->n == 0;
179 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
185 pdeq_putr (pdeq *dq, const void *x)
194 if (rdq->n >= NDATA) { /* tailblock full */
197 ndq = dq; /* try to reuse trunk, but ... */
198 if (dq->n) { /* ... if trunk used */
199 /* allocate and init new block */
200 ndq = alloc_pdeq_block ();
201 pr = d_ (df_pdeq, 2);
203 ndq->cookie = PDEQ_COOKIE2;
205 ndq->l_end = ndq->r_end = NULL;
209 ndq->l = rdq; rdq->r = ndq;
210 ndq->n = 0; ndq->p = 0;
215 n = rdq->n++ + rdq->p;
216 if (n >= NDATA) n -= NDATA;
221 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
227 pdeq_putl (pdeq *dq, const void *x)
236 if (ldq->n >= NDATA) { /* headblock full */
239 ndq = dq; /* try to reuse trunk, but ... */
240 if (dq->n) { /* ... if trunk used */
241 /* allocate and init new block */
242 ndq = alloc_pdeq_block ();
243 pr = d_ (df_pdeq, 2);
245 ndq->cookie = PDEQ_COOKIE2;
247 ndq->l_end = ndq->r_end = NULL;
251 ndq->r = ldq; ldq->l = ndq;
252 ndq->n = 0; ndq->p = 0;
259 if (p < 0) p += NDATA;
265 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
279 assert (dq->l_end->n);
282 n = rdq->p + --rdq->n;
283 if (n>=NDATA) n -= NDATA;
292 dq->r_end = dq->l_end = dq;
295 free_pdeq_block (rdq);
296 pr = d_ (df_pdeq, 2);
301 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
315 assert (dq->l_end->n);
320 if (++p >= NDATA) p = 0;
329 dq->l_end = dq->r_end = dq;
332 free_pdeq_block (ldq);
333 pr = d_ (df_pdeq, 2);
338 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
344 pdeq_contains (pdeq *dq, const void *x)
354 p = q->p; ep = p + q->n;
357 do if (q->data[p] == x) goto found; while (++p < NDATA);
362 while (p < ep) if (q->data[p++] == x) goto found;
367 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
370 found: /* The two gotos can be optimized away, if !DEBUG */
371 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
377 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
388 p = q->p; ep = p + q->n;
391 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
396 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
401 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
404 found: /* The two gotos can be optimized away, if !DEBUG */
405 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
406 dq, cmp, key, q->data[p]);
407 return (void *)q->data[p-1];
412 pdeq_copyl (pdeq *dq, const void **dst)
415 const void **d = dst;
427 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
431 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
436 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
442 pdeq_copyr (pdeq *dq, const void **dst)
445 const void **d = dst;
453 p = q->p; i = q->n + p - 1;
456 do *d++ = q->data[i]; while (--i >= 0);
460 do *d++ = q->data[i]; while (--i >= p);
465 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);