]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
Make new tinfo-system work on AMD64.
[ginac.git] / ginac / utils.h
1 /** @file utils.h
2  *
3  *  Interface to several small and furry utilities needed within GiNaC but not
4  *  of any interest to the user of the library. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  */
23
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
26
27 #include "config.h"
28
29 #include <string>
30 #include <functional>
31
32 #include "assertion.h"
33
34 namespace GiNaC {
35
36 /** Exception class thrown by functions to signal unimplemented functionality
37  *  so the expression may just be .hold() */
38 class dunno {};
39
40 // some compilers (e.g. cygwin) define a macro log2, causing confusion
41 #ifdef log2
42 #undef log2
43 #endif
44
45 unsigned log2(unsigned n);
46
47 /** Compare two pointers (just to establish some sort of canonical order).
48  *  @return -1, 0, or 1 */
49 template <class T>
50 inline int compare_pointers(const T * a, const T * b)
51 {
52         // '<' is not defined for pointers that don't point to the same array,
53         // but std::less is.
54         if (std::less<const T *>()(a, b))
55                 return -1;
56         else if (std::less<const T *>()(b, a))
57                 return 1;
58         return 0;
59 }
60
61 /** Rotate bits of unsigned value by one bit to the left. */
62 inline unsigned rotate_left(unsigned n)
63 {
64         return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
65 }
66
67 #if SIZEOF_VOID_P == SIZEOF_INT
68 typedef unsigned int p_int;
69 #elif SIZEOF_VOID_P == SIZEOF_LONG
70 typedef unsigned long p_int;
71 #elif SIZEOF_VOID_P == SIZEOF_LONG_LONG
72 typedef unsigned long long p_int;
73 #else
74 typedef unsigned long p_int;
75 #endif
76
77 /** Truncated multiplication with golden ratio, for computing hash values. */
78 inline unsigned golden_ratio_hash(p_int n)
79 {
80         // This function works much better when fast arithmetic with at
81         // least 64 significant bits is available.
82 #if SIZEOF_LONG >= 8
83         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
84         // more efficient than 'long long'.
85         unsigned long l = n * 0x4f1bbcddUL;
86         return (unsigned)l;
87 #elif SIZEOF_LONG_LONG >= 8
88         // This requires 'long long' (or an equivalent 64 bit type)---which is,
89         // unfortunately, not ANSI-C++-compliant.
90         // (Yet C99 demands it, which is reason for hope.)
91         unsigned long long l = n * 0x4f1bbcddULL;
92         return (unsigned)l;
93 #else
94         // Without a type with 64 significant bits do the multiplication manually
95         // by splitting n up into the lower and upper two bytes.
96         const unsigned n0 = (n & 0x0000ffffU);
97         const unsigned n1 = (n & 0xffff0000U) >> 16;
98         return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
99 #endif
100 }
101
102 /* Compute the sign of a permutation of a container, with and without an
103    explicitly supplied comparison function. If the sign returned is 1 or -1,
104    the container is sorted after the operation. */
105 template <class It>
106 int permutation_sign(It first, It last)
107 {
108         if (first == last)
109                 return 0;
110         --last;
111         if (first == last)
112                 return 0;
113         It flag = first;
114         int sign = 1;
115
116         do {
117                 It i = last, other = last;
118                 --other;
119                 bool swapped = false;
120                 while (i != first) {
121                         if (*i < *other) {
122                                 std::iter_swap(other, i);
123                                 flag = other;
124                                 swapped = true;
125                                 sign = -sign;
126                         } else if (!(*other < *i))
127                                 return 0;
128                         --i; --other;
129                 }
130                 if (!swapped)
131                         return sign;
132                 ++flag;
133                 if (flag == last)
134                         return sign;
135                 first = flag;
136                 i = first; other = first;
137                 ++other;
138                 swapped = false;
139                 while (i != last) {
140                         if (*other < *i) {
141                                 std::iter_swap(i, other);
142                                 flag = other;
143                                 swapped = true;
144                                 sign = -sign;
145                         } else if (!(*i < *other))
146                                 return 0;
147                         ++i; ++other;
148                 }
149                 if (!swapped)
150                         return sign;
151                 last = flag;
152                 --last;
153         } while (first != last);
154
155         return sign;
156 }
157
158 template <class It, class Cmp, class Swap>
159 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
160 {
161         if (first == last)
162                 return 0;
163         --last;
164         if (first == last)
165                 return 0;
166         It flag = first;
167         int sign = 1;
168
169         do {
170                 It i = last, other = last;
171                 --other;
172                 bool swapped = false;
173                 while (i != first) {
174                         if (comp(*i, *other)) {
175                                 swapit(*other, *i);
176                                 flag = other;
177                                 swapped = true;
178                                 sign = -sign;
179                         } else if (!comp(*other, *i))
180                                 return 0;
181                         --i; --other;
182                 }
183                 if (!swapped)
184                         return sign;
185                 ++flag;
186                 if (flag == last)
187                         return sign;
188                 first = flag;
189                 i = first; other = first;
190                 ++other;
191                 swapped = false;
192                 while (i != last) {
193                         if (comp(*other, *i)) {
194                                 swapit(*i, *other);
195                                 flag = other;
196                                 swapped = true;
197                                 sign = -sign;
198                         } else if (!comp(*i, *other))
199                                 return 0;
200                         ++i; ++other;
201                 }
202                 if (!swapped)
203                         return sign;
204                 last = flag;
205                 --last;
206         } while (first != last);
207
208         return sign;
209 }
210
211 /* Implementation of shaker sort, only compares adjacent elements. */
212 template <class It, class Cmp, class Swap>
213 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
214 {
215         if (first == last)
216                 return;
217         --last;
218         if (first == last)
219                 return;
220         It flag = first;
221
222         do {
223                 It i = last, other = last;
224                 --other;
225                 bool swapped = false;
226                 while (i != first) {
227                         if (comp(*i, *other)) {
228                                 swapit(*other, *i);
229                                 flag = other;
230                                 swapped = true;
231                         }
232                         --i; --other;
233                 }
234                 if (!swapped)
235                         return;
236                 ++flag;
237                 if (flag == last)
238                         return;
239                 first = flag;
240                 i = first; other = first;
241                 ++other;
242                 swapped = false;
243                 while (i != last) {
244                         if (comp(*other, *i)) {
245                                 swapit(*i, *other);
246                                 flag = other;
247                                 swapped = true;
248                         }
249                         ++i; ++other;
250                 }
251                 if (!swapped)
252                         return;
253                 last = flag;
254                 --last;
255         } while (first != last);
256 }
257
258 /* In-place cyclic permutation of a container (no copying, only swapping). */
259 template <class It, class Swap>
260 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
261 {
262         unsigned num = last - first;
263 again:
264         if (first == new_first || num < 2)
265                 return;
266
267         unsigned num1 = new_first - first, num2 = last - new_first;
268         if (num1 >= num2) {
269                 It a = first, b = new_first;
270                 while (b != last) {
271                         swapit(*a, *b);
272                         ++a; ++b;
273                 }
274                 if (num1 > num2) {
275                         first += num2;
276                         num = num1;
277                         goto again;
278                 }
279         } else {
280                 It a = new_first, b = last;
281                 do {
282                         --a; --b;
283                         swapit(*a, *b);
284                 } while (a != first);
285                 last -= num1;
286                 num = num2;
287                 goto again;
288         }
289 }
290
291
292 // Collection of `construct on first use' wrappers for safely avoiding
293 // internal object replication without running into the `static
294 // initialization order fiasco'.  This chest of numbers helps speed up
295 // the library but should not be used outside it since it is
296 // potentially confusing.
297
298 class ex;
299
300 extern const numeric *_num_120_p;
301 extern const ex _ex_120;
302 extern const numeric *_num_60_p;
303 extern const ex _ex_60;
304 extern const numeric *_num_48_p;
305 extern const ex _ex_48;
306 extern const numeric *_num_30_p;
307 extern const ex _ex_30;
308 extern const numeric *_num_25_p;
309 extern const ex _ex_25;
310 extern const numeric *_num_24_p;
311 extern const ex _ex_24;
312 extern const numeric *_num_20_p;
313 extern const ex _ex_20;
314 extern const numeric *_num_18_p;
315 extern const ex _ex_18;
316 extern const numeric *_num_15_p;
317 extern const ex _ex_15;
318 extern const numeric *_num_12_p;
319 extern const ex _ex_12;
320 extern const numeric *_num_11_p;
321 extern const ex _ex_11;
322 extern const numeric *_num_10_p;
323 extern const ex _ex_10;
324 extern const numeric *_num_9_p;
325 extern const ex _ex_9;
326 extern const numeric *_num_8_p;
327 extern const ex _ex_8;
328 extern const numeric *_num_7_p;
329 extern const ex _ex_7;
330 extern const numeric *_num_6_p;
331 extern const ex _ex_6;
332 extern const numeric *_num_5_p;
333 extern const ex _ex_5;
334 extern const numeric *_num_4_p;
335 extern const ex _ex_4;
336 extern const numeric *_num_3_p;
337 extern const ex _ex_3;
338 extern const numeric *_num_2_p;
339 extern const ex _ex_2;
340 extern const numeric *_num_1_p;
341 extern const ex _ex_1;
342 extern const numeric *_num_1_2_p;
343 extern const ex _ex_1_2;
344 extern const numeric *_num_1_3_p;
345 extern const ex _ex_1_3;
346 extern const numeric *_num_1_4_p;
347 extern const ex _ex_1_4;
348 extern const numeric *_num0_p;
349 extern const basic *_num0_bp;
350 extern const ex _ex0;
351 extern const numeric *_num1_4_p;
352 extern const ex _ex1_4;
353 extern const numeric *_num1_3_p;
354 extern const ex _ex1_3;
355 extern const numeric *_num1_2_p;
356 extern const ex _ex1_2;
357 extern const numeric *_num1_p;
358 extern const ex _ex1;
359 extern const numeric *_num2_p;
360 extern const ex _ex2;
361 extern const numeric *_num3_p;
362 extern const ex _ex3;
363 extern const numeric *_num4_p;
364 extern const ex _ex4;
365 extern const numeric *_num5_p;
366 extern const ex _ex5;
367 extern const numeric *_num6_p;
368 extern const ex _ex6;
369 extern const numeric *_num7_p;
370 extern const ex _ex7;
371 extern const numeric *_num8_p;
372 extern const ex _ex8;
373 extern const numeric *_num9_p;
374 extern const ex _ex9;
375 extern const numeric *_num10_p;
376 extern const ex _ex10;
377 extern const numeric *_num11_p;
378 extern const ex _ex11;
379 extern const numeric *_num12_p;
380 extern const ex _ex12;
381 extern const numeric *_num15_p;
382 extern const ex _ex15;
383 extern const numeric *_num18_p;
384 extern const ex _ex18;
385 extern const numeric *_num20_p;
386 extern const ex _ex20;
387 extern const numeric *_num24_p;
388 extern const ex _ex24;
389 extern const numeric *_num25_p;
390 extern const ex _ex25;
391 extern const numeric *_num30_p;
392 extern const ex _ex30;
393 extern const numeric *_num48_p;
394 extern const ex _ex48;
395 extern const numeric *_num60_p;
396 extern const ex _ex60;
397 extern const numeric *_num120_p;
398 extern const ex _ex120;
399
400
401 // Helper macros for class implementations (mostly useful for trivial classes)
402
403 #define DEFAULT_CTOR(classname) \
404 classname::classname() : inherited(&classname::tinfo_static) { setflag(status_flags::evaluated | status_flags::expanded); }
405
406 #define DEFAULT_UNARCHIVE(classname) \
407 ex classname::unarchive(const archive_node &n, lst &sym_lst) \
408 { \
409         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
410 }
411
412 #define DEFAULT_ARCHIVING(classname) \
413 classname::classname(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} \
414 DEFAULT_UNARCHIVE(classname) \
415 void classname::archive(archive_node &n) const \
416 { \
417         inherited::archive(n); \
418 }
419
420 #define DEFAULT_COMPARE(classname) \
421 int classname::compare_same_type(const basic & other) const \
422 { \
423         /* by default, the objects are always identical */ \
424         return 0; \
425 }
426
427 #define DEFAULT_PRINT(classname, text) \
428 void classname::do_print(const print_context & c, unsigned level) const \
429 { \
430         c.s << text; \
431 }
432
433 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
434 DEFAULT_PRINT(classname, text) \
435 void classname::do_print_latex(const print_latex & c, unsigned level) const \
436 { \
437         c.s << latex; \
438 }
439
440 } // namespace GiNaC
441
442
443 #endif // ndef __GINAC_UTILS_H__