]> www.ginac.de Git - ginac.git/blob - ginac/utils.h
- modified the comment blocks so the copyright message no longer appears in
[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
30 template<class T>
31 string ToString(T const & t)
32 {
33     char buf[256];
34     ostrstream(buf,sizeof(buf)) << t << ends;
35     return buf;
36 }
37
38 unsigned log2(unsigned n);
39
40 int compare_pointers(void const * a, void const * b);
41
42 #define DYNCONSTCAST(FINALTYPE,BASICTYPE,EXPRESSION) \
43     dynamic_cast<FINALTYPE>(const_cast<BASICTYPE>(EXPRESSION))
44
45 // modified from stl_algo.h: always do com(*first1,*first2) instead of comp(*first2,*first1)
46 template <class InputIterator1, class InputIterator2, class OutputIterator,
47           class Compare>
48 OutputIterator mymerge(InputIterator1 first1, InputIterator1 last1,
49                        InputIterator2 first2, InputIterator2 last2,
50                        OutputIterator result, Compare comp) {
51   while (first1 != last1 && first2 != last2) {
52     if (comp(*first1, *first2)) {
53       *result = *first1;
54       ++first1;
55     }
56     else {
57       *result = *first2;
58       ++first2;
59     }
60     ++result;
61   }
62   return copy(first2, last2, copy(first1, last1, result));
63 }
64
65 // like merge(), but three lists with *last2<*first3
66 template <class InputIterator1, class InputIterator2, class InputIterator3,
67           class OutputIterator, class Compare>
68 OutputIterator mymerge3(InputIterator1 first1, InputIterator1 last1,
69                         InputIterator2 first2, InputIterator2 last2,
70                         InputIterator3 first3, InputIterator3 last3,
71                         OutputIterator result, Compare comp) {
72   while (first1 != last1 && first2 != last2) {
73     if (comp(*first1, *first2)) {
74       *result = *first1;
75       ++first1;
76     }
77     else {
78       *result = *first2;
79       ++first2;
80     }
81     ++result;
82   }
83
84   if (first1==last1) {
85     // list1 empty, copy rest of list2, then list3
86     return copy(first3, last3, copy(first2, last2, result));
87   } else {
88     // list2 empty, merge rest of list1 with list3
89     return mymerge(first1,last1,first3,last3,result,comp);
90   }
91 }
92
93 #endif // ndef __GINAC_UTILS_H__