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