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