1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * Some useful template utility functions |
---|
5 | * |
---|
6 | * $Id: utility.h,v 1.17 2003/11/26 18:05:21 rgesteve Exp $ |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef COMMON_UTILITY_H |
---|
11 | #define COMMON_UTILITY_H |
---|
12 | |
---|
13 | #include <iostream> |
---|
14 | #include <strstream> |
---|
15 | #include <iterator> |
---|
16 | #include <string> |
---|
17 | #include <cctype> |
---|
18 | #include <list> |
---|
19 | |
---|
20 | template< typename T > |
---|
21 | static inline T* |
---|
22 | maybeClone( const T *orig ) |
---|
23 | { |
---|
24 | if( orig ) { |
---|
25 | return orig->clone(); |
---|
26 | } else { |
---|
27 | return 0; |
---|
28 | } |
---|
29 | } |
---|
30 | |
---|
31 | template< typename T, typename U > |
---|
32 | static inline T* |
---|
33 | maybeBuild( const U *orig ) |
---|
34 | { |
---|
35 | if( orig ) { |
---|
36 | return orig->build(); |
---|
37 | } else { |
---|
38 | return 0; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | template< typename Input_iterator > |
---|
43 | void |
---|
44 | printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) |
---|
45 | { |
---|
46 | for( Input_iterator i = begin; i != end; ++i ) { |
---|
47 | os << name_array[ *i ] << ' '; |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | template< typename Container > |
---|
52 | void |
---|
53 | deleteAll( Container &container ) |
---|
54 | { |
---|
55 | for( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
56 | delete *i; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | template< typename Container > |
---|
61 | void |
---|
62 | printAll( const Container &container, std::ostream &os, int indent = 0 ) |
---|
63 | { |
---|
64 | for( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
65 | if( *i ) { |
---|
66 | os << std::string(indent, ' '); |
---|
67 | (*i)->print( os, indent + 2 ); |
---|
68 | os << std::endl; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | template< typename SrcContainer, typename DestContainer > |
---|
74 | void |
---|
75 | cloneAll( const SrcContainer &src, DestContainer &dest ) |
---|
76 | { |
---|
77 | typename SrcContainer::const_iterator in = src.begin(); |
---|
78 | std::back_insert_iterator< DestContainer > out( dest ); |
---|
79 | while( in != src.end() ) { |
---|
80 | *out++ = (*in++)->clone(); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | template< typename Container > |
---|
85 | void |
---|
86 | assertAll( const Container &container ) |
---|
87 | { |
---|
88 | int count = 0; |
---|
89 | for( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
90 | if( !(*i) ) { |
---|
91 | std::cerr << count << " is null" << std::endl; |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | static inline std::string |
---|
97 | assign_strptr( std::string *str ) |
---|
98 | { |
---|
99 | if( str == 0 ) { |
---|
100 | return ""; |
---|
101 | } else { |
---|
102 | std::string tmp; |
---|
103 | tmp = *str; |
---|
104 | delete str; |
---|
105 | return tmp; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | template< class T, typename ResultType, ResultType (T::* memfunc)() > |
---|
110 | ResultType dispatch(T *pT){ |
---|
111 | return (pT->*memfunc)(); |
---|
112 | } |
---|
113 | |
---|
114 | template < typename T > |
---|
115 | std::list<T> tail( std::list<T> l ) |
---|
116 | { |
---|
117 | if(! l.empty()){ |
---|
118 | std::list<T> ret(++(l.begin()), l.end()); |
---|
119 | return ret; |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | template < typename T > |
---|
124 | std::list<T> flatten( std::list < std::list<T> > l) { |
---|
125 | typedef std::list <T> Ts; |
---|
126 | |
---|
127 | Ts ret; |
---|
128 | |
---|
129 | switch( l.size() ){ |
---|
130 | case 0: |
---|
131 | return ret; |
---|
132 | case 1: |
---|
133 | return l.front(); |
---|
134 | default: |
---|
135 | ret = flatten(tail(l)); |
---|
136 | ret.insert(ret.begin(), l.front().begin(), l.front().end()); |
---|
137 | return ret; |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | template < typename T > |
---|
142 | std::string toString ( T value ) { |
---|
143 | std::ostrstream os; |
---|
144 | |
---|
145 | os << value; // << std::ends; |
---|
146 | os.freeze( false ); |
---|
147 | |
---|
148 | return std::string(os.str(), os.pcount()); |
---|
149 | } |
---|
150 | |
---|
151 | template< class Constructed, typename Arg > |
---|
152 | Constructed *ctor( Arg arg ) { |
---|
153 | Constructed *c = new Constructed( arg ); |
---|
154 | return c; |
---|
155 | } |
---|
156 | |
---|
157 | template< class Constructed, typename Arg > |
---|
158 | Constructed ctor_noptr( Arg arg ) { |
---|
159 | return Constructed( arg ); |
---|
160 | } |
---|
161 | |
---|
162 | template< typename T > |
---|
163 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { |
---|
164 | // TIter should secretly be a typename std::list< T >::iterator |
---|
165 | // ( g++ 3.2 issues a 'is implicitly a typename' warning if I make this explicit ) |
---|
166 | typename std::list< T >::iterator next = pos; advance( next, 1 ); |
---|
167 | |
---|
168 | //if ( next != org.end() ) { |
---|
169 | org.erase( pos ); |
---|
170 | org.splice( next, with ); |
---|
171 | //} |
---|
172 | |
---|
173 | return; |
---|
174 | } |
---|
175 | |
---|
176 | template< typename T1, typename T2 > |
---|
177 | T2 *cast_ptr( T1 *from ) { |
---|
178 | return dynamic_cast< T2 * >( from ); |
---|
179 | } |
---|
180 | |
---|
181 | template< class Exception, typename Arg > |
---|
182 | void inline assert_throw( bool pred, Arg arg ) { |
---|
183 | if (pred) throw Exception( arg ); |
---|
184 | } |
---|
185 | |
---|
186 | template< typename T > |
---|
187 | struct is_null_pointer { |
---|
188 | bool operator()( const T *ptr ){ return ( ptr == 0 ); } |
---|
189 | }; |
---|
190 | |
---|
191 | template< class InputIterator, class OutputIterator, class Predicate > |
---|
192 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) |
---|
193 | { |
---|
194 | while ( begin++ != end ) |
---|
195 | if( pred(*begin) ) *out++ = *begin; |
---|
196 | |
---|
197 | return; |
---|
198 | } |
---|
199 | |
---|
200 | template< class InputIterator1, class InputIterator2, class OutputIterator > |
---|
201 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) { |
---|
202 | while( b1 != e1 && b2 != e2 ) |
---|
203 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++); |
---|
204 | } |
---|
205 | |
---|
206 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > |
---|
207 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { |
---|
208 | while( b1 != e1 && b2 != e2 ) |
---|
209 | *out++ = func(*b1++, *b2++); |
---|
210 | } |
---|
211 | |
---|
212 | #endif /* #ifndef COMMON_UTILITY_H */ |
---|
213 | |
---|
214 | /* |
---|
215 | Local Variables: |
---|
216 | mode: c++ |
---|
217 | End: |
---|
218 | */ |
---|