]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
fixed is_exactly_a<symbol>
[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-2004 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 <functional>
31
32 #include "assertion.h"
33
34 namespace GiNaC {
35
36 /** Exception class thrown by functions to signal unimplemented functionality
37  *  so the expression may just be .hold() */
38 class dunno {};
39
40 // some compilers (e.g. cygwin) define a macro log2, causing confusion
41 #ifndef log2
42 unsigned log2(unsigned n);
43 #endif
44
45 /** Compare two pointers (just to establish some sort of canonical order).
46  *  @return -1, 0, or 1 */
47 template <class T>
48 inline int compare_pointers(const T * a, const T * b)
49 {
50         // '<' is not defined for pointers that don't point to the same array,
51         // but std::less is.
52         if (std::less<const T *>()(a, b))
53                 return -1;
54         else if (std::less<const T *>()(b, a))
55                 return 1;
56         return 0;
57 }
58
59 /** Rotate bits of unsigned value by one bit to the left. */
60 inline unsigned rotate_left(unsigned n)
61 {
62         return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
63 }
64
65 /** Truncated multiplication with golden ratio, for computing hash values. */
66 inline unsigned golden_ratio_hash(unsigned n)
67 {
68         // This function works much better when fast arithmetic with at
69         // least 64 significant bits is available.
70 #if SIZEOF_LONG >= 8
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;
74         return (unsigned)l;
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;
80         return (unsigned)l;
81 #else
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);
87 #endif
88 }
89
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. */
93 template <class It>
94 int permutation_sign(It first, It last)
95 {
96         if (first == last)
97                 return 0;
98         --last;
99         if (first == last)
100                 return 0;
101         It flag = first;
102         int sign = 1;
103
104         do {
105                 It i = last, other = last;
106                 --other;
107                 bool swapped = false;
108                 while (i != first) {
109                         if (*i < *other) {
110                                 std::iter_swap(other, i);
111                                 flag = other;
112                                 swapped = true;
113                                 sign = -sign;
114                         } else if (!(*other < *i))
115                                 return 0;
116                         --i; --other;
117                 }
118                 if (!swapped)
119                         return sign;
120                 ++flag;
121                 if (flag == last)
122                         return sign;
123                 first = flag;
124                 i = first; other = first;
125                 ++other;
126                 swapped = false;
127                 while (i != last) {
128                         if (*other < *i) {
129                                 std::iter_swap(i, other);
130                                 flag = other;
131                                 swapped = true;
132                                 sign = -sign;
133                         } else if (!(*i < *other))
134                                 return 0;
135                         ++i; ++other;
136                 }
137                 if (!swapped)
138                         return sign;
139                 last = flag;
140                 --last;
141         } while (first != last);
142
143         return sign;
144 }
145
146 template <class It, class Cmp, class Swap>
147 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
148 {
149         if (first == last)
150                 return 0;
151         --last;
152         if (first == last)
153                 return 0;
154         It flag = first;
155         int sign = 1;
156
157         do {
158                 It i = last, other = last;
159                 --other;
160                 bool swapped = false;
161                 while (i != first) {
162                         if (comp(*i, *other)) {
163                                 swapit(*other, *i);
164                                 flag = other;
165                                 swapped = true;
166                                 sign = -sign;
167                         } else if (!comp(*other, *i))
168                                 return 0;
169                         --i; --other;
170                 }
171                 if (!swapped)
172                         return sign;
173                 ++flag;
174                 if (flag == last)
175                         return sign;
176                 first = flag;
177                 i = first; other = first;
178                 ++other;
179                 swapped = false;
180                 while (i != last) {
181                         if (comp(*other, *i)) {
182                                 swapit(*i, *other);
183                                 flag = other;
184                                 swapped = true;
185                                 sign = -sign;
186                         } else if (!comp(*i, *other))
187                                 return 0;
188                         ++i; ++other;
189                 }
190                 if (!swapped)
191                         return sign;
192                 last = flag;
193                 --last;
194         } while (first != last);
195
196         return sign;
197 }
198
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)
202 {
203         if (first == last)
204                 return;
205         --last;
206         if (first == last)
207                 return;
208         It flag = first;
209
210         do {
211                 It i = last, other = last;
212                 --other;
213                 bool swapped = false;
214                 while (i != first) {
215                         if (comp(*i, *other)) {
216                                 swapit(*other, *i);
217                                 flag = other;
218                                 swapped = true;
219                         }
220                         --i; --other;
221                 }
222                 if (!swapped)
223                         return;
224                 ++flag;
225                 if (flag == last)
226                         return;
227                 first = flag;
228                 i = first; other = first;
229                 ++other;
230                 swapped = false;
231                 while (i != last) {
232                         if (comp(*other, *i)) {
233                                 swapit(*i, *other);
234                                 flag = other;
235                                 swapped = true;
236                         }
237                         ++i; ++other;
238                 }
239                 if (!swapped)
240                         return;
241                 last = flag;
242                 --last;
243         } while (first != last);
244 }
245
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)
249 {
250         unsigned num = last - first;
251 again:
252         if (first == new_first || num < 2)
253                 return;
254
255         unsigned num1 = new_first - first, num2 = last - new_first;
256         if (num1 >= num2) {
257                 It a = first, b = new_first;
258                 while (b != last) {
259                         swapit(*a, *b);
260                         ++a; ++b;
261                 }
262                 if (num1 > num2) {
263                         first += num2;
264                         num = num1;
265                         goto again;
266                 }
267         } else {
268                 It a = new_first, b = last;
269                 do {
270                         --a; --b;
271                         swapit(*a, *b);
272                 } while (a != first);
273                 last -= num1;
274                 num = num2;
275                 goto again;
276         }
277 }
278
279
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.
285
286 class ex;
287
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;
436
437
438 // Helper macros for class implementations (mostly useful for trivial classes)
439
440 #define DEFAULT_CTOR(classname) \
441 classname::classname() : inherited(TINFO_##classname) {}
442
443 #define DEFAULT_UNARCHIVE(classname) \
444 ex classname::unarchive(const archive_node &n, lst &sym_lst) \
445 { \
446         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
447 }
448
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 \
453 { \
454         inherited::archive(n); \
455 }
456
457 #define DEFAULT_COMPARE(classname) \
458 int classname::compare_same_type(const basic & other) const \
459 { \
460         /* by default, the objects are always identical */ \
461         return 0; \
462 }
463
464 #define DEFAULT_PRINT(classname, text) \
465 void classname::do_print(const print_context & c, unsigned level) const \
466 { \
467         c.s << text; \
468 }
469
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 \
473 { \
474         c.s << latex; \
475 }
476
477 } // namespace GiNaC
478
479
480 #endif // ndef __GINAC_UTILS_H__