| 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 : Wed Jul 24 14:28:19 2019
|
|---|
| 13 | // Update Count : 41
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #pragma once
|
|---|
| 17 |
|
|---|
| 18 | #include <cassert>
|
|---|
| 19 | #include <cctype>
|
|---|
| 20 | #include <algorithm>
|
|---|
| 21 | #include <functional>
|
|---|
| 22 | #include <iostream>
|
|---|
| 23 | #include <iterator>
|
|---|
| 24 | #include <list>
|
|---|
| 25 | #include <memory>
|
|---|
| 26 | #include <sstream>
|
|---|
| 27 | #include <string>
|
|---|
| 28 | #include <type_traits>
|
|---|
| 29 | #include <utility>
|
|---|
| 30 | #include <vector>
|
|---|
| 31 |
|
|---|
| 32 | #include "Common/Indenter.h"
|
|---|
| 33 |
|
|---|
| 34 | class Expression;
|
|---|
| 35 |
|
|---|
| 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 |
|
|---|
| 43 | template< typename T >
|
|---|
| 44 | static inline T * maybeClone( const T *orig ) {
|
|---|
| 45 | if ( orig ) {
|
|---|
| 46 | return orig->clone();
|
|---|
| 47 | } else {
|
|---|
| 48 | return 0;
|
|---|
| 49 | } // if
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | template< typename T, typename U >
|
|---|
| 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 |
|
|---|
| 63 | template< typename T, typename U >
|
|---|
| 64 | static inline T * maybeBuild( const U *orig ) {
|
|---|
| 65 | return maybeBuild_t<T,U>::doit(orig);
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 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 |
|
|---|
| 75 | template< typename Input_iterator >
|
|---|
| 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
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | template< typename Container >
|
|---|
| 83 | void deleteAll( const Container &container ) {
|
|---|
| 84 | for ( const auto &i : container ) {
|
|---|
| 85 | delete i;
|
|---|
| 86 | } // for
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | template< typename Container >
|
|---|
| 90 | void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
|
|---|
| 91 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
|---|
| 92 | if ( *i ) {
|
|---|
| 93 | os << indent;
|
|---|
| 94 | (*i)->print( os, indent );
|
|---|
| 95 | // need an endl after each element because it's not easy to know when each individual item should end
|
|---|
| 96 | os << std::endl;
|
|---|
| 97 | } // if
|
|---|
| 98 | } // for
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | template< typename SrcContainer, typename DestContainer >
|
|---|
| 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
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 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 |
|
|---|
| 120 | template< typename Container >
|
|---|
| 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 |
|
|---|
| 130 | template < typename T >
|
|---|
| 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
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | template < typename T >
|
|---|
| 139 | std::list<T> flatten( std::list < std::list<T> > l) {
|
|---|
| 140 | typedef std::list <T> Ts;
|
|---|
| 141 |
|
|---|
| 142 | Ts ret;
|
|---|
| 143 |
|
|---|
| 144 | switch ( l.size() ) {
|
|---|
| 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
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 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 |
|
|---|
| 171 | template < typename T >
|
|---|
| 172 | void toString_single( std::ostream & os, const T & value ) {
|
|---|
| 173 | os << value;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | template < typename T, typename... Params >
|
|---|
| 177 | void toString_single( std::ostream & os, const T & value, const Params & ... params ) {
|
|---|
| 178 | os << value;
|
|---|
| 179 | toString_single( os, params ... );
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | template < typename ... Params >
|
|---|
| 183 | std::string toString( const Params & ... params ) {
|
|---|
| 184 | std::ostringstream os;
|
|---|
| 185 | toString_single( os, params... );
|
|---|
| 186 | return os.str();
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | #define toCString( ... ) toString( __VA_ARGS__ ).c_str()
|
|---|
| 190 |
|
|---|
| 191 | // replace element of list with all elements of another list
|
|---|
| 192 | template< typename T >
|
|---|
| 193 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
|---|
| 194 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
|---|
| 195 |
|
|---|
| 196 | //if ( next != org.end() ) {
|
|---|
| 197 | org.erase( pos );
|
|---|
| 198 | org.splice( next, with );
|
|---|
| 199 | //}
|
|---|
| 200 |
|
|---|
| 201 | return;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 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 |
|
|---|
| 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)...);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 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 |
|
|---|
| 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);
|
|---|
| 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 ) {
|
|---|
| 238 | while ( b1 != e1 && b2 != e2 )
|
|---|
| 239 | *out++ = func(*b1++, *b2++);
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
|---|
| 243 | template< class InputIt, class Distance >
|
|---|
| 244 | InputIt operator+( InputIt it, Distance n ) {
|
|---|
| 245 | advance(it, n);
|
|---|
| 246 | return it;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 293 | template< typename ThisType >
|
|---|
| 294 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
|
|---|
| 295 |
|
|---|
| 296 | // -----------------------------------------------------------------------------
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 350 | // -----------------------------------------------------------------------------
|
|---|
| 351 | // Helper struct and function to support
|
|---|
| 352 | // for ( val : reverseIterate( container ) ) {}
|
|---|
| 353 | // syntax to have a for each that iterates backwards
|
|---|
| 354 |
|
|---|
| 355 | template< typename T >
|
|---|
| 356 | struct reverse_iterate_t {
|
|---|
| 357 | T& ref;
|
|---|
| 358 |
|
|---|
| 359 | reverse_iterate_t( T & ref ) : ref(ref) {}
|
|---|
| 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 >
|
|---|
| 367 | reverse_iterate_t< T > reverseIterate( T & ref ) {
|
|---|
| 368 | return reverse_iterate_t< T >( ref );
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 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 |
|
|---|
| 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
|
|---|
| 389 | // TODO: update to use variadic arguments
|
|---|
| 390 |
|
|---|
| 391 | template< typename T1, typename T2 >
|
|---|
| 392 | struct group_iterate_t {
|
|---|
| 393 | private:
|
|---|
| 394 | std::tuple<T1, T2> args;
|
|---|
| 395 | public:
|
|---|
| 396 | group_iterate_t( bool skipBoundsCheck, const T1 & v1, const T2 & v2 ) : args(v1, v2) {
|
|---|
| 397 | assertf(skipBoundsCheck || v1.size() == v2.size(), "group iteration requires containers of the same size: <%zd, %zd>.", v1.size(), v2.size());
|
|---|
| 398 | };
|
|---|
| 399 |
|
|---|
| 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 |
|
|---|
| 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; }
|
|---|
| 412 | value_type operator*() const { return std::tie( *std::get<0>(it), *std::get<1>(it) ); }
|
|---|
| 413 | };
|
|---|
| 414 |
|
|---|
| 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 |
|
|---|
| 419 | /// performs bounds check to ensure that all arguments are of the same length.
|
|---|
| 420 | template< typename... Args >
|
|---|
| 421 | group_iterate_t<Args...> group_iterate( Args &&... args ) {
|
|---|
| 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 )...);
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 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 {
|
|---|
| 437 | const T & ref;
|
|---|
| 438 | std::function<Func> f;
|
|---|
| 439 |
|
|---|
| 440 | struct iterator {
|
|---|
| 441 | typedef decltype(begin(ref)) Iter;
|
|---|
| 442 | Iter it;
|
|---|
| 443 | std::function<Func> f;
|
|---|
| 444 | iterator( Iter it, std::function<Func> f ) : it(it), f(f) {}
|
|---|
| 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 |
|
|---|
| 452 | lambda_iterate_t( const T & ref, std::function<Func> f ) : ref(ref), f(f) {}
|
|---|
| 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 >
|
|---|
| 459 | lambda_iterate_t<Args...> lazy_map( const Args &... args ) {
|
|---|
| 460 | return lambda_iterate_t<Args...>( args...);
|
|---|
| 461 | }
|
|---|
| 462 |
|
|---|
| 463 | // -----------------------------------------------------------------------------
|
|---|
| 464 | // O(1) polymorphic integer ilog2, using clz, which returns the number of leading 0-bits, starting at the most
|
|---|
| 465 | // significant bit (single instruction on x86)
|
|---|
| 466 |
|
|---|
| 467 | template<typename T>
|
|---|
| 468 | inline
|
|---|
| 469 | #if defined(__GNUC__) && __GNUC__ > 4
|
|---|
| 470 | constexpr
|
|---|
| 471 | #endif
|
|---|
| 472 | T ilog2(const T & t) {
|
|---|
| 473 | if(std::is_integral<T>::value) {
|
|---|
| 474 | const constexpr int r = sizeof(t) * __CHAR_BIT__ - 1;
|
|---|
| 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);
|
|---|
| 480 | return -1;
|
|---|
| 481 | } // ilog2
|
|---|
| 482 |
|
|---|
| 483 | // -----------------------------------------------------------------------------
|
|---|
| 484 | /// evaluates expr as a long long int. If second is false, expr could not be evaluated
|
|---|
| 485 | std::pair<long long int, bool> eval(const Expression * expr);
|
|---|
| 486 |
|
|---|
| 487 | namespace ast {
|
|---|
| 488 | class Expr;
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | std::pair<long long int, bool> eval(const ast::Expr * expr);
|
|---|
| 492 |
|
|---|
| 493 | // -----------------------------------------------------------------------------
|
|---|
| 494 | /// Reorders the input range in-place so that the minimal-value elements according to the
|
|---|
| 495 | /// comparator are in front;
|
|---|
| 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;
|
|---|
| 500 |
|
|---|
| 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 |
|
|---|
| 527 | // Local Variables: //
|
|---|
| 528 | // tab-width: 4 //
|
|---|
| 529 | // mode: c++ //
|
|---|
| 530 | // compile-command: "make install" //
|
|---|
| 531 | // End: //
|
|---|