[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 | //
|
---|
[8f06277] | 7 | // utility.h -- General utilities used across the compiler.
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[298fe57] | 11 | // Last Modified By : Andrew Beach
|
---|
[8f06277] | 12 | // Last Modified On : Fri Feb 17 15:25:00 2023
|
---|
| 13 | // Update Count : 53
|
---|
[51587aa] | 14 | //
|
---|
[01aeade] | 15 |
|
---|
[6b0b624] | 16 | #pragma once
|
---|
[51b73452] | 17 |
|
---|
[234b1cb] | 18 | #include <cassert>
|
---|
[e491159] | 19 | #include <cctype>
|
---|
[940bba3] | 20 | #include <algorithm>
|
---|
[51b73452] | 21 | #include <iostream>
|
---|
| 22 | #include <list>
|
---|
[e491159] | 23 | #include <memory>
|
---|
| 24 | #include <sstream>
|
---|
| 25 | #include <string>
|
---|
[55a68c3] | 26 | #include <type_traits>
|
---|
[234b1cb] | 27 | #include <vector>
|
---|
[8abca06] | 28 | #include <cstring> // memcmp
|
---|
[be9288a] | 29 |
|
---|
[50377a4] | 30 | #include "Common/Indenter.h"
|
---|
| 31 |
|
---|
[5cbacf1] | 32 | class Expression;
|
---|
| 33 |
|
---|
[c1ed2ee] | 34 | /// bring std::move into global scope
|
---|
| 35 | using std::move;
|
---|
| 36 |
|
---|
| 37 | /// partner to move that copies any copyable type
|
---|
| 38 | template<typename T>
|
---|
| 39 | T copy( const T & x ) { return x; }
|
---|
| 40 |
|
---|
[51b73452] | 41 | template< typename T >
|
---|
[01aeade] | 42 | static inline T * maybeClone( const T *orig ) {
|
---|
| 43 | if ( orig ) {
|
---|
| 44 | return orig->clone();
|
---|
| 45 | } else {
|
---|
| 46 | return 0;
|
---|
| 47 | } // if
|
---|
[51b73452] | 48 | }
|
---|
| 49 |
|
---|
| 50 | template< typename Input_iterator >
|
---|
[01aeade] | 51 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
|
---|
| 52 | for ( Input_iterator i = begin; i != end; ++i ) {
|
---|
| 53 | os << name_array[ *i ] << ' ';
|
---|
| 54 | } // for
|
---|
[51b73452] | 55 | }
|
---|
| 56 |
|
---|
| 57 | template< typename Container >
|
---|
[8abee136] | 58 | void deleteAll( const Container &container ) {
|
---|
| 59 | for ( const auto &i : container ) {
|
---|
| 60 | delete i;
|
---|
[01aeade] | 61 | } // for
|
---|
[51b73452] | 62 | }
|
---|
| 63 |
|
---|
| 64 | template< typename Container >
|
---|
[50377a4] | 65 | void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
|
---|
[01aeade] | 66 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
| 67 | if ( *i ) {
|
---|
[50377a4] | 68 | os << indent;
|
---|
| 69 | (*i)->print( os, indent );
|
---|
[60089f4] | 70 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
[01aeade] | 71 | os << std::endl;
|
---|
| 72 | } // if
|
---|
| 73 | } // for
|
---|
[51b73452] | 74 | }
|
---|
| 75 |
|
---|
| 76 | template< typename SrcContainer, typename DestContainer >
|
---|
[01aeade] | 77 | void cloneAll( const SrcContainer &src, DestContainer &dest ) {
|
---|
| 78 | typename SrcContainer::const_iterator in = src.begin();
|
---|
| 79 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
| 80 | while ( in != src.end() ) {
|
---|
| 81 | *out++ = (*in++)->clone();
|
---|
| 82 | } // while
|
---|
[51b73452] | 83 | }
|
---|
| 84 |
|
---|
[54c9000] | 85 | template< typename SrcContainer, typename DestContainer, typename Predicate >
|
---|
| 86 | void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) {
|
---|
| 87 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
| 88 | for ( auto x : src ) {
|
---|
| 89 | if ( pred(x) ) {
|
---|
| 90 | *out++ = x->clone();
|
---|
| 91 | }
|
---|
| 92 | } // while
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[51b73452] | 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 |
|
---|
[51b73452] | 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
|
---|
[51b73452] | 111 | }
|
---|
| 112 |
|
---|
| 113 | template < typename T >
|
---|
| 114 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
[01aeade] | 115 | typedef std::list <T> Ts;
|
---|
[51b73452] | 116 |
|
---|
[01aeade] | 117 | Ts ret;
|
---|
[51b73452] | 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
|
---|
[51b73452] | 129 | }
|
---|
| 130 |
|
---|
[234b1cb] | 131 | /// Splice src onto the end of dst, clearing src
|
---|
| 132 | template< typename T >
|
---|
| 133 | void splice( std::vector< T > & dst, std::vector< T > & src ) {
|
---|
| 134 | dst.reserve( dst.size() + src.size() );
|
---|
| 135 | for ( T & x : src ) { dst.emplace_back( std::move( x ) ); }
|
---|
| 136 | src.clear();
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /// Splice src onto the begining of dst, clearing src
|
---|
| 140 | template< typename T >
|
---|
| 141 | void spliceBegin( std::vector< T > & dst, std::vector< T > & src ) {
|
---|
| 142 | splice( src, dst );
|
---|
| 143 | dst.swap( src );
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[60089f4] | 146 | template < typename T >
|
---|
[2298f728] | 147 | void toString_single( std::ostream & os, const T & value ) {
|
---|
[79970ed] | 148 | os << value;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | template < typename T, typename... Params >
|
---|
[2298f728] | 152 | void toString_single( std::ostream & os, const T & value, const Params & ... params ) {
|
---|
[79970ed] | 153 | os << value;
|
---|
| 154 | toString_single( os, params ... );
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | template < typename ... Params >
|
---|
[2298f728] | 158 | std::string toString( const Params & ... params ) {
|
---|
[5f2f2d7] | 159 | std::ostringstream os;
|
---|
[79970ed] | 160 | toString_single( os, params... );
|
---|
[5f2f2d7] | 161 | return os.str();
|
---|
[51b73452] | 162 | }
|
---|
| 163 |
|
---|
[babeeda] | 164 | #define toCString( ... ) toString( __VA_ARGS__ ).c_str()
|
---|
| 165 |
|
---|
[940bba3] | 166 | template< typename... Args >
|
---|
| 167 | auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
|
---|
| 168 | return std::copy_if(std::forward<Args>(args)...);
|
---|
[51b73452] | 169 | }
|
---|
| 170 |
|
---|
[2bf9c37] | 171 | template <typename E, typename UnaryPredicate, template< typename, typename...> class Container, typename... Args >
|
---|
| 172 | void filter( Container< E *, Args... > & container, UnaryPredicate pred, bool doDelete ) {
|
---|
| 173 | auto i = begin( container );
|
---|
| 174 | while ( i != end( container ) ) {
|
---|
| 175 | auto it = next( i );
|
---|
| 176 | if ( pred( *i ) ) {
|
---|
| 177 | if ( doDelete ) {
|
---|
| 178 | delete *i;
|
---|
| 179 | } // if
|
---|
| 180 | container.erase( i );
|
---|
| 181 | } // if
|
---|
| 182 | i = it;
|
---|
| 183 | } // while
|
---|
| 184 | }
|
---|
| 185 |
|
---|
[298fe57] | 186 | template<typename Container, typename Pred>
|
---|
| 187 | void erase_if( Container & cont, Pred && pred ) {
|
---|
| 188 | auto keep_end = std::remove_if( cont.begin(), cont.end(), pred );
|
---|
| 189 | cont.erase( keep_end, cont.end() );
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[79970ed] | 192 | template< typename... Params >
|
---|
| 193 | void warn( const Params & ... params ) {
|
---|
| 194 | std::cerr << "Warning: ";
|
---|
[8f06277] | 195 | toString_single( std::cerr, params... );
|
---|
| 196 | std::cerr << std::endl;
|
---|
[79970ed] | 197 | }
|
---|
| 198 |
|
---|
[8abca06] | 199 | // determines if pref is a prefix of str
|
---|
| 200 | static inline bool isPrefix( const std::string & str, const std::string & pref, unsigned int start = 0 ) {
|
---|
[bc3127d] | 201 | if ( pref.size() > str.size() ) return false;
|
---|
[8f06277] | 202 | return pref == str.substr(start, pref.size());
|
---|
[bc3127d] | 203 | }
|
---|
| 204 |
|
---|
[940bba3] | 205 | // -----------------------------------------------------------------------------
|
---|
[c8dfcd3] | 206 | // RAII object to regulate "save and restore" behaviour, e.g.
|
---|
| 207 | // void Foo::bar() {
|
---|
| 208 | // ValueGuard<int> guard(var); // var is a member of type Foo
|
---|
| 209 | // var = ...;
|
---|
| 210 | // } // var's original value is restored
|
---|
| 211 | template< typename T >
|
---|
| 212 | struct ValueGuard {
|
---|
| 213 | T old;
|
---|
| 214 | T& ref;
|
---|
| 215 |
|
---|
| 216 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
|
---|
| 217 | ~ValueGuard() { ref = old; }
|
---|
| 218 | };
|
---|
| 219 |
|
---|
[134322e] | 220 | template< typename T >
|
---|
| 221 | struct ValueGuardPtr {
|
---|
| 222 | T old;
|
---|
| 223 | T* ref;
|
---|
| 224 |
|
---|
| 225 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {}
|
---|
[1b65595] | 226 | ValueGuardPtr(const ValueGuardPtr& other) = delete;
|
---|
| 227 | ValueGuardPtr(ValueGuardPtr&& other) : old(other.old), ref(other.ref) { other.ref = nullptr; }
|
---|
[134322e] | 228 | ~ValueGuardPtr() { if( ref ) *ref = old; }
|
---|
| 229 | };
|
---|
| 230 |
|
---|
[234223f] | 231 | template< typename aT >
|
---|
| 232 | struct FuncGuard {
|
---|
| 233 | aT m_after;
|
---|
| 234 |
|
---|
| 235 | template< typename bT >
|
---|
| 236 | FuncGuard( bT before, aT after ) : m_after( after ) {
|
---|
| 237 | before();
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | ~FuncGuard() {
|
---|
| 241 | m_after();
|
---|
| 242 | }
|
---|
| 243 | };
|
---|
| 244 |
|
---|
| 245 | template< typename bT, typename aT >
|
---|
| 246 | FuncGuard<aT> makeFuncGuard( bT && before, aT && after ) {
|
---|
| 247 | return FuncGuard<aT>( std::forward<bT>(before), std::forward<aT>(after) );
|
---|
| 248 | }
|
---|
| 249 |
|
---|
[134322e] | 250 | template< typename T >
|
---|
| 251 | struct ValueGuardPtr< std::list< T > > {
|
---|
| 252 | std::list< T > old;
|
---|
| 253 | std::list< T >* ref;
|
---|
| 254 |
|
---|
| 255 | ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) {
|
---|
| 256 | if( ref ) { swap( *ref, old ); }
|
---|
| 257 | }
|
---|
| 258 | ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } }
|
---|
| 259 | };
|
---|
| 260 |
|
---|
[9dc31c10] | 261 | // -----------------------------------------------------------------------------
|
---|
[f14d956] | 262 | // O(1) polymorphic integer ilog2, using clz, which returns the number of leading 0-bits, starting at the most
|
---|
[9dc31c10] | 263 | // significant bit (single instruction on x86)
|
---|
| 264 |
|
---|
| 265 | template<typename T>
|
---|
[d28a03e] | 266 | inline
|
---|
[b6d7f44] | 267 | #if defined(__GNUC__) && __GNUC__ > 4
|
---|
[d28a03e] | 268 | constexpr
|
---|
| 269 | #endif
|
---|
| 270 | T ilog2(const T & t) {
|
---|
| 271 | if(std::is_integral<T>::value) {
|
---|
[9dc31c10] | 272 | const constexpr int r = sizeof(t) * __CHAR_BIT__ - 1;
|
---|
[d28a03e] | 273 | if( sizeof(T) == sizeof(unsigned int) ) return r - __builtin_clz ( t );
|
---|
| 274 | if( sizeof(T) == sizeof(unsigned long) ) return r - __builtin_clzl ( t );
|
---|
| 275 | if( sizeof(T) == sizeof(unsigned long long) ) return r - __builtin_clzll( t );
|
---|
| 276 | }
|
---|
| 277 | assert(false);
|
---|
[9dc31c10] | 278 | return -1;
|
---|
[01b8ccf1] | 279 | } // ilog2
|
---|
[108f3cdb] | 280 |
|
---|
[51587aa] | 281 | // Local Variables: //
|
---|
| 282 | // tab-width: 4 //
|
---|
| 283 | // mode: c++ //
|
---|
| 284 | // compile-command: "make install" //
|
---|
| 285 | // End: //
|
---|