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