1 /* Pdeq --- double ended queue of generic pointers.
2 Copyright (C) 1995, 1996 Christian von Roques */
22 /* # of data items in block */
23 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
26 # define VRFY(dq) ((void)0)
29 (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
37 pdeq *l_end, *r_end; /* left and right ends of the deque */
38 pdeq *l, *r; /* left and right neighbour */
44 /* cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq. */
45 /* +1 for compilers that can't grok empty arrays */
46 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
47 unsigned pdeqs_cached; /* # pdeqs in pdeq_store */
51 free_pdeq_block (pdeq *p)
54 p->cookie = 0xbadf00d1;
56 if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
57 if (d_ (df_pdeq, 2)) printf ("[%p ==> pdeq_block_cache] ", p);
58 pdeq_block_cache[pdeqs_cached++] = p;
60 if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
67 alloc_pdeq_block (void)
70 if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
71 p = pdeq_block_cache[--pdeqs_cached];
72 if (d_ (df_pdeq, 2)) printf ("[pdeq_block_cache ==> %p] ", p);
74 p = xmalloc (PREF_MALLOC_SIZE);
75 if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
87 if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
90 && (dq->cookie == PDEQ_COOKIE1)
91 && (dq->l_end && dq->r_end));
94 assert ( ((q == dq) || (q->cookie == PDEQ_COOKIE2))
95 && ((q == dq->l_end) ^ (q->l!=0))
96 && ((q == dq->r_end) ^ (q->r!=0))
97 && (!q->l || (q == q->l->r))
98 && ((q->n>=0) && (q->n<=NDATA))
99 && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
100 && ((q->p>=0) && (q->p<NDATA)));
112 dq = alloc_pdeq_block();
115 dq->cookie = PDEQ_COOKIE1;
117 dq->l_end = dq->r_end = dq;
118 dq->l = dq->r = NULL;
122 if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
128 new_pdeq1 (const void *x)
130 return pdeq_putr (new_pdeq(), x);
141 q = dq->l_end; /* left end of chain */
142 /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
143 if (dq->n == 0 && dq->l_end != dq ) {
144 free_pdeq_block (dq);
147 /* Free all blocks in the pdeq chain */
153 if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
158 pdeq_empty (pdeq *dq)
161 if (d_ (df_pdeq, 4)) printf ("(pdeq_empty %p ==> %d)\n", dq, dq->l_end->n == 0);
162 return dq->l_end->n == 0;
181 if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
187 pdeq_putr (pdeq *dq, const void *x)
196 if (rdq->n >= NDATA) { /* tailblock full */
199 ndq = dq; /* try to reuse trunk, but ... */
200 if (dq->n) { /* ... if trunk used */
201 /* allocate and init new block */
202 ndq = alloc_pdeq_block ();
203 pr = d_ (df_pdeq, 2);
205 ndq->cookie = PDEQ_COOKIE2;
207 ndq->l_end = ndq->r_end = NULL;
211 ndq->l = rdq; rdq->r = ndq;
212 ndq->n = 0; ndq->p = 0;
217 n = rdq->n++ + rdq->p;
218 if (n >= NDATA) n -= NDATA;
223 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
229 pdeq_putl (pdeq *dq, const void *x)
238 if (ldq->n >= NDATA) { /* headblock full */
241 ndq = dq; /* try to reuse trunk, but ... */
242 if (dq->n) { /* ... if trunk used */
243 /* allocate and init new block */
244 ndq = alloc_pdeq_block ();
245 pr = d_ (df_pdeq, 2);
247 ndq->cookie = PDEQ_COOKIE2;
249 ndq->l_end = ndq->r_end = NULL;
253 ndq->r = ldq; ldq->l = ndq;
254 ndq->n = 0; ndq->p = 0;
261 if (p < 0) p += NDATA;
267 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
281 assert (dq->l_end->n);
284 n = rdq->p + --rdq->n;
285 if (n>=NDATA) n -= NDATA;
294 dq->r_end = dq->l_end = dq;
297 free_pdeq_block (rdq);
298 pr = d_ (df_pdeq, 2);
303 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
317 assert (dq->l_end->n);
322 if (++p >= NDATA) p = 0;
331 dq->l_end = dq->r_end = dq;
334 free_pdeq_block (ldq);
335 pr = d_ (df_pdeq, 2);
340 if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
346 pdeq_contains (pdeq *dq, const void *x)
356 p = q->p; ep = p + q->n;
359 do if (q->data[p] == x) goto found; while (++p < NDATA);
364 while (p < ep) if (q->data[p++] == x) goto found;
369 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
372 found: /* The two gotos can be optimized away, if !DEBUG */
373 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 1)\n", dq, x);
379 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
390 p = q->p; ep = p + q->n;
393 do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
398 while (p < ep) if (!cmp (q->data[p++], key)) goto found;
403 if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
406 found: /* The two gotos can be optimized away, if !DEBUG */
407 if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p %p ==> %p)\n",
408 dq, cmp, key, q->data[p]);
409 return (void *)q->data[p-1];
414 pdeq_copyl (pdeq *dq, const void **dst)
417 const void **d = dst;
429 memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
433 memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
438 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
444 pdeq_copyr (pdeq *dq, const void **dst)
447 const void **d = dst;
455 p = q->p; i = q->n + p - 1;
458 do *d++ = q->data[i]; while (--i >= 0);
462 do *d++ = q->data[i]; while (--i >= p);
467 if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);