Grrrr Hmph @#$%#%@
[libfirm] / ir / adt / pdeq.c
1 /* Pdeq --- double ended queue of generic pointers.
2    Copyright (C) 1995, 1996 Christian von Roques */
3
4 /* $Id$ */
5
6 #ifdef HAVE_CONFIG_H
7 # include <config.h>
8 #endif
9
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 # ifdef HAVE_STRING_H
14 #  include <string.h>
15 # endif
16 #include "tune.h"
17 #include "cookies.h"
18 #include "debug.h"
19 #include "malloc.h"
20 #include "pdeq.h"
21
22 /* # of data items in block */
23 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
24
25 #ifdef NDEBUG
26 # define VRFY(dq) ((void)0)
27 #else
28 # define VRFY(dq) \
29     (d_(df_vrfy_level,1) ? _pdeq_vrfy ((dq)) : assert ((dq) && ((dq)->cookie == PDEQ_COOKIE1)))
30 #endif
31
32
33 struct pdeq {
34 #ifndef NDEBUG
35   unsigned cookie;
36 #endif
37   pdeq *l_end, *r_end;          /* left and right ends of the deque */
38   pdeq *l, *r;                  /*  left and right neighbour */
39   int n, p;
40   const void *data[1];
41 };
42
43
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 */
48
49
50 static inline void
51 free_pdeq_block (pdeq *p)
52 {
53 #ifndef NDEBUG
54   p->cookie = 0xbadf00d1;
55 #endif
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;
59   } else {
60     if (d_ (df_pdeq, 2)) printf ("[%p ==> free] ", p);
61     xfree (p);
62   }
63 }
64
65
66 static inline pdeq *
67 alloc_pdeq_block (void)
68 {
69   pdeq *p;
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);
73   } else {
74     p = xmalloc (PREF_MALLOC_SIZE);
75     if (d_ (df_pdeq, 2)) printf ("[malloc ==> %p] ", p);
76   }
77   return p;
78 }
79
80
81 #ifndef NDEBUG
82 void
83 _pdeq_vrfy (pdeq *dq)
84 {
85   pdeq *q;
86
87   if (d_ (df_pdeq, 5)) printf ("[pdeq_vrfy %p] ", dq);
88
89   assert (   dq
90           && (dq->cookie == PDEQ_COOKIE1)
91           && (dq->l_end && dq->r_end));
92   q = dq->l_end;
93   while (q) {
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)));
101     q = q->r;
102   }
103 }
104 #endif
105
106
107 pdeq *
108 new_pdeq (void)
109 {
110   pdeq *dq;
111
112   dq = alloc_pdeq_block();
113
114 #ifndef NDEBUG
115   dq->cookie = PDEQ_COOKIE1;
116 #endif
117   dq->l_end = dq->r_end = dq;
118   dq->l = dq->r = NULL;
119   dq->n = dq->p = 0;
120
121   VRFY (dq);
122   if (d_ (df_pdeq, 1)) printf ("(new_pdeq ==> %p)\n", dq);
123   return dq;
124 }
125
126
127 pdeq *
128 new_pdeq1 (const void *x)
129 {
130   return pdeq_putr (new_pdeq(), x);
131 }
132
133
134 void
135 del_pdeq (pdeq *dq)
136 {
137   pdeq *q, *qq;
138
139   VRFY (dq);
140
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);
145   }
146
147   /* Free all blocks in the pdeq chain */
148   do {
149     qq = q->r;
150     free_pdeq_block (q);
151   } while ((q = qq));
152
153   if (d_ (df_pdeq, 1)) printf ("(del_pdeq %p)\n", dq);
154 }
155
156
157 bool
158 pdeq_empty (pdeq *dq)
159 {
160   VRFY (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;
163 }
164
165
166 int
167 pdeq_len (pdeq *dq)
168 {
169   int n;
170   pdeq *q;
171
172   VRFY (dq);
173
174   n = 0;
175   q = dq->l_end;
176   do {
177     n += q->n;
178     q = q->r;
179   }  while (q);
180
181   if (d_ (df_pdeq, 4)) printf ("(pdeq_len %p ==> %d)\n", dq, n);
182   return n;
183 }
184
185
186 pdeq *
187 pdeq_putr (pdeq *dq, const void *x)
188 {
189   pdeq *rdq;
190   int n;
191   bool pr = 0;
192
193   VRFY (dq);
194
195   rdq = dq->r_end;
196   if (rdq->n >= NDATA) {        /* tailblock full */
197     pdeq *ndq;
198
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);
204 #ifndef NDEBUG
205       ndq->cookie = PDEQ_COOKIE2;
206 #endif
207       ndq->l_end = ndq->r_end = NULL;
208     }
209
210     ndq->r = NULL;
211     ndq->l = rdq; rdq->r = ndq;
212     ndq->n = 0; ndq->p = 0;
213     dq->r_end = ndq;
214     rdq = ndq;
215   }
216
217   n = rdq->n++ + rdq->p;
218   if (n >= NDATA) n -= NDATA;
219
220   rdq->data[n] = x;
221
222   VRFY (dq);
223   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putr %p %p)\n", dq, x);
224   return dq;
225 }
226
227
228 pdeq *
229 pdeq_putl (pdeq *dq, const void *x)
230 {
231   pdeq *ldq;
232   int p;
233   bool pr = 0;
234
235   VRFY (dq);
236
237   ldq = dq->l_end;
238   if (ldq->n >= NDATA) {        /* headblock full */
239     pdeq *ndq;
240
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);
246 #ifndef NDEBUG
247       ndq->cookie = PDEQ_COOKIE2;
248 #endif
249       ndq->l_end = ndq->r_end = NULL;
250     }
251
252     ndq->l = NULL;
253     ndq->r = ldq; ldq->l = ndq;
254     ndq->n = 0; ndq->p = 0;
255     dq->l_end = ndq;
256     ldq = ndq;
257   }
258
259   ldq->n++;
260   p = ldq->p - 1;
261   if (p < 0) p += NDATA;
262   ldq->p = p;
263
264   ldq->data[p] = x;
265
266   VRFY (dq);
267   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_putl %p %p)\n", dq, x);
268   return dq;
269 }
270
271
272 void *
273 pdeq_getr (pdeq *dq)
274 {
275   pdeq *rdq;
276   const void *x;
277   int n;
278   bool pr = 0;
279
280   VRFY (dq);
281   assert (dq->l_end->n);
282
283   rdq = dq->r_end;
284   n = rdq->p + --rdq->n;
285   if (n>=NDATA) n -= NDATA;
286   x = rdq->data[n];
287
288   if (rdq->n == 0) {
289     if (rdq->l) {
290       dq->r_end = rdq->l;
291       rdq->l->r = NULL;
292       rdq->l = NULL;
293     } else {
294       dq->r_end = dq->l_end = dq;
295     }
296     if (dq != rdq) {
297       free_pdeq_block (rdq);
298       pr = d_ (df_pdeq, 2);
299     }
300   }
301
302   VRFY (dq);
303   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getr %p ==> %p)\n", dq, x);
304   return (void *)x;
305 }
306
307
308 void *
309 pdeq_getl (pdeq *dq)
310 {
311   pdeq *ldq;
312   const void *x;
313   int p;
314   bool pr = 0;
315
316   VRFY (dq);
317   assert (dq->l_end->n);
318
319   ldq = dq->l_end;
320   p = ldq->p;
321   x = ldq->data[p];
322   if (++p >= NDATA) p = 0;
323   ldq->p = p;
324
325   if (--ldq->n == 0) {
326     if (ldq->r) {
327       dq->l_end = ldq->r;
328       ldq->r->l = NULL;
329       ldq->r = NULL;
330     } else {
331       dq->l_end = dq->r_end = dq;
332     }
333     if (dq != ldq) {
334       free_pdeq_block (ldq);
335       pr = d_ (df_pdeq, 2);
336     }
337   }
338
339   VRFY (dq);
340   if (d_ (df_pdeq, 3) || pr) printf ("(pdeq_getl %p ==> %p)\n", dq, x);
341   return (void *)x;
342 }
343
344
345 bool
346 pdeq_contains (pdeq *dq, const void *x)
347 {
348   pdeq *q;
349
350   VRFY (dq);
351
352   q = dq->l_end;
353   do {
354     int p, ep;
355
356     p = q->p; ep = p + q->n;
357
358     if (ep > NDATA) {
359       do if (q->data[p] == x) goto found; while (++p < NDATA);
360       p = 0;
361       ep -= NDATA;
362     }
363
364     while (p < ep) if (q->data[p++] == x) goto found;
365
366     q = q->r;
367   } while (q);
368
369   if (d_ (df_pdeq, 3)) printf ("(pdeq_contains %p %p ==> 0)\n", dq, x);
370   return 0;
371
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);
374   return 1;
375 }
376
377
378 void *
379 pdeq_search (pdeq *dq, cmp_fun cmp, const void *key)
380 {
381   pdeq *q;
382   int p;
383
384   VRFY (dq);
385
386   q = dq->l_end;
387   do {
388     int ep;
389
390     p = q->p; ep = p + q->n;
391
392     if (ep > NDATA) {
393       do if (!cmp (q->data[p], key)) goto found; while (++p < NDATA);
394       p = 0;
395       ep -= NDATA;
396     }
397
398     while (p < ep) if (!cmp (q->data[p++], key)) goto found;
399
400     q = q->r;
401   } while (q);
402
403   if (d_ (df_pdeq, 3)) printf ("(pdeq_search %p %p %p ==> 0)\n", dq, cmp, key);
404   return NULL;
405
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];
410 }
411
412
413 void **
414 pdeq_copyl (pdeq *dq, const void **dst)
415 {
416   pdeq *q;
417   const void **d = dst;
418
419   VRFY (dq);
420
421   q = dq->l_end;
422   while (q) {
423     int p, n;
424
425     p = q->p; n = q->n;
426
427     if (n + p > NDATA) {
428       int nn = NDATA - p;
429       memcpy (d, &q->data[p], nn * sizeof (void *)); d += nn;
430       p = 0; n -= nn;
431     }
432
433     memcpy (d, &q->data[p], n * sizeof (void *)); d += n;
434
435     q = q->r;
436   }
437
438   if (d_ (df_pdeq, 3)) printf ("(pdeq_copyl %p %p ==> %p)\n", dq, dst, d);
439   return (void **)dst;
440 }
441
442
443 void **
444 pdeq_copyr (pdeq *dq, const void **dst)
445 {
446   pdeq *q;
447   const void **d = dst;
448
449   VRFY (dq);
450
451   q = dq->r_end;
452   while (q) {
453     int p, i;
454
455     p = q->p; i = q->n + p - 1;
456     if (i >= NDATA) {
457       i -= NDATA;
458       do *d++ = q->data[i]; while (--i >= 0);
459       i = NDATA - 1;
460     }
461
462     do *d++ = q->data[i]; while (--i >= p);
463
464     q = q->l;
465   }
466
467   if (d_ (df_pdeq, 3)) printf ("(pdeq_copyr %p %p ==> %p)\n", dq, dst, d);
468   return (void **)dst;
469 }