[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
|
---|
[51b73452] | 18 |
|
---|
[e491159] | 19 | #include <cctype>
|
---|
[940bba3] | 20 | #include <algorithm>
|
---|
[51b73452] | 21 | #include <iostream>
|
---|
| 22 | #include <iterator>
|
---|
| 23 | #include <list>
|
---|
[e491159] | 24 | #include <memory>
|
---|
| 25 | #include <sstream>
|
---|
| 26 | #include <string>
|
---|
[51b73452] | 27 |
|
---|
| 28 | template< typename T >
|
---|
[01aeade] | 29 | static inline T * maybeClone( const T *orig ) {
|
---|
| 30 | if ( orig ) {
|
---|
| 31 | return orig->clone();
|
---|
| 32 | } else {
|
---|
| 33 | return 0;
|
---|
| 34 | } // if
|
---|
[51b73452] | 35 | }
|
---|
| 36 |
|
---|
[e04ef3a] | 37 | template<typename T, typename U>
|
---|
| 38 | struct maybeBuild_t {
|
---|
| 39 | static T * doit( const U *orig ) {
|
---|
| 40 | if ( orig ) {
|
---|
| 41 | return orig->build();
|
---|
| 42 | } else {
|
---|
| 43 | return 0;
|
---|
| 44 | } // if
|
---|
| 45 | }
|
---|
| 46 | };
|
---|
| 47 |
|
---|
[51b73452] | 48 | template< typename T, typename U >
|
---|
[01aeade] | 49 | static inline T * maybeBuild( const U *orig ) {
|
---|
[e04ef3a] | 50 | return maybeBuild_t<T,U>::doit(orig);
|
---|
[51b73452] | 51 | }
|
---|
| 52 |
|
---|
[7ecbb7e] | 53 | template< typename T, typename U >
|
---|
| 54 | static inline T * maybeMoveBuild( const U *orig ) {
|
---|
| 55 | T* ret = maybeBuild<T>(orig);
|
---|
| 56 | delete orig;
|
---|
| 57 | return ret;
|
---|
| 58 | }
|
---|
| 59 |
|
---|
[51b73452] | 60 | template< typename Input_iterator >
|
---|
[01aeade] | 61 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
|
---|
| 62 | for ( Input_iterator i = begin; i != end; ++i ) {
|
---|
| 63 | os << name_array[ *i ] << ' ';
|
---|
| 64 | } // for
|
---|
[51b73452] | 65 | }
|
---|
| 66 |
|
---|
| 67 | template< typename Container >
|
---|
[01aeade] | 68 | void deleteAll( Container &container ) {
|
---|
| 69 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
| 70 | delete *i;
|
---|
| 71 | } // for
|
---|
[51b73452] | 72 | }
|
---|
| 73 |
|
---|
| 74 | template< typename Container >
|
---|
[01aeade] | 75 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
|
---|
| 76 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
| 77 | if ( *i ) {
|
---|
[cf0941d] | 78 | os << std::string( indent, ' ' );
|
---|
[01aeade] | 79 | (*i)->print( os, indent + 2 );
|
---|
[60089f4] | 80 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
[01aeade] | 81 | os << std::endl;
|
---|
| 82 | } // if
|
---|
| 83 | } // for
|
---|
[51b73452] | 84 | }
|
---|
| 85 |
|
---|
| 86 | template< typename SrcContainer, typename DestContainer >
|
---|
[01aeade] | 87 | void cloneAll( const SrcContainer &src, DestContainer &dest ) {
|
---|
| 88 | typename SrcContainer::const_iterator in = src.begin();
|
---|
| 89 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
| 90 | while ( in != src.end() ) {
|
---|
| 91 | *out++ = (*in++)->clone();
|
---|
| 92 | } // while
|
---|
[51b73452] | 93 | }
|
---|
| 94 |
|
---|
| 95 | template< typename Container >
|
---|
[01aeade] | 96 | void assertAll( const Container &container ) {
|
---|
| 97 | int count = 0;
|
---|
| 98 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
| 99 | if ( !(*i) ) {
|
---|
| 100 | std::cerr << count << " is null" << std::endl;
|
---|
| 101 | } // if
|
---|
| 102 | } // for
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[2871210] | 105 | static inline std::string assign_strptr( const std::string *str ) {
|
---|
[01aeade] | 106 | if ( str == 0 ) {
|
---|
| 107 | return "";
|
---|
| 108 | } else {
|
---|
| 109 | std::string tmp;
|
---|
| 110 | tmp = *str;
|
---|
| 111 | delete str;
|
---|
| 112 | return tmp;
|
---|
| 113 | } // if
|
---|
[51b73452] | 114 | }
|
---|
| 115 |
|
---|
| 116 | template < typename T >
|
---|
[01aeade] | 117 | std::list<T> tail( std::list<T> l ) {
|
---|
| 118 | if ( ! l.empty() ) {
|
---|
| 119 | std::list<T> ret(++(l.begin()), l.end());
|
---|
| 120 | return ret;
|
---|
| 121 | } // if
|
---|
[51b73452] | 122 | }
|
---|
| 123 |
|
---|
| 124 | template < typename T >
|
---|
| 125 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
[01aeade] | 126 | typedef std::list <T> Ts;
|
---|
[51b73452] | 127 |
|
---|
[01aeade] | 128 | Ts ret;
|
---|
[51b73452] | 129 |
|
---|
[a08ba92] | 130 | switch ( l.size() ) {
|
---|
[01aeade] | 131 | case 0:
|
---|
| 132 | return ret;
|
---|
| 133 | case 1:
|
---|
| 134 | return l.front();
|
---|
| 135 | default:
|
---|
| 136 | ret = flatten(tail(l));
|
---|
| 137 | ret.insert(ret.begin(), l.front().begin(), l.front().end());
|
---|
| 138 | return ret;
|
---|
| 139 | } // switch
|
---|
[51b73452] | 140 | }
|
---|
| 141 |
|
---|
[60089f4] | 142 | template < typename T >
|
---|
[79970ed] | 143 | void toString_single ( std::ostream & os, const T & value ) {
|
---|
| 144 | os << value;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | template < typename T, typename... Params >
|
---|
| 148 | void toString_single ( std::ostream & os, const T & value, const Params & ... params ) {
|
---|
| 149 | os << value;
|
---|
| 150 | toString_single( os, params ... );
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | template < typename ... Params >
|
---|
| 154 | std::string toString ( const Params & ... params ) {
|
---|
[5f2f2d7] | 155 | std::ostringstream os;
|
---|
[79970ed] | 156 | toString_single( os, params... );
|
---|
[5f2f2d7] | 157 | return os.str();
|
---|
[51b73452] | 158 | }
|
---|
| 159 |
|
---|
| 160 | template< typename T >
|
---|
| 161 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
---|
[01aeade] | 162 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
---|
[51b73452] | 163 |
|
---|
[01aeade] | 164 | //if ( next != org.end() ) {
|
---|
[a08ba92] | 165 | org.erase( pos );
|
---|
| 166 | org.splice( next, with );
|
---|
| 167 | //}
|
---|
[51b73452] | 168 |
|
---|
[01aeade] | 169 | return;
|
---|
[51b73452] | 170 | }
|
---|
| 171 |
|
---|
[940bba3] | 172 | template< typename... Args >
|
---|
| 173 | auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
|
---|
| 174 | return std::copy_if(std::forward<Args>(args)...);
|
---|
[51b73452] | 175 | }
|
---|
| 176 |
|
---|
[940bba3] | 177 | template< typename... Args >
|
---|
| 178 | auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
|
---|
| 179 | return zipWith(std::forward<Args>(args)..., std::make_pair);
|
---|
[51b73452] | 180 | }
|
---|
| 181 |
|
---|
| 182 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
|
---|
| 183 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
|
---|
[01aeade] | 184 | while ( b1 != e1 && b2 != e2 )
|
---|
| 185 | *out++ = func(*b1++, *b2++);
|
---|
[51b73452] | 186 | }
|
---|
| 187 |
|
---|
[d939274] | 188 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
---|
[940bba3] | 189 | template< class InputIt, class Distance >
|
---|
| 190 | InputIt operator+( InputIt it, Distance n ) {
|
---|
| 191 | advance(it, n);
|
---|
| 192 | return it;
|
---|
[d939274] | 193 | }
|
---|
| 194 |
|
---|
[79970ed] | 195 | template< typename T >
|
---|
| 196 | void warn_single( const T & arg ) {
|
---|
| 197 | std::cerr << arg << std::endl;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | template< typename T, typename... Params >
|
---|
| 201 | void warn_single(const T & arg, const Params & ... params ) {
|
---|
| 202 | std::cerr << arg;
|
---|
| 203 | warn_single( params... );
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | template< typename... Params >
|
---|
| 207 | void warn( const Params & ... params ) {
|
---|
| 208 | std::cerr << "Warning: ";
|
---|
| 209 | warn_single( params... );
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[940bba3] | 212 | // -----------------------------------------------------------------------------
|
---|
| 213 | // Ref Counted Singleton class
|
---|
| 214 | // Objects that inherit from this class will have at most one reference to it
|
---|
| 215 | // but if all references die, the object will be deleted.
|
---|
| 216 |
|
---|
[e491159] | 217 | template< typename ThisType >
|
---|
| 218 | class RefCountSingleton {
|
---|
| 219 | public:
|
---|
| 220 | static std::shared_ptr<ThisType> get() {
|
---|
| 221 | if( global_instance.expired() ) {
|
---|
| 222 | std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
|
---|
| 223 | global_instance = new_instance;
|
---|
| 224 | return std::move(new_instance);
|
---|
| 225 | }
|
---|
| 226 | return global_instance.lock();
|
---|
| 227 | }
|
---|
| 228 | private:
|
---|
| 229 | static std::weak_ptr<ThisType> global_instance;
|
---|
| 230 | };
|
---|
| 231 |
|
---|
[1f75e2d] | 232 | template< typename ThisType >
|
---|
| 233 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
|
---|
| 234 |
|
---|
[940bba3] | 235 | // -----------------------------------------------------------------------------
|
---|
[c8dfcd3] | 236 | // RAII object to regulate "save and restore" behaviour, e.g.
|
---|
| 237 | // void Foo::bar() {
|
---|
| 238 | // ValueGuard<int> guard(var); // var is a member of type Foo
|
---|
| 239 | // var = ...;
|
---|
| 240 | // } // var's original value is restored
|
---|
| 241 | template< typename T >
|
---|
| 242 | struct ValueGuard {
|
---|
| 243 | T old;
|
---|
| 244 | T& ref;
|
---|
| 245 |
|
---|
| 246 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
|
---|
| 247 | ~ValueGuard() { ref = old; }
|
---|
| 248 | };
|
---|
| 249 |
|
---|
[940bba3] | 250 | // -----------------------------------------------------------------------------
|
---|
| 251 | // Helper struct and function to support
|
---|
| 252 | // for ( val : reverseIterate( container ) ) {}
|
---|
| 253 | // syntax to have a for each that iterates backwards
|
---|
| 254 |
|
---|
[44f6341] | 255 | template< typename T >
|
---|
[1ba88a0] | 256 | struct reverseIterate_t {
|
---|
[44f6341] | 257 | T& ref;
|
---|
| 258 |
|
---|
[1ba88a0] | 259 | reverseIterate_t( T & ref ) : ref(ref) {}
|
---|
[44f6341] | 260 |
|
---|
| 261 | typedef typename T::reverse_iterator iterator;
|
---|
| 262 | iterator begin() { return ref.rbegin(); }
|
---|
| 263 | iterator end() { return ref.rend(); }
|
---|
| 264 | };
|
---|
| 265 |
|
---|
| 266 | template< typename T >
|
---|
[1ba88a0] | 267 | reverseIterate_t< T > reverseIterate( T & ref ) {
|
---|
| 268 | return reverseIterate_t< T >( ref );
|
---|
[44f6341] | 269 | }
|
---|
| 270 |
|
---|
[01aeade] | 271 | #endif // _UTILITY_H
|
---|
[51b73452] | 272 |
|
---|
[51587aa] | 273 | // Local Variables: //
|
---|
| 274 | // tab-width: 4 //
|
---|
| 275 | // mode: c++ //
|
---|
| 276 | // compile-command: "make install" //
|
---|
| 277 | // End: //
|
---|