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