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