tv: Remove mul_table[][][] and simply use * and <<.
[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  */
77 static pdeq *pdeq_block_cache[TUNE_NSAVED_PDEQS];
78
79 /**
80  * Number of pdeqs in pdeq_store.
81  */
82 static unsigned pdeqs_cached;
83
84 /**
85  * Free a pdeq chunk, put in into the cache if possible.
86  *
87  * @param p   The pdeq chunk.
88  */
89 static inline void free_pdeq_block (pdeq *p)
90 {
91 #ifndef NDEBUG
92         p->magic = 0xbadf00d1;
93 #endif
94         if (pdeqs_cached < TUNE_NSAVED_PDEQS) {
95                 pdeq_block_cache[pdeqs_cached++] = p;
96         } else {
97                 xfree (p);
98         }
99 }
100
101 /**
102  * Allocate a new pdeq chunk, get it from the cache if possible.
103  *
104  * @return A new pdeq chunk.
105  */
106 static inline pdeq *alloc_pdeq_block (void)
107 {
108         pdeq *p;
109         if (pdeqs_cached > 0) {
110                 p = pdeq_block_cache[--pdeqs_cached];
111         } else {
112                 p = (pdeq*) xmalloc(PREF_MALLOC_SIZE);
113         }
114         return p;
115 }
116
117 /* Creates a new double ended pointer list. */
118 pdeq *new_pdeq(void)
119 {
120         pdeq *dq;
121
122         dq = alloc_pdeq_block();
123
124 #ifndef NDEBUG
125         dq->magic = PDEQ_MAGIC1;
126 #endif
127         dq->l_end = dq->r_end = dq;
128         dq->l = dq->r = NULL;
129         dq->n = dq->p = 0;
130
131         VRFY(dq);
132         return dq;
133 }
134
135 /* Creates a new double ended pointer list and puts an initial pointer element in. */
136 pdeq *new_pdeq1(const void *x)
137 {
138         return pdeq_putr(new_pdeq(), x);
139 }
140
141 /* Delete a double ended pointer list. */
142 void del_pdeq(pdeq *dq)
143 {
144         pdeq *q, *qq;
145
146         VRFY(dq);
147
148         q = dq->l_end; /* left end of chain */
149         /* pdeq trunk empty, but !pdeq_empty() ==> trunk not in chain */
150         if (dq->n == 0 && dq->l_end != dq ) {
151                 free_pdeq_block(dq);
152         }
153
154         /* Free all blocks in the pdeq chain */
155         do {
156                 qq = q->r;
157                 free_pdeq_block(q);
158         } while ((q = qq));
159 }
160
161 /* Checks if a list is empty. */
162 int pdeq_empty(pdeq *dq)
163 {
164         VRFY(dq);
165         return dq->l_end->n == 0;
166 }
167
168 /* Returns the length of a double ended pointer list. */
169 size_t pdeq_len(pdeq *dq)
170 {
171         size_t n;
172         pdeq *q;
173
174         VRFY(dq);
175
176         n = 0;
177         q = dq->l_end;
178         do {
179                 n += q->n;
180                 q = q->r;
181         }  while (q);
182
183         return n;
184 }
185
186 /* Add a pointer to the right site of a double ended pointer list. */
187 pdeq *pdeq_putr(pdeq *dq, const void *x)
188 {
189         pdeq *rdq;
190         size_t n;
191
192         VRFY(dq);
193
194         rdq = dq->r_end;
195         if (rdq->n >= NDATA) {  /* tailblock full */
196                 pdeq *ndq;
197
198                 ndq = dq;           /* try to reuse trunk, but ... */
199                 if (dq->n) {        /* ... if trunk used */
200                         /* allocate and init new block */
201                         ndq = alloc_pdeq_block();
202 #ifndef NDEBUG
203                         ndq->magic = PDEQ_MAGIC2;
204 #endif
205                         ndq->l_end = ndq->r_end = NULL;
206                 }
207
208                 ndq->r = NULL;
209                 ndq->l = rdq; rdq->r = ndq;
210                 ndq->n = 0; ndq->p = 0;
211                 dq->r_end = ndq;
212                 rdq = ndq;
213         }
214
215         n = rdq->n++ + rdq->p;
216         if (n >= NDATA) n -= NDATA;
217
218         rdq->data[n] = x;
219
220         VRFY(dq);
221         return dq;
222 }
223
224 /* Add a pointer to the left site of a double ended pointer list. */
225 pdeq *pdeq_putl(pdeq *dq, const void *x)
226 {
227         pdeq *ldq;
228         size_t p;
229
230         VRFY(dq);
231
232         ldq = dq->l_end;
233         if (ldq->n >= NDATA) {  /* headblock full */
234                 pdeq *ndq;
235
236                 ndq = dq;           /* try to reuse trunk, but ... */
237                 if (dq->n) {        /* ... if trunk used */
238                         /* allocate and init new block */
239                         ndq = alloc_pdeq_block();
240 #ifndef NDEBUG
241                         ndq->magic = PDEQ_MAGIC2;
242 #endif
243                         ndq->l_end = ndq->r_end = NULL;
244                 }
245
246                 ndq->l = NULL;
247                 ndq->r = ldq; ldq->l = ndq;
248                 ndq->n = 0; ndq->p = 0;
249                 dq->l_end = ndq;
250                 ldq = ndq;
251         }
252
253         ldq->n++;
254         if (ldq->p == 0)
255                 p = NDATA;
256         else
257                 p = ldq->p - 1;
258         ldq->p = p;
259
260         ldq->data[p] = x;
261
262         VRFY(dq);
263         return dq;
264 }
265
266 /* Retrieve a pointer from the right site of a double ended pointer list. */
267 void *pdeq_getr(pdeq *dq)
268 {
269         pdeq *rdq;
270         const void *x;
271         size_t n;
272
273         VRFY(dq);
274         assert(dq->l_end->n);
275
276         rdq = dq->r_end;
277         n = rdq->p + --rdq->n;
278         if (n >= NDATA) n -= NDATA;
279         x = rdq->data[n];
280
281         if (rdq->n == 0) {
282                 if (rdq->l) {
283                         dq->r_end = rdq->l;
284                         rdq->l->r = NULL;
285                         rdq->l = NULL;
286                 } else {
287                         dq->r_end = dq->l_end = dq;
288                 }
289                 if (dq != rdq) {
290                         free_pdeq_block(rdq);
291                 }
292         }
293
294         VRFY(dq);
295         return (void *)x;
296 }
297
298 /* Retrieve a pointer from the left site of a double ended pointer list. */
299 void *pdeq_getl(pdeq *dq)
300 {
301         pdeq *ldq;
302         const void *x;
303         size_t p;
304
305         VRFY(dq);
306         assert(dq->l_end->n);
307
308         ldq = dq->l_end;
309         p = ldq->p;
310         x = ldq->data[p];
311         if (++p >= NDATA) p = 0;
312         ldq->p = p;
313
314         if (--ldq->n == 0) {
315                 if (ldq->r) {
316                         dq->l_end = ldq->r;
317                         ldq->r->l = NULL;
318                         ldq->r = NULL;
319                 } else {
320                         dq->l_end = dq->r_end = dq;
321                 }
322                 if (dq != ldq) {
323                         free_pdeq_block(ldq);
324                 }
325         }
326
327         VRFY(dq);
328         return (void *)x;
329 }
330
331 /*
332  * Returns non-zero if a double ended pointer list
333  * contains a pointer x.
334  */
335 int pdeq_contains(pdeq *dq, const void *x)
336 {
337         pdeq *q;
338
339         VRFY(dq);
340
341         q = dq->l_end;
342         do {
343                 size_t p, ep;
344
345                 p = q->p; ep = p + q->n;
346
347                 if (ep > NDATA) {
348                         do {
349                                 if (q->data[p] == x) return 1;
350                         } while (++p < NDATA);
351                         p = 0;
352                         ep -= NDATA;
353                 }
354
355                 while (p < ep) {
356                         if (q->data[p++] == x) return 1;
357                 }
358
359                 q = q->r;
360         } while (q);
361
362         return 0;
363 }
364
365 /*
366  * Search a key in a double ended pointer list, the search
367  * is controlled by a compare function.
368  * An element is found, if the compare function returns 0.
369  * The search is started from the left site of the list.
370  */
371 void *pdeq_search(pdeq *dq, cmp_fun cmp, const void *key)
372 {
373         pdeq *q;
374         size_t p;
375
376         VRFY(dq);
377
378         q = dq->l_end;
379         do {
380                 size_t ep;
381
382                 p = q->p; ep = p + q->n;
383
384                 if (ep > NDATA) {
385                         do {
386                                 if (!cmp(q->data[p], key)) return (void *)q->data[p-1];
387                         } while (++p < NDATA);
388                         p = 0;
389                         ep -= NDATA;
390                 }
391
392                 while (p < ep) {
393                         if (!cmp(q->data[p++], key)) return (void *)q->data[p-1];
394                 }
395
396                 q = q->r;
397         } while (q);
398
399         return NULL;
400 }
401
402 /*
403  * Convert the double ended pointer list into a linear array beginning from
404  * left, the first element in the linear array will be the left one.
405  */
406 void **pdeq_copyl(pdeq *dq, const void **dst)
407 {
408         pdeq *q;
409         const void **d = dst;
410
411         VRFY(dq);
412
413         q = dq->l_end;
414         while (q) {
415                 size_t p, n;
416
417                 p = q->p; n = q->n;
418
419                 if (n + p > NDATA) {
420                         /* p is always < NDATA */
421                         size_t nn = NDATA - p;
422                         memcpy((void *) d, &q->data[p], nn * sizeof(void *)); d += nn;
423                         p = 0; n -= nn;
424                 }
425
426                 memcpy((void *) d, &q->data[p], n * sizeof(void *)); d += n;
427
428                 q = q->r;
429         }
430
431         return (void **)dst;
432 }
433
434 /*
435  * Convert the double ended pointer list into a linear array beginning from
436  * right, the first element in the linear array will be the right one.
437  */
438 void **pdeq_copyr(pdeq *dq, const void **dst)
439 {
440         pdeq *q;
441         const void **d = dst;
442
443         VRFY(dq);
444
445         q = dq->r_end;
446         while (q) {
447                 size_t p, i;
448
449                 p = q->p; i = q->n + p - 1;
450                 if (i >= NDATA) {
451                         i -= NDATA;
452                         for (;; --i) {
453                                 *d++ = q->data[i];
454                                 if (i == 0)
455                                         break;
456                         }
457                         i = NDATA - 1;
458                 }
459
460                 for (;; --i) {
461                         *d++ = q->data[i];
462                         if (i <= p)
463                                 break;
464                 }
465
466                 q = q->l;
467         }
468
469         return (void **)dst;
470 }