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