]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
29eef7cad8753edd12f12af3430ba13ddad58fd2
[ginac.git] / ginac / utils.h
1 /** @file utils.h
2  *
3  *  Interface to several small and furry utilities needed within GiNaC but not
4  *  of any interest to the user of the library. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
26
27 #include "config.h"
28
29 #include <string>
30 #include <stdexcept>
31 #include <functional>
32 #if defined(HAVE_SSTREAM)
33 #include <sstream>
34 #elif defined(HAVE_STRSTREAM)
35 #include <strstream>
36 #else
37 #error Need either sstream or strstream
38 #endif
39 #include "assertion.h"
40
41 namespace GiNaC {
42
43 // This should be obsoleted once <sstream> is widely deployed.
44 template<class T>
45 std::string ToString(const T & t)
46 {
47 #if defined(HAVE_SSTREAM)
48         std::ostringstream buf;
49         buf << t << std::ends;
50         return buf.str();
51 #else
52         char buf[256];
53         std::ostrstream(buf,sizeof(buf)) << t << std::ends;
54         return buf;
55 #endif
56 }
57
58 /** Exception class thrown by classes which provide their own series expansion
59  *  to signal that ordinary Taylor expansion is safe. */
60 class do_taylor {};
61
62 /** Exception class thrown when a singularity is encountered. */
63 class pole_error : public std::domain_error {
64 public:
65         explicit pole_error(const std::string& what_arg, int degree);
66         int degree(void) const;
67 private:
68         int deg;
69 };
70
71 // some compilers (e.g. cygwin) define a macro log2, causing confusion
72 #ifndef log2
73 unsigned log2(unsigned n);
74 #endif
75
76 /** Compare two pointers (just to establish some sort of canonical order).
77  *  @return -1, 0, or 1 */
78 inline int compare_pointers(const void * a, const void * b)
79 {
80         if (a<b)
81                 return -1;
82         else if (a>b)
83                 return 1;
84         return 0;
85 }
86
87 /** Rotate lower 31 bits of unsigned value by one bit to the left
88  *  (upper bit gets cleared). */
89 inline unsigned rotate_left_31(unsigned n)
90 {
91         // clear highest bit and shift 1 bit to the left
92         n = (n & 0x7FFFFFFFU) << 1;
93         
94         // overflow? clear highest bit and set lowest bit
95         if (n & 0x80000000U)
96                 n = (n & 0x7FFFFFFFU) | 0x00000001U;
97         
98         GINAC_ASSERT(n<0x80000000U);
99         
100         return n;
101 }
102
103 /** Golden ratio hash function for the 31 least significant bits. */
104 inline unsigned golden_ratio_hash(unsigned n)
105 {
106         // This function requires arithmetic with at least 64 significant bits
107 #if SIZEOF_LONG >= 8
108         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
109         // more efficient than 'long long'.
110         unsigned long l = n * 0x4f1bbcddL;
111         return (l & 0x7fffffffU) ^ (l >> 32);
112 #elif SIZEOF_LONG_LONG >= 8
113         // This requires 'long long' (or an equivalent 64 bit type)---which is,
114         // unfortunately, not ANSI-C++-compliant.
115         // (Yet C99 demands it, which is reason for hope.)
116         unsigned long long l = n * 0x4f1bbcddL;
117         return (l & 0x7fffffffU) ^ (l >> 32);
118 #elif SIZEOF_LONG_DOUBLE > 8
119         // If 'long double' is bigger than 64 bits, we assume that the mantissa
120         // has at least 64 bits. This is not guaranteed but it's a good guess.
121         // Unfortunately, it may lead to horribly slow code.
122         const static long double golden_ratio = .618033988749894848204586834370;
123         long double m = golden_ratio * n;
124         return unsigned((m - int(m)) * 0x80000000);
125 #else
126 #error "No 64 bit data type. You lose."
127 #endif
128 }
129
130 /* Compute the sign of a permutation of a container, with and without an
131    explicitly supplied comparison function. If the sign returned is 1 or -1,
132    the container is sorted after the operation. */
133 template <class It>
134 int permutation_sign(It first, It last)
135 {
136         if (first == last)
137                 return 0;
138         --last;
139         if (first == last)
140                 return 0;
141         It flag = first;
142         int sign = 1;
143
144         do {
145                 It i = last, other = last;
146                 --other;
147                 bool swapped = false;
148                 while (i != first) {
149                         if (*i < *other) {
150                                 std::iter_swap(other, i);
151                                 flag = other;
152                                 swapped = true;
153                                 sign = -sign;
154                         } else if (!(*other < *i))
155                                 return 0;
156                         --i; --other;
157                 }
158                 if (!swapped)
159                         return sign;
160                 ++flag;
161                 if (flag == last)
162                         return sign;
163                 first = flag;
164                 i = first; other = first;
165                 ++other;
166                 swapped = false;
167                 while (i != last) {
168                         if (*other < *i) {
169                                 std::iter_swap(i, other);
170                                 flag = other;
171                                 swapped = true;
172                                 sign = -sign;
173                         } else if (!(*i < *other))
174                                 return 0;
175                         ++i; ++other;
176                 }
177                 if (!swapped)
178                         return sign;
179                 last = flag;
180                 --last;
181         } while (first != last);
182
183         return sign;
184 }
185
186 template <class It, class Cmp>
187 int permutation_sign(It first, It last, Cmp comp)
188 {
189         if (first == last)
190                 return 0;
191         --last;
192         if (first == last)
193                 return 0;
194         It flag = first;
195         int sign = 1;
196
197         do {
198                 It i = last, other = last;
199                 --other;
200                 bool swapped = false;
201                 while (i != first) {
202                         if (comp(*i, *other)) {
203                                 std::iter_swap(other, i);
204                                 flag = other;
205                                 swapped = true;
206                                 sign = -sign;
207                         } else if (!comp(*other, *i))
208                                 return 0;
209                         --i; --other;
210                 }
211                 if (!swapped)
212                         return sign;
213                 ++flag;
214                 if (flag == last)
215                         return sign;
216                 first = flag;
217                 i = first; other = first;
218                 ++other;
219                 swapped = false;
220                 while (i != last) {
221                         if (comp(*other, *i)) {
222                                 std::iter_swap(i, other);
223                                 flag = other;
224                                 swapped = true;
225                                 sign = -sign;
226                         } else if (!comp(*i, *other))
227                                 return 0;
228                         ++i; ++other;
229                 }
230                 if (!swapped)
231                         return sign;
232                 last = flag;
233                 --last;
234         } while (first != last);
235
236         return sign;
237 }
238
239 /* Implementation of shaker sort, only compares adjacent elements. */
240 template <class It, class Cmp>
241 void shaker_sort(It first, It last, Cmp comp)
242 {
243         if (first == last)
244                 return;
245         --last;
246         if (first == last)
247                 return;
248         It flag = first;
249
250         do {
251                 It i = last, other = last;
252                 --other;
253                 bool swapped = false;
254                 while (i != first) {
255                         if (comp(*i, *other)) {
256                                 std::iter_swap(other, i);
257                                 flag = other;
258                                 swapped = true;
259                         }
260                         --i; --other;
261                 }
262                 if (!swapped)
263                         return;
264                 ++flag;
265                 if (flag == last)
266                         return;
267                 first = flag;
268                 i = first; other = first;
269                 ++other;
270                 swapped = false;
271                 while (i != last) {
272                         if (comp(*other, *i)) {
273                                 std::iter_swap(i, other);
274                                 flag = other;
275                                 swapped = true;
276                         }
277                         ++i; ++other;
278                 }
279                 if (!swapped)
280                         return;
281                 last = flag;
282                 --last;
283         } while (first != last);
284 }
285
286 /* In-place cyclic permutation of a container (no copying, only swapping). */
287 template <class It>
288 void cyclic_permutation(It first, It last, It new_first)
289 {
290         unsigned num = last - first;
291 again:
292         if (first == new_first || num < 2)
293                 return;
294
295         unsigned num1 = new_first - first, num2 = last - new_first;
296         if (num1 >= num2) {
297                 It a = first, b = new_first;
298                 while (b != last) {
299                         std::iter_swap(a, b);
300                         ++a; ++b;
301                 }
302                 if (num1 > num2) {
303                         first += num2;
304                         num = num1;
305                         goto again;
306                 }
307         } else {
308                 It a = new_first, b = last;
309                 do {
310                         --a; --b;
311                         std::iter_swap(a, b);
312                 } while (a != first);
313                 last -= num1;
314                 num = num2;
315                 goto again;
316         }
317 }
318
319 /* Function objects for STL sort() etc. */
320 struct ex_is_less : public std::binary_function<ex, ex, bool> {
321         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
322 };
323
324 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
325         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
326 };
327
328 // Collection of `construct on first use' wrappers for safely avoiding
329 // internal object replication without running into the `static
330 // initialization order fiasco'.  This chest of numbers helps speed up
331 // the library but should not be used outside it since it is
332 // potentially confusing.
333
334 class numeric;
335 class ex;
336
337 const numeric & _num_120(void);   // -120
338 const ex & _ex_120(void);
339 const numeric & _num_60(void);    // -60
340 const ex & _ex_60(void);
341 const numeric & _num_48(void);    // -48
342 const ex & _ex_48(void);
343 const numeric & _num_30(void);    // -30
344 const ex & _ex_30(void);
345 const numeric & _num_25(void);    // -25
346 const ex & _ex_25(void);
347 const numeric & _num_24(void);    // -24
348 const ex & _ex_24(void);
349 const numeric & _num_20(void);    // -20
350 const ex & _ex_20(void);
351 const numeric & _num_18(void);    // -18
352 const ex & _ex_18(void);
353 const numeric & _num_15(void);    // -15
354 const ex & _ex_15(void);
355 const numeric & _num_12(void);    // -12
356 const ex & _ex_12(void);
357 const numeric & _num_11(void);    // -11
358 const ex & _ex_11(void);
359 const numeric & _num_10(void);    // -10
360 const ex & _ex_10(void);
361 const numeric & _num_9(void);     // -9
362 const ex & _ex_9(void);
363 const numeric & _num_8(void);     // -8
364 const ex & _ex_8(void);
365 const numeric & _num_7(void);     // -7
366 const ex & _ex_7(void);
367 const numeric & _num_6(void);     // -6
368 const ex & _ex_6(void);
369 const numeric & _num_5(void);     // -5
370 const ex & _ex_5(void);
371 const numeric & _num_4(void);     // -4
372 const ex & _ex_4(void);
373 const numeric & _num_3(void);     // -3
374 const ex & _ex_3(void);
375 const numeric & _num_2(void);     // -2
376 const ex & _ex_2(void);
377 const numeric & _num_1(void);     // -1
378 const ex & _ex_1(void);
379 const numeric & _num_1_2(void);   // -1/2
380 const ex & _ex_1_2(void);
381 const numeric & _num_1_3(void);   // -1/3
382 const ex & _ex_1_3(void);
383 const numeric & _num_1_4(void);   // -1/4
384 const ex & _ex_1_4(void);
385 const numeric & _num0(void);      //  0
386 const ex & _ex0(void);
387 const numeric & _num1_4(void);    //  1/4
388 const ex & _ex1_4(void);
389 const numeric & _num1_3(void);    //  1/3
390 const ex & _ex1_3(void);
391 const numeric & _num1_2(void);    //  1/2
392 const ex & _ex1_2(void);
393 const numeric & _num1(void);      //  1
394 const ex & _ex1(void);
395 const numeric & _num2(void);      //  2
396 const ex & _ex2(void);
397 const numeric & _num3(void);      //  3
398 const ex & _ex3(void);
399 const numeric & _num4(void);      //  4
400 const ex & _ex4(void);
401 const numeric & _num5(void);      //  5
402 const ex & _ex5(void);
403 const numeric & _num6(void);      //  6
404 const ex & _ex6(void);
405 const numeric & _num7(void);      //  7
406 const ex & _ex7(void);
407 const numeric & _num8(void);      //  8
408 const ex & _ex8(void);
409 const numeric & _num9(void);      //  9
410 const ex & _ex9(void);
411 const numeric & _num10(void);     //  10
412 const ex & _ex10(void);
413 const numeric & _num11(void);     //  11
414 const ex & _ex11(void);
415 const numeric & _num12(void);     //  12
416 const ex & _ex12(void);
417 const numeric & _num15(void);     //  15
418 const ex & _ex15(void);
419 const numeric & _num18(void);     //  18
420 const ex & _ex18(void);
421 const numeric & _num20(void);     //  20
422 const ex & _ex20(void);
423 const numeric & _num24(void);     //  24
424 const ex & _ex24(void);
425 const numeric & _num25(void);     //  25
426 const ex & _ex25(void);
427 const numeric & _num30(void);     //  30
428 const ex & _ex30(void);
429 const numeric & _num48(void);     //  48
430 const ex & _ex48(void);
431 const numeric & _num60(void);     //  60
432 const ex & _ex60(void);
433 const numeric & _num120(void);    //  120
434 const ex & _ex120(void);
435
436
437 // Helper macros for class implementations (mostly useful for trivial classes)
438
439 #define DEFAULT_COPY(classname) \
440 void classname::copy(const classname & other) \
441 { \
442         inherited::copy(other); \
443 }
444
445 #define DEFAULT_DESTROY(classname) \
446 void classname::destroy(bool call_parent) \
447 { \
448         if (call_parent) \
449                 inherited::destroy(call_parent); \
450 }
451
452 #define DEFAULT_CTORS(classname) \
453 classname::classname() : inherited(TINFO_##classname) \
454 { \
455         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
456 } \
457 DEFAULT_COPY(classname) \
458 DEFAULT_DESTROY(classname)
459
460 #define DEFAULT_UNARCHIVE(classname) \
461 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
462 { \
463         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
464 }
465
466 #define DEFAULT_ARCHIVING(classname) \
467 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
468 { \
469         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
470 } \
471 DEFAULT_UNARCHIVE(classname) \
472 void classname::archive(archive_node &n) const \
473 { \
474         inherited::archive(n); \
475 }
476
477 #define DEFAULT_COMPARE(classname) \
478 int classname::compare_same_type(const basic & other) const \
479 { \
480         /* by default, the objects are always identical */ \
481         return 0; \
482 }
483
484 #define DEFAULT_PRINT(classname, text) \
485 void classname::print(const print_context & c, unsigned level) const \
486 { \
487         debugmsg(#classname " print", LOGLEVEL_PRINT); \
488         if (is_of_type(c, print_tree)) \
489                 inherited::print(c, level); \
490         else \
491                 c.s << text; \
492 }
493
494 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
495 void classname::print(const print_context & c, unsigned level) const \
496 { \
497         debugmsg(#classname " print", LOGLEVEL_PRINT); \
498         if (is_of_type(c, print_tree)) \
499                 inherited::print(c, level); \
500         else if (is_of_type(c, print_latex)) \
501                 c.s << latex; \
502         else \
503                 c.s << text; \
504 }
505
506 } // namespace GiNaC
507
508 #endif // ndef __GINAC_UTILS_H__