| [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 | // | 
|---|
| [01aeade] | 7 | // utility.h -- | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [d939274] | 11 | // Last Modified By : Rob Schluntz | 
|---|
|  | 12 | // Last Modified On : Fri May 29 16:17:02 2015 | 
|---|
|  | 13 | // Update Count     : 7 | 
|---|
| [51587aa] | 14 | // | 
|---|
| [01aeade] | 15 |  | 
|---|
|  | 16 | #ifndef _UTILITY_H | 
|---|
|  | 17 | #define _UTILITY_H | 
|---|
| [51b73452] | 18 |  | 
|---|
|  | 19 | #include <iostream> | 
|---|
|  | 20 | #include <strstream> | 
|---|
|  | 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 | 
|---|
| [51b73452] | 33 | } | 
|---|
|  | 34 |  | 
|---|
|  | 35 | template< typename T, typename U > | 
|---|
| [01aeade] | 36 | static inline T * maybeBuild( const U *orig ) { | 
|---|
|  | 37 | if ( orig ) { | 
|---|
|  | 38 | return orig->build(); | 
|---|
|  | 39 | } else { | 
|---|
|  | 40 | return 0; | 
|---|
|  | 41 | } // if | 
|---|
| [51b73452] | 42 | } | 
|---|
|  | 43 |  | 
|---|
|  | 44 | template< typename Input_iterator > | 
|---|
| [01aeade] | 45 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) { | 
|---|
|  | 46 | for ( Input_iterator i = begin; i != end; ++i ) { | 
|---|
|  | 47 | os << name_array[ *i ] << ' '; | 
|---|
|  | 48 | } // for | 
|---|
| [51b73452] | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | template< typename Container > | 
|---|
| [01aeade] | 52 | void deleteAll( Container &container ) { | 
|---|
|  | 53 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
|  | 54 | delete *i; | 
|---|
|  | 55 | } // for | 
|---|
| [51b73452] | 56 | } | 
|---|
|  | 57 |  | 
|---|
|  | 58 | template< typename Container > | 
|---|
| [01aeade] | 59 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) { | 
|---|
|  | 60 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
|  | 61 | if ( *i ) { | 
|---|
|  | 62 | os << std::string(indent,  ' '); | 
|---|
|  | 63 | (*i)->print( os, indent + 2 ); | 
|---|
|  | 64 | os << std::endl; | 
|---|
|  | 65 | } // if | 
|---|
|  | 66 | } // for | 
|---|
| [51b73452] | 67 | } | 
|---|
|  | 68 |  | 
|---|
|  | 69 | template< typename SrcContainer, typename DestContainer > | 
|---|
| [01aeade] | 70 | void cloneAll( const SrcContainer &src, DestContainer &dest ) { | 
|---|
|  | 71 | typename SrcContainer::const_iterator in = src.begin(); | 
|---|
|  | 72 | std::back_insert_iterator< DestContainer > out( dest ); | 
|---|
|  | 73 | while ( in != src.end() ) { | 
|---|
|  | 74 | *out++ = (*in++)->clone(); | 
|---|
|  | 75 | } // while | 
|---|
| [51b73452] | 76 | } | 
|---|
|  | 77 |  | 
|---|
|  | 78 | template< typename Container > | 
|---|
| [01aeade] | 79 | void assertAll( const Container &container ) { | 
|---|
|  | 80 | int count = 0; | 
|---|
|  | 81 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
|  | 82 | if ( !(*i) ) { | 
|---|
|  | 83 | std::cerr << count << " is null" << std::endl; | 
|---|
|  | 84 | } // if | 
|---|
|  | 85 | } // for | 
|---|
|  | 86 | } | 
|---|
|  | 87 |  | 
|---|
|  | 88 | static inline std::string assign_strptr( std::string *str ) { | 
|---|
|  | 89 | if ( str == 0 ) { | 
|---|
|  | 90 | return ""; | 
|---|
|  | 91 | } else { | 
|---|
|  | 92 | std::string tmp; | 
|---|
|  | 93 | tmp = *str; | 
|---|
|  | 94 | delete str; | 
|---|
|  | 95 | return tmp; | 
|---|
|  | 96 | } // if | 
|---|
| [51b73452] | 97 | } | 
|---|
|  | 98 |  | 
|---|
|  | 99 | template< class T, typename ResultType, ResultType (T::* memfunc)() > | 
|---|
| [01aeade] | 100 | ResultType dispatch( T *pT ) { | 
|---|
|  | 101 | return (pT->*memfunc)(); | 
|---|
| [51b73452] | 102 | } | 
|---|
|  | 103 |  | 
|---|
|  | 104 | template < typename T > | 
|---|
| [01aeade] | 105 | std::list<T> tail( std::list<T> l ) { | 
|---|
|  | 106 | if ( ! l.empty() ) { | 
|---|
|  | 107 | std::list<T> ret(++(l.begin()), l.end()); | 
|---|
|  | 108 | return ret; | 
|---|
|  | 109 | } // if | 
|---|
| [51b73452] | 110 | } | 
|---|
|  | 111 |  | 
|---|
|  | 112 | template < typename T > | 
|---|
|  | 113 | std::list<T> flatten( std::list < std::list<T> > l) { | 
|---|
| [01aeade] | 114 | typedef std::list <T> Ts; | 
|---|
| [51b73452] | 115 |  | 
|---|
| [01aeade] | 116 | Ts ret; | 
|---|
| [51b73452] | 117 |  | 
|---|
| [a08ba92] | 118 | switch ( l.size() ) { | 
|---|
| [01aeade] | 119 | case 0: | 
|---|
|  | 120 | return ret; | 
|---|
|  | 121 | case 1: | 
|---|
|  | 122 | return l.front(); | 
|---|
|  | 123 | default: | 
|---|
|  | 124 | ret = flatten(tail(l)); | 
|---|
|  | 125 | ret.insert(ret.begin(), l.front().begin(), l.front().end()); | 
|---|
|  | 126 | return ret; | 
|---|
|  | 127 | } // switch | 
|---|
| [51b73452] | 128 | } | 
|---|
|  | 129 |  | 
|---|
|  | 130 | template < typename T > | 
|---|
|  | 131 | std::string toString ( T value ) { | 
|---|
| [01aeade] | 132 | std::ostrstream os; | 
|---|
| [51b73452] | 133 |  | 
|---|
| [01aeade] | 134 | os << value; // << std::ends; | 
|---|
|  | 135 | os.freeze( false ); | 
|---|
| [51b73452] | 136 |  | 
|---|
| [01aeade] | 137 | return std::string(os.str(), os.pcount()); | 
|---|
| [51b73452] | 138 | } | 
|---|
|  | 139 |  | 
|---|
|  | 140 | template< class Constructed, typename Arg > | 
|---|
|  | 141 | Constructed *ctor( Arg arg ) { | 
|---|
| [01aeade] | 142 | Constructed *c = new Constructed( arg ); | 
|---|
|  | 143 | return c; | 
|---|
| [51b73452] | 144 | } | 
|---|
|  | 145 |  | 
|---|
|  | 146 | template< class Constructed, typename Arg > | 
|---|
|  | 147 | Constructed ctor_noptr( Arg arg ) { | 
|---|
| [01aeade] | 148 | return Constructed( arg ); | 
|---|
| [51b73452] | 149 | } | 
|---|
|  | 150 |  | 
|---|
|  | 151 | template< typename T > | 
|---|
|  | 152 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) { | 
|---|
| [01aeade] | 153 | // TIter should secretly be a typename std::list< T >::iterator | 
|---|
|  | 154 | //   ( g++ 3.2 issues a 'is implicitly a typename' warning if I make this explicit ) | 
|---|
|  | 155 | typename std::list< T >::iterator next = pos; advance( next, 1 ); | 
|---|
| [51b73452] | 156 |  | 
|---|
| [01aeade] | 157 | //if ( next != org.end() ) { | 
|---|
| [a08ba92] | 158 | org.erase( pos ); | 
|---|
|  | 159 | org.splice( next, with ); | 
|---|
|  | 160 | //} | 
|---|
| [51b73452] | 161 |  | 
|---|
| [01aeade] | 162 | return; | 
|---|
| [51b73452] | 163 | } | 
|---|
|  | 164 |  | 
|---|
|  | 165 | template< typename T1, typename T2 > | 
|---|
|  | 166 | T2 *cast_ptr( T1 *from ) { | 
|---|
| [01aeade] | 167 | return dynamic_cast< T2 * >( from ); | 
|---|
| [51b73452] | 168 | } | 
|---|
|  | 169 |  | 
|---|
|  | 170 | template< class Exception, typename Arg > | 
|---|
|  | 171 | void inline assert_throw( bool pred, Arg arg ) { | 
|---|
| [01aeade] | 172 | if (pred) throw Exception( arg ); | 
|---|
| [51b73452] | 173 | } | 
|---|
|  | 174 |  | 
|---|
|  | 175 | template< typename T > | 
|---|
|  | 176 | struct is_null_pointer { | 
|---|
| [a08ba92] | 177 | bool operator()( const T *ptr ) { return ( ptr == 0 ); } | 
|---|
| [51b73452] | 178 | }; | 
|---|
|  | 179 |  | 
|---|
|  | 180 | template< class InputIterator, class OutputIterator, class Predicate > | 
|---|
| [01aeade] | 181 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) { | 
|---|
|  | 182 | while ( begin++ != end ) | 
|---|
|  | 183 | if ( pred(*begin) ) *out++ = *begin; | 
|---|
| [51b73452] | 184 |  | 
|---|
| [01aeade] | 185 | return; | 
|---|
| [51b73452] | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | template< class InputIterator1, class InputIterator2, class OutputIterator > | 
|---|
|  | 189 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) { | 
|---|
| [01aeade] | 190 | while ( b1 != e1 && b2 != e2 ) | 
|---|
|  | 191 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++); | 
|---|
| [51b73452] | 192 | } | 
|---|
|  | 193 |  | 
|---|
|  | 194 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction > | 
|---|
|  | 195 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) { | 
|---|
| [01aeade] | 196 | while ( b1 != e1 && b2 != e2 ) | 
|---|
|  | 197 | *out++ = func(*b1++, *b2++); | 
|---|
| [51b73452] | 198 | } | 
|---|
|  | 199 |  | 
|---|
| [d939274] | 200 | // it's nice to actually be able to increment iterators by an arbitrary amount | 
|---|
|  | 201 | template< typename Iterator > | 
|---|
|  | 202 | Iterator operator+(Iterator i, int inc) { | 
|---|
|  | 203 | while ( inc > 0 ) { | 
|---|
|  | 204 | ++i; | 
|---|
|  | 205 | --inc; | 
|---|
|  | 206 | } | 
|---|
|  | 207 | return i; | 
|---|
|  | 208 | } | 
|---|
|  | 209 |  | 
|---|
| [01aeade] | 210 | #endif // _UTILITY_H | 
|---|
| [51b73452] | 211 |  | 
|---|
| [51587aa] | 212 | // Local Variables: // | 
|---|
|  | 213 | // tab-width: 4 // | 
|---|
|  | 214 | // mode: c++ // | 
|---|
|  | 215 | // compile-command: "make install" // | 
|---|
|  | 216 | // End: // | 
|---|