[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 |
---|
[e04ef3a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[2298f728] | 12 | // Last Modified On : Fri Sep 23 11:46:47 2016 |
---|
| 13 | // Update Count : 28 |
---|
[51587aa] | 14 | // |
---|
[01aeade] | 15 | |
---|
| 16 | #ifndef _UTILITY_H |
---|
| 17 | #define _UTILITY_H |
---|
[51b7345] | 18 | |
---|
[e491159] | 19 | #include <cctype> |
---|
[940bba3] | 20 | #include <algorithm> |
---|
[51b7345] | 21 | #include <iostream> |
---|
| 22 | #include <iterator> |
---|
| 23 | #include <list> |
---|
[e491159] | 24 | #include <memory> |
---|
| 25 | #include <sstream> |
---|
| 26 | #include <string> |
---|
[1b772749] | 27 | #include <cassert> |
---|
[51b7345] | 28 | |
---|
| 29 | template< typename T > |
---|
[01aeade] | 30 | static inline T * maybeClone( const T *orig ) { |
---|
| 31 | if ( orig ) { |
---|
| 32 | return orig->clone(); |
---|
| 33 | } else { |
---|
| 34 | return 0; |
---|
| 35 | } // if |
---|
[51b7345] | 36 | } |
---|
| 37 | |
---|
[e04ef3a] | 38 | template<typename T, typename U> |
---|
| 39 | struct maybeBuild_t { |
---|
| 40 | static T * doit( const U *orig ) { |
---|
| 41 | if ( orig ) { |
---|
| 42 | return orig->build(); |
---|
| 43 | } else { |
---|
| 44 | return 0; |
---|
| 45 | } // if |
---|
| 46 | } |
---|
| 47 | }; |
---|
| 48 | |
---|
[51b7345] | 49 | template< typename T, typename U > |
---|
[01aeade] | 50 | static inline T * maybeBuild( const U *orig ) { |
---|
[e04ef3a] | 51 | return maybeBuild_t<T,U>::doit(orig); |
---|
[51b7345] | 52 | } |
---|
| 53 | |
---|
[7ecbb7e] | 54 | template< typename T, typename U > |
---|
| 55 | static inline T * maybeMoveBuild( const U *orig ) { |
---|
| 56 | T* ret = maybeBuild<T>(orig); |
---|
| 57 | delete orig; |
---|
| 58 | return ret; |
---|
| 59 | } |
---|
| 60 | |
---|
[51b7345] | 61 | template< typename Input_iterator > |
---|
[01aeade] | 62 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) { |
---|
| 63 | for ( Input_iterator i = begin; i != end; ++i ) { |
---|
| 64 | os << name_array[ *i ] << ' '; |
---|
| 65 | } // for |
---|
[51b7345] | 66 | } |
---|
| 67 | |
---|
| 68 | template< typename Container > |
---|
[01aeade] | 69 | void deleteAll( Container &container ) { |
---|
| 70 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 71 | delete *i; |
---|
| 72 | } // for |
---|
[51b7345] | 73 | } |
---|
| 74 | |
---|
| 75 | template< typename Container > |
---|
[01aeade] | 76 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) { |
---|
| 77 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 78 | if ( *i ) { |
---|
[cf0941d] | 79 | os << std::string( indent, ' ' ); |
---|
[01aeade] | 80 | (*i)->print( os, indent + 2 ); |
---|
[60089f4] | 81 | // need an endl after each element because it's not easy to know when each individual item should end |
---|
[01aeade] | 82 | os << std::endl; |
---|
| 83 | } // if |
---|
| 84 | } // for |
---|
[51b7345] | 85 | } |
---|
| 86 | |
---|
| 87 | template< typename SrcContainer, typename DestContainer > |
---|
[01aeade] | 88 | void cloneAll( const SrcContainer &src, DestContainer &dest ) { |
---|
| 89 | typename SrcContainer::const_iterator in = src.begin(); |
---|
| 90 | std::back_insert_iterator< DestContainer > out( dest ); |
---|
| 91 | while ( in != src.end() ) { |
---|
| 92 | *out++ = (*in++)->clone(); |
---|
| 93 | } // while |
---|
[51b7345] | 94 | } |
---|
| 95 | |
---|
| 96 | template< typename Container > |
---|
[01aeade] | 97 | void assertAll( const Container &container ) { |
---|
| 98 | int count = 0; |
---|
| 99 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 100 | if ( !(*i) ) { |
---|
| 101 | std::cerr << count << " is null" << std::endl; |
---|
| 102 | } // if |
---|
| 103 | } // for |
---|
| 104 | } |
---|
| 105 | |
---|
[51b7345] | 106 | template < typename T > |
---|
[01aeade] | 107 | std::list<T> tail( std::list<T> l ) { |
---|
| 108 | if ( ! l.empty() ) { |
---|
| 109 | std::list<T> ret(++(l.begin()), l.end()); |
---|
| 110 | return ret; |
---|
| 111 | } // if |
---|
[51b7345] | 112 | } |
---|
| 113 | |
---|
| 114 | template < typename T > |
---|
| 115 | std::list<T> flatten( std::list < std::list<T> > l) { |
---|
[01aeade] | 116 | typedef std::list <T> Ts; |
---|
[51b7345] | 117 | |
---|
[01aeade] | 118 | Ts ret; |
---|
[51b7345] | 119 | |
---|
[a08ba92] | 120 | switch ( l.size() ) { |
---|
[01aeade] | 121 | case 0: |
---|
| 122 | return ret; |
---|
| 123 | case 1: |
---|
| 124 | return l.front(); |
---|
| 125 | default: |
---|
| 126 | ret = flatten(tail(l)); |
---|
| 127 | ret.insert(ret.begin(), l.front().begin(), l.front().end()); |
---|
| 128 | return ret; |
---|
| 129 | } // switch |
---|
[51b7345] | 130 | } |
---|
| 131 | |
---|
[60089f4] | 132 | template < typename T > |
---|
[2298f728] | 133 | void toString_single( std::ostream & os, const T & value ) { |
---|
[79970ed] | 134 | os << value; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | template < typename T, typename... Params > |
---|
[2298f728] | 138 | void toString_single( std::ostream & os, const T & value, const Params & ... params ) { |
---|
[79970ed] | 139 | os << value; |
---|
| 140 | toString_single( os, params ... ); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | template < typename ... Params > |
---|
[2298f728] | 144 | std::string toString( const Params & ... params ) { |
---|
[5f2f2d7] | 145 | std::ostringstream os; |
---|
[79970ed] | 146 | toString_single( os, params... ); |
---|
[5f2f2d7] | 147 | return os.str(); |
---|
[51b7345] | 148 | } |
---|
| 149 | |
---|
| 150 | template< typename T > |
---|
| 151 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { |
---|
[01aeade] | 152 | typename std::list< T >::iterator next = pos; advance( next, 1 ); |
---|
[51b7345] | 153 | |
---|
[01aeade] | 154 | //if ( next != org.end() ) { |
---|
[a08ba92] | 155 | org.erase( pos ); |
---|
| 156 | org.splice( next, with ); |
---|
| 157 | //} |
---|
[51b7345] | 158 | |
---|
[01aeade] | 159 | return; |
---|
[51b7345] | 160 | } |
---|
| 161 | |
---|
[940bba3] | 162 | template< typename... Args > |
---|
| 163 | auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) { |
---|
| 164 | return std::copy_if(std::forward<Args>(args)...); |
---|
[51b7345] | 165 | } |
---|
| 166 | |
---|
[940bba3] | 167 | template< typename... Args > |
---|
| 168 | auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) { |
---|
| 169 | return zipWith(std::forward<Args>(args)..., std::make_pair); |
---|
[51b7345] | 170 | } |
---|
| 171 | |
---|
| 172 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > |
---|
| 173 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { |
---|
[01aeade] | 174 | while ( b1 != e1 && b2 != e2 ) |
---|
| 175 | *out++ = func(*b1++, *b2++); |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
[d939274] | 178 | // it's nice to actually be able to increment iterators by an arbitrary amount |
---|
[940bba3] | 179 | template< class InputIt, class Distance > |
---|
| 180 | InputIt operator+( InputIt it, Distance n ) { |
---|
| 181 | advance(it, n); |
---|
| 182 | return it; |
---|
[d939274] | 183 | } |
---|
| 184 | |
---|
[79970ed] | 185 | template< typename T > |
---|
| 186 | void warn_single( const T & arg ) { |
---|
| 187 | std::cerr << arg << std::endl; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | template< typename T, typename... Params > |
---|
| 191 | void warn_single(const T & arg, const Params & ... params ) { |
---|
| 192 | std::cerr << arg; |
---|
| 193 | warn_single( params... ); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | template< typename... Params > |
---|
| 197 | void warn( const Params & ... params ) { |
---|
| 198 | std::cerr << "Warning: "; |
---|
| 199 | warn_single( params... ); |
---|
| 200 | } |
---|
| 201 | |
---|
[940bba3] | 202 | // ----------------------------------------------------------------------------- |
---|
| 203 | // Ref Counted Singleton class |
---|
| 204 | // Objects that inherit from this class will have at most one reference to it |
---|
| 205 | // but if all references die, the object will be deleted. |
---|
| 206 | |
---|
[e491159] | 207 | template< typename ThisType > |
---|
| 208 | class RefCountSingleton { |
---|
| 209 | public: |
---|
| 210 | static std::shared_ptr<ThisType> get() { |
---|
| 211 | if( global_instance.expired() ) { |
---|
| 212 | std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>(); |
---|
| 213 | global_instance = new_instance; |
---|
| 214 | return std::move(new_instance); |
---|
| 215 | } |
---|
| 216 | return global_instance.lock(); |
---|
| 217 | } |
---|
| 218 | private: |
---|
| 219 | static std::weak_ptr<ThisType> global_instance; |
---|
| 220 | }; |
---|
| 221 | |
---|
[1f75e2d] | 222 | template< typename ThisType > |
---|
| 223 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance; |
---|
| 224 | |
---|
[940bba3] | 225 | // ----------------------------------------------------------------------------- |
---|
[c8dfcd3] | 226 | // RAII object to regulate "save and restore" behaviour, e.g. |
---|
| 227 | // void Foo::bar() { |
---|
| 228 | // ValueGuard<int> guard(var); // var is a member of type Foo |
---|
| 229 | // var = ...; |
---|
| 230 | // } // var's original value is restored |
---|
| 231 | template< typename T > |
---|
| 232 | struct ValueGuard { |
---|
| 233 | T old; |
---|
| 234 | T& ref; |
---|
| 235 | |
---|
| 236 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {} |
---|
| 237 | ~ValueGuard() { ref = old; } |
---|
| 238 | }; |
---|
| 239 | |
---|
[940bba3] | 240 | // ----------------------------------------------------------------------------- |
---|
| 241 | // Helper struct and function to support |
---|
| 242 | // for ( val : reverseIterate( container ) ) {} |
---|
| 243 | // syntax to have a for each that iterates backwards |
---|
| 244 | |
---|
[44f6341] | 245 | template< typename T > |
---|
[1ba88a0] | 246 | struct reverseIterate_t { |
---|
[44f6341] | 247 | T& ref; |
---|
| 248 | |
---|
[1ba88a0] | 249 | reverseIterate_t( T & ref ) : ref(ref) {} |
---|
[44f6341] | 250 | |
---|
| 251 | typedef typename T::reverse_iterator iterator; |
---|
| 252 | iterator begin() { return ref.rbegin(); } |
---|
| 253 | iterator end() { return ref.rend(); } |
---|
| 254 | }; |
---|
| 255 | |
---|
| 256 | template< typename T > |
---|
[1ba88a0] | 257 | reverseIterate_t< T > reverseIterate( T & ref ) { |
---|
| 258 | return reverseIterate_t< T >( ref ); |
---|
[44f6341] | 259 | } |
---|
| 260 | |
---|
[01aeade] | 261 | #endif // _UTILITY_H |
---|
[51b7345] | 262 | |
---|
[51587aa] | 263 | // Local Variables: // |
---|
| 264 | // tab-width: 4 // |
---|
| 265 | // mode: c++ // |
---|
| 266 | // compile-command: "make install" // |
---|
| 267 | // End: // |
---|