]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- is_zero() is now called on expanded expressions in gcd()
[ginac.git] / ginac / utils.h
1 /** @file utils.h
2  *
3  *  Interface to several small and furry utilities. */
4
5 /*
6  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_UTILS_H__
24 #define __GINAC_UTILS_H__
25
26 #include <strstream>
27 #include <string>
28 #include "config.h"
29 #include "assertion.h"
30
31 namespace GiNaC {
32
33 template<class T>
34 string ToString(T const & t)
35 {
36     char buf[256];
37     ostrstream(buf,sizeof(buf)) << t << ends;
38     return buf;
39 }
40
41 unsigned log2(unsigned n);
42
43 int compare_pointers(void const * a, void const * b);
44
45 /** Rotate lower 31 bits of unsigned value by one bit to the left
46  *  (upper bits get cleared). */
47 inline unsigned rotate_left_31(unsigned n)
48 {
49     // clear highest bit and shift 1 bit to the left
50     n=(n & 0x7FFFFFFFU) << 1;
51
52     // overflow? clear highest bit and set lowest bit
53     if (n & 0x80000000U) {
54         n=(n & 0x7FFFFFFFU) | 0x00000001U;
55     }
56     ASSERT(n<0x80000000U);
57
58     return n;
59 }
60
61 /** Golden ratio hash function. */
62 inline unsigned golden_ratio_hash(unsigned n)
63 {
64         // This function requires arithmetic with at least 64 significant bits
65 #if SIZEOF_LONG_DOUBLE > 8
66         // If "long double" is bigger than 64 bits, we assume that the mantissa
67         // has at least 64 bits. This is not guaranteed but it's a good guess.
68     const static long double golden_ratio = .618033988749894848204586834370;
69     long double m = golden_ratio * n;
70     return unsigned((m - int(m)) * 0x80000000);
71 #elif SIZEOF_LONG >= 8
72         // "long" has 64 bits, so we prefer it because it might be more efficient
73         // than "long long"
74         unsigned long l = n * 0x4f1bbcddL;
75         return (l & 0x7fffffffU) ^ (l >> 32);
76 #elif SIZEOF_LONG_LONG >= 8
77         // This requires ´long long´ (or an equivalent 64 bit type)---which is,
78     // unfortunately, not ANSI-compliant:
79         unsigned long long l = n * 0x4f1bbcddLL;
80         return (l & 0x7fffffffU) ^ (l >> 32);
81 #else
82 #error "No 64 bit data type. You lose."
83 #endif
84 }
85
86 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
87 template <class InputIterator1, class InputIterator2, class OutputIterator,
88           class Compare>
89 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
90                        InputIterator2 first2, InputIterator2 last2,
91                        OutputIterator result, Compare comp) {
92   while (first1 != last1 && first2 != last2) {
93     if (comp(*first1, *first2)) {
94       *result = *first1;
95       ++first1;
96     }
97     else {
98       *result = *first2;
99       ++first2;
100     }
101     ++result;
102   }
103   return copy(first2, last2, copy(first1, last1, result));
104 }
105
106 // like merge(), but three lists with *last2<*first3
107 template <class InputIterator1, class InputIterator2, class InputIterator3,
108           class OutputIterator, class Compare>
109 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
110                         InputIterator2 first2, InputIterator2 last2,
111                         InputIterator3 first3, InputIterator3 last3,
112                         OutputIterator result, Compare comp) {
113   while (first1 != last1 && first2 != last2) {
114     if (comp(*first1, *first2)) {
115       *result = *first1;
116       ++first1;
117     }
118     else {
119       *result = *first2;
120       ++first2;
121     }
122     ++result;
123   }
124
125   if (first1==last1) {
126     // list1 empty, copy rest of list2, then list3
127     return copy(first3, last3, copy(first2, last2, result));
128   } else {
129     // list2 empty, merge rest of list1 with list3
130     return mymerge(first1,last1,first3,last3,result,comp);
131   }
132 }
133
134 } // namespace GiNaC
135
136 #endif // ndef __GINAC_UTILS_H__