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-2016 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"
30 #include <cstdint> // for uintptr_t
35 /** Exception class thrown by functions to signal unimplemented functionality
36 * so the expression may just be .hold() */
39 // some compilers (e.g. cygwin) define a macro log2, causing confusion
44 unsigned log2(unsigned n);
46 /** Rotate bits of unsigned value by one bit to the left.
47 * This can be necessary if the user wants to define its own hashes. */
48 inline unsigned rotate_left(unsigned n)
50 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
53 /** Compare two pointers (just to establish some sort of canonical order).
54 * @return -1, 0, or 1 */
56 inline int compare_pointers(const T * a, const T * b)
58 // '<' is not defined for pointers that don't point to the same array,
60 if (std::less<const T *>()(a, b))
62 else if (std::less<const T *>()(b, a))
67 /** Truncated multiplication with golden ratio, for computing hash values. */
68 inline unsigned golden_ratio_hash(uintptr_t n)
70 return n * UINT64_C(0x4f1bbcdd);
73 /* Compute the sign of a permutation of a container, with and without an
74 explicitly supplied comparison function. If the sign returned is 1 or -1,
75 the container is sorted after the operation. */
77 int permutation_sign(It first, It last)
89 It i = last, other = last;
98 } else if (!(*other < *i))
110 i = first; other = first;
119 } else if (!(*i < *other))
129 } while (first != last);
134 template <class It, class Cmp, class Swap>
135 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
146 It i = last, other = last;
148 bool swapped = false;
150 if (comp(*i, *other)) {
155 } else if (!comp(*other, *i))
167 i = first; other = first;
171 if (comp(*other, *i)) {
176 } else if (!comp(*i, *other))
186 } while (first != last);
191 /* Implementation of shaker sort, only compares adjacent elements. */
192 template <class It, class Cmp, class Swap>
193 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
203 It i = last, other = last;
205 bool swapped = false;
207 if (comp(*i, *other)) {
222 i = first; other = first;
226 if (comp(*other, *i)) {
239 } while (first != last);
242 /* In-place cyclic permutation of a container (no copying, only swapping). */
243 template <class It, class Swap>
244 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
246 unsigned num = last - first;
248 if (first == new_first || num < 2)
251 unsigned num1 = new_first - first, num2 = last - new_first;
253 It a = first, b = new_first;
264 It a = new_first, b = last;
268 } while (a != first);
276 // Collection of `construct on first use' wrappers for safely avoiding
277 // internal object replication without running into the `static
278 // initialization order fiasco'. This chest of numbers helps speed up
279 // the library but should not be used outside it since it is
280 // potentially confusing.
284 extern const numeric *_num_120_p;
285 extern const ex _ex_120;
286 extern const numeric *_num_60_p;
287 extern const ex _ex_60;
288 extern const numeric *_num_48_p;
289 extern const ex _ex_48;
290 extern const numeric *_num_30_p;
291 extern const ex _ex_30;
292 extern const numeric *_num_25_p;
293 extern const ex _ex_25;
294 extern const numeric *_num_24_p;
295 extern const ex _ex_24;
296 extern const numeric *_num_20_p;
297 extern const ex _ex_20;
298 extern const numeric *_num_18_p;
299 extern const ex _ex_18;
300 extern const numeric *_num_15_p;
301 extern const ex _ex_15;
302 extern const numeric *_num_12_p;
303 extern const ex _ex_12;
304 extern const numeric *_num_11_p;
305 extern const ex _ex_11;
306 extern const numeric *_num_10_p;
307 extern const ex _ex_10;
308 extern const numeric *_num_9_p;
309 extern const ex _ex_9;
310 extern const numeric *_num_8_p;
311 extern const ex _ex_8;
312 extern const numeric *_num_7_p;
313 extern const ex _ex_7;
314 extern const numeric *_num_6_p;
315 extern const ex _ex_6;
316 extern const numeric *_num_5_p;
317 extern const ex _ex_5;
318 extern const numeric *_num_4_p;
319 extern const ex _ex_4;
320 extern const numeric *_num_3_p;
321 extern const ex _ex_3;
322 extern const numeric *_num_2_p;
323 extern const ex _ex_2;
324 extern const numeric *_num_1_p;
325 extern const ex _ex_1;
326 extern const numeric *_num_1_2_p;
327 extern const ex _ex_1_2;
328 extern const numeric *_num_1_3_p;
329 extern const ex _ex_1_3;
330 extern const numeric *_num_1_4_p;
331 extern const ex _ex_1_4;
332 extern const numeric *_num0_p;
333 extern const basic *_num0_bp;
334 extern const ex _ex0;
335 extern const numeric *_num1_4_p;
336 extern const ex _ex1_4;
337 extern const numeric *_num1_3_p;
338 extern const ex _ex1_3;
339 extern const numeric *_num1_2_p;
340 extern const ex _ex1_2;
341 extern const numeric *_num1_p;
342 extern const ex _ex1;
343 extern const numeric *_num2_p;
344 extern const ex _ex2;
345 extern const numeric *_num3_p;
346 extern const ex _ex3;
347 extern const numeric *_num4_p;
348 extern const ex _ex4;
349 extern const numeric *_num5_p;
350 extern const ex _ex5;
351 extern const numeric *_num6_p;
352 extern const ex _ex6;
353 extern const numeric *_num7_p;
354 extern const ex _ex7;
355 extern const numeric *_num8_p;
356 extern const ex _ex8;
357 extern const numeric *_num9_p;
358 extern const ex _ex9;
359 extern const numeric *_num10_p;
360 extern const ex _ex10;
361 extern const numeric *_num11_p;
362 extern const ex _ex11;
363 extern const numeric *_num12_p;
364 extern const ex _ex12;
365 extern const numeric *_num15_p;
366 extern const ex _ex15;
367 extern const numeric *_num18_p;
368 extern const ex _ex18;
369 extern const numeric *_num20_p;
370 extern const ex _ex20;
371 extern const numeric *_num24_p;
372 extern const ex _ex24;
373 extern const numeric *_num25_p;
374 extern const ex _ex25;
375 extern const numeric *_num30_p;
376 extern const ex _ex30;
377 extern const numeric *_num48_p;
378 extern const ex _ex48;
379 extern const numeric *_num60_p;
380 extern const ex _ex60;
381 extern const numeric *_num120_p;
382 extern const ex _ex120;
385 // Helper macros for class implementations (mostly useful for trivial classes)
387 #define DEFAULT_CTOR(classname) \
388 classname::classname() { setflag(status_flags::evaluated | status_flags::expanded); }
390 #define DEFAULT_COMPARE(classname) \
391 int classname::compare_same_type(const basic & other) const \
393 /* by default, the objects are always identical */ \
397 #define DEFAULT_PRINT(classname, text) \
398 void classname::do_print(const print_context & c, unsigned level) const \
403 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
404 DEFAULT_PRINT(classname, text) \
405 void classname::do_print_latex(const print_latex & c, unsigned level) const \
412 #endif // ndef GINAC_UTILS_H