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