[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 |
---|
| 12 | // Last Modified On : Wed Jun 8 17:33:59 2016 |
---|
| 13 | // Update Count : 22 |
---|
[51587aa] | 14 | // |
---|
[01aeade] | 15 | |
---|
| 16 | #ifndef _UTILITY_H |
---|
| 17 | #define _UTILITY_H |
---|
[51b7345] | 18 | |
---|
| 19 | #include <iostream> |
---|
[5f2f2d7] | 20 | #include <sstream> |
---|
[51b7345] | 21 | #include <iterator> |
---|
| 22 | #include <string> |
---|
| 23 | #include <cctype> |
---|
| 24 | #include <list> |
---|
| 25 | |
---|
| 26 | template< typename T > |
---|
[01aeade] | 27 | static inline T * maybeClone( const T *orig ) { |
---|
| 28 | if ( orig ) { |
---|
| 29 | return orig->clone(); |
---|
| 30 | } else { |
---|
| 31 | return 0; |
---|
| 32 | } // if |
---|
[51b7345] | 33 | } |
---|
| 34 | |
---|
[e04ef3a] | 35 | template<typename T, typename U> |
---|
| 36 | struct maybeBuild_t { |
---|
| 37 | static T * doit( const U *orig ) { |
---|
| 38 | if ( orig ) { |
---|
| 39 | return orig->build(); |
---|
| 40 | } else { |
---|
| 41 | return 0; |
---|
| 42 | } // if |
---|
| 43 | } |
---|
| 44 | }; |
---|
| 45 | |
---|
[51b7345] | 46 | template< typename T, typename U > |
---|
[01aeade] | 47 | static inline T * maybeBuild( const U *orig ) { |
---|
[e04ef3a] | 48 | return maybeBuild_t<T,U>::doit(orig); |
---|
[51b7345] | 49 | } |
---|
| 50 | |
---|
[7ecbb7e] | 51 | template< typename T, typename U > |
---|
| 52 | static inline T * maybeMoveBuild( const U *orig ) { |
---|
| 53 | T* ret = maybeBuild<T>(orig); |
---|
| 54 | delete orig; |
---|
| 55 | return ret; |
---|
| 56 | } |
---|
| 57 | |
---|
[51b7345] | 58 | template< typename Input_iterator > |
---|
[01aeade] | 59 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) { |
---|
| 60 | for ( Input_iterator i = begin; i != end; ++i ) { |
---|
| 61 | os << name_array[ *i ] << ' '; |
---|
| 62 | } // for |
---|
[51b7345] | 63 | } |
---|
| 64 | |
---|
| 65 | template< typename Container > |
---|
[01aeade] | 66 | void deleteAll( Container &container ) { |
---|
| 67 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 68 | delete *i; |
---|
| 69 | } // for |
---|
[51b7345] | 70 | } |
---|
| 71 | |
---|
| 72 | template< typename Container > |
---|
[01aeade] | 73 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) { |
---|
| 74 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 75 | if ( *i ) { |
---|
[cf0941d] | 76 | os << std::string( indent, ' ' ); |
---|
[01aeade] | 77 | (*i)->print( os, indent + 2 ); |
---|
[60089f4] | 78 | // need an endl after each element because it's not easy to know when each individual item should end |
---|
[01aeade] | 79 | os << std::endl; |
---|
| 80 | } // if |
---|
| 81 | } // for |
---|
[51b7345] | 82 | } |
---|
| 83 | |
---|
| 84 | template< typename SrcContainer, typename DestContainer > |
---|
[01aeade] | 85 | void cloneAll( const SrcContainer &src, DestContainer &dest ) { |
---|
| 86 | typename SrcContainer::const_iterator in = src.begin(); |
---|
| 87 | std::back_insert_iterator< DestContainer > out( dest ); |
---|
| 88 | while ( in != src.end() ) { |
---|
| 89 | *out++ = (*in++)->clone(); |
---|
| 90 | } // while |
---|
[51b7345] | 91 | } |
---|
| 92 | |
---|
| 93 | template< typename Container > |
---|
[01aeade] | 94 | void assertAll( const Container &container ) { |
---|
| 95 | int count = 0; |
---|
| 96 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 97 | if ( !(*i) ) { |
---|
| 98 | std::cerr << count << " is null" << std::endl; |
---|
| 99 | } // if |
---|
| 100 | } // for |
---|
| 101 | } |
---|
| 102 | |
---|
[2871210] | 103 | static inline std::string assign_strptr( const std::string *str ) { |
---|
[01aeade] | 104 | if ( str == 0 ) { |
---|
| 105 | return ""; |
---|
| 106 | } else { |
---|
| 107 | std::string tmp; |
---|
| 108 | tmp = *str; |
---|
| 109 | delete str; |
---|
| 110 | return tmp; |
---|
| 111 | } // if |
---|
[51b7345] | 112 | } |
---|
| 113 | |
---|
[2871210] | 114 | template< class T, typename ResultType, ResultType( T::* memfunc )() > |
---|
[01aeade] | 115 | ResultType dispatch( T *pT ) { |
---|
| 116 | return (pT->*memfunc)(); |
---|
[51b7345] | 117 | } |
---|
| 118 | |
---|
| 119 | template < typename T > |
---|
[01aeade] | 120 | std::list<T> tail( std::list<T> l ) { |
---|
| 121 | if ( ! l.empty() ) { |
---|
| 122 | std::list<T> ret(++(l.begin()), l.end()); |
---|
| 123 | return ret; |
---|
| 124 | } // if |
---|
[51b7345] | 125 | } |
---|
| 126 | |
---|
| 127 | template < typename T > |
---|
| 128 | std::list<T> flatten( std::list < std::list<T> > l) { |
---|
[01aeade] | 129 | typedef std::list <T> Ts; |
---|
[51b7345] | 130 | |
---|
[01aeade] | 131 | Ts ret; |
---|
[51b7345] | 132 | |
---|
[a08ba92] | 133 | switch ( l.size() ) { |
---|
[01aeade] | 134 | case 0: |
---|
| 135 | return ret; |
---|
| 136 | case 1: |
---|
| 137 | return l.front(); |
---|
| 138 | default: |
---|
| 139 | ret = flatten(tail(l)); |
---|
| 140 | ret.insert(ret.begin(), l.front().begin(), l.front().end()); |
---|
| 141 | return ret; |
---|
| 142 | } // switch |
---|
[51b7345] | 143 | } |
---|
| 144 | |
---|
[60089f4] | 145 | template < typename T > |
---|
[79970ed] | 146 | void toString_single ( std::ostream & os, const T & value ) { |
---|
| 147 | os << value; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | template < typename T, typename... Params > |
---|
| 151 | void toString_single ( std::ostream & os, const T & value, const Params & ... params ) { |
---|
| 152 | os << value; |
---|
| 153 | toString_single( os, params ... ); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | template < typename ... Params > |
---|
| 157 | std::string toString ( const Params & ... params ) { |
---|
[5f2f2d7] | 158 | std::ostringstream os; |
---|
[79970ed] | 159 | toString_single( os, params... ); |
---|
[5f2f2d7] | 160 | return os.str(); |
---|
[51b7345] | 161 | } |
---|
| 162 | |
---|
| 163 | template< class Constructed, typename Arg > |
---|
| 164 | Constructed *ctor( Arg arg ) { |
---|
[01aeade] | 165 | Constructed *c = new Constructed( arg ); |
---|
| 166 | return c; |
---|
[51b7345] | 167 | } |
---|
| 168 | |
---|
| 169 | template< class Constructed, typename Arg > |
---|
| 170 | Constructed ctor_noptr( Arg arg ) { |
---|
[01aeade] | 171 | return Constructed( arg ); |
---|
[51b7345] | 172 | } |
---|
| 173 | |
---|
| 174 | template< typename T > |
---|
| 175 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { |
---|
[01aeade] | 176 | typename std::list< T >::iterator next = pos; advance( next, 1 ); |
---|
[51b7345] | 177 | |
---|
[01aeade] | 178 | //if ( next != org.end() ) { |
---|
[a08ba92] | 179 | org.erase( pos ); |
---|
| 180 | org.splice( next, with ); |
---|
| 181 | //} |
---|
[51b7345] | 182 | |
---|
[01aeade] | 183 | return; |
---|
[51b7345] | 184 | } |
---|
| 185 | |
---|
| 186 | template< typename T1, typename T2 > |
---|
| 187 | T2 *cast_ptr( T1 *from ) { |
---|
[01aeade] | 188 | return dynamic_cast< T2 * >( from ); |
---|
[51b7345] | 189 | } |
---|
| 190 | |
---|
| 191 | template< class Exception, typename Arg > |
---|
| 192 | void inline assert_throw( bool pred, Arg arg ) { |
---|
[01aeade] | 193 | if (pred) throw Exception( arg ); |
---|
[51b7345] | 194 | } |
---|
| 195 | |
---|
| 196 | template< typename T > |
---|
| 197 | struct is_null_pointer { |
---|
[a08ba92] | 198 | bool operator()( const T *ptr ) { return ( ptr == 0 ); } |
---|
[51b7345] | 199 | }; |
---|
| 200 | |
---|
| 201 | template< class InputIterator, class OutputIterator, class Predicate > |
---|
[01aeade] | 202 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) { |
---|
| 203 | while ( begin++ != end ) |
---|
| 204 | if ( pred(*begin) ) *out++ = *begin; |
---|
[51b7345] | 205 | |
---|
[01aeade] | 206 | return; |
---|
[51b7345] | 207 | } |
---|
| 208 | |
---|
| 209 | template< class InputIterator1, class InputIterator2, class OutputIterator > |
---|
| 210 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) { |
---|
[01aeade] | 211 | while ( b1 != e1 && b2 != e2 ) |
---|
| 212 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++); |
---|
[51b7345] | 213 | } |
---|
| 214 | |
---|
| 215 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > |
---|
| 216 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { |
---|
[01aeade] | 217 | while ( b1 != e1 && b2 != e2 ) |
---|
| 218 | *out++ = func(*b1++, *b2++); |
---|
[51b7345] | 219 | } |
---|
| 220 | |
---|
[d939274] | 221 | // it's nice to actually be able to increment iterators by an arbitrary amount |
---|
| 222 | template< typename Iterator > |
---|
| 223 | Iterator operator+(Iterator i, int inc) { |
---|
| 224 | while ( inc > 0 ) { |
---|
| 225 | ++i; |
---|
| 226 | --inc; |
---|
| 227 | } |
---|
| 228 | return i; |
---|
| 229 | } |
---|
| 230 | |
---|
[79970ed] | 231 | template< typename T > |
---|
| 232 | void warn_single( const T & arg ) { |
---|
| 233 | std::cerr << arg << std::endl; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | template< typename T, typename... Params > |
---|
| 237 | void warn_single(const T & arg, const Params & ... params ) { |
---|
| 238 | std::cerr << arg; |
---|
| 239 | warn_single( params... ); |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | template< typename... Params > |
---|
| 243 | void warn( const Params & ... params ) { |
---|
| 244 | std::cerr << "Warning: "; |
---|
| 245 | warn_single( params... ); |
---|
| 246 | } |
---|
| 247 | |
---|
[01aeade] | 248 | #endif // _UTILITY_H |
---|
[51b7345] | 249 | |
---|
[51587aa] | 250 | // Local Variables: // |
---|
| 251 | // tab-width: 4 // |
---|
| 252 | // mode: c++ // |
---|
| 253 | // compile-command: "make install" // |
---|
| 254 | // End: // |
---|