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