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-2014 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
27 #include "assertion.h"
34 #include <stdint.h> // for uintptr_t
40 /** Exception class thrown by functions to signal unimplemented functionality
41 * so the expression may just be .hold() */
44 // some compilers (e.g. cygwin) define a macro log2, causing confusion
49 unsigned log2(unsigned n);
51 /** Rotate bits of unsigned value by one bit to the left.
52 * This can be necesary if the user wants to define its own hashes. */
53 inline unsigned rotate_left(unsigned n)
55 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
58 /** Compare two pointers (just to establish some sort of canonical order).
59 * @return -1, 0, or 1 */
61 inline int compare_pointers(const T * a, const T * b)
63 // '<' is not defined for pointers that don't point to the same array,
65 if (std::less<const T *>()(a, b))
67 else if (std::less<const T *>()(b, a))
73 typedef uintptr_t p_int;
75 typedef unsigned long p_int;
78 /** Truncated multiplication with golden ratio, for computing hash values. */
79 inline unsigned golden_ratio_hash(p_int n)
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;
90 else if (sizeof(long long) >= 8) {
91 // This requires 'long long' (or an equivalent 64 bit type)---which is,
92 // unfortunately, not ANSI-C++-compliant.
93 // (Yet C99 demands it, which is reason for hope.)
94 unsigned long long l = n * 0x4f1bbcddULL;
98 // Without a type with 64 significant bits do the multiplication manually
99 // by splitting n up into the lower and upper two bytes.
100 const unsigned n0 = (n & 0x0000ffffU);
101 const unsigned n1 = (n & 0xffff0000U) >> 16;
102 return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
105 /* Compute the sign of a permutation of a container, with and without an
106 explicitly supplied comparison function. If the sign returned is 1 or -1,
107 the container is sorted after the operation. */
109 int permutation_sign(It first, It last)
120 It i = last, other = last;
122 bool swapped = false;
125 std::iter_swap(other, i);
129 } else if (!(*other < *i))
141 i = first; other = first;
146 std::iter_swap(i, other);
150 } else if (!(*i < *other))
160 } while (first != last);
165 template <class It, class Cmp, class Swap>
166 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
177 It i = last, other = last;
179 bool swapped = false;
181 if (comp(*i, *other)) {
186 } else if (!comp(*other, *i))
198 i = first; other = first;
202 if (comp(*other, *i)) {
207 } else if (!comp(*i, *other))
217 } while (first != last);
222 /* Implementation of shaker sort, only compares adjacent elements. */
223 template <class It, class Cmp, class Swap>
224 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
234 It i = last, other = last;
236 bool swapped = false;
238 if (comp(*i, *other)) {
253 i = first; other = first;
257 if (comp(*other, *i)) {
270 } while (first != last);
273 /* In-place cyclic permutation of a container (no copying, only swapping). */
274 template <class It, class Swap>
275 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
277 unsigned num = last - first;
279 if (first == new_first || num < 2)
282 unsigned num1 = new_first - first, num2 = last - new_first;
284 It a = first, b = new_first;
295 It a = new_first, b = last;
299 } while (a != first);
307 // Collection of `construct on first use' wrappers for safely avoiding
308 // internal object replication without running into the `static
309 // initialization order fiasco'. This chest of numbers helps speed up
310 // the library but should not be used outside it since it is
311 // potentially confusing.
315 extern const numeric *_num_120_p;
316 extern const ex _ex_120;
317 extern const numeric *_num_60_p;
318 extern const ex _ex_60;
319 extern const numeric *_num_48_p;
320 extern const ex _ex_48;
321 extern const numeric *_num_30_p;
322 extern const ex _ex_30;
323 extern const numeric *_num_25_p;
324 extern const ex _ex_25;
325 extern const numeric *_num_24_p;
326 extern const ex _ex_24;
327 extern const numeric *_num_20_p;
328 extern const ex _ex_20;
329 extern const numeric *_num_18_p;
330 extern const ex _ex_18;
331 extern const numeric *_num_15_p;
332 extern const ex _ex_15;
333 extern const numeric *_num_12_p;
334 extern const ex _ex_12;
335 extern const numeric *_num_11_p;
336 extern const ex _ex_11;
337 extern const numeric *_num_10_p;
338 extern const ex _ex_10;
339 extern const numeric *_num_9_p;
340 extern const ex _ex_9;
341 extern const numeric *_num_8_p;
342 extern const ex _ex_8;
343 extern const numeric *_num_7_p;
344 extern const ex _ex_7;
345 extern const numeric *_num_6_p;
346 extern const ex _ex_6;
347 extern const numeric *_num_5_p;
348 extern const ex _ex_5;
349 extern const numeric *_num_4_p;
350 extern const ex _ex_4;
351 extern const numeric *_num_3_p;
352 extern const ex _ex_3;
353 extern const numeric *_num_2_p;
354 extern const ex _ex_2;
355 extern const numeric *_num_1_p;
356 extern const ex _ex_1;
357 extern const numeric *_num_1_2_p;
358 extern const ex _ex_1_2;
359 extern const numeric *_num_1_3_p;
360 extern const ex _ex_1_3;
361 extern const numeric *_num_1_4_p;
362 extern const ex _ex_1_4;
363 extern const numeric *_num0_p;
364 extern const basic *_num0_bp;
365 extern const ex _ex0;
366 extern const numeric *_num1_4_p;
367 extern const ex _ex1_4;
368 extern const numeric *_num1_3_p;
369 extern const ex _ex1_3;
370 extern const numeric *_num1_2_p;
371 extern const ex _ex1_2;
372 extern const numeric *_num1_p;
373 extern const ex _ex1;
374 extern const numeric *_num2_p;
375 extern const ex _ex2;
376 extern const numeric *_num3_p;
377 extern const ex _ex3;
378 extern const numeric *_num4_p;
379 extern const ex _ex4;
380 extern const numeric *_num5_p;
381 extern const ex _ex5;
382 extern const numeric *_num6_p;
383 extern const ex _ex6;
384 extern const numeric *_num7_p;
385 extern const ex _ex7;
386 extern const numeric *_num8_p;
387 extern const ex _ex8;
388 extern const numeric *_num9_p;
389 extern const ex _ex9;
390 extern const numeric *_num10_p;
391 extern const ex _ex10;
392 extern const numeric *_num11_p;
393 extern const ex _ex11;
394 extern const numeric *_num12_p;
395 extern const ex _ex12;
396 extern const numeric *_num15_p;
397 extern const ex _ex15;
398 extern const numeric *_num18_p;
399 extern const ex _ex18;
400 extern const numeric *_num20_p;
401 extern const ex _ex20;
402 extern const numeric *_num24_p;
403 extern const ex _ex24;
404 extern const numeric *_num25_p;
405 extern const ex _ex25;
406 extern const numeric *_num30_p;
407 extern const ex _ex30;
408 extern const numeric *_num48_p;
409 extern const ex _ex48;
410 extern const numeric *_num60_p;
411 extern const ex _ex60;
412 extern const numeric *_num120_p;
413 extern const ex _ex120;
416 // Helper macros for class implementations (mostly useful for trivial classes)
418 #define DEFAULT_CTOR(classname) \
419 classname::classname() { setflag(status_flags::evaluated | status_flags::expanded); }
421 #define DEFAULT_COMPARE(classname) \
422 int classname::compare_same_type(const basic & other) const \
424 /* by default, the objects are always identical */ \
428 #define DEFAULT_PRINT(classname, text) \
429 void classname::do_print(const print_context & c, unsigned level) const \
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 \
443 #endif // ndef GINAC_UTILS_H