[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 | |
---|
| 51 | template< typename Input_iterator > |
---|
[01aeade] | 52 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) { |
---|
| 53 | for ( Input_iterator i = begin; i != end; ++i ) { |
---|
| 54 | os << name_array[ *i ] << ' '; |
---|
| 55 | } // for |
---|
[51b7345] | 56 | } |
---|
| 57 | |
---|
| 58 | template< typename Container > |
---|
[01aeade] | 59 | void deleteAll( Container &container ) { |
---|
| 60 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 61 | delete *i; |
---|
| 62 | } // for |
---|
[51b7345] | 63 | } |
---|
| 64 | |
---|
| 65 | template< typename Container > |
---|
[01aeade] | 66 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) { |
---|
| 67 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 68 | if ( *i ) { |
---|
[cf0941d] | 69 | os << std::string( indent, ' ' ); |
---|
[01aeade] | 70 | (*i)->print( os, indent + 2 ); |
---|
[60089f4] | 71 | // need an endl after each element because it's not easy to know when each individual item should end |
---|
[01aeade] | 72 | os << std::endl; |
---|
| 73 | } // if |
---|
| 74 | } // for |
---|
[51b7345] | 75 | } |
---|
| 76 | |
---|
| 77 | template< typename SrcContainer, typename DestContainer > |
---|
[01aeade] | 78 | void cloneAll( const SrcContainer &src, DestContainer &dest ) { |
---|
| 79 | typename SrcContainer::const_iterator in = src.begin(); |
---|
| 80 | std::back_insert_iterator< DestContainer > out( dest ); |
---|
| 81 | while ( in != src.end() ) { |
---|
| 82 | *out++ = (*in++)->clone(); |
---|
| 83 | } // while |
---|
[51b7345] | 84 | } |
---|
| 85 | |
---|
| 86 | template< typename Container > |
---|
[01aeade] | 87 | void assertAll( const Container &container ) { |
---|
| 88 | int count = 0; |
---|
| 89 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { |
---|
| 90 | if ( !(*i) ) { |
---|
| 91 | std::cerr << count << " is null" << std::endl; |
---|
| 92 | } // if |
---|
| 93 | } // for |
---|
| 94 | } |
---|
| 95 | |
---|
[2871210] | 96 | static inline std::string assign_strptr( const std::string *str ) { |
---|
[01aeade] | 97 | if ( str == 0 ) { |
---|
| 98 | return ""; |
---|
| 99 | } else { |
---|
| 100 | std::string tmp; |
---|
| 101 | tmp = *str; |
---|
| 102 | delete str; |
---|
| 103 | return tmp; |
---|
| 104 | } // if |
---|
[51b7345] | 105 | } |
---|
| 106 | |
---|
[2871210] | 107 | template< class T, typename ResultType, ResultType( T::* memfunc )() > |
---|
[01aeade] | 108 | ResultType dispatch( T *pT ) { |
---|
| 109 | return (pT->*memfunc)(); |
---|
[51b7345] | 110 | } |
---|
| 111 | |
---|
| 112 | template < typename T > |
---|
[01aeade] | 113 | std::list<T> tail( std::list<T> l ) { |
---|
| 114 | if ( ! l.empty() ) { |
---|
| 115 | std::list<T> ret(++(l.begin()), l.end()); |
---|
| 116 | return ret; |
---|
| 117 | } // if |
---|
[51b7345] | 118 | } |
---|
| 119 | |
---|
| 120 | template < typename T > |
---|
| 121 | std::list<T> flatten( std::list < std::list<T> > l) { |
---|
[01aeade] | 122 | typedef std::list <T> Ts; |
---|
[51b7345] | 123 | |
---|
[01aeade] | 124 | Ts ret; |
---|
[51b7345] | 125 | |
---|
[a08ba92] | 126 | switch ( l.size() ) { |
---|
[01aeade] | 127 | case 0: |
---|
| 128 | return ret; |
---|
| 129 | case 1: |
---|
| 130 | return l.front(); |
---|
| 131 | default: |
---|
| 132 | ret = flatten(tail(l)); |
---|
| 133 | ret.insert(ret.begin(), l.front().begin(), l.front().end()); |
---|
| 134 | return ret; |
---|
| 135 | } // switch |
---|
[51b7345] | 136 | } |
---|
| 137 | |
---|
[60089f4] | 138 | template < typename T > |
---|
[51b7345] | 139 | std::string toString ( T value ) { |
---|
[5f2f2d7] | 140 | std::ostringstream os; |
---|
[01aeade] | 141 | os << value; // << std::ends; |
---|
[5f2f2d7] | 142 | return os.str(); |
---|
[51b7345] | 143 | } |
---|
| 144 | |
---|
| 145 | template< class Constructed, typename Arg > |
---|
| 146 | Constructed *ctor( Arg arg ) { |
---|
[01aeade] | 147 | Constructed *c = new Constructed( arg ); |
---|
| 148 | return c; |
---|
[51b7345] | 149 | } |
---|
| 150 | |
---|
| 151 | template< class Constructed, typename Arg > |
---|
| 152 | Constructed ctor_noptr( Arg arg ) { |
---|
[01aeade] | 153 | return Constructed( arg ); |
---|
[51b7345] | 154 | } |
---|
| 155 | |
---|
| 156 | template< typename T > |
---|
| 157 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { |
---|
[01aeade] | 158 | typename std::list< T >::iterator next = pos; advance( next, 1 ); |
---|
[51b7345] | 159 | |
---|
[01aeade] | 160 | //if ( next != org.end() ) { |
---|
[a08ba92] | 161 | org.erase( pos ); |
---|
| 162 | org.splice( next, with ); |
---|
| 163 | //} |
---|
[51b7345] | 164 | |
---|
[01aeade] | 165 | return; |
---|
[51b7345] | 166 | } |
---|
| 167 | |
---|
| 168 | template< typename T1, typename T2 > |
---|
| 169 | T2 *cast_ptr( T1 *from ) { |
---|
[01aeade] | 170 | return dynamic_cast< T2 * >( from ); |
---|
[51b7345] | 171 | } |
---|
| 172 | |
---|
| 173 | template< class Exception, typename Arg > |
---|
| 174 | void inline assert_throw( bool pred, Arg arg ) { |
---|
[01aeade] | 175 | if (pred) throw Exception( arg ); |
---|
[51b7345] | 176 | } |
---|
| 177 | |
---|
| 178 | template< typename T > |
---|
| 179 | struct is_null_pointer { |
---|
[a08ba92] | 180 | bool operator()( const T *ptr ) { return ( ptr == 0 ); } |
---|
[51b7345] | 181 | }; |
---|
| 182 | |
---|
| 183 | template< class InputIterator, class OutputIterator, class Predicate > |
---|
[01aeade] | 184 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) { |
---|
| 185 | while ( begin++ != end ) |
---|
| 186 | if ( pred(*begin) ) *out++ = *begin; |
---|
[51b7345] | 187 | |
---|
[01aeade] | 188 | return; |
---|
[51b7345] | 189 | } |
---|
| 190 | |
---|
| 191 | template< class InputIterator1, class InputIterator2, class OutputIterator > |
---|
| 192 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) { |
---|
[01aeade] | 193 | while ( b1 != e1 && b2 != e2 ) |
---|
| 194 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++); |
---|
[51b7345] | 195 | } |
---|
| 196 | |
---|
| 197 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > |
---|
| 198 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { |
---|
[01aeade] | 199 | while ( b1 != e1 && b2 != e2 ) |
---|
| 200 | *out++ = func(*b1++, *b2++); |
---|
[51b7345] | 201 | } |
---|
| 202 | |
---|
[d939274] | 203 | // it's nice to actually be able to increment iterators by an arbitrary amount |
---|
| 204 | template< typename Iterator > |
---|
| 205 | Iterator operator+(Iterator i, int inc) { |
---|
| 206 | while ( inc > 0 ) { |
---|
| 207 | ++i; |
---|
| 208 | --inc; |
---|
| 209 | } |
---|
| 210 | return i; |
---|
| 211 | } |
---|
| 212 | |
---|
[01aeade] | 213 | #endif // _UTILITY_H |
---|
[51b7345] | 214 | |
---|
[51587aa] | 215 | // Local Variables: // |
---|
| 216 | // tab-width: 4 // |
---|
| 217 | // mode: c++ // |
---|
| 218 | // compile-command: "make install" // |
---|
| 219 | // End: // |
---|