]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- do something about the mad cast disease.
[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 #if defined(HAVE_SSTREAM)
32 #include <sstream>
33 #elif defined(HAVE_STRSTREAM)
34 #include <strstream>
35 #else
36 #error Need either sstream or strstream
37 #endif
38 #include "assertion.h"
39
40 namespace GiNaC {
41
42 // This should be obsoleted once <sstream> is widely deployed.
43 template<class T>
44 std::string ToString(const T & t)
45 {
46 #if defined(HAVE_SSTREAM)
47         std::ostringstream buf;
48         buf << t << std::ends;
49         return buf.str();
50 #else
51         char buf[256];
52         std::ostrstream(buf,sizeof(buf)) << t << std::ends;
53         return buf;
54 #endif
55 }
56
57 /** Exception class thrown by classes which provide their own series expansion
58  *  to signal that ordinary Taylor expansion is safe. */
59 class do_taylor {};
60
61 /** Exception class thrown when a singularity is encountered. */
62 class pole_error : public std::domain_error {
63 public:
64         explicit pole_error(const std::string& what_arg, int degree);
65         int degree(void) const;
66 private:
67         int deg;
68 };
69
70 // some compilers (e.g. cygwin) define a macro log2, causing confusion
71 #ifndef log2
72 unsigned log2(unsigned n);
73 #endif
74
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)
78 {
79         if (a<b)
80                 return -1;
81         else if (a>b)
82                 return 1;
83         return 0;
84 }
85
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)
89 {
90         // clear highest bit and shift 1 bit to the left
91         n = (n & 0x7FFFFFFFU) << 1;
92         
93         // overflow? clear highest bit and set lowest bit
94         if (n & 0x80000000U)
95                 n = (n & 0x7FFFFFFFU) | 0x00000001U;
96         
97         GINAC_ASSERT(n<0x80000000U);
98         
99         return n;
100 }
101
102 /** Golden ratio hash function for the 31 least significant bits. */
103 inline unsigned golden_ratio_hash(unsigned n)
104 {
105         // This function requires arithmetic with at least 64 significant bits
106 #if SIZEOF_LONG >= 8
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);
124 #else
125 #error "No 64 bit data type. You lose."
126 #endif
127 }
128
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. */
132 template <class It>
133 int permutation_sign(It first, It last)
134 {
135         if (first == last)
136                 return 0;
137         --last;
138         if (first == last)
139                 return 0;
140         It flag = first;
141         int sign = 1;
142
143         do {
144                 It i = last, other = last;
145                 --other;
146                 bool swapped = false;
147                 while (i != first) {
148                         if (*i < *other) {
149                                 std::iter_swap(other, i);
150                                 flag = other;
151                                 swapped = true;
152                                 sign = -sign;
153                         } else if (!(*other < *i))
154                                 return 0;
155                         --i; --other;
156                 }
157                 if (!swapped)
158                         return sign;
159                 ++flag;
160                 if (flag == last)
161                         return sign;
162                 first = flag;
163                 i = first; other = first;
164                 ++other;
165                 swapped = false;
166                 while (i != last) {
167                         if (*other < *i) {
168                                 std::iter_swap(i, other);
169                                 flag = other;
170                                 swapped = true;
171                                 sign = -sign;
172                         } else if (!(*i < *other))
173                                 return 0;
174                         ++i; ++other;
175                 }
176                 if (!swapped)
177                         return sign;
178                 last = flag;
179                 --last;
180         } while (first != last);
181
182         return sign;
183 }
184
185 template <class It, class Cmp, class Swap>
186 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
187 {
188         if (first == last)
189                 return 0;
190         --last;
191         if (first == last)
192                 return 0;
193         It flag = first;
194         int sign = 1;
195
196         do {
197                 It i = last, other = last;
198                 --other;
199                 bool swapped = false;
200                 while (i != first) {
201                         if (comp(*i, *other)) {
202                                 swapit(*other, *i);
203                                 flag = other;
204                                 swapped = true;
205                                 sign = -sign;
206                         } else if (!comp(*other, *i))
207                                 return 0;
208                         --i; --other;
209                 }
210                 if (!swapped)
211                         return sign;
212                 ++flag;
213                 if (flag == last)
214                         return sign;
215                 first = flag;
216                 i = first; other = first;
217                 ++other;
218                 swapped = false;
219                 while (i != last) {
220                         if (comp(*other, *i)) {
221                                 swapit(*i, *other);
222                                 flag = other;
223                                 swapped = true;
224                                 sign = -sign;
225                         } else if (!comp(*i, *other))
226                                 return 0;
227                         ++i; ++other;
228                 }
229                 if (!swapped)
230                         return sign;
231                 last = flag;
232                 --last;
233         } while (first != last);
234
235         return sign;
236 }
237
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)
241 {
242         if (first == last)
243                 return;
244         --last;
245         if (first == last)
246                 return;
247         It flag = first;
248
249         do {
250                 It i = last, other = last;
251                 --other;
252                 bool swapped = false;
253                 while (i != first) {
254                         if (comp(*i, *other)) {
255                                 swapit(*other, *i);
256                                 flag = other;
257                                 swapped = true;
258                         }
259                         --i; --other;
260                 }
261                 if (!swapped)
262                         return;
263                 ++flag;
264                 if (flag == last)
265                         return;
266                 first = flag;
267                 i = first; other = first;
268                 ++other;
269                 swapped = false;
270                 while (i != last) {
271                         if (comp(*other, *i)) {
272                                 swapit(*i, *other);
273                                 flag = other;
274                                 swapped = true;
275                         }
276                         ++i; ++other;
277                 }
278                 if (!swapped)
279                         return;
280                 last = flag;
281                 --last;
282         } while (first != last);
283 }
284
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)
288 {
289         unsigned num = last - first;
290 again:
291         if (first == new_first || num < 2)
292                 return;
293
294         unsigned num1 = new_first - first, num2 = last - new_first;
295         if (num1 >= num2) {
296                 It a = first, b = new_first;
297                 while (b != last) {
298                         swapit(*a, *b);
299                         ++a; ++b;
300                 }
301                 if (num1 > num2) {
302                         first += num2;
303                         num = num1;
304                         goto again;
305                 }
306         } else {
307                 It a = new_first, b = last;
308                 do {
309                         --a; --b;
310                         swapit(*a, *b);
311                 } while (a != first);
312                 last -= num1;
313                 num = num2;
314                 goto again;
315         }
316 }
317
318
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.
324
325 class numeric;
326 class ex;
327
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);
426
427
428 // Helper macros for class implementations (mostly useful for trivial classes)
429
430 #define DEFAULT_COPY(classname) \
431 void classname::copy(const classname & other) \
432 { \
433         inherited::copy(other); \
434 }
435
436 #define DEFAULT_DESTROY(classname) \
437 void classname::destroy(bool call_parent) \
438 { \
439         if (call_parent) \
440                 inherited::destroy(call_parent); \
441 }
442
443 #define DEFAULT_CTORS(classname) \
444 classname::classname() : inherited(TINFO_##classname) \
445 { \
446         debugmsg(#classname " default constructor", LOGLEVEL_CONSTRUCT); \
447 } \
448 DEFAULT_COPY(classname) \
449 DEFAULT_DESTROY(classname)
450
451 #define DEFAULT_UNARCHIVE(classname) \
452 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
453 { \
454         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
455 }
456
457 #define DEFAULT_ARCHIVING(classname) \
458 classname::classname(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst) \
459 { \
460         debugmsg(#classname " constructor from archive_node", LOGLEVEL_CONSTRUCT); \
461 } \
462 DEFAULT_UNARCHIVE(classname) \
463 void classname::archive(archive_node &n) const \
464 { \
465         inherited::archive(n); \
466 }
467
468 #define DEFAULT_COMPARE(classname) \
469 int classname::compare_same_type(const basic & other) const \
470 { \
471         /* by default, the objects are always identical */ \
472         return 0; \
473 }
474
475 #define DEFAULT_PRINT(classname, text) \
476 void classname::print(const print_context & c, unsigned level) const \
477 { \
478         debugmsg(#classname " print", LOGLEVEL_PRINT); \
479         if (is_a<print_tree>(c)) \
480                 inherited::print(c, level); \
481         else \
482                 c.s << text; \
483 }
484
485 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
486 void classname::print(const print_context & c, unsigned level) const \
487 { \
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)) \
492                 c.s << latex; \
493         else \
494                 c.s << text; \
495 }
496
497 } // namespace GiNaC
498
499 #endif // ndef __GINAC_UTILS_H__