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