[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 |
---|
[21f0aa8] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thr Aug 17 11:38:00 2017 |
---|
| 13 | // Update Count : 34 |
---|
[51587aa] | 14 | // |
---|
[01aeade] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[e491159] | 18 | #include <cctype> |
---|
[940bba3] | 19 | #include <algorithm> |
---|
[51b7345] | 20 | #include <iostream> |
---|
| 21 | #include <iterator> |
---|
| 22 | #include <list> |
---|
[e491159] | 23 | #include <memory> |
---|
| 24 | #include <sstream> |
---|
| 25 | #include <string> |
---|
[55a68c3] | 26 | #include <type_traits> |
---|
[51b7345] | 27 | |
---|
[294647b] | 28 | #include <cassert> |
---|
[be9288a] | 29 | |
---|
[51b7345] | 30 | template< typename T > |
---|
[01aeade] | 31 | static inline T * maybeClone( const T *orig ) { |
---|
| 32 | if ( orig ) { |
---|
| 33 | return orig->clone(); |
---|
| 34 | } else { |
---|
| 35 | return 0; |
---|
| 36 | } // if |
---|
[51b7345] | 37 | } |
---|
| 38 | |
---|
[a7741435] | 39 | template< typename T, typename U > |
---|
[e04ef3a] | 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 | |
---|
[51b7345] | 50 | template< typename T, typename U > |
---|
[01aeade] | 51 | static inline T * maybeBuild( const U *orig ) { |
---|
[e04ef3a] | 52 | return maybeBuild_t<T,U>::doit(orig); |
---|
[51b7345] | 53 | } |
---|
| 54 | |
---|
[7ecbb7e] | 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 | |
---|
[51b7345] | 62 | template< typename Input_iterator > |
---|
[01aeade] | 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 |
---|
[51b7345] | 67 | } |
---|
| 68 | |
---|
| 69 | template< typename Container > |
---|
[01aeade] | 70 | void deleteAll( Container &container ) { |
---|
| 71 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 72 | delete *i; |
---|
| 73 | } // for |
---|
[51b7345] | 74 | } |
---|
| 75 | |
---|
| 76 | template< typename Container > |
---|
[01aeade] | 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 ) { |
---|
[cf0941d] | 80 | os << std::string( indent, ' ' ); |
---|
[01aeade] | 81 | (*i)->print( os, indent + 2 ); |
---|
[60089f4] | 82 | // need an endl after each element because it's not easy to know when each individual item should end |
---|
[01aeade] | 83 | os << std::endl; |
---|
| 84 | } // if |
---|
| 85 | } // for |
---|
[51b7345] | 86 | } |
---|
| 87 | |
---|
| 88 | template< typename SrcContainer, typename DestContainer > |
---|
[01aeade] | 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 |
---|
[51b7345] | 95 | } |
---|
| 96 | |
---|
| 97 | template< typename Container > |
---|
[01aeade] | 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 | |
---|
[51b7345] | 107 | template < typename T > |
---|
[01aeade] | 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 |
---|
[51b7345] | 113 | } |
---|
| 114 | |
---|
| 115 | template < typename T > |
---|
| 116 | std::list<T> flatten( std::list < std::list<T> > l) { |
---|
[01aeade] | 117 | typedef std::list <T> Ts; |
---|
[51b7345] | 118 | |
---|
[01aeade] | 119 | Ts ret; |
---|
[51b7345] | 120 | |
---|
[a08ba92] | 121 | switch ( l.size() ) { |
---|
[01aeade] | 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 |
---|
[51b7345] | 131 | } |
---|
| 132 | |
---|
[60089f4] | 133 | template < typename T > |
---|
[2298f728] | 134 | void toString_single( std::ostream & os, const T & value ) { |
---|
[79970ed] | 135 | os << value; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | template < typename T, typename... Params > |
---|
[2298f728] | 139 | void toString_single( std::ostream & os, const T & value, const Params & ... params ) { |
---|
[79970ed] | 140 | os << value; |
---|
| 141 | toString_single( os, params ... ); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | template < typename ... Params > |
---|
[2298f728] | 145 | std::string toString( const Params & ... params ) { |
---|
[5f2f2d7] | 146 | std::ostringstream os; |
---|
[79970ed] | 147 | toString_single( os, params... ); |
---|
[5f2f2d7] | 148 | return os.str(); |
---|
[51b7345] | 149 | } |
---|
| 150 | |
---|
[3c13c03] | 151 | // replace element of list with all elements of another list |
---|
[51b7345] | 152 | template< typename T > |
---|
| 153 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { |
---|
[01aeade] | 154 | typename std::list< T >::iterator next = pos; advance( next, 1 ); |
---|
[51b7345] | 155 | |
---|
[01aeade] | 156 | //if ( next != org.end() ) { |
---|
[a08ba92] | 157 | org.erase( pos ); |
---|
| 158 | org.splice( next, with ); |
---|
| 159 | //} |
---|
[51b7345] | 160 | |
---|
[01aeade] | 161 | return; |
---|
[51b7345] | 162 | } |
---|
| 163 | |
---|
[3c13c03] | 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 | |
---|
[940bba3] | 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)...); |
---|
[51b7345] | 174 | } |
---|
| 175 | |
---|
[940bba3] | 176 | template< typename... Args > |
---|
| 177 | auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) { |
---|
| 178 | return zipWith(std::forward<Args>(args)..., std::make_pair); |
---|
[51b7345] | 179 | } |
---|
| 180 | |
---|
| 181 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > |
---|
| 182 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { |
---|
[01aeade] | 183 | while ( b1 != e1 && b2 != e2 ) |
---|
| 184 | *out++ = func(*b1++, *b2++); |
---|
[51b7345] | 185 | } |
---|
| 186 | |
---|
[d939274] | 187 | // it's nice to actually be able to increment iterators by an arbitrary amount |
---|
[940bba3] | 188 | template< class InputIt, class Distance > |
---|
| 189 | InputIt operator+( InputIt it, Distance n ) { |
---|
| 190 | advance(it, n); |
---|
| 191 | return it; |
---|
[d939274] | 192 | } |
---|
| 193 | |
---|
[79970ed] | 194 | template< typename T > |
---|
| 195 | void warn_single( const T & arg ) { |
---|
| 196 | std::cerr << arg << std::endl; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | template< typename T, typename... Params > |
---|
| 200 | void warn_single(const T & arg, const Params & ... params ) { |
---|
| 201 | std::cerr << arg; |
---|
| 202 | warn_single( params... ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | template< typename... Params > |
---|
| 206 | void warn( const Params & ... params ) { |
---|
| 207 | std::cerr << "Warning: "; |
---|
| 208 | warn_single( params... ); |
---|
| 209 | } |
---|
| 210 | |
---|
[940bba3] | 211 | // ----------------------------------------------------------------------------- |
---|
| 212 | // Ref Counted Singleton class |
---|
| 213 | // Objects that inherit from this class will have at most one reference to it |
---|
| 214 | // but if all references die, the object will be deleted. |
---|
| 215 | |
---|
[e491159] | 216 | template< typename ThisType > |
---|
| 217 | class RefCountSingleton { |
---|
| 218 | public: |
---|
| 219 | static std::shared_ptr<ThisType> get() { |
---|
| 220 | if( global_instance.expired() ) { |
---|
| 221 | std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>(); |
---|
| 222 | global_instance = new_instance; |
---|
| 223 | return std::move(new_instance); |
---|
| 224 | } |
---|
| 225 | return global_instance.lock(); |
---|
| 226 | } |
---|
| 227 | private: |
---|
| 228 | static std::weak_ptr<ThisType> global_instance; |
---|
| 229 | }; |
---|
| 230 | |
---|
[1f75e2d] | 231 | template< typename ThisType > |
---|
| 232 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance; |
---|
| 233 | |
---|
[940bba3] | 234 | // ----------------------------------------------------------------------------- |
---|
[c8dfcd3] | 235 | // RAII object to regulate "save and restore" behaviour, e.g. |
---|
| 236 | // void Foo::bar() { |
---|
| 237 | // ValueGuard<int> guard(var); // var is a member of type Foo |
---|
| 238 | // var = ...; |
---|
| 239 | // } // var's original value is restored |
---|
| 240 | template< typename T > |
---|
| 241 | struct ValueGuard { |
---|
| 242 | T old; |
---|
| 243 | T& ref; |
---|
| 244 | |
---|
| 245 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {} |
---|
| 246 | ~ValueGuard() { ref = old; } |
---|
| 247 | }; |
---|
| 248 | |
---|
[134322e] | 249 | template< typename T > |
---|
| 250 | struct ValueGuardPtr { |
---|
| 251 | T old; |
---|
| 252 | T* ref; |
---|
| 253 | |
---|
| 254 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {} |
---|
| 255 | ~ValueGuardPtr() { if( ref ) *ref = old; } |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | template< typename T > |
---|
| 259 | struct ValueGuardPtr< std::list< T > > { |
---|
| 260 | std::list< T > old; |
---|
| 261 | std::list< T >* ref; |
---|
| 262 | |
---|
| 263 | ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) { |
---|
| 264 | if( ref ) { swap( *ref, old ); } |
---|
| 265 | } |
---|
| 266 | ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } } |
---|
| 267 | }; |
---|
| 268 | |
---|
[940bba3] | 269 | // ----------------------------------------------------------------------------- |
---|
| 270 | // Helper struct and function to support |
---|
| 271 | // for ( val : reverseIterate( container ) ) {} |
---|
| 272 | // syntax to have a for each that iterates backwards |
---|
| 273 | |
---|
[44f6341] | 274 | template< typename T > |
---|
[4a9ccc3] | 275 | struct reverse_iterate_t { |
---|
[44f6341] | 276 | T& ref; |
---|
| 277 | |
---|
[4a9ccc3] | 278 | reverse_iterate_t( T & ref ) : ref(ref) {} |
---|
[44f6341] | 279 | |
---|
| 280 | typedef typename T::reverse_iterator iterator; |
---|
| 281 | iterator begin() { return ref.rbegin(); } |
---|
| 282 | iterator end() { return ref.rend(); } |
---|
| 283 | }; |
---|
| 284 | |
---|
| 285 | template< typename T > |
---|
[4a9ccc3] | 286 | reverse_iterate_t< T > reverseIterate( T & ref ) { |
---|
| 287 | return reverse_iterate_t< T >( ref ); |
---|
[44f6341] | 288 | } |
---|
| 289 | |
---|
[3ad7978] | 290 | template< typename OutType, typename Range, typename Functor > |
---|
| 291 | OutType map_range( const Range& range, Functor&& functor ) { |
---|
| 292 | OutType out; |
---|
| 293 | |
---|
| 294 | std::transform( |
---|
| 295 | begin( range ), |
---|
| 296 | end( range ), |
---|
| 297 | std::back_inserter( out ), |
---|
| 298 | std::forward< Functor >( functor ) |
---|
| 299 | ); |
---|
| 300 | |
---|
| 301 | return out; |
---|
| 302 | } |
---|
| 303 | |
---|
[4a9ccc3] | 304 | // ----------------------------------------------------------------------------- |
---|
| 305 | // Helper struct and function to support |
---|
| 306 | // for ( val : group_iterate( container1, container2, ... ) ) {} |
---|
| 307 | // syntax to have a for each that iterates multiple containers of the same length |
---|
[55a68c3] | 308 | // TODO: update to use variadic arguments |
---|
[4a9ccc3] | 309 | |
---|
| 310 | template< typename T1, typename T2 > |
---|
| 311 | struct group_iterate_t { |
---|
[6d267ca] | 312 | group_iterate_t( bool skipBoundsCheck, const T1 & v1, const T2 & v2 ) : args(v1, v2) { |
---|
[3c09d47] | 313 | assertf(skipBoundsCheck || v1.size() == v2.size(), "group iteration requires containers of the same size: <%zd, %zd>.", v1.size(), v2.size()); |
---|
[4a9ccc3] | 314 | }; |
---|
| 315 | |
---|
| 316 | struct iterator { |
---|
[55a68c3] | 317 | typedef typename std::remove_reference<T1>::type T1val; |
---|
| 318 | typedef typename std::remove_reference<T2>::type T2val; |
---|
| 319 | typedef std::tuple<typename T1val::value_type &, typename T2val::value_type &> value_type; |
---|
| 320 | typedef typename T1val::iterator T1Iter; |
---|
| 321 | typedef typename T2val::iterator T2Iter; |
---|
[4a9ccc3] | 322 | typedef std::tuple<T1Iter, T2Iter> IterTuple; |
---|
| 323 | IterTuple it; |
---|
| 324 | iterator( T1Iter i1, T2Iter i2 ) : it( i1, i2 ) {} |
---|
| 325 | iterator operator++() { |
---|
| 326 | return iterator( ++std::get<0>(it), ++std::get<1>(it) ); |
---|
| 327 | } |
---|
| 328 | bool operator!=( const iterator &other ) const { return it != other.it; } |
---|
[55a68c3] | 329 | value_type operator*() const { return std::tie( *std::get<0>(it), *std::get<1>(it) ); } |
---|
[4a9ccc3] | 330 | }; |
---|
| 331 | iterator begin() { return iterator( std::get<0>(args).begin(), std::get<1>(args).begin() ); } |
---|
| 332 | iterator end() { return iterator( std::get<0>(args).end(), std::get<1>(args).end() ); } |
---|
| 333 | |
---|
| 334 | private: |
---|
| 335 | std::tuple<T1, T2> args; |
---|
| 336 | }; |
---|
| 337 | |
---|
[6d267ca] | 338 | /// performs bounds check to ensure that all arguments are of the same length. |
---|
[4a9ccc3] | 339 | template< typename... Args > |
---|
[55a68c3] | 340 | group_iterate_t<Args...> group_iterate( Args &&... args ) { |
---|
[6d267ca] | 341 | return group_iterate_t<Args...>(false, std::forward<Args>( args )...); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | /// does not perform a bounds check - requires user to ensure that iteration terminates when appropriate. |
---|
| 345 | template< typename... Args > |
---|
| 346 | group_iterate_t<Args...> unsafe_group_iterate( Args &&... args ) { |
---|
| 347 | return group_iterate_t<Args...>(true, std::forward<Args>( args )...); |
---|
[4a9ccc3] | 348 | } |
---|
[294647b] | 349 | |
---|
[51587aa] | 350 | // Local Variables: // |
---|
| 351 | // tab-width: 4 // |
---|
| 352 | // mode: c++ // |
---|
| 353 | // compile-command: "make install" // |
---|
| 354 | // End: // |
---|