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