]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
8a33be0ed6085ebfd32578a53e8560b3eabb8093
[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-2002 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 "assertion.h"
32
33 namespace GiNaC {
34
35 /** Exception class thrown by classes which provide their own series expansion
36  *  to signal that ordinary Taylor expansion is safe. */
37 class do_taylor {};
38
39 /** Exception class thrown by functions to signal unimplemented functionality
40  *  so the expression may just be .hold() */
41 class dunno {};
42
43 /** Exception class thrown when a singularity is encountered. */
44 class pole_error : public std::domain_error {
45 public:
46         explicit pole_error(const std::string& what_arg, int degree);
47         int degree(void) const;
48 private:
49         int deg;
50 };
51
52 // some compilers (e.g. cygwin) define a macro log2, causing confusion
53 #ifndef log2
54 unsigned log2(unsigned n);
55 #endif
56
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)
60 {
61         if (a<b)
62                 return -1;
63         else if (a>b)
64                 return 1;
65         return 0;
66 }
67
68 /** Rotate bits of unsigned value by one bit to the left. */
69 inline unsigned rotate_left(unsigned n)
70 {
71         return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
72 }
73
74 /** Truncated multiplication with golden ratio, for computing hash values. */
75 inline unsigned golden_ratio_hash(unsigned n)
76 {
77         // This function works much better when fast arithmetic with at
78         // least 64 significant bits is available.
79 #if SIZEOF_LONG >= 8
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;
83         return (unsigned)l;
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;
89         return (unsigned)l;
90 #else
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);
96 #endif
97 }
98
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. */
102 template <class It>
103 int permutation_sign(It first, It last)
104 {
105         if (first == last)
106                 return 0;
107         --last;
108         if (first == last)
109                 return 0;
110         It flag = first;
111         int sign = 1;
112
113         do {
114                 It i = last, other = last;
115                 --other;
116                 bool swapped = false;
117                 while (i != first) {
118                         if (*i < *other) {
119                                 std::iter_swap(other, i);
120                                 flag = other;
121                                 swapped = true;
122                                 sign = -sign;
123                         } else if (!(*other < *i))
124                                 return 0;
125                         --i; --other;
126                 }
127                 if (!swapped)
128                         return sign;
129                 ++flag;
130                 if (flag == last)
131                         return sign;
132                 first = flag;
133                 i = first; other = first;
134                 ++other;
135                 swapped = false;
136                 while (i != last) {
137                         if (*other < *i) {
138                                 std::iter_swap(i, other);
139                                 flag = other;
140                                 swapped = true;
141                                 sign = -sign;
142                         } else if (!(*i < *other))
143                                 return 0;
144                         ++i; ++other;
145                 }
146                 if (!swapped)
147                         return sign;
148                 last = flag;
149                 --last;
150         } while (first != last);
151
152         return sign;
153 }
154
155 template <class It, class Cmp, class Swap>
156 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
157 {
158         if (first == last)
159                 return 0;
160         --last;
161         if (first == last)
162                 return 0;
163         It flag = first;
164         int sign = 1;
165
166         do {
167                 It i = last, other = last;
168                 --other;
169                 bool swapped = false;
170                 while (i != first) {
171                         if (comp(*i, *other)) {
172                                 swapit(*other, *i);
173                                 flag = other;
174                                 swapped = true;
175                                 sign = -sign;
176                         } else if (!comp(*other, *i))
177                                 return 0;
178                         --i; --other;
179                 }
180                 if (!swapped)
181                         return sign;
182                 ++flag;
183                 if (flag == last)
184                         return sign;
185                 first = flag;
186                 i = first; other = first;
187                 ++other;
188                 swapped = false;
189                 while (i != last) {
190                         if (comp(*other, *i)) {
191                                 swapit(*i, *other);
192                                 flag = other;
193                                 swapped = true;
194                                 sign = -sign;
195                         } else if (!comp(*i, *other))
196                                 return 0;
197                         ++i; ++other;
198                 }
199                 if (!swapped)
200                         return sign;
201                 last = flag;
202                 --last;
203         } while (first != last);
204
205         return sign;
206 }
207
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)
211 {
212         if (first == last)
213                 return;
214         --last;
215         if (first == last)
216                 return;
217         It flag = first;
218
219         do {
220                 It i = last, other = last;
221                 --other;
222                 bool swapped = false;
223                 while (i != first) {
224                         if (comp(*i, *other)) {
225                                 swapit(*other, *i);
226                                 flag = other;
227                                 swapped = true;
228                         }
229                         --i; --other;
230                 }
231                 if (!swapped)
232                         return;
233                 ++flag;
234                 if (flag == last)
235                         return;
236                 first = flag;
237                 i = first; other = first;
238                 ++other;
239                 swapped = false;
240                 while (i != last) {
241                         if (comp(*other, *i)) {
242                                 swapit(*i, *other);
243                                 flag = other;
244                                 swapped = true;
245                         }
246                         ++i; ++other;
247                 }
248                 if (!swapped)
249                         return;
250                 last = flag;
251                 --last;
252         } while (first != last);
253 }
254
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)
258 {
259         unsigned num = last - first;
260 again:
261         if (first == new_first || num < 2)
262                 return;
263
264         unsigned num1 = new_first - first, num2 = last - new_first;
265         if (num1 >= num2) {
266                 It a = first, b = new_first;
267                 while (b != last) {
268                         swapit(*a, *b);
269                         ++a; ++b;
270                 }
271                 if (num1 > num2) {
272                         first += num2;
273                         num = num1;
274                         goto again;
275                 }
276         } else {
277                 It a = new_first, b = last;
278                 do {
279                         --a; --b;
280                         swapit(*a, *b);
281                 } while (a != first);
282                 last -= num1;
283                 num = num2;
284                 goto again;
285         }
286 }
287
288
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.
294
295 class numeric;
296 class ex;
297
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;
445
446
447 // Helper macros for class implementations (mostly useful for trivial classes)
448
449 #define DEFAULT_COPY(classname) \
450 void classname::copy(const classname & other) \
451 { \
452         inherited::copy(other); \
453 }
454
455 #define DEFAULT_DESTROY(classname) \
456 void classname::destroy(bool call_parent) \
457 { \
458         if (call_parent) \
459                 inherited::destroy(call_parent); \
460 }
461
462 #define DEFAULT_CTORS(classname) \
463 classname::classname() : inherited(TINFO_##classname) {} \
464 DEFAULT_COPY(classname) \
465 DEFAULT_DESTROY(classname)
466
467 #define DEFAULT_UNARCHIVE(classname) \
468 ex classname::unarchive(const archive_node &n, const lst &sym_lst) \
469 { \
470         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
471 }
472
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 \
477 { \
478         inherited::archive(n); \
479 }
480
481 #define DEFAULT_COMPARE(classname) \
482 int classname::compare_same_type(const basic & other) const \
483 { \
484         /* by default, the objects are always identical */ \
485         return 0; \
486 }
487
488 #define DEFAULT_PRINT(classname, text) \
489 void classname::print(const print_context & c, unsigned level) const \
490 { \
491         if (is_a<print_tree>(c)) \
492                 inherited::print(c, level); \
493         else \
494                 c.s << text; \
495 }
496
497 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
498 void classname::print(const print_context & c, unsigned level) const \
499 { \
500         if (is_a<print_tree>(c)) \
501                 inherited::print(c, level); \
502         else if (is_a<print_latex>(c)) \
503                 c.s << latex; \
504         else \
505                 c.s << text; \
506 }
507
508 } // namespace GiNaC
509
510
511 #endif // ndef __GINAC_UTILS_H__