]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- enforced GiNaC coding standards :-)
[ginac.git] / ginac / utils.h
1 /** @file utils.h
2  *
3  *  Interface to several small and furry utilities.
4  *
5  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifndef __GINAC_UTILS_H__
23 #define __GINAC_UTILS_H__
24
25 #include <strstream>
26 #include <string>
27 #include "config.h"
28
29 template<class T>
30 string ToString(T const & t)
31 {
32     char buf[256];
33     ostrstream(buf,sizeof(buf)) << t << ends;
34     return buf;
35 }
36
37 unsigned log2(unsigned n);
38
39 int compare_pointers(void const * a, void const * b);
40
41 #define DYNCONSTCAST(FINALTYPE,BASICTYPE,EXPRESSION) \
42     dynamic_cast<FINALTYPE>(const_cast<BASICTYPE>(EXPRESSION))
43
44 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
45 template <class InputIterator1, class InputIterator2, class OutputIterator,
46           class Compare>
47 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
48                        InputIterator2 first2, InputIterator2 last2,
49                        OutputIterator result, Compare comp) {
50   while (first1 != last1 && first2 != last2) {
51     if (comp(*first1, *first2)) {
52       *result = *first1;
53       ++first1;
54     }
55     else {
56       *result = *first2;
57       ++first2;
58     }
59     ++result;
60   }
61   return copy(first2, last2, copy(first1, last1, result));
62 }
63
64 // like merge(), but three lists with *last2<*first3
65 template <class InputIterator1, class InputIterator2, class InputIterator3,
66           class OutputIterator, class Compare>
67 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
68                         InputIterator2 first2, InputIterator2 last2,
69                         InputIterator3 first3, InputIterator3 last3,
70                         OutputIterator result, Compare comp) {
71   while (first1 != last1 && first2 != last2) {
72     if (comp(*first1, *first2)) {
73       *result = *first1;
74       ++first1;
75     }
76     else {
77       *result = *first2;
78       ++first2;
79     }
80     ++result;
81   }
82
83   if (first1==last1) {
84     // list1 empty, copy rest of list2, then list3
85     return copy(first3, last3, copy(first2, last2, result));
86   } else {
87     // list2 empty, merge rest of list1 with list3
88     return mymerge(first1,last1,first3,last3,result,comp);
89   }
90 }
91
92 #endif // ndef __GINAC_UTILS_H__