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