]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
Remove iter_swap specializations.
[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-2015 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 necessary 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         using std::swap;
112         if (first == last)
113                 return 0;
114         --last;
115         if (first == last)
116                 return 0;
117         It flag = first;
118         int sign = 1;
119
120         do {
121                 It i = last, other = last;
122                 --other;
123                 bool swapped = false;
124                 while (i != first) {
125                         if (*i < *other) {
126                                 swap(*other, *i);
127                                 flag = other;
128                                 swapped = true;
129                                 sign = -sign;
130                         } else if (!(*other < *i))
131                                 return 0;
132                         --i;
133                         if (i != first)
134                                 --other;
135                 }
136                 if (!swapped)
137                         return sign;
138                 ++flag;
139                 if (flag == last)
140                         return sign;
141                 first = flag;
142                 i = first; other = first;
143                 ++other;
144                 swapped = false;
145                 while (i != last) {
146                         if (*other < *i) {
147                                 swap(*i, *other);
148                                 flag = other;
149                                 swapped = true;
150                                 sign = -sign;
151                         } else if (!(*i < *other))
152                                 return 0;
153                         ++i;
154                         if (i != last)
155                                 ++other;
156                 }
157                 if (!swapped)
158                         return sign;
159                 last = flag;
160                 --last;
161         } while (first != last);
162
163         return sign;
164 }
165
166 template <class It, class Cmp, class Swap>
167 int permutation_sign(It first, It last, Cmp comp, Swap swapit)
168 {
169         if (first == last)
170                 return 0;
171         --last;
172         if (first == last)
173                 return 0;
174         It flag = first;
175         int sign = 1;
176
177         do {
178                 It i = last, other = last;
179                 --other;
180                 bool swapped = false;
181                 while (i != first) {
182                         if (comp(*i, *other)) {
183                                 swapit(*other, *i);
184                                 flag = other;
185                                 swapped = true;
186                                 sign = -sign;
187                         } else if (!comp(*other, *i))
188                                 return 0;
189                         --i;
190                         if (i != first)
191                                 --other;
192                 }
193                 if (!swapped)
194                         return sign;
195                 ++flag;
196                 if (flag == last)
197                         return sign;
198                 first = flag;
199                 i = first; other = first;
200                 ++other;
201                 swapped = false;
202                 while (i != last) {
203                         if (comp(*other, *i)) {
204                                 swapit(*i, *other);
205                                 flag = other;
206                                 swapped = true;
207                                 sign = -sign;
208                         } else if (!comp(*i, *other))
209                                 return 0;
210                         ++i; 
211                         if (i != last)
212                                 ++other;
213                 }
214                 if (!swapped)
215                         return sign;
216                 last = flag;
217                 --last;
218         } while (first != last);
219
220         return sign;
221 }
222
223 /* Implementation of shaker sort, only compares adjacent elements. */
224 template <class It, class Cmp, class Swap>
225 void shaker_sort(It first, It last, Cmp comp, Swap swapit)
226 {
227         if (first == last)
228                 return;
229         --last;
230         if (first == last)
231                 return;
232         It flag = first;
233
234         do {
235                 It i = last, other = last;
236                 --other;
237                 bool swapped = false;
238                 while (i != first) {
239                         if (comp(*i, *other)) {
240                                 swapit(*other, *i);
241                                 flag = other;
242                                 swapped = true;
243                         }
244                         --i;
245                         if (i != first)
246                                 --other;
247                 }
248                 if (!swapped)
249                         return;
250                 ++flag;
251                 if (flag == last)
252                         return;
253                 first = flag;
254                 i = first; other = first;
255                 ++other;
256                 swapped = false;
257                 while (i != last) {
258                         if (comp(*other, *i)) {
259                                 swapit(*i, *other);
260                                 flag = other;
261                                 swapped = true;
262                         }
263                         ++i;
264                         if (i != last)
265                                 ++other;
266                 }
267                 if (!swapped)
268                         return;
269                 last = flag;
270                 --last;
271         } while (first != last);
272 }
273
274 /* In-place cyclic permutation of a container (no copying, only swapping). */
275 template <class It, class Swap>
276 void cyclic_permutation(It first, It last, It new_first, Swap swapit)
277 {
278         unsigned num = last - first;
279 again:
280         if (first == new_first || num < 2)
281                 return;
282
283         unsigned num1 = new_first - first, num2 = last - new_first;
284         if (num1 >= num2) {
285                 It a = first, b = new_first;
286                 while (b != last) {
287                         swapit(*a, *b);
288                         ++a; ++b;
289                 }
290                 if (num1 > num2) {
291                         first += num2;
292                         num = num1;
293                         goto again;
294                 }
295         } else {
296                 It a = new_first, b = last;
297                 do {
298                         --a; --b;
299                         swapit(*a, *b);
300                 } while (a != first);
301                 last -= num1;
302                 num = num2;
303                 goto again;
304         }
305 }
306
307
308 // Collection of `construct on first use' wrappers for safely avoiding
309 // internal object replication without running into the `static
310 // initialization order fiasco'.  This chest of numbers helps speed up
311 // the library but should not be used outside it since it is
312 // potentially confusing.
313
314 class ex;
315
316 extern const numeric *_num_120_p;
317 extern const ex _ex_120;
318 extern const numeric *_num_60_p;
319 extern const ex _ex_60;
320 extern const numeric *_num_48_p;
321 extern const ex _ex_48;
322 extern const numeric *_num_30_p;
323 extern const ex _ex_30;
324 extern const numeric *_num_25_p;
325 extern const ex _ex_25;
326 extern const numeric *_num_24_p;
327 extern const ex _ex_24;
328 extern const numeric *_num_20_p;
329 extern const ex _ex_20;
330 extern const numeric *_num_18_p;
331 extern const ex _ex_18;
332 extern const numeric *_num_15_p;
333 extern const ex _ex_15;
334 extern const numeric *_num_12_p;
335 extern const ex _ex_12;
336 extern const numeric *_num_11_p;
337 extern const ex _ex_11;
338 extern const numeric *_num_10_p;
339 extern const ex _ex_10;
340 extern const numeric *_num_9_p;
341 extern const ex _ex_9;
342 extern const numeric *_num_8_p;
343 extern const ex _ex_8;
344 extern const numeric *_num_7_p;
345 extern const ex _ex_7;
346 extern const numeric *_num_6_p;
347 extern const ex _ex_6;
348 extern const numeric *_num_5_p;
349 extern const ex _ex_5;
350 extern const numeric *_num_4_p;
351 extern const ex _ex_4;
352 extern const numeric *_num_3_p;
353 extern const ex _ex_3;
354 extern const numeric *_num_2_p;
355 extern const ex _ex_2;
356 extern const numeric *_num_1_p;
357 extern const ex _ex_1;
358 extern const numeric *_num_1_2_p;
359 extern const ex _ex_1_2;
360 extern const numeric *_num_1_3_p;
361 extern const ex _ex_1_3;
362 extern const numeric *_num_1_4_p;
363 extern const ex _ex_1_4;
364 extern const numeric *_num0_p;
365 extern const basic *_num0_bp;
366 extern const ex _ex0;
367 extern const numeric *_num1_4_p;
368 extern const ex _ex1_4;
369 extern const numeric *_num1_3_p;
370 extern const ex _ex1_3;
371 extern const numeric *_num1_2_p;
372 extern const ex _ex1_2;
373 extern const numeric *_num1_p;
374 extern const ex _ex1;
375 extern const numeric *_num2_p;
376 extern const ex _ex2;
377 extern const numeric *_num3_p;
378 extern const ex _ex3;
379 extern const numeric *_num4_p;
380 extern const ex _ex4;
381 extern const numeric *_num5_p;
382 extern const ex _ex5;
383 extern const numeric *_num6_p;
384 extern const ex _ex6;
385 extern const numeric *_num7_p;
386 extern const ex _ex7;
387 extern const numeric *_num8_p;
388 extern const ex _ex8;
389 extern const numeric *_num9_p;
390 extern const ex _ex9;
391 extern const numeric *_num10_p;
392 extern const ex _ex10;
393 extern const numeric *_num11_p;
394 extern const ex _ex11;
395 extern const numeric *_num12_p;
396 extern const ex _ex12;
397 extern const numeric *_num15_p;
398 extern const ex _ex15;
399 extern const numeric *_num18_p;
400 extern const ex _ex18;
401 extern const numeric *_num20_p;
402 extern const ex _ex20;
403 extern const numeric *_num24_p;
404 extern const ex _ex24;
405 extern const numeric *_num25_p;
406 extern const ex _ex25;
407 extern const numeric *_num30_p;
408 extern const ex _ex30;
409 extern const numeric *_num48_p;
410 extern const ex _ex48;
411 extern const numeric *_num60_p;
412 extern const ex _ex60;
413 extern const numeric *_num120_p;
414 extern const ex _ex120;
415
416
417 // Helper macros for class implementations (mostly useful for trivial classes)
418
419 #define DEFAULT_CTOR(classname) \
420 classname::classname() { setflag(status_flags::evaluated | status_flags::expanded); }
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 #endif // ndef GINAC_UTILS_H