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-2001 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 #if defined(HAVE_SSTREAM)
33 #elif defined(HAVE_STRSTREAM)
36 #error Need either sstream or strstream
38 #include "assertion.h"
42 // This should be obsoleted once <sstream> is widely deployed.
44 std::string ToString(const T & t)
46 #if defined(HAVE_SSTREAM)
47 std::ostringstream buf;
48 buf << t << std::ends;
52 std::ostrstream(buf,sizeof(buf)) << t << std::ends;
57 /** Exception class thrown by classes which provide their own series expansion
58 * to signal that ordinary Taylor expansion is safe. */
61 /** Exception class thrown when a singularity is encountered. */
62 class pole_error : public std::domain_error {
64 explicit pole_error(const std::string& what_arg, int degree);
65 int degree(void) const;
70 // some compilers (e.g. cygwin) define a macro log2, causing confusion
72 unsigned log2(unsigned n);
75 /** Compare two pointers (just to establish some sort of canonical order).
76 * @return -1, 0, or 1 */
77 inline int compare_pointers(const void * a, const void * b)
86 /** Rotate lower 31 bits of unsigned value by one bit to the left
87 * (upper bit gets cleared). */
88 inline unsigned rotate_left_31(unsigned n)
90 // clear highest bit and shift 1 bit to the left
91 n = (n & 0x7FFFFFFFU) << 1;
93 // overflow? clear highest bit and set lowest bit
95 n = (n & 0x7FFFFFFFU) | 0x00000001U;
97 GINAC_ASSERT(n<0x80000000U);
102 /** Golden ratio hash function for the 31 least significant bits. */
103 inline unsigned golden_ratio_hash(unsigned n)
105 // This function requires arithmetic with at least 64 significant bits
107 // So 'long' has 64 bits. Excellent! We prefer it because it might be
108 // more efficient than 'long long'.
109 unsigned long l = n * 0x4f1bbcddL;
110 return (l & 0x7fffffffU) ^ (l >> 32);
111 #elif SIZEOF_LONG_LONG >= 8
112 // This requires 'long long' (or an equivalent 64 bit type)---which is,
113 // unfortunately, not ANSI-C++-compliant.
114 // (Yet C99 demands it, which is reason for hope.)
115 unsigned long long l = n * 0x4f1bbcddL;
116 return (l & 0x7fffffffU) ^ (l >> 32);
117 #elif SIZEOF_LONG_DOUBLE > 8
118 // If 'long double' is bigger than 64 bits, we assume that the mantissa
119 // has at least 64 bits. This is not guaranteed but it's a good guess.
120 // Unfortunately, it may lead to horribly slow code.
121 const static long double golden_ratio = .618033988749894848204586834370;
122 long double m = golden_ratio * n;
123 return unsigned((m - int(m)) * 0x80000000);
125 #error "No 64 bit data type. You lose."
129 /* Compute the sign of a permutation of a container, with and without an
130 explicitly supplied comparison function. If the sign returned is 1 or -1,
131 the container is sorted after the operation. */
133 int permutation_sign(It first, It last)
144 It i = last, other = last;
146 bool swapped = false;
149 std::iter_swap(other, i);
153 } else if (!(*other < *i))
163 i = first; other = first;
168 std::iter_swap(i, other);
172 } else if (!(*i < *other))
180 } while (first != last);
185 template <class It, class Cmp, class Swap>
186 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
197 It i = last, other = last;
199 bool swapped = false;
201 if (comp(*i, *other)) {
206 } else if (!comp(*other, *i))
216 i = first; other = first;
220 if (comp(*other, *i)) {
225 } else if (!comp(*i, *other))
233 } while (first != last);
238 /* Implementation of shaker sort, only compares adjacent elements. */
239 template <class It, class Cmp, class Swap>
240 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
250 It i = last, other = last;
252 bool swapped = false;
254 if (comp(*i, *other)) {
267 i = first; other = first;
271 if (comp(*other, *i)) {
282 } while (first != last);
285 /* In-place cyclic permutation of a container (no copying, only swapping). */
286 template <class It, class Swap>
287 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
289 unsigned num = last - first;
291 if (first == new_first || num < 2)
294 unsigned num1 = new_first - first, num2 = last - new_first;
296 It a = first, b = new_first;
307 It a = new_first, b = last;
311 } while (a != first);
319 // Collection of `construct on first use' wrappers for safely avoiding
320 // internal object replication without running into the `static
321 // initialization order fiasco'. This chest of numbers helps speed up
322 // the library but should not be used outside it since it is
323 // potentially confusing.
328 const numeric & _num_120(void); // -120
329 const ex & _ex_120(void);
330 const numeric & _num_60(void); // -60
331 const ex & _ex_60(void);
332 const numeric & _num_48(void); // -48
333 const ex & _ex_48(void);
334 const numeric & _num_30(void); // -30
335 const ex & _ex_30(void);
336 const numeric & _num_25(void); // -25
337 const ex & _ex_25(void);
338 const numeric & _num_24(void); // -24
339 const ex & _ex_24(void);
340 const numeric & _num_20(void); // -20
341 const ex & _ex_20(void);
342 const numeric & _num_18(void); // -18
343 const ex & _ex_18(void);
344 const numeric & _num_15(void); // -15
345 const ex & _ex_15(void);
346 const numeric & _num_12(void); // -12
347 const ex & _ex_12(void);
348 const numeric & _num_11(void); // -11
349 const ex & _ex_11(void);
350 const numeric & _num_10(void); // -10
351 const ex & _ex_10(void);
352 const numeric & _num_9(void); // -9
353 const ex & _ex_9(void);
354 const numeric & _num_8(void); // -8
355 const ex & _ex_8(void);
356 const numeric & _num_7(void); // -7
357 const ex & _ex_7(void);
358 const numeric & _num_6(void); // -6
359 const ex & _ex_6(void);
360 const numeric & _num_5(void); // -5
361 const ex & _ex_5(void);
362 const numeric & _num_4(void); // -4
363 const ex & _ex_4(void);
364 const numeric & _num_3(void); // -3
365 const ex & _ex_3(void);
366 const numeric & _num_2(void); // -2
367 const ex & _ex_2(void);
368 const numeric & _num_1(void); // -1
369 const ex & _ex_1(void);
370 const numeric & _num_1_2(void); // -1/2
371 const ex & _ex_1_2(void);
372 const numeric & _num_1_3(void); // -1/3
373 const ex & _ex_1_3(void);
374 const numeric & _num_1_4(void); // -1/4
375 const ex & _ex_1_4(void);
376 const numeric & _num0(void); // 0
377 const ex & _ex0(void);
378 const numeric & _num1_4(void); // 1/4
379 const ex & _ex1_4(void);
380 const numeric & _num1_3(void); // 1/3
381 const ex & _ex1_3(void);
382 const numeric & _num1_2(void); // 1/2
383 const ex & _ex1_2(void);
384 const numeric & _num1(void); // 1
385 const ex & _ex1(void);
386 const numeric & _num2(void); // 2
387 const ex & _ex2(void);
388 const numeric & _num3(void); // 3
389 const ex & _ex3(void);
390 const numeric & _num4(void); // 4
391 const ex & _ex4(void);
392 const numeric & _num5(void); // 5
393 const ex & _ex5(void);
394 const numeric & _num6(void); // 6
395 const ex & _ex6(void);
396 const numeric & _num7(void); // 7
397 const ex & _ex7(void);
398 const numeric & _num8(void); // 8
399 const ex & _ex8(void);
400 const numeric & _num9(void); // 9
401 const ex & _ex9(void);
402 const numeric & _num10(void); // 10
403 const ex & _ex10(void);
404 const numeric & _num11(void); // 11
405 const ex & _ex11(void);
406 const numeric & _num12(void); // 12
407 const ex & _ex12(void);
408 const numeric & _num15(void); // 15
409 const ex & _ex15(void);
410 const numeric & _num18(void); // 18
411 const ex & _ex18(void);
412 const numeric & _num20(void); // 20
413 const ex & _ex20(void);
414 const numeric & _num24(void); // 24
415 const ex & _ex24(void);
416 const numeric & _num25(void); // 25
417 const ex & _ex25(void);
418 const numeric & _num30(void); // 30
419 const ex & _ex30(void);
420 const numeric & _num48(void); // 48
421 const ex & _ex48(void);
422 const numeric & _num60(void); // 60
423 const ex & _ex60(void);
424 const numeric & _num120(void); // 120
425 const ex & _ex120(void);
428 // Helper macros for class implementations (mostly useful for trivial classes)
430 #define DEFAULT_COPY(classname) \
431 void classname::copy(const classname & other) \
433 inherited::copy(other); \
436 #define DEFAULT_DESTROY(classname) \
437 void classname::destroy(bool call_parent) \
440 inherited::destroy(call_parent); \
443 #define DEFAULT_CTORS(classname) \
444 classname::classname() : inherited(TINFO_##classname) \
446 debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
448 DEFAULT_COPY(classname) \
449 DEFAULT_DESTROY(classname)
451 #define DEFAULT_UNARCHIVE(classname) \
452 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
454 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
457 #define DEFAULT_ARCHIVING(classname) \
458 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
460 debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
462 DEFAULT_UNARCHIVE(classname) \
463 void classname::archive(archive_node &n) const \
465 inherited::archive(n); \
468 #define DEFAULT_COMPARE(classname) \
469 int classname::compare_same_type(const basic & other) const \
471 /* by default, the objects are always identical */ \
475 #define DEFAULT_PRINT(classname, text) \
476 void classname::print(const print_context & c, unsigned level) const \
478 debugmsg(#classname " print", LOGLEVEL_PRINT); \
479 if (is_a<print_tree>(c)) \
480 inherited::print(c, level); \
485 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
486 void classname::print(const print_context & c, unsigned level) const \
488 debugmsg(#classname " print", LOGLEVEL_PRINT); \
489 if (is_a<print_tree>(c)) \
490 inherited::print(c, level); \
491 else if (is_a<print_latex>(c)) \
499 #endif // ndef __GINAC_UTILS_H__