1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // utility.h --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Wed Jun 8 17:33:59 2016
|
---|
13 | // Update Count : 22
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef _UTILITY_H
|
---|
17 | #define _UTILITY_H
|
---|
18 |
|
---|
19 | #include <cctype>
|
---|
20 | #include <iostream>
|
---|
21 | #include <iterator>
|
---|
22 | #include <list>
|
---|
23 | #include <memory>
|
---|
24 | #include <sstream>
|
---|
25 | #include <string>
|
---|
26 |
|
---|
27 | template< typename T >
|
---|
28 | static inline T * maybeClone( const T *orig ) {
|
---|
29 | if ( orig ) {
|
---|
30 | return orig->clone();
|
---|
31 | } else {
|
---|
32 | return 0;
|
---|
33 | } // if
|
---|
34 | }
|
---|
35 |
|
---|
36 | template<typename T, typename U>
|
---|
37 | struct maybeBuild_t {
|
---|
38 | static T * doit( const U *orig ) {
|
---|
39 | if ( orig ) {
|
---|
40 | return orig->build();
|
---|
41 | } else {
|
---|
42 | return 0;
|
---|
43 | } // if
|
---|
44 | }
|
---|
45 | };
|
---|
46 |
|
---|
47 | template< typename T, typename U >
|
---|
48 | static inline T * maybeBuild( const U *orig ) {
|
---|
49 | return maybeBuild_t<T,U>::doit(orig);
|
---|
50 | }
|
---|
51 |
|
---|
52 | template< typename T, typename U >
|
---|
53 | static inline T * maybeMoveBuild( const U *orig ) {
|
---|
54 | T* ret = maybeBuild<T>(orig);
|
---|
55 | delete orig;
|
---|
56 | return ret;
|
---|
57 | }
|
---|
58 |
|
---|
59 | template< typename Input_iterator >
|
---|
60 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
|
---|
61 | for ( Input_iterator i = begin; i != end; ++i ) {
|
---|
62 | os << name_array[ *i ] << ' ';
|
---|
63 | } // for
|
---|
64 | }
|
---|
65 |
|
---|
66 | template< typename Container >
|
---|
67 | void deleteAll( Container &container ) {
|
---|
68 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
69 | delete *i;
|
---|
70 | } // for
|
---|
71 | }
|
---|
72 |
|
---|
73 | template< typename Container >
|
---|
74 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
|
---|
75 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
76 | if ( *i ) {
|
---|
77 | os << std::string( indent, ' ' );
|
---|
78 | (*i)->print( os, indent + 2 );
|
---|
79 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
80 | os << std::endl;
|
---|
81 | } // if
|
---|
82 | } // for
|
---|
83 | }
|
---|
84 |
|
---|
85 | template< typename SrcContainer, typename DestContainer >
|
---|
86 | void cloneAll( const SrcContainer &src, DestContainer &dest ) {
|
---|
87 | typename SrcContainer::const_iterator in = src.begin();
|
---|
88 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
89 | while ( in != src.end() ) {
|
---|
90 | *out++ = (*in++)->clone();
|
---|
91 | } // while
|
---|
92 | }
|
---|
93 |
|
---|
94 | template< typename Container >
|
---|
95 | void assertAll( const Container &container ) {
|
---|
96 | int count = 0;
|
---|
97 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
98 | if ( !(*i) ) {
|
---|
99 | std::cerr << count << " is null" << std::endl;
|
---|
100 | } // if
|
---|
101 | } // for
|
---|
102 | }
|
---|
103 |
|
---|
104 | static inline std::string assign_strptr( const std::string *str ) {
|
---|
105 | if ( str == 0 ) {
|
---|
106 | return "";
|
---|
107 | } else {
|
---|
108 | std::string tmp;
|
---|
109 | tmp = *str;
|
---|
110 | delete str;
|
---|
111 | return tmp;
|
---|
112 | } // if
|
---|
113 | }
|
---|
114 |
|
---|
115 | template< class T, typename ResultType, ResultType( T::* memfunc )() >
|
---|
116 | ResultType dispatch( T *pT ) {
|
---|
117 | return (pT->*memfunc)();
|
---|
118 | }
|
---|
119 |
|
---|
120 | template < typename T >
|
---|
121 | std::list<T> tail( std::list<T> l ) {
|
---|
122 | if ( ! l.empty() ) {
|
---|
123 | std::list<T> ret(++(l.begin()), l.end());
|
---|
124 | return ret;
|
---|
125 | } // if
|
---|
126 | }
|
---|
127 |
|
---|
128 | template < typename T >
|
---|
129 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
130 | typedef std::list <T> Ts;
|
---|
131 |
|
---|
132 | Ts ret;
|
---|
133 |
|
---|
134 | switch ( l.size() ) {
|
---|
135 | case 0:
|
---|
136 | return ret;
|
---|
137 | case 1:
|
---|
138 | return l.front();
|
---|
139 | default:
|
---|
140 | ret = flatten(tail(l));
|
---|
141 | ret.insert(ret.begin(), l.front().begin(), l.front().end());
|
---|
142 | return ret;
|
---|
143 | } // switch
|
---|
144 | }
|
---|
145 |
|
---|
146 | template < typename T >
|
---|
147 | void toString_single ( std::ostream & os, const T & value ) {
|
---|
148 | os << value;
|
---|
149 | }
|
---|
150 |
|
---|
151 | template < typename T, typename... Params >
|
---|
152 | void toString_single ( std::ostream & os, const T & value, const Params & ... params ) {
|
---|
153 | os << value;
|
---|
154 | toString_single( os, params ... );
|
---|
155 | }
|
---|
156 |
|
---|
157 | template < typename ... Params >
|
---|
158 | std::string toString ( const Params & ... params ) {
|
---|
159 | std::ostringstream os;
|
---|
160 | toString_single( os, params... );
|
---|
161 | return os.str();
|
---|
162 | }
|
---|
163 |
|
---|
164 | template< class Constructed, typename Arg >
|
---|
165 | Constructed *ctor( Arg arg ) {
|
---|
166 | Constructed *c = new Constructed( arg );
|
---|
167 | return c;
|
---|
168 | }
|
---|
169 |
|
---|
170 | template< class Constructed, typename Arg >
|
---|
171 | Constructed ctor_noptr( Arg arg ) {
|
---|
172 | return Constructed( arg );
|
---|
173 | }
|
---|
174 |
|
---|
175 | template< typename T >
|
---|
176 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
---|
177 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
---|
178 |
|
---|
179 | //if ( next != org.end() ) {
|
---|
180 | org.erase( pos );
|
---|
181 | org.splice( next, with );
|
---|
182 | //}
|
---|
183 |
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | template< typename T1, typename T2 >
|
---|
188 | T2 *cast_ptr( T1 *from ) {
|
---|
189 | return dynamic_cast< T2 * >( from );
|
---|
190 | }
|
---|
191 |
|
---|
192 | template< class Exception, typename Arg >
|
---|
193 | void inline assert_throw( bool pred, Arg arg ) {
|
---|
194 | if (pred) throw Exception( arg );
|
---|
195 | }
|
---|
196 |
|
---|
197 | template< typename T >
|
---|
198 | struct is_null_pointer {
|
---|
199 | bool operator()( const T *ptr ) { return ( ptr == 0 ); }
|
---|
200 | };
|
---|
201 |
|
---|
202 | template< class InputIterator, class OutputIterator, class Predicate >
|
---|
203 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
|
---|
204 | while ( begin++ != end )
|
---|
205 | if ( pred(*begin) ) *out++ = *begin;
|
---|
206 |
|
---|
207 | return;
|
---|
208 | }
|
---|
209 |
|
---|
210 | template< class InputIterator1, class InputIterator2, class OutputIterator >
|
---|
211 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
|
---|
212 | while ( b1 != e1 && b2 != e2 )
|
---|
213 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
|
---|
214 | }
|
---|
215 |
|
---|
216 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
|
---|
217 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
|
---|
218 | while ( b1 != e1 && b2 != e2 )
|
---|
219 | *out++ = func(*b1++, *b2++);
|
---|
220 | }
|
---|
221 |
|
---|
222 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
---|
223 | template< typename Iterator >
|
---|
224 | Iterator operator+(Iterator i, int inc) {
|
---|
225 | while ( inc > 0 ) {
|
---|
226 | ++i;
|
---|
227 | --inc;
|
---|
228 | }
|
---|
229 | return i;
|
---|
230 | }
|
---|
231 |
|
---|
232 | template< typename T >
|
---|
233 | void warn_single( const T & arg ) {
|
---|
234 | std::cerr << arg << std::endl;
|
---|
235 | }
|
---|
236 |
|
---|
237 | template< typename T, typename... Params >
|
---|
238 | void warn_single(const T & arg, const Params & ... params ) {
|
---|
239 | std::cerr << arg;
|
---|
240 | warn_single( params... );
|
---|
241 | }
|
---|
242 |
|
---|
243 | template< typename... Params >
|
---|
244 | void warn( const Params & ... params ) {
|
---|
245 | std::cerr << "Warning: ";
|
---|
246 | warn_single( params... );
|
---|
247 | }
|
---|
248 |
|
---|
249 | template< typename ThisType >
|
---|
250 | class RefCountSingleton {
|
---|
251 | public:
|
---|
252 | static std::shared_ptr<ThisType> get() {
|
---|
253 | if( global_instance.expired() ) {
|
---|
254 | std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
|
---|
255 | global_instance = new_instance;
|
---|
256 | return std::move(new_instance);
|
---|
257 | }
|
---|
258 | return global_instance.lock();
|
---|
259 | }
|
---|
260 | private:
|
---|
261 | static std::weak_ptr<ThisType> global_instance;
|
---|
262 | };
|
---|
263 |
|
---|
264 | template< typename ThisType >
|
---|
265 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
|
---|
266 |
|
---|
267 | // RAII object to regulate "save and restore" behaviour, e.g.
|
---|
268 | // void Foo::bar() {
|
---|
269 | // ValueGuard<int> guard(var); // var is a member of type Foo
|
---|
270 | // var = ...;
|
---|
271 | // } // var's original value is restored
|
---|
272 | template< typename T >
|
---|
273 | struct ValueGuard {
|
---|
274 | T old;
|
---|
275 | T& ref;
|
---|
276 |
|
---|
277 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
|
---|
278 | ~ValueGuard() { ref = old; }
|
---|
279 | };
|
---|
280 |
|
---|
281 | template< typename T >
|
---|
282 | struct reverseIterate_t {
|
---|
283 | T& ref;
|
---|
284 |
|
---|
285 | reverseIterate_t( T & ref ) : ref(ref) {}
|
---|
286 |
|
---|
287 | typedef typename T::reverse_iterator iterator;
|
---|
288 | iterator begin() { return ref.rbegin(); }
|
---|
289 | iterator end() { return ref.rend(); }
|
---|
290 | };
|
---|
291 |
|
---|
292 | template< typename T >
|
---|
293 | reverseIterate_t< T > reverseIterate( T & ref ) {
|
---|
294 | return reverseIterate_t< T >( ref );
|
---|
295 | }
|
---|
296 |
|
---|
297 | #endif // _UTILITY_H
|
---|
298 |
|
---|
299 | // Local Variables: //
|
---|
300 | // tab-width: 4 //
|
---|
301 | // mode: c++ //
|
---|
302 | // compile-command: "make install" //
|
---|
303 | // End: //
|
---|