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-2003 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__
32 #include "assertion.h"
36 /** Exception class thrown by functions to signal unimplemented functionality
37 * so the expression may just be .hold() */
40 // some compilers (e.g. cygwin) define a macro log2, causing confusion
42 unsigned log2(unsigned n);
45 /** Compare two pointers (just to establish some sort of canonical order).
46 * @return -1, 0, or 1 */
48 inline int compare_pointers(const T * a, const T * b)
50 // '<' is not defined for pointers that don't point to the same array,
52 if (std::less<const T *>()(a, b))
54 else if (std::less<const T *>()(b, a))
59 /** Rotate bits of unsigned value by one bit to the left. */
60 inline unsigned rotate_left(unsigned n)
62 return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
65 /** Truncated multiplication with golden ratio, for computing hash values. */
66 inline unsigned golden_ratio_hash(unsigned n)
68 // This function works much better when fast arithmetic with at
69 // least 64 significant bits is available.
71 // So 'long' has 64 bits. Excellent! We prefer it because it might be
72 // more efficient than 'long long'.
73 unsigned long l = n * 0x4f1bbcddUL;
75 #elif SIZEOF_LONG_LONG >= 8
76 // This requires 'long long' (or an equivalent 64 bit type)---which is,
77 // unfortunately, not ANSI-C++-compliant.
78 // (Yet C99 demands it, which is reason for hope.)
79 unsigned long long l = n * 0x4f1bbcddULL;
82 // Without a type with 64 significant bits do the multiplication manually
83 // by splitting n up into the lower and upper two bytes.
84 const unsigned n0 = (n & 0x0000ffffU);
85 const unsigned n1 = (n & 0xffff0000U) >> 16;
86 return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
90 /* Compute the sign of a permutation of a container, with and without an
91 explicitly supplied comparison function. If the sign returned is 1 or -1,
92 the container is sorted after the operation. */
94 int permutation_sign(It first, It last)
105 It i = last, other = last;
107 bool swapped = false;
110 std::iter_swap(other, i);
114 } else if (!(*other < *i))
124 i = first; other = first;
129 std::iter_swap(i, other);
133 } else if (!(*i < *other))
141 } while (first != last);
146 template <class It, class Cmp, class Swap>
147 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
158 It i = last, other = last;
160 bool swapped = false;
162 if (comp(*i, *other)) {
167 } else if (!comp(*other, *i))
177 i = first; other = first;
181 if (comp(*other, *i)) {
186 } else if (!comp(*i, *other))
194 } while (first != last);
199 /* Implementation of shaker sort, only compares adjacent elements. */
200 template <class It, class Cmp, class Swap>
201 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
211 It i = last, other = last;
213 bool swapped = false;
215 if (comp(*i, *other)) {
228 i = first; other = first;
232 if (comp(*other, *i)) {
243 } while (first != last);
246 /* In-place cyclic permutation of a container (no copying, only swapping). */
247 template <class It, class Swap>
248 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
250 unsigned num = last - first;
252 if (first == new_first || num < 2)
255 unsigned num1 = new_first - first, num2 = last - new_first;
257 It a = first, b = new_first;
268 It a = new_first, b = last;
272 } while (a != first);
280 // Collection of `construct on first use' wrappers for safely avoiding
281 // internal object replication without running into the `static
282 // initialization order fiasco'. This chest of numbers helps speed up
283 // the library but should not be used outside it since it is
284 // potentially confusing.
288 extern const numeric *_num_120_p;
289 extern const numeric &_num_120;
290 extern const ex _ex_120;
291 extern const numeric *_num_60_p;
292 extern const numeric &_num_60;
293 extern const ex _ex_60;
294 extern const numeric *_num_48_p;
295 extern const numeric &_num_48;
296 extern const ex _ex_48;
297 extern const numeric *_num_30_p;
298 extern const numeric &_num_30;
299 extern const ex _ex_30;
300 extern const numeric *_num_25_p;
301 extern const numeric &_num_25;
302 extern const ex _ex_25;
303 extern const numeric *_num_24_p;
304 extern const numeric &_num_24;
305 extern const ex _ex_24;
306 extern const numeric *_num_20_p;
307 extern const numeric &_num_20;
308 extern const ex _ex_20;
309 extern const numeric *_num_18_p;
310 extern const numeric &_num_18;
311 extern const ex _ex_18;
312 extern const numeric *_num_15_p;
313 extern const numeric &_num_15;
314 extern const ex _ex_15;
315 extern const numeric *_num_12_p;
316 extern const numeric &_num_12;
317 extern const ex _ex_12;
318 extern const numeric *_num_11_p;
319 extern const numeric &_num_11;
320 extern const ex _ex_11;
321 extern const numeric *_num_10_p;
322 extern const numeric &_num_10;
323 extern const ex _ex_10;
324 extern const numeric *_num_9_p;
325 extern const numeric &_num_9;
326 extern const ex _ex_9;
327 extern const numeric *_num_8_p;
328 extern const numeric &_num_8;
329 extern const ex _ex_8;
330 extern const numeric *_num_7_p;
331 extern const numeric &_num_7;
332 extern const ex _ex_7;
333 extern const numeric *_num_6_p;
334 extern const numeric &_num_6;
335 extern const ex _ex_6;
336 extern const numeric *_num_5_p;
337 extern const numeric &_num_5;
338 extern const ex _ex_5;
339 extern const numeric *_num_4_p;
340 extern const numeric &_num_4;
341 extern const ex _ex_4;
342 extern const numeric *_num_3_p;
343 extern const numeric &_num_3;
344 extern const ex _ex_3;
345 extern const numeric *_num_2_p;
346 extern const numeric &_num_2;
347 extern const ex _ex_2;
348 extern const numeric *_num_1_p;
349 extern const numeric &_num_1;
350 extern const ex _ex_1;
351 extern const numeric *_num_1_2_p;
352 extern const numeric &_num_1_2;
353 extern const ex _ex_1_2;
354 extern const numeric *_num_1_3_p;
355 extern const numeric &_num_1_3;
356 extern const ex _ex_1_3;
357 extern const numeric *_num_1_4_p;
358 extern const numeric &_num_1_4;
359 extern const ex _ex_1_4;
360 extern const numeric *_num0_p;
361 extern const basic *_num0_bp;
362 extern const numeric &_num0;
363 extern const ex _ex0;
364 extern const numeric *_num1_4_p;
365 extern const numeric &_num1_4;
366 extern const ex _ex1_4;
367 extern const numeric *_num1_3_p;
368 extern const numeric &_num1_3;
369 extern const ex _ex1_3;
370 extern const numeric *_num1_2_p;
371 extern const numeric &_num1_2;
372 extern const ex _ex1_2;
373 extern const numeric *_num1_p;
374 extern const numeric &_num1;
375 extern const ex _ex1;
376 extern const numeric *_num2_p;
377 extern const numeric &_num2;
378 extern const ex _ex2;
379 extern const numeric *_num3_p;
380 extern const numeric &_num3;
381 extern const ex _ex3;
382 extern const numeric *_num4_p;
383 extern const numeric &_num4;
384 extern const ex _ex4;
385 extern const numeric *_num5_p;
386 extern const numeric &_num5;
387 extern const ex _ex5;
388 extern const numeric *_num6_p;
389 extern const numeric &_num6;
390 extern const ex _ex6;
391 extern const numeric *_num7_p;
392 extern const numeric &_num7;
393 extern const ex _ex7;
394 extern const numeric *_num8_p;
395 extern const numeric &_num8;
396 extern const ex _ex8;
397 extern const numeric *_num9_p;
398 extern const numeric &_num9;
399 extern const ex _ex9;
400 extern const numeric *_num10_p;
401 extern const numeric &_num10;
402 extern const ex _ex10;
403 extern const numeric *_num11_p;
404 extern const numeric &_num11;
405 extern const ex _ex11;
406 extern const numeric *_num12_p;
407 extern const numeric &_num12;
408 extern const ex _ex12;
409 extern const numeric *_num15_p;
410 extern const numeric &_num15;
411 extern const ex _ex15;
412 extern const numeric *_num18_p;
413 extern const numeric &_num18;
414 extern const ex _ex18;
415 extern const numeric *_num20_p;
416 extern const numeric &_num20;
417 extern const ex _ex20;
418 extern const numeric *_num24_p;
419 extern const numeric &_num24;
420 extern const ex _ex24;
421 extern const numeric *_num25_p;
422 extern const numeric &_num25;
423 extern const ex _ex25;
424 extern const numeric *_num30_p;
425 extern const numeric &_num30;
426 extern const ex _ex30;
427 extern const numeric *_num48_p;
428 extern const numeric &_num48;
429 extern const ex _ex48;
430 extern const numeric *_num60_p;
431 extern const numeric &_num60;
432 extern const ex _ex60;
433 extern const numeric *_num120_p;
434 extern const numeric &_num120;
435 extern const ex _ex120;
438 // Helper macros for class implementations (mostly useful for trivial classes)
440 #define DEFAULT_CTOR(classname) \
441 classname::classname() : inherited(TINFO_##classname) {}
443 #define DEFAULT_UNARCHIVE(classname) \
444 ex classname::unarchive(const archive_node &n, lst &sym_lst) \
446 return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
449 #define DEFAULT_ARCHIVING(classname) \
450 classname::classname(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} \
451 DEFAULT_UNARCHIVE(classname) \
452 void classname::archive(archive_node &n) const \
454 inherited::archive(n); \
457 #define DEFAULT_COMPARE(classname) \
458 int classname::compare_same_type(const basic & other) const \
460 /* by default, the objects are always identical */ \
464 #define DEFAULT_PRINT(classname, text) \
465 void classname::do_print(const print_context & c, unsigned level) const \
470 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
471 DEFAULT_PRINT(classname, text) \
472 void classname::do_print_latex(const print_latex & c, unsigned level) const \
480 #endif // ndef __GINAC_UTILS_H__