1 /* Pdeq --- double ended queue of generic pointers.
2 Copyright (C) 1995, 1996 Christian von Roques */
30 /* # of data items in block */
31 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
34 # define VRFY(dq) ((void)0)
37 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
45 pdeq *l_end, *r_end; /* left and right ends of the deque */
46 pdeq *l, *r; /* left and right neighbour */
52 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
53 /* +1 for compilers that can't grok empty arrays */
54 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
55 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
59 free_pdeq_block (pdeq *p)
62 p->cookie = 0xbadf00d1;
64 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
65 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
66 pdeq_block_cache[pdeqs_cached++] = p;
68 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
75 alloc_pdeq_block (void)
78 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
79 p = pdeq_block_cache[--pdeqs_cached];
80 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
82 p = xmalloc (PREF_MALLOC_SIZE);
83 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
95 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
98 && (dq->cookie == PDEQ_COOKIE1)
99 && (dq->l_end && dq->r_end));
102 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
103 && ((q == dq->l_end) ^ (q->l!=0))
104 && ((q == dq->r_end) ^ (q->r!=0))
105 && (!q->l || (q == q->l->r))
106 && ((q->n>=0) && (q->n<=NDATA))
107 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
108 && ((q->p>=0) && (q->p<NDATA)));
120 dq = alloc_pdeq_block();
123 dq->cookie = PDEQ_COOKIE1;
125 dq->l_end = dq->r_end = dq;
126 dq->l = dq->r = NULL;
130 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
136 new_pdeq1 (const void *x)
138 return pdeq_putr (new_pdeq(), x);
149 q = dq->l_end; /* left end of chain */
150 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
151 if (dq->n == 0 && dq->l_end != dq ) {
152 free_pdeq_block (dq);
155 /* Free all blocks in the pdeq chain */
161 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
166 pdeq_empty (pdeq *dq)
169 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
170 return dq->l_end->n == 0;
189 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
195 pdeq_putr (pdeq *dq, const void *x)
204 if (rdq->n >= NDATA) { /* tailblock full */
207 ndq = dq; /* try to reuse trunk, but ... */
208 if (dq->n) { /* ... if trunk used */
209 /* allocate and init new block */
210 ndq = alloc_pdeq_block ();
211 pr = d_ (df_pdeq, 2);
213 ndq->cookie = PDEQ_COOKIE2;
215 ndq->l_end = ndq->r_end = NULL;
219 ndq->l = rdq; rdq->r = ndq;
220 ndq->n = 0; ndq->p = 0;
225 n = rdq->n++ + rdq->p;
226 if (n >= NDATA) n -= NDATA;
231 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
237 pdeq_putl (pdeq *dq, const void *x)
246 if (ldq->n >= NDATA) { /* headblock full */
249 ndq = dq; /* try to reuse trunk, but ... */
250 if (dq->n) { /* ... if trunk used */
251 /* allocate and init new block */
252 ndq = alloc_pdeq_block ();
253 pr = d_ (df_pdeq, 2);
255 ndq->cookie = PDEQ_COOKIE2;
257 ndq->l_end = ndq->r_end = NULL;
261 ndq->r = ldq; ldq->l = ndq;
262 ndq->n = 0; ndq->p = 0;
269 if (p < 0) p += NDATA;
275 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
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);
306 pr = d_ (df_pdeq, 2);
311 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
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);
343 pr = d_ (df_pdeq, 2);
348 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
354 pdeq_contains (pdeq *dq, const void *x)
364 p = q->p; ep = p + q->n;
367 do if (q->data[p] == x) goto found; while (++p < NDATA);
372 while (p < ep) if (q->data[p++] == x) goto found;
377 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
380 found: /* The two gotos can be optimized away, if !DEBUG */
381 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
387 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
398 p = q->p; ep = p + q->n;
401 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
406 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
411 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
414 found: /* The two gotos can be optimized away, if !DEBUG */
415 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
416 dq, cmp, key, q->data[p]);
417 return (void *)q->data[p-1];
422 pdeq_copyl (pdeq *dq, const void **dst)
425 const void **d = dst;
437 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
441 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
446 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
452 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);
475 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);