]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
fixed typo in comment
[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 #ifdef log2
42 #undef log2
43 #endif
44
45 unsigned log2(unsigned n);
46
47 /** Compare two pointers (just to establish some sort of canonical order).
48  *  @return -1, 0, or 1 */
49 template <class T>
50 inline int compare_pointers(const T * a, const T * b)
51 {
52         // '<' is not defined for pointers that don't point to the same array,
53         // but std::less is.
54         if (std::less<const T *>()(a, b))
55                 return -1;
56         else if (std::less<const T *>()(b, a))
57                 return 1;
58         return 0;
59 }
60
61 /** Rotate bits of unsigned value by one bit to the left. */
62 inline unsigned rotate_left(unsigned n)
63 {
64         return (n & 0x80000000U) ? (n << 1 | 0x00000001U) : (n << 1);
65 }
66
67 /** Truncated multiplication with golden ratio, for computing hash values. */
68 inline unsigned golden_ratio_hash(unsigned n)
69 {
70         // This function works much better when fast arithmetic with at
71         // least 64 significant bits is available.
72 #if SIZEOF_LONG >= 8
73         // So 'long' has 64 bits.  Excellent!  We prefer it because it might be
74         // more efficient than 'long long'.
75         unsigned long l = n * 0x4f1bbcddUL;
76         return (unsigned)l;
77 #elif SIZEOF_LONG_LONG >= 8
78         // This requires 'long long' (or an equivalent 64 bit type)---which is,
79         // unfortunately, not ANSI-C++-compliant.
80         // (Yet C99 demands it, which is reason for hope.)
81         unsigned long long l = n * 0x4f1bbcddULL;
82         return (unsigned)l;
83 #else
84         // Without a type with 64 significant bits do the multiplication manually
85         // by splitting n up into the lower and upper two bytes.
86         const unsigned n0 = (n & 0x0000ffffU);
87         const unsigned n1 = (n & 0xffff0000U) >> 16;
88         return (n0 * 0x0000bcddU) + ((n1 * 0x0000bcddU + n0 * 0x00004f1bU) << 16);
89 #endif
90 }
91
92 /* Compute the sign of a permutation of a container, with and without an
93    explicitly supplied comparison function. If the sign returned is 1 or -1,
94    the container is sorted after the operation. */
95 template <class It>
96 int permutation_sign(It first, It last)
97 {
98         if (first == last)
99                 return 0;
100         --last;
101         if (first == last)
102                 return 0;
103         It flag = first;
104         int sign = 1;
105
106         do {
107                 It i = last, other = last;
108                 --other;
109                 bool swapped = false;
110                 while (i != first) {
111                         if (*i < *other) {
112                                 std::iter_swap(other, i);
113                                 flag = other;
114                                 swapped = true;
115                                 sign = -sign;
116                         } else if (!(*other < *i))
117                                 return 0;
118                         --i; --other;
119                 }
120                 if (!swapped)
121                         return sign;
122                 ++flag;
123                 if (flag == last)
124                         return sign;
125                 first = flag;
126                 i = first; other = first;
127                 ++other;
128                 swapped = false;
129                 while (i != last) {
130                         if (*other < *i) {
131                                 std::iter_swap(i, other);
132                                 flag = other;
133                                 swapped = true;
134                                 sign = -sign;
135                         } else if (!(*i < *other))
136                                 return 0;
137                         ++i; ++other;
138                 }
139                 if (!swapped)
140                         return sign;
141                 last = flag;
142                 --last;
143         } while (first != last);
144
145         return sign;
146 }
147
148 template <class It, class Cmp, class Swap>
149 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
150 {
151         if (first == last)
152                 return 0;
153         --last;
154         if (first == last)
155                 return 0;
156         It flag = first;
157         int sign = 1;
158
159         do {
160                 It i = last, other = last;
161                 --other;
162                 bool swapped = false;
163                 while (i != first) {
164                         if (comp(*i, *other)) {
165                                 swapit(*other, *i);
166                                 flag = other;
167                                 swapped = true;
168                                 sign = -sign;
169                         } else if (!comp(*other, *i))
170                                 return 0;
171                         --i; --other;
172                 }
173                 if (!swapped)
174                         return sign;
175                 ++flag;
176                 if (flag == last)
177                         return sign;
178                 first = flag;
179                 i = first; other = first;
180                 ++other;
181                 swapped = false;
182                 while (i != last) {
183                         if (comp(*other, *i)) {
184                                 swapit(*i, *other);
185                                 flag = other;
186                                 swapped = true;
187                                 sign = -sign;
188                         } else if (!comp(*i, *other))
189                                 return 0;
190                         ++i; ++other;
191                 }
192                 if (!swapped)
193                         return sign;
194                 last = flag;
195                 --last;
196         } while (first != last);
197
198         return sign;
199 }
200
201 /* Implementation of shaker sort, only compares adjacent elements. */
202 template <class It, class Cmp, class Swap>
203 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
204 {
205         if (first == last)
206                 return;
207         --last;
208         if (first == last)
209                 return;
210         It flag = first;
211
212         do {
213                 It i = last, other = last;
214                 --other;
215                 bool swapped = false;
216                 while (i != first) {
217                         if (comp(*i, *other)) {
218                                 swapit(*other, *i);
219                                 flag = other;
220                                 swapped = true;
221                         }
222                         --i; --other;
223                 }
224                 if (!swapped)
225                         return;
226                 ++flag;
227                 if (flag == last)
228                         return;
229                 first = flag;
230                 i = first; other = first;
231                 ++other;
232                 swapped = false;
233                 while (i != last) {
234                         if (comp(*other, *i)) {
235                                 swapit(*i, *other);
236                                 flag = other;
237                                 swapped = true;
238                         }
239                         ++i; ++other;
240                 }
241                 if (!swapped)
242                         return;
243                 last = flag;
244                 --last;
245         } while (first != last);
246 }
247
248 /* In-place cyclic permutation of a container (no copying, only swapping). */
249 template <class It, class Swap>
250 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
251 {
252         unsigned num = last - first;
253 again:
254         if (first == new_first || num < 2)
255                 return;
256
257         unsigned num1 = new_first - first, num2 = last - new_first;
258         if (num1 >= num2) {
259                 It a = first, b = new_first;
260                 while (b != last) {
261                         swapit(*a, *b);
262                         ++a; ++b;
263                 }
264                 if (num1 > num2) {
265                         first += num2;
266                         num = num1;
267                         goto again;
268                 }
269         } else {
270                 It a = new_first, b = last;
271                 do {
272                         --a; --b;
273                         swapit(*a, *b);
274                 } while (a != first);
275                 last -= num1;
276                 num = num2;
277                 goto again;
278         }
279 }
280
281
282 // Collection of `construct on first use' wrappers for safely avoiding
283 // internal object replication without running into the `static
284 // initialization order fiasco'.  This chest of numbers helps speed up
285 // the library but should not be used outside it since it is
286 // potentially confusing.
287
288 class ex;
289
290 extern const numeric *_num_120_p;
291 extern const numeric &_num_120;
292 extern const ex _ex_120;
293 extern const numeric *_num_60_p;
294 extern const numeric &_num_60;
295 extern const ex _ex_60;
296 extern const numeric *_num_48_p;
297 extern const numeric &_num_48;
298 extern const ex _ex_48;
299 extern const numeric *_num_30_p;
300 extern const numeric &_num_30;
301 extern const ex _ex_30;
302 extern const numeric *_num_25_p;
303 extern const numeric &_num_25;
304 extern const ex _ex_25;
305 extern const numeric *_num_24_p;
306 extern const numeric &_num_24;
307 extern const ex _ex_24;
308 extern const numeric *_num_20_p;
309 extern const numeric &_num_20;
310 extern const ex _ex_20;
311 extern const numeric *_num_18_p;
312 extern const numeric &_num_18;
313 extern const ex _ex_18;
314 extern const numeric *_num_15_p;
315 extern const numeric &_num_15;
316 extern const ex _ex_15;
317 extern const numeric *_num_12_p;
318 extern const numeric &_num_12;
319 extern const ex _ex_12;
320 extern const numeric *_num_11_p;
321 extern const numeric &_num_11;
322 extern const ex _ex_11;
323 extern const numeric *_num_10_p;
324 extern const numeric &_num_10;
325 extern const ex _ex_10;
326 extern const numeric *_num_9_p;
327 extern const numeric &_num_9;
328 extern const ex _ex_9;
329 extern const numeric *_num_8_p;
330 extern const numeric &_num_8;
331 extern const ex _ex_8;
332 extern const numeric *_num_7_p;
333 extern const numeric &_num_7;
334 extern const ex _ex_7;
335 extern const numeric *_num_6_p;
336 extern const numeric &_num_6;
337 extern const ex _ex_6;
338 extern const numeric *_num_5_p;
339 extern const numeric &_num_5;
340 extern const ex _ex_5;
341 extern const numeric *_num_4_p;
342 extern const numeric &_num_4;
343 extern const ex _ex_4;
344 extern const numeric *_num_3_p;
345 extern const numeric &_num_3;
346 extern const ex _ex_3;
347 extern const numeric *_num_2_p;
348 extern const numeric &_num_2;
349 extern const ex _ex_2;
350 extern const numeric *_num_1_p;
351 extern const numeric &_num_1;
352 extern const ex _ex_1;
353 extern const numeric *_num_1_2_p;
354 extern const numeric &_num_1_2;
355 extern const ex _ex_1_2;
356 extern const numeric *_num_1_3_p;
357 extern const numeric &_num_1_3;
358 extern const ex _ex_1_3;
359 extern const numeric *_num_1_4_p;
360 extern const numeric &_num_1_4;
361 extern const ex _ex_1_4;
362 extern const numeric *_num0_p;
363 extern const basic *_num0_bp;
364 extern const numeric &_num0;
365 extern const ex _ex0;
366 extern const numeric *_num1_4_p;
367 extern const numeric &_num1_4;
368 extern const ex _ex1_4;
369 extern const numeric *_num1_3_p;
370 extern const numeric &_num1_3;
371 extern const ex _ex1_3;
372 extern const numeric *_num1_2_p;
373 extern const numeric &_num1_2;
374 extern const ex _ex1_2;
375 extern const numeric *_num1_p;
376 extern const numeric &_num1;
377 extern const ex _ex1;
378 extern const numeric *_num2_p;
379 extern const numeric &_num2;
380 extern const ex _ex2;
381 extern const numeric *_num3_p;
382 extern const numeric &_num3;
383 extern const ex _ex3;
384 extern const numeric *_num4_p;
385 extern const numeric &_num4;
386 extern const ex _ex4;
387 extern const numeric *_num5_p;
388 extern const numeric &_num5;
389 extern const ex _ex5;
390 extern const numeric *_num6_p;
391 extern const numeric &_num6;
392 extern const ex _ex6;
393 extern const numeric *_num7_p;
394 extern const numeric &_num7;
395 extern const ex _ex7;
396 extern const numeric *_num8_p;
397 extern const numeric &_num8;
398 extern const ex _ex8;
399 extern const numeric *_num9_p;
400 extern const numeric &_num9;
401 extern const ex _ex9;
402 extern const numeric *_num10_p;
403 extern const numeric &_num10;
404 extern const ex _ex10;
405 extern const numeric *_num11_p;
406 extern const numeric &_num11;
407 extern const ex _ex11;
408 extern const numeric *_num12_p;
409 extern const numeric &_num12;
410 extern const ex _ex12;
411 extern const numeric *_num15_p;
412 extern const numeric &_num15;
413 extern const ex _ex15;
414 extern const numeric *_num18_p;
415 extern const numeric &_num18;
416 extern const ex _ex18;
417 extern const numeric *_num20_p;
418 extern const numeric &_num20;
419 extern const ex _ex20;
420 extern const numeric *_num24_p;
421 extern const numeric &_num24;
422 extern const ex _ex24;
423 extern const numeric *_num25_p;
424 extern const numeric &_num25;
425 extern const ex _ex25;
426 extern const numeric *_num30_p;
427 extern const numeric &_num30;
428 extern const ex _ex30;
429 extern const numeric *_num48_p;
430 extern const numeric &_num48;
431 extern const ex _ex48;
432 extern const numeric *_num60_p;
433 extern const numeric &_num60;
434 extern const ex _ex60;
435 extern const numeric *_num120_p;
436 extern const numeric &_num120;
437 extern const ex _ex120;
438
439
440 // Helper macros for class implementations (mostly useful for trivial classes)
441
442 #define DEFAULT_CTOR(classname) \
443 classname::classname() : inherited(TINFO_##classname) { setflag(status_flags::evaluated | status_flags::expanded); }
444
445 #define DEFAULT_UNARCHIVE(classname) \
446 ex classname::unarchive(const archive_node &n, lst &sym_lst) \
447 { \
448         return (new classname(n, sym_lst))->setflag(status_flags::dynallocated); \
449 }
450
451 #define DEFAULT_ARCHIVING(classname) \
452 classname::classname(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst) {} \
453 DEFAULT_UNARCHIVE(classname) \
454 void classname::archive(archive_node &n) const \
455 { \
456         inherited::archive(n); \
457 }
458
459 #define DEFAULT_COMPARE(classname) \
460 int classname::compare_same_type(const basic & other) const \
461 { \
462         /* by default, the objects are always identical */ \
463         return 0; \
464 }
465
466 #define DEFAULT_PRINT(classname, text) \
467 void classname::do_print(const print_context & c, unsigned level) const \
468 { \
469         c.s << text; \
470 }
471
472 #define DEFAULT_PRINT_LATEX(classname, text, latex) \
473 DEFAULT_PRINT(classname, text) \
474 void classname::do_print_latex(const print_latex & c, unsigned level) const \
475 { \
476         c.s << latex; \
477 }
478
479 } // namespace GiNaC
480
481
482 #endif // ndef __GINAC_UTILS_H__