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