3 * Interface to several small and furry utilities needed within GiNaC but not
4 * of any interest to the user of the library. */
7 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #ifndef __GINAC_UTILS_H__
25 #define __GINAC_UTILS_H__
30 #include "assertion.h"
32 #ifndef NO_GINAC_NAMESPACE
34 #endif // ndef NO_GINAC_NAMESPACE
37 string ToString(T const & t)
40 ostrstream(buf,sizeof(buf)) << t << ends;
44 /** Exception thrown by classes which provide their own series expansion to
45 * signal that ordinary Taylor expansion is safe. */
48 unsigned log2(unsigned n);
50 int compare_pointers(void const * a, void const * b);
52 /** Rotate lower 31 bits of unsigned value by one bit to the left
53 * (upper bits get cleared). */
54 inline unsigned rotate_left_31(unsigned n)
56 // clear highest bit and shift 1 bit to the left
57 n=(n & 0x7FFFFFFFU) << 1;
59 // overflow? clear highest bit and set lowest bit
60 if (n & 0x80000000U) {
61 n=(n & 0x7FFFFFFFU) | 0x00000001U;
63 GINAC_ASSERT(n<0x80000000U);
68 /** Golden ratio hash function. */
69 inline unsigned golden_ratio_hash(unsigned n)
71 // This function requires arithmetic with at least 64 significant bits
72 #if SIZEOF_LONG_DOUBLE > 8
73 // If "long double" is bigger than 64 bits, we assume that the mantissa
74 // has at least 64 bits. This is not guaranteed but it's a good guess.
75 const static long double golden_ratio = .618033988749894848204586834370;
76 long double m = golden_ratio * n;
77 return unsigned((m - int(m)) * 0x80000000);
78 #elif SIZEOF_LONG >= 8
79 // "long" has 64 bits, so we prefer it because it might be more efficient
81 unsigned long l = n * 0x4f1bbcddL;
82 return (l & 0x7fffffffU) ^ (l >> 32);
83 #elif SIZEOF_LONG_LONG >= 8
84 // This requires ´long long´ (or an equivalent 64 bit type)---which is,
85 // unfortunately, not ANSI-compliant:
86 unsigned long long l = n * 0x4f1bbcddLL;
87 return (l & 0x7fffffffU) ^ (l >> 32);
89 #error "No 64 bit data type. You lose."
93 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
94 template <class InputIterator1, class InputIterator2, class OutputIterator,
96 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
97 InputIterator2 first2, InputIterator2 last2,
98 OutputIterator result, Compare comp) {
99 while (first1 != last1 && first2 != last2) {
100 if (comp(*first1, *first2)) {
110 return copy(first2, last2, copy(first1, last1, result));
113 // like merge(), but three lists with *last2<*first3
114 template <class InputIterator1, class InputIterator2, class InputIterator3,
115 class OutputIterator, class Compare>
116 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
117 InputIterator2 first2, InputIterator2 last2,
118 InputIterator3 first3, InputIterator3 last3,
119 OutputIterator result, Compare comp) {
120 while (first1 != last1 && first2 != last2) {
121 if (comp(*first1, *first2)) {
133 // list1 empty, copy rest of list2, then list3
134 return copy(first3, last3, copy(first2, last2, result));
136 // list2 empty, merge rest of list1 with list3
137 return mymerge(first1,last1,first3,last3,result,comp);
141 // Collection of `construct on first use' wrappers for safely avoiding
142 // internal object replication without running into the `static
143 // initialization order fiasco'. This chest of numbers helps speed up
144 // the library but should not be used outside it since it is
145 // potentially confusing.
150 numeric const & _num_12(void); // -12
151 numeric const & _num_11(void); // -11
152 numeric const & _num_10(void); // -10
153 numeric const & _num_9(void); // -9
154 numeric const & _num_8(void); // -8
155 numeric const & _num_7(void); // -7
156 numeric const & _num_6(void); // -6
157 numeric const & _num_5(void); // -5
158 numeric const & _num_4(void); // -4
159 numeric const & _num_3(void); // -3
160 numeric const & _num_2(void); // -2
161 numeric const & _num_1(void); // -1
162 numeric const & _num_1_2(void); // -1/2
163 numeric const & _num_1_3(void); // -1/3
164 numeric const & _num0(void); // 0
165 numeric const & _num1_3(void); // 1/3
166 numeric const & _num1_2(void); // 1/2
167 numeric const & _num1(void); // 1
168 numeric const & _num2(void); // 2
169 numeric const & _num3(void); // 3
170 numeric const & _num4(void); // 4
171 numeric const & _num5(void); // 5
172 numeric const & _num6(void); // 6
173 numeric const & _num7(void); // 7
174 numeric const & _num8(void); // 8
175 numeric const & _num9(void); // 9
176 numeric const & _num10(void); // 10
177 numeric const & _num11(void); // 11
178 numeric const & _num12(void); // 12
179 ex const & _ex_12(void); // -12
180 ex const & _ex_11(void); // -11
181 ex const & _ex_10(void); // -10
182 ex const & _ex_9(void); // -9
183 ex const & _ex_8(void); // -8
184 ex const & _ex_7(void); // -7
185 ex const & _ex_6(void); // -6
186 ex const & _ex_5(void); // -5
187 ex const & _ex_4(void); // -4
188 ex const & _ex_3(void); // -3
189 ex const & _ex_2(void); // -2
190 ex const & _ex_1(void); // -1
191 ex const & _ex_1_2(void); // -1/2
192 ex const & _ex_1_3(void); // -1/3
193 ex const & _ex0(void); // 0
194 ex const & _ex1_3(void); // 1/3
195 ex const & _ex1_2(void); // 1/2
196 ex const & _ex1(void); // 1
197 ex const & _ex2(void); // 2
198 ex const & _ex3(void); // 3
199 ex const & _ex4(void); // 4
200 ex const & _ex5(void); // 5
201 ex const & _ex6(void); // 6
202 ex const & _ex7(void); // 7
203 ex const & _ex8(void); // 8
204 ex const & _ex9(void); // 9
205 ex const & _ex10(void); // 10
206 ex const & _ex11(void); // 11
207 ex const & _ex12(void); // 12
209 #ifndef NO_GINAC_NAMESPACE
211 #endif // ndef NO_GINAC_NAMESPACE
213 #endif // ndef __GINAC_UTILS_H__