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