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