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-2002 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
31 #include "assertion.h"
35 /** Exception class thrown by classes which provide their own series expansion
36 * to signal that ordinary Taylor expansion is safe. */
39 /** Exception class thrown by functions to signal unimplemented functionality
40 * so the expression may just be .hold() */
43 /** Exception class thrown when a singularity is encountered. */
44 class pole_error : public std::domain_error {
46 explicit pole_error(const std::string& what_arg, int degree);
47 int degree(void) const;
52 // some compilers (e.g. cygwin) define a macro log2, causing confusion
54 unsigned log2(unsigned n);
57 /** Compare two pointers (just to establish some sort of canonical order).
58 * @return -1, 0, or 1 */
59 inline int compare_pointers(const void * a, const void * b)
68 /** Rotate bits of unsigned value by one bit to the left. */
69 inline unsigned rotate_left(unsigned n)
71 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
74 /** Truncated multiplication with golden ratio, for computing hash values. */
75 inline unsigned golden_ratio_hash(unsigned n)
77 // This function works much better when fast arithmetic with at
78 // least 64 significant bits is available.
80 // So 'long' has 64 bits. Excellent! We prefer it because it might be
81 // more efficient than 'long long'.
82 unsigned long l = n * 0x4f1bbcddUL;
84 #elif SIZEOF_LONG_LONG >= 8
85 // This requires 'long long' (or an equivalent 64 bit type)---which is,
86 // unfortunately, not ANSI-C++-compliant.
87 // (Yet C99 demands it, which is reason for hope.)
88 unsigned long long l = n * 0x4f1bbcddULL;
91 // Without a type with 64 significant bits do the multiplication manually
92 // by splitting n up into the lower and upper two bytes.
93 const unsigned n0 = (n & 0x0000ffffU);
94 const unsigned n1 = (n & 0xffff0000U) >> 16;
95 return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
99 /* Compute the sign of a permutation of a container, with and without an
100 explicitly supplied comparison function. If the sign returned is 1 or -1,
101 the container is sorted after the operation. */
103 int permutation_sign(It first, It last)
114 It i = last, other = last;
116 bool swapped = false;
119 std::iter_swap(other, i);
123 } else if (!(*other < *i))
133 i = first; other = first;
138 std::iter_swap(i, other);
142 } else if (!(*i < *other))
150 } while (first != last);
155 template <class It, class Cmp, class Swap>
156 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
167 It i = last, other = last;
169 bool swapped = false;
171 if (comp(*i, *other)) {
176 } else if (!comp(*other, *i))
186 i = first; other = first;
190 if (comp(*other, *i)) {
195 } else if (!comp(*i, *other))
203 } while (first != last);
208 /* Implementation of shaker sort, only compares adjacent elements. */
209 template <class It, class Cmp, class Swap>
210 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
220 It i = last, other = last;
222 bool swapped = false;
224 if (comp(*i, *other)) {
237 i = first; other = first;
241 if (comp(*other, *i)) {
252 } while (first != last);
255 /* In-place cyclic permutation of a container (no copying, only swapping). */
256 template <class It, class Swap>
257 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
259 unsigned num = last - first;
261 if (first == new_first || num < 2)
264 unsigned num1 = new_first - first, num2 = last - new_first;
266 It a = first, b = new_first;
277 It a = new_first, b = last;
281 } while (a != first);
289 // Collection of `construct on first use' wrappers for safely avoiding
290 // internal object replication without running into the `static
291 // initialization order fiasco'. This chest of numbers helps speed up
292 // the library but should not be used outside it since it is
293 // potentially confusing.
298 extern const numeric *_num_120_p;
299 extern const numeric &_num_120;
300 extern const ex _ex_120;
301 extern const numeric *_num_60_p;
302 extern const numeric &_num_60;
303 extern const ex _ex_60;
304 extern const numeric *_num_48_p;
305 extern const numeric &_num_48;
306 extern const ex _ex_48;
307 extern const numeric *_num_30_p;
308 extern const numeric &_num_30;
309 extern const ex _ex_30;
310 extern const numeric *_num_25_p;
311 extern const numeric &_num_25;
312 extern const ex _ex_25;
313 extern const numeric *_num_24_p;
314 extern const numeric &_num_24;
315 extern const ex _ex_24;
316 extern const numeric *_num_20_p;
317 extern const numeric &_num_20;
318 extern const ex _ex_20;
319 extern const numeric *_num_18_p;
320 extern const numeric &_num_18;
321 extern const ex _ex_18;
322 extern const numeric *_num_15_p;
323 extern const numeric &_num_15;
324 extern const ex _ex_15;
325 extern const numeric *_num_12_p;
326 extern const numeric &_num_12;
327 extern const ex _ex_12;
328 extern const numeric *_num_11_p;
329 extern const numeric &_num_11;
330 extern const ex _ex_11;
331 extern const numeric *_num_10_p;
332 extern const numeric &_num_10;
333 extern const ex _ex_10;
334 extern const numeric *_num_9_p;
335 extern const numeric &_num_9;
336 extern const ex _ex_9;
337 extern const numeric *_num_8_p;
338 extern const numeric &_num_8;
339 extern const ex _ex_8;
340 extern const numeric *_num_7_p;
341 extern const numeric &_num_7;
342 extern const ex _ex_7;
343 extern const numeric *_num_6_p;
344 extern const numeric &_num_6;
345 extern const ex _ex_6;
346 extern const numeric *_num_5_p;
347 extern const numeric &_num_5;
348 extern const ex _ex_5;
349 extern const numeric *_num_4_p;
350 extern const numeric &_num_4;
351 extern const ex _ex_4;
352 extern const numeric *_num_3_p;
353 extern const numeric &_num_3;
354 extern const ex _ex_3;
355 extern const numeric *_num_2_p;
356 extern const numeric &_num_2;
357 extern const ex _ex_2;
358 extern const numeric *_num_1_p;
359 extern const numeric &_num_1;
360 extern const ex _ex_1;
361 extern const numeric *_num_1_2_p;
362 extern const numeric &_num_1_2;
363 extern const ex _ex_1_2;
364 extern const numeric *_num_1_3_p;
365 extern const numeric &_num_1_3;
366 extern const ex _ex_1_3;
367 extern const numeric *_num_1_4_p;
368 extern const numeric &_num_1_4;
369 extern const ex _ex_1_4;
370 extern const numeric *_num0_p;
371 extern const numeric &_num0;
372 extern const ex _ex0;
373 extern const numeric *_num1_4_p;
374 extern const numeric &_num1_4;
375 extern const ex _ex1_4;
376 extern const numeric *_num1_3_p;
377 extern const numeric &_num1_3;
378 extern const ex _ex1_3;
379 extern const numeric *_num1_2_p;
380 extern const numeric &_num1_2;
381 extern const ex _ex1_2;
382 extern const numeric *_num1_p;
383 extern const numeric &_num1;
384 extern const ex _ex1;
385 extern const numeric *_num2_p;
386 extern const numeric &_num2;
387 extern const ex _ex2;
388 extern const numeric *_num3_p;
389 extern const numeric &_num3;
390 extern const ex _ex3;
391 extern const numeric *_num4_p;
392 extern const numeric &_num4;
393 extern const ex _ex4;
394 extern const numeric *_num5_p;
395 extern const numeric &_num5;
396 extern const ex _ex5;
397 extern const numeric *_num6_p;
398 extern const numeric &_num6;
399 extern const ex _ex6;
400 extern const numeric *_num7_p;
401 extern const numeric &_num7;
402 extern const ex _ex7;
403 extern const numeric *_num8_p;
404 extern const numeric &_num8;
405 extern const ex _ex8;
406 extern const numeric *_num9_p;
407 extern const numeric &_num9;
408 extern const ex _ex9;
409 extern const numeric *_num10_p;
410 extern const numeric &_num10;
411 extern const ex _ex10;
412 extern const numeric *_num11_p;
413 extern const numeric &_num11;
414 extern const ex _ex11;
415 extern const numeric *_num12_p;
416 extern const numeric &_num12;
417 extern const ex _ex12;
418 extern const numeric *_num15_p;
419 extern const numeric &_num15;
420 extern const ex _ex15;
421 extern const numeric *_num18_p;
422 extern const numeric &_num18;
423 extern const ex _ex18;
424 extern const numeric *_num20_p;
425 extern const numeric &_num20;
426 extern const ex _ex20;
427 extern const numeric *_num24_p;
428 extern const numeric &_num24;
429 extern const ex _ex24;
430 extern const numeric *_num25_p;
431 extern const numeric &_num25;
432 extern const ex _ex25;
433 extern const numeric *_num30_p;
434 extern const numeric &_num30;
435 extern const ex _ex30;
436 extern const numeric *_num48_p;
437 extern const numeric &_num48;
438 extern const ex _ex48;
439 extern const numeric *_num60_p;
440 extern const numeric &_num60;
441 extern const ex _ex60;
442 extern const numeric *_num120_p;
443 extern const numeric &_num120;
444 extern const ex _ex120;
447 // Helper macros for class implementations (mostly useful for trivial classes)
449 #define DEFAULT_COPY(classname) \
450 void classname::copy(const classname & other) \
452 inherited::copy(other); \
455 #define DEFAULT_DESTROY(classname) \
456 void classname::destroy(bool call_parent) \
459 inherited::destroy(call_parent); \
462 #define DEFAULT_CTORS(classname) \
463 classname::classname() : inherited(TINFO_##classname) {} \
464 DEFAULT_COPY(classname) \
465 DEFAULT_DESTROY(classname)
467 #define DEFAULT_UNARCHIVE(classname) \
468 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
470 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
473 #define DEFAULT_ARCHIVING(classname) \
474 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) {} \
475 DEFAULT_UNARCHIVE(classname) \
476 void classname::archive(archive_node &n) const \
478 inherited::archive(n); \
481 #define DEFAULT_COMPARE(classname) \
482 int classname::compare_same_type(const basic & other) const \
484 /* by default, the objects are always identical */ \
488 #define DEFAULT_PRINT(classname, text) \
489 void classname::print(const print_context & c, unsigned level) const \
491 if (is_a<print_tree>(c)) \
492 inherited::print(c, level); \
497 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
498 void classname::print(const print_context & c, unsigned level) const \
500 if (is_a<print_tree>(c)) \
501 inherited::print(c, level); \
502 else if (is_a<print_latex>(c)) \
511 #endif // ndef __GINAC_UTILS_H__