3 * Interface to several small and furry utilities needed within GiNaC but not
4 * of any interest to the user of the library. */
7 * GiNaC Copyright (C) 1999-2008 Johannes Gutenberg University Mainz, Germany
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.
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.
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
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
34 #include <stdint.h> // for uintptr_t
37 #include "assertion.h"
41 /** Exception class thrown by functions to signal unimplemented functionality
42 * so the expression may just be .hold() */
45 // some compilers (e.g. cygwin) define a macro log2, causing confusion
50 unsigned log2(unsigned n);
52 /** Rotate bits of unsigned value by one bit to the left.
53 * This can be necesary if the user wants to define its own hashes. */
54 inline unsigned rotate_left(unsigned n)
56 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
59 /** Compare two pointers (just to establish some sort of canonical order).
60 * @return -1, 0, or 1 */
62 inline int compare_pointers(const T * a, const T * b)
64 // '<' is not defined for pointers that don't point to the same array,
66 if (std::less<const T *>()(a, b))
68 else if (std::less<const T *>()(b, a))
74 typedef uintptr_t p_int;
76 typedef unsigned long p_int;
79 /** Truncated multiplication with golden ratio, for computing hash values. */
80 inline unsigned golden_ratio_hash(p_int n)
82 // This function works much better when fast arithmetic with at
83 // least 64 significant bits is available.
84 if (sizeof(long) >= 8) {
85 // So 'long' has 64 bits. Excellent! We prefer it because it might be
86 // more efficient than 'long long'.
87 unsigned long l = n * 0x4f1bbcddUL;
91 else if (sizeof(long long) >= 8) {
92 // This requires 'long long' (or an equivalent 64 bit type)---which is,
93 // unfortunately, not ANSI-C++-compliant.
94 // (Yet C99 demands it, which is reason for hope.)
95 unsigned long long l = n * 0x4f1bbcddULL;
99 // Without a type with 64 significant bits do the multiplication manually
100 // by splitting n up into the lower and upper two bytes.
101 const unsigned n0 = (n & 0x0000ffffU);
102 const unsigned n1 = (n & 0xffff0000U) >> 16;
103 return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
106 /* Compute the sign of a permutation of a container, with and without an
107 explicitly supplied comparison function. If the sign returned is 1 or -1,
108 the container is sorted after the operation. */
110 int permutation_sign(It first, It last)
121 It i = last, other = last;
123 bool swapped = false;
126 std::iter_swap(other, i);
130 } else if (!(*other < *i))
140 i = first; other = first;
145 std::iter_swap(i, other);
149 } else if (!(*i < *other))
157 } while (first != last);
162 template <class It, class Cmp, class Swap>
163 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
174 It i = last, other = last;
176 bool swapped = false;
178 if (comp(*i, *other)) {
183 } else if (!comp(*other, *i))
193 i = first; other = first;
197 if (comp(*other, *i)) {
202 } else if (!comp(*i, *other))
210 } while (first != last);
215 /* Implementation of shaker sort, only compares adjacent elements. */
216 template <class It, class Cmp, class Swap>
217 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
227 It i = last, other = last;
229 bool swapped = false;
231 if (comp(*i, *other)) {
244 i = first; other = first;
248 if (comp(*other, *i)) {
259 } while (first != last);
262 /* In-place cyclic permutation of a container (no copying, only swapping). */
263 template <class It, class Swap>
264 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
266 unsigned num = last - first;
268 if (first == new_first || num < 2)
271 unsigned num1 = new_first - first, num2 = last - new_first;
273 It a = first, b = new_first;
284 It a = new_first, b = last;
288 } while (a != first);
296 // Collection of `construct on first use' wrappers for safely avoiding
297 // internal object replication without running into the `static
298 // initialization order fiasco'. This chest of numbers helps speed up
299 // the library but should not be used outside it since it is
300 // potentially confusing.
304 extern const numeric *_num_120_p;
305 extern const ex _ex_120;
306 extern const numeric *_num_60_p;
307 extern const ex _ex_60;
308 extern const numeric *_num_48_p;
309 extern const ex _ex_48;
310 extern const numeric *_num_30_p;
311 extern const ex _ex_30;
312 extern const numeric *_num_25_p;
313 extern const ex _ex_25;
314 extern const numeric *_num_24_p;
315 extern const ex _ex_24;
316 extern const numeric *_num_20_p;
317 extern const ex _ex_20;
318 extern const numeric *_num_18_p;
319 extern const ex _ex_18;
320 extern const numeric *_num_15_p;
321 extern const ex _ex_15;
322 extern const numeric *_num_12_p;
323 extern const ex _ex_12;
324 extern const numeric *_num_11_p;
325 extern const ex _ex_11;
326 extern const numeric *_num_10_p;
327 extern const ex _ex_10;
328 extern const numeric *_num_9_p;
329 extern const ex _ex_9;
330 extern const numeric *_num_8_p;
331 extern const ex _ex_8;
332 extern const numeric *_num_7_p;
333 extern const ex _ex_7;
334 extern const numeric *_num_6_p;
335 extern const ex _ex_6;
336 extern const numeric *_num_5_p;
337 extern const ex _ex_5;
338 extern const numeric *_num_4_p;
339 extern const ex _ex_4;
340 extern const numeric *_num_3_p;
341 extern const ex _ex_3;
342 extern const numeric *_num_2_p;
343 extern const ex _ex_2;
344 extern const numeric *_num_1_p;
345 extern const ex _ex_1;
346 extern const numeric *_num_1_2_p;
347 extern const ex _ex_1_2;
348 extern const numeric *_num_1_3_p;
349 extern const ex _ex_1_3;
350 extern const numeric *_num_1_4_p;
351 extern const ex _ex_1_4;
352 extern const numeric *_num0_p;
353 extern const basic *_num0_bp;
354 extern const ex _ex0;
355 extern const numeric *_num1_4_p;
356 extern const ex _ex1_4;
357 extern const numeric *_num1_3_p;
358 extern const ex _ex1_3;
359 extern const numeric *_num1_2_p;
360 extern const ex _ex1_2;
361 extern const numeric *_num1_p;
362 extern const ex _ex1;
363 extern const numeric *_num2_p;
364 extern const ex _ex2;
365 extern const numeric *_num3_p;
366 extern const ex _ex3;
367 extern const numeric *_num4_p;
368 extern const ex _ex4;
369 extern const numeric *_num5_p;
370 extern const ex _ex5;
371 extern const numeric *_num6_p;
372 extern const ex _ex6;
373 extern const numeric *_num7_p;
374 extern const ex _ex7;
375 extern const numeric *_num8_p;
376 extern const ex _ex8;
377 extern const numeric *_num9_p;
378 extern const ex _ex9;
379 extern const numeric *_num10_p;
380 extern const ex _ex10;
381 extern const numeric *_num11_p;
382 extern const ex _ex11;
383 extern const numeric *_num12_p;
384 extern const ex _ex12;
385 extern const numeric *_num15_p;
386 extern const ex _ex15;
387 extern const numeric *_num18_p;
388 extern const ex _ex18;
389 extern const numeric *_num20_p;
390 extern const ex _ex20;
391 extern const numeric *_num24_p;
392 extern const ex _ex24;
393 extern const numeric *_num25_p;
394 extern const ex _ex25;
395 extern const numeric *_num30_p;
396 extern const ex _ex30;
397 extern const numeric *_num48_p;
398 extern const ex _ex48;
399 extern const numeric *_num60_p;
400 extern const ex _ex60;
401 extern const numeric *_num120_p;
402 extern const ex _ex120;
405 // Helper macros for class implementations (mostly useful for trivial classes)
407 #define DEFAULT_CTOR(classname) \
408 classname::classname() : inherited(&classname::tinfo_static) { setflag(status_flags::evaluated | status_flags::expanded); }
410 #define DEFAULT_UNARCHIVE(classname) \
411 ex classname::unarchive(const archive_node &n, lst &sym_lst) \
413 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
416 #define DEFAULT_ARCHIVING(classname) \
417 classname::classname(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} \
418 DEFAULT_UNARCHIVE(classname) \
419 void classname::archive(archive_node &n) const \
421 inherited::archive(n); \
424 #define DEFAULT_COMPARE(classname) \
425 int classname::compare_same_type(const basic & other) const \
427 /* by default, the objects are always identical */ \
431 #define DEFAULT_PRINT(classname, text) \
432 void classname::do_print(const print_context & c, unsigned level) const \
437 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
438 DEFAULT_PRINT(classname, text) \
439 void classname::do_print_latex(const print_latex & c, unsigned level) const \
447 #endif // ndef __GINAC_UTILS_H__