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