Just fixed a typo (HASHPTR -> HASH_PTR)
[libfirm] / ir / adt / pdeq.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/adt/pdeq.c
4  * Purpose:     Pdeq --- double ended queue of generic pointers.
5  * Author:      Christian von Roques
6  * Modified by:
7  * Created:     1999 by getting from fiasco
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1995, 1996 Christian von Roques
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
17
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 # ifdef HAVE_STRING_H
22 #  include <string.h>
23 # endif
24
25 #include "cookies.h"
26 #include "pdeq.h"
27 #include "xmalloc.h"
28
29
30 /** Size of pdeq block cache. */
31 #define TUNE_NSAVED_PDEQS 16
32
33 /**
34  * Maximal number of data items in a pdeq chunk.
35  */
36 #define NDATA ((int)((PREF_MALLOC_SIZE - offsetof (pdeq, data)) / sizeof (void *)))
37
38 #ifdef NDEBUG
39 # define VRFY(dq) ((void)0)
40 #else
41 # define VRFY(dq) assert((dq) && ((dq)->cookie == PDEQ_COOKIE1))
42 #endif
43
44 /**
45  * A pointer double ended queue.
46  * This structure is used as a list chunk either.
47  */
48 struct pdeq {
49 #ifndef NDEBUG
50   unsigned cookie;              /**< debug cookie */
51 #endif
52   pdeq *l_end, *r_end;          /**< left and right ends of the deque */
53   pdeq *l, *r;                  /**< left and right neighbour */
54   int n;                        /**< number of elements in the current chunk */
55   int p;                        /**< the read/write pointer */
56   const void *data[1];          /**< storage for elements */
57 };
58
59
60 /**
61  * cache of unused, pdeq blocks to speed up new_pdeq and del_pdeq.
62  * +1 for compilers that can't grok empty arrays
63  */
64 pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS+1];
65
66 /**
67  * Number of pdeqs in pdeq_store.
68  */
69 unsigned pdeqs_cached;
70
71 /**
72  * Free a pdeq chunk, put in into the cache if possible.
73  *
74  * @param p   The pdeq chunk.
75  */
76 static INLINE void free_pdeq_block (pdeq *p)
77 {
78 #ifndef NDEBUG
79   p->cookie = 0xbadf00d1;
80 #endif
81   if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
82     pdeq_block_cache[pdeqs_cached++] = p;
83   } else {
84     xfree (p);
85   }
86 }
87
88 /**
89  * Allocate a new pdeq chunk, get it from the cache if possible.
90  *
91  * @return A new pdeq chunk.
92  */
93 static INLINE pdeq *alloc_pdeq_block (void)
94 {
95   pdeq *p;
96   if (TUNE_NSAVED_PDEQS && pdeqs_cached) {
97     p = pdeq_block_cache[--pdeqs_cached];
98   } else {
99     p = xmalloc (PREF_MALLOC_SIZE);
100   }
101   return p;
102 }
103
104
105 #ifndef NDEBUG
106 /**
107  * Verify a double ended list, assert if failure.
108  *
109  * @param dq   The list to verify.
110  */
111 void _pdeq_vrfy(pdeq *dq)
112 {
113   pdeq *q;
114
115
116   assert (   dq
117           && (dq->cookie == PDEQ_COOKIE1)
118           && (dq->l_end && dq->r_end));
119   q = dq->l_end;
120   while (q) {
121     assert (   ((q == dq) || (q->cookie == PDEQ_COOKIE2))
122             && ((q == dq->l_end) ^ (q->l != NULL))
123             && ((q == dq->r_end) ^ (q->r != NULL))
124             && (!q->l || (q == q->l->r))
125             && ((q->n >= 0) && (q->n <= NDATA))
126             && ((q == dq->l_end) || (q == dq->r_end) || (q->n == NDATA))
127             && ((q->p >= 0) && (q->p < NDATA)));
128     q = q->r;
129   }
130 }
131 #endif
132
133 /* Creates a new double ended pointer list. */
134 pdeq *new_pdeq(void)
135 {
136   pdeq *dq;
137
138   dq = alloc_pdeq_block();
139
140 #ifndef NDEBUG
141   dq->cookie = PDEQ_COOKIE1;
142 #endif
143   dq->l_end = dq->r_end = dq;
144   dq->l = dq->r = NULL;
145   dq->n = dq->p = 0;
146
147   VRFY(dq);
148   return dq;
149 }
150
151 /* Creates a new double ended pointer list and puts an initial pointer element in. */
152 pdeq *new_pdeq1(const void *x)
153 {
154   return pdeq_putr(new_pdeq(), x);
155 }
156
157 /* Delete a double ended pointer list. */
158 void del_pdeq(pdeq *dq)
159 {
160   pdeq *q, *qq;
161
162   VRFY(dq);
163
164   q = dq->l_end;                /* left end of chain */
165   /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
166   if (dq->n == 0 && dq->l_end != dq ) {
167     free_pdeq_block(dq);
168   }
169
170   /* Free all blocks in the pdeq chain */
171   do {
172     qq = q->r;
173     free_pdeq_block(q);
174   } while ((q = qq));
175
176 }
177
178 /* Checks if a list is empty. */
179 int pdeq_empty(pdeq *dq)
180 {
181   VRFY(dq);
182   return dq->l_end->n == 0;
183 }
184
185 /* Returns the lenght of a double ended pointer list. */
186 int pdeq_len(pdeq *dq)
187 {
188   int n;
189   pdeq *q;
190
191   VRFY(dq);
192
193   n = 0;
194   q = dq->l_end;
195   do {
196     n += q->n;
197     q = q->r;
198   }  while (q);
199
200   return n;
201 }
202
203 /* Add a pointer to the right site of a double ended pointer list. */
204 pdeq * pdeq_putr(pdeq *dq, const void *x)
205 {
206   pdeq *rdq;
207   int n;
208
209   VRFY(dq);
210
211   rdq = dq->r_end;
212   if (rdq->n >= NDATA) {        /* tailblock full */
213     pdeq *ndq;
214
215     ndq = dq;                   /* try to reuse trunk, but ... */
216     if (dq->n) {                /* ... if trunk used */
217       /* allocate and init new block */
218       ndq = alloc_pdeq_block();
219 #ifndef NDEBUG
220       ndq->cookie = PDEQ_COOKIE2;
221 #endif
222       ndq->l_end = ndq->r_end = NULL;
223     }
224
225     ndq->r = NULL;
226     ndq->l = rdq; rdq->r = ndq;
227     ndq->n = 0; ndq->p = 0;
228     dq->r_end = ndq;
229     rdq = ndq;
230   }
231
232   n = rdq->n++ + rdq->p;
233   if (n >= NDATA) n -= NDATA;
234
235   rdq->data[n] = x;
236
237   VRFY(dq);
238   return dq;
239 }
240
241 /* Add a pointer to the left site of a double ended pointer list. */
242 pdeq *pdeq_putl(pdeq *dq, const void *x)
243 {
244   pdeq *ldq;
245   int p;
246
247   VRFY(dq);
248
249   ldq = dq->l_end;
250   if (ldq->n >= NDATA) {        /* headblock full */
251     pdeq *ndq;
252
253     ndq = dq;                   /* try to reuse trunk, but ... */
254     if (dq->n) {                /* ... if trunk used */
255       /* allocate and init new block */
256       ndq = alloc_pdeq_block();
257 #ifndef NDEBUG
258       ndq->cookie = PDEQ_COOKIE2;
259 #endif
260       ndq->l_end = ndq->r_end = NULL;
261     }
262
263     ndq->l = NULL;
264     ndq->r = ldq; ldq->l = ndq;
265     ndq->n = 0; ndq->p = 0;
266     dq->l_end = ndq;
267     ldq = ndq;
268   }
269
270   ldq->n++;
271   p = ldq->p - 1;
272   if (p < 0) p += NDATA;
273   ldq->p = p;
274
275   ldq->data[p] = x;
276
277   VRFY(dq);
278   return dq;
279 }
280
281 /* Retrieve a pointer from the right site of a double ended pointer list. */
282 void *pdeq_getr(pdeq *dq)
283 {
284   pdeq *rdq;
285   const void *x;
286   int n;
287
288   VRFY(dq);
289   assert(dq->l_end->n);
290
291   rdq = dq->r_end;
292   n = rdq->p + --rdq->n;
293   if (n >= NDATA) n -= NDATA;
294   x = rdq->data[n];
295
296   if (rdq->n == 0) {
297     if (rdq->l) {
298       dq->r_end = rdq->l;
299       rdq->l->r = NULL;
300       rdq->l = NULL;
301     } else {
302       dq->r_end = dq->l_end = dq;
303     }
304     if (dq != rdq) {
305       free_pdeq_block(rdq);
306     }
307   }
308
309   VRFY(dq);
310   return (void *)x;
311 }
312
313 /* Retrieve a pointer from the left site of a double ended pointer list. */
314 void *pdeq_getl(pdeq *dq)
315 {
316   pdeq *ldq;
317   const void *x;
318   int p;
319
320   VRFY(dq);
321   assert(dq->l_end->n);
322
323   ldq = dq->l_end;
324   p = ldq->p;
325   x = ldq->data[p];
326   if (++p >= NDATA) p = 0;
327   ldq->p = p;
328
329   if (--ldq->n == 0) {
330     if (ldq->r) {
331       dq->l_end = ldq->r;
332       ldq->r->l = NULL;
333       ldq->r = NULL;
334     } else {
335       dq->l_end = dq->r_end = dq;
336     }
337     if (dq != ldq) {
338       free_pdeq_block(ldq);
339     }
340   }
341
342   VRFY(dq);
343   return (void *)x;
344 }
345
346 /*
347  * Returns non-zero if a double ended pointer list
348  * contains a pointer x.
349  */
350 int pdeq_contains(pdeq *dq, const void *x)
351 {
352   pdeq *q;
353
354   VRFY(dq);
355
356   q = dq->l_end;
357   do {
358     int p, ep;
359
360     p = q->p; ep = p + q->n;
361
362     if (ep > NDATA) {
363       do {
364         if (q->data[p] == x) return 1;
365       } while (++p < NDATA);
366       p = 0;
367       ep -= NDATA;
368     }
369
370     while (p < ep) {
371       if (q->data[p++] == x) return 1;
372     }
373
374     q = q->r;
375   } while (q);
376
377   return 0;
378 }
379
380 /*
381  * Search a key in a double ended pointer list, the search
382  * is controlled by a compare function.
383  * An element is found, if the compare function returns 0.
384  * The search is started from the left site of the list.
385  */
386 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
387 {
388   pdeq *q;
389   int p;
390
391   VRFY(dq);
392
393   q = dq->l_end;
394   do {
395     int ep;
396
397     p = q->p; ep = p + q->n;
398
399     if (ep > NDATA) {
400       do {
401         if (!cmp (q->data[p], key)) return (void *)q->data[p-1];
402       } while (++p < NDATA);
403       p = 0;
404       ep -= NDATA;
405     }
406
407     while (p < ep) {
408       if (!cmp (q->data[p++], key)) return (void *)q->data[p-1];
409     }
410
411     q = q->r;
412   } while (q);
413
414   return NULL;
415 }
416
417 /*
418  * Convert the double ended pointer list into a linear array beginning from
419  * left, the first element in the linear array will be the left one.
420  */
421 void **pdeq_copyl(pdeq *dq, const void **dst)
422 {
423   pdeq *q;
424   const void **d = dst;
425
426   VRFY(dq);
427
428   q = dq->l_end;
429   while (q) {
430     int p, n;
431
432     p = q->p; n = q->n;
433
434     if (n + p > NDATA) {
435       int nn = NDATA - p;
436       memcpy(d, &q->data[p], nn * sizeof(void *)); d += nn;
437       p = 0; n -= nn;
438     }
439
440     memcpy(d, &q->data[p], n * sizeof(void *)); d += n;
441
442     q = q->r;
443   }
444
445   return (void **)dst;
446 }
447
448 /*
449  * Convert the double ended pointer list into a linear array beginning from
450  * right, the first element in the linear array will be the right one.
451  */
452 void **pdeq_copyr(pdeq *dq, const void **dst)
453 {
454   pdeq *q;
455   const void **d = dst;
456
457   VRFY(dq);
458
459   q = dq->r_end;
460   while (q) {
461     int p, i;
462
463     p = q->p; i = q->n + p - 1;
464     if (i >= NDATA) {
465       i -= NDATA;
466       do *d++ = q->data[i]; while (--i >= 0);
467       i = NDATA - 1;
468     }
469
470     do *d++ = q->data[i]; while (--i >= p);
471
472     q = q->l;
473   }
474
475   return (void **)dst;
476 }