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 : Andrew Beach
|
---|
12 | // Last Modified On : Fri May 5 11:03:00 2017
|
---|
13 | // Update Count : 32
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef _UTILITY_H
|
---|
17 | #define _UTILITY_H
|
---|
18 |
|
---|
19 | #include <cctype>
|
---|
20 | #include <algorithm>
|
---|
21 | #include <iostream>
|
---|
22 | #include <iterator>
|
---|
23 | #include <list>
|
---|
24 | #include <memory>
|
---|
25 | #include <sstream>
|
---|
26 | #include <string>
|
---|
27 |
|
---|
28 | #include <cassert>
|
---|
29 | template< typename T >
|
---|
30 | static inline T * maybeClone( const T *orig ) {
|
---|
31 | if ( orig ) {
|
---|
32 | return orig->clone();
|
---|
33 | } else {
|
---|
34 | return 0;
|
---|
35 | } // if
|
---|
36 | }
|
---|
37 |
|
---|
38 | template< typename T, typename U >
|
---|
39 | struct maybeBuild_t {
|
---|
40 | static T * doit( const U *orig ) {
|
---|
41 | if ( orig ) {
|
---|
42 | return orig->build();
|
---|
43 | } else {
|
---|
44 | return 0;
|
---|
45 | } // if
|
---|
46 | }
|
---|
47 | };
|
---|
48 |
|
---|
49 | template< typename T, typename U >
|
---|
50 | static inline T * maybeBuild( const U *orig ) {
|
---|
51 | return maybeBuild_t<T,U>::doit(orig);
|
---|
52 | }
|
---|
53 |
|
---|
54 | template< typename T, typename U >
|
---|
55 | static inline T * maybeMoveBuild( const U *orig ) {
|
---|
56 | T* ret = maybeBuild<T>(orig);
|
---|
57 | delete orig;
|
---|
58 | return ret;
|
---|
59 | }
|
---|
60 |
|
---|
61 | template< typename Input_iterator >
|
---|
62 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
|
---|
63 | for ( Input_iterator i = begin; i != end; ++i ) {
|
---|
64 | os << name_array[ *i ] << ' ';
|
---|
65 | } // for
|
---|
66 | }
|
---|
67 |
|
---|
68 | template< typename Container >
|
---|
69 | void deleteAll( Container &container ) {
|
---|
70 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
71 | delete *i;
|
---|
72 | } // for
|
---|
73 | }
|
---|
74 |
|
---|
75 | template< typename Container >
|
---|
76 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
|
---|
77 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
78 | if ( *i ) {
|
---|
79 | os << std::string( indent, ' ' );
|
---|
80 | (*i)->print( os, indent + 2 );
|
---|
81 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
82 | os << std::endl;
|
---|
83 | } // if
|
---|
84 | } // for
|
---|
85 | }
|
---|
86 |
|
---|
87 | template< typename SrcContainer, typename DestContainer >
|
---|
88 | void cloneAll( const SrcContainer &src, DestContainer &dest ) {
|
---|
89 | typename SrcContainer::const_iterator in = src.begin();
|
---|
90 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
91 | while ( in != src.end() ) {
|
---|
92 | *out++ = (*in++)->clone();
|
---|
93 | } // while
|
---|
94 | }
|
---|
95 |
|
---|
96 | template< typename Container >
|
---|
97 | void assertAll( const Container &container ) {
|
---|
98 | int count = 0;
|
---|
99 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
100 | if ( !(*i) ) {
|
---|
101 | std::cerr << count << " is null" << std::endl;
|
---|
102 | } // if
|
---|
103 | } // for
|
---|
104 | }
|
---|
105 |
|
---|
106 | template < typename T >
|
---|
107 | std::list<T> tail( std::list<T> l ) {
|
---|
108 | if ( ! l.empty() ) {
|
---|
109 | std::list<T> ret(++(l.begin()), l.end());
|
---|
110 | return ret;
|
---|
111 | } // if
|
---|
112 | }
|
---|
113 |
|
---|
114 | template < typename T >
|
---|
115 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
116 | typedef std::list <T> Ts;
|
---|
117 |
|
---|
118 | Ts ret;
|
---|
119 |
|
---|
120 | switch ( l.size() ) {
|
---|
121 | case 0:
|
---|
122 | return ret;
|
---|
123 | case 1:
|
---|
124 | return l.front();
|
---|
125 | default:
|
---|
126 | ret = flatten(tail(l));
|
---|
127 | ret.insert(ret.begin(), l.front().begin(), l.front().end());
|
---|
128 | return ret;
|
---|
129 | } // switch
|
---|
130 | }
|
---|
131 |
|
---|
132 | template < typename T >
|
---|
133 | void toString_single( std::ostream & os, const T & value ) {
|
---|
134 | os << value;
|
---|
135 | }
|
---|
136 |
|
---|
137 | template < typename T, typename... Params >
|
---|
138 | void toString_single( std::ostream & os, const T & value, const Params & ... params ) {
|
---|
139 | os << value;
|
---|
140 | toString_single( os, params ... );
|
---|
141 | }
|
---|
142 |
|
---|
143 | template < typename ... Params >
|
---|
144 | std::string toString( const Params & ... params ) {
|
---|
145 | std::ostringstream os;
|
---|
146 | toString_single( os, params... );
|
---|
147 | return os.str();
|
---|
148 | }
|
---|
149 |
|
---|
150 | // replace element of list with all elements of another list
|
---|
151 | template< typename T >
|
---|
152 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
---|
153 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
---|
154 |
|
---|
155 | //if ( next != org.end() ) {
|
---|
156 | org.erase( pos );
|
---|
157 | org.splice( next, with );
|
---|
158 | //}
|
---|
159 |
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | // replace range of a list with a single element
|
---|
164 | template< typename T >
|
---|
165 | void replace( std::list< T > &org, typename std::list< T >::iterator begin, typename std::list< T >::iterator end, const T & with ) {
|
---|
166 | org.insert( begin, with );
|
---|
167 | org.erase( begin, end );
|
---|
168 | }
|
---|
169 |
|
---|
170 | template< typename... Args >
|
---|
171 | auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
|
---|
172 | return std::copy_if(std::forward<Args>(args)...);
|
---|
173 | }
|
---|
174 |
|
---|
175 | template< typename... Args >
|
---|
176 | auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
|
---|
177 | return zipWith(std::forward<Args>(args)..., std::make_pair);
|
---|
178 | }
|
---|
179 |
|
---|
180 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
|
---|
181 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
|
---|
182 | while ( b1 != e1 && b2 != e2 )
|
---|
183 | *out++ = func(*b1++, *b2++);
|
---|
184 | }
|
---|
185 |
|
---|
186 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
---|
187 | template< class InputIt, class Distance >
|
---|
188 | InputIt operator+( InputIt it, Distance n ) {
|
---|
189 | advance(it, n);
|
---|
190 | return it;
|
---|
191 | }
|
---|
192 |
|
---|
193 | template< typename T >
|
---|
194 | void warn_single( const T & arg ) {
|
---|
195 | std::cerr << arg << std::endl;
|
---|
196 | }
|
---|
197 |
|
---|
198 | template< typename T, typename... Params >
|
---|
199 | void warn_single(const T & arg, const Params & ... params ) {
|
---|
200 | std::cerr << arg;
|
---|
201 | warn_single( params... );
|
---|
202 | }
|
---|
203 |
|
---|
204 | template< typename... Params >
|
---|
205 | void warn( const Params & ... params ) {
|
---|
206 | std::cerr << "Warning: ";
|
---|
207 | warn_single( params... );
|
---|
208 | }
|
---|
209 |
|
---|
210 | // -----------------------------------------------------------------------------
|
---|
211 | // Ref Counted Singleton class
|
---|
212 | // Objects that inherit from this class will have at most one reference to it
|
---|
213 | // but if all references die, the object will be deleted.
|
---|
214 |
|
---|
215 | template< typename ThisType >
|
---|
216 | class RefCountSingleton {
|
---|
217 | public:
|
---|
218 | static std::shared_ptr<ThisType> get() {
|
---|
219 | if( global_instance.expired() ) {
|
---|
220 | std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
|
---|
221 | global_instance = new_instance;
|
---|
222 | return std::move(new_instance);
|
---|
223 | }
|
---|
224 | return global_instance.lock();
|
---|
225 | }
|
---|
226 | private:
|
---|
227 | static std::weak_ptr<ThisType> global_instance;
|
---|
228 | };
|
---|
229 |
|
---|
230 | template< typename ThisType >
|
---|
231 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
|
---|
232 |
|
---|
233 | // -----------------------------------------------------------------------------
|
---|
234 | // RAII object to regulate "save and restore" behaviour, e.g.
|
---|
235 | // void Foo::bar() {
|
---|
236 | // ValueGuard<int> guard(var); // var is a member of type Foo
|
---|
237 | // var = ...;
|
---|
238 | // } // var's original value is restored
|
---|
239 | template< typename T >
|
---|
240 | struct ValueGuard {
|
---|
241 | T old;
|
---|
242 | T& ref;
|
---|
243 |
|
---|
244 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
|
---|
245 | ~ValueGuard() { ref = old; }
|
---|
246 | };
|
---|
247 |
|
---|
248 | template< typename T >
|
---|
249 | struct ValueGuardPtr {
|
---|
250 | T old;
|
---|
251 | T* ref;
|
---|
252 |
|
---|
253 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {}
|
---|
254 | ~ValueGuardPtr() { if( ref ) *ref = old; }
|
---|
255 | };
|
---|
256 |
|
---|
257 | template< typename T >
|
---|
258 | struct ValueGuardPtr< std::list< T > > {
|
---|
259 | std::list< T > old;
|
---|
260 | std::list< T >* ref;
|
---|
261 |
|
---|
262 | ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) {
|
---|
263 | if( ref ) { swap( *ref, old ); }
|
---|
264 | }
|
---|
265 | ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } }
|
---|
266 | };
|
---|
267 |
|
---|
268 | // -----------------------------------------------------------------------------
|
---|
269 | // Helper struct and function to support
|
---|
270 | // for ( val : reverseIterate( container ) ) {}
|
---|
271 | // syntax to have a for each that iterates backwards
|
---|
272 |
|
---|
273 | template< typename T >
|
---|
274 | struct reverse_iterate_t {
|
---|
275 | T& ref;
|
---|
276 |
|
---|
277 | reverse_iterate_t( T & ref ) : ref(ref) {}
|
---|
278 |
|
---|
279 | typedef typename T::reverse_iterator iterator;
|
---|
280 | iterator begin() { return ref.rbegin(); }
|
---|
281 | iterator end() { return ref.rend(); }
|
---|
282 | };
|
---|
283 |
|
---|
284 | template< typename T >
|
---|
285 | reverse_iterate_t< T > reverseIterate( T & ref ) {
|
---|
286 | return reverse_iterate_t< T >( ref );
|
---|
287 | }
|
---|
288 |
|
---|
289 | template< typename OutType, typename Range, typename Functor >
|
---|
290 | OutType map_range( const Range& range, Functor&& functor ) {
|
---|
291 | OutType out;
|
---|
292 |
|
---|
293 | std::transform(
|
---|
294 | begin( range ),
|
---|
295 | end( range ),
|
---|
296 | std::back_inserter( out ),
|
---|
297 | std::forward< Functor >( functor )
|
---|
298 | );
|
---|
299 |
|
---|
300 | return out;
|
---|
301 | }
|
---|
302 |
|
---|
303 | // -----------------------------------------------------------------------------
|
---|
304 | // Helper struct and function to support
|
---|
305 | // for ( val : group_iterate( container1, container2, ... ) ) {}
|
---|
306 | // syntax to have a for each that iterates multiple containers of the same length
|
---|
307 | // TODO: update to use variadic arguments, perfect forwarding
|
---|
308 |
|
---|
309 | template< typename T1, typename T2 >
|
---|
310 | struct group_iterate_t {
|
---|
311 | group_iterate_t( const T1 & v1, const T2 & v2 ) : args(v1, v2) {
|
---|
312 | assertf(v1.size() == v2.size(), "group iteration requires containers of the same size.");
|
---|
313 | };
|
---|
314 |
|
---|
315 | struct iterator {
|
---|
316 | typedef std::tuple<typename T1::value_type, typename T2::value_type> value_type;
|
---|
317 | typedef typename T1::iterator T1Iter;
|
---|
318 | typedef typename T2::iterator T2Iter;
|
---|
319 | typedef std::tuple<T1Iter, T2Iter> IterTuple;
|
---|
320 | IterTuple it;
|
---|
321 | iterator( T1Iter i1, T2Iter i2 ) : it( i1, i2 ) {}
|
---|
322 | iterator operator++() {
|
---|
323 | return iterator( ++std::get<0>(it), ++std::get<1>(it) );
|
---|
324 | }
|
---|
325 | bool operator!=( const iterator &other ) const { return it != other.it; }
|
---|
326 | value_type operator*() const { return std::make_tuple( *std::get<0>(it), *std::get<1>(it) ); }
|
---|
327 | };
|
---|
328 | iterator begin() { return iterator( std::get<0>(args).begin(), std::get<1>(args).begin() ); }
|
---|
329 | iterator end() { return iterator( std::get<0>(args).end(), std::get<1>(args).end() ); }
|
---|
330 |
|
---|
331 | private:
|
---|
332 | std::tuple<T1, T2> args;
|
---|
333 | };
|
---|
334 |
|
---|
335 | template< typename... Args >
|
---|
336 | group_iterate_t<Args...> group_iterate( const Args &... args ) {
|
---|
337 | return group_iterate_t<Args...>(args...);
|
---|
338 | }
|
---|
339 |
|
---|
340 | struct CodeLocation {
|
---|
341 | int linenumber;
|
---|
342 | std::string filename;
|
---|
343 |
|
---|
344 | /// Create a new unset CodeLocation.
|
---|
345 | CodeLocation()
|
---|
346 | : linenumber( -1 )
|
---|
347 | , filename("")
|
---|
348 | {}
|
---|
349 |
|
---|
350 | /// Create a new CodeLocation with the given values.
|
---|
351 | CodeLocation( const char* filename, int lineno )
|
---|
352 | : linenumber( lineno )
|
---|
353 | , filename(filename ? filename : "")
|
---|
354 | {}
|
---|
355 |
|
---|
356 | CodeLocation( const CodeLocation& rhs ) = default;
|
---|
357 |
|
---|
358 | bool isSet () const {
|
---|
359 | return -1 != linenumber;
|
---|
360 | }
|
---|
361 |
|
---|
362 | bool isUnset () const {
|
---|
363 | return !isSet();
|
---|
364 | }
|
---|
365 |
|
---|
366 | void unset () {
|
---|
367 | linenumber = -1;
|
---|
368 | filename = "";
|
---|
369 | }
|
---|
370 |
|
---|
371 | // Use field access for set.
|
---|
372 | };
|
---|
373 |
|
---|
374 | inline std::string to_string( const CodeLocation& location ) {
|
---|
375 | return location.isSet() ? location.filename + ":" + std::to_string(location.linenumber) + " " : "";
|
---|
376 | }
|
---|
377 |
|
---|
378 | #endif // _UTILITY_H
|
---|
379 |
|
---|
380 | // Local Variables: //
|
---|
381 | // tab-width: 4 //
|
---|
382 | // mode: c++ //
|
---|
383 | // compile-command: "make install" //
|
---|
384 | // End: //
|
---|