| [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 <string> | 
|---|
| [55a68c3] | 25 | #include <type_traits> | 
|---|
| [234b1cb] | 26 | #include <vector> | 
|---|
| [8abca06] | 27 | #include <cstring>                                                                              // memcmp | 
|---|
| [be9288a] | 28 |  | 
|---|
| [50377a4] | 29 | #include "Common/Indenter.h" | 
|---|
|  | 30 |  | 
|---|
| [5cbacf1] | 31 | class Expression; | 
|---|
|  | 32 |  | 
|---|
| [c1ed2ee] | 33 | /// bring std::move into global scope | 
|---|
|  | 34 | using std::move; | 
|---|
|  | 35 |  | 
|---|
|  | 36 | /// partner to move that copies any copyable type | 
|---|
|  | 37 | template<typename T> | 
|---|
|  | 38 | T copy( const T & x ) { return x; } | 
|---|
|  | 39 |  | 
|---|
| [51b73452] | 40 | template< typename T > | 
|---|
| [01aeade] | 41 | static inline T * maybeClone( const T *orig ) { | 
|---|
|  | 42 | if ( orig ) { | 
|---|
|  | 43 | return orig->clone(); | 
|---|
|  | 44 | } else { | 
|---|
|  | 45 | return 0; | 
|---|
|  | 46 | } // if | 
|---|
| [51b73452] | 47 | } | 
|---|
|  | 48 |  | 
|---|
|  | 49 | template< typename Input_iterator > | 
|---|
| [01aeade] | 50 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) { | 
|---|
|  | 51 | for ( Input_iterator i = begin; i != end; ++i ) { | 
|---|
|  | 52 | os << name_array[ *i ] << ' '; | 
|---|
|  | 53 | } // for | 
|---|
| [51b73452] | 54 | } | 
|---|
|  | 55 |  | 
|---|
|  | 56 | template< typename Container > | 
|---|
| [8abee136] | 57 | void deleteAll( const Container &container ) { | 
|---|
|  | 58 | for ( const auto &i : container ) { | 
|---|
|  | 59 | delete i; | 
|---|
| [01aeade] | 60 | } // for | 
|---|
| [51b73452] | 61 | } | 
|---|
|  | 62 |  | 
|---|
|  | 63 | template< typename Container > | 
|---|
| [50377a4] | 64 | void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) { | 
|---|
| [01aeade] | 65 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
|  | 66 | if ( *i ) { | 
|---|
| [50377a4] | 67 | os << indent; | 
|---|
|  | 68 | (*i)->print( os, indent ); | 
|---|
| [60089f4] | 69 | // need an endl after each element because it's not easy to know when each individual item should end | 
|---|
| [01aeade] | 70 | os << std::endl; | 
|---|
|  | 71 | } // if | 
|---|
|  | 72 | } // for | 
|---|
| [51b73452] | 73 | } | 
|---|
|  | 74 |  | 
|---|
|  | 75 | template< typename SrcContainer, typename DestContainer > | 
|---|
| [01aeade] | 76 | void cloneAll( const SrcContainer &src, DestContainer &dest ) { | 
|---|
|  | 77 | typename SrcContainer::const_iterator in = src.begin(); | 
|---|
|  | 78 | std::back_insert_iterator< DestContainer > out( dest ); | 
|---|
|  | 79 | while ( in != src.end() ) { | 
|---|
|  | 80 | *out++ = (*in++)->clone(); | 
|---|
|  | 81 | } // while | 
|---|
| [51b73452] | 82 | } | 
|---|
|  | 83 |  | 
|---|
| [54c9000] | 84 | template< typename SrcContainer, typename DestContainer, typename Predicate > | 
|---|
|  | 85 | void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) { | 
|---|
|  | 86 | std::back_insert_iterator< DestContainer > out( dest ); | 
|---|
|  | 87 | for ( auto x : src ) { | 
|---|
|  | 88 | if ( pred(x) ) { | 
|---|
|  | 89 | *out++ = x->clone(); | 
|---|
|  | 90 | } | 
|---|
|  | 91 | } // while | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [51b73452] | 94 | template< typename Container > | 
|---|
| [01aeade] | 95 | void assertAll( const Container &container ) { | 
|---|
|  | 96 | int count = 0; | 
|---|
|  | 97 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) { | 
|---|
|  | 98 | if ( !(*i) ) { | 
|---|
|  | 99 | std::cerr << count << " is null" << std::endl; | 
|---|
|  | 100 | } // if | 
|---|
|  | 101 | } // for | 
|---|
|  | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [51b73452] | 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 |  | 
|---|
| [234b1cb] | 130 | /// Splice src onto the end of dst, clearing src | 
|---|
|  | 131 | template< typename T > | 
|---|
|  | 132 | void splice( std::vector< T > & dst, std::vector< T > & src ) { | 
|---|
|  | 133 | dst.reserve( dst.size() + src.size() ); | 
|---|
|  | 134 | for ( T & x : src ) { dst.emplace_back( std::move( x ) ); } | 
|---|
|  | 135 | src.clear(); | 
|---|
|  | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | /// Splice src onto the begining of dst, clearing src | 
|---|
|  | 139 | template< typename T > | 
|---|
|  | 140 | void spliceBegin( std::vector< T > & dst, std::vector< T > & src ) { | 
|---|
|  | 141 | splice( src, dst ); | 
|---|
|  | 142 | dst.swap( src ); | 
|---|
|  | 143 | } | 
|---|
|  | 144 |  | 
|---|
| [940bba3] | 145 | template< typename... Args > | 
|---|
|  | 146 | auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) { | 
|---|
|  | 147 | return std::copy_if(std::forward<Args>(args)...); | 
|---|
| [51b73452] | 148 | } | 
|---|
|  | 149 |  | 
|---|
| [2bf9c37] | 150 | template <typename E, typename UnaryPredicate, template< typename, typename...> class Container, typename... Args > | 
|---|
|  | 151 | void filter( Container< E *, Args... > & container, UnaryPredicate pred, bool doDelete ) { | 
|---|
|  | 152 | auto i = begin( container ); | 
|---|
|  | 153 | while ( i != end( container ) ) { | 
|---|
|  | 154 | auto it = next( i ); | 
|---|
|  | 155 | if ( pred( *i ) ) { | 
|---|
|  | 156 | if ( doDelete ) { | 
|---|
|  | 157 | delete *i; | 
|---|
|  | 158 | } // if | 
|---|
|  | 159 | container.erase( i ); | 
|---|
|  | 160 | } // if | 
|---|
|  | 161 | i = it; | 
|---|
|  | 162 | } // while | 
|---|
|  | 163 | } | 
|---|
|  | 164 |  | 
|---|
| [298fe57] | 165 | template<typename Container, typename Pred> | 
|---|
|  | 166 | void erase_if( Container & cont, Pred && pred ) { | 
|---|
|  | 167 | auto keep_end = std::remove_if( cont.begin(), cont.end(), pred ); | 
|---|
|  | 168 | cont.erase( keep_end, cont.end() ); | 
|---|
|  | 169 | } | 
|---|
|  | 170 |  | 
|---|
| [8abca06] | 171 | // determines if pref is a prefix of str | 
|---|
|  | 172 | static inline bool isPrefix( const std::string & str, const std::string & pref, unsigned int start = 0 ) { | 
|---|
| [bc3127d] | 173 | if ( pref.size() > str.size() ) return false; | 
|---|
| [8f06277] | 174 | return pref == str.substr(start, pref.size()); | 
|---|
| [bc3127d] | 175 | } | 
|---|
|  | 176 |  | 
|---|
| [940bba3] | 177 | // ----------------------------------------------------------------------------- | 
|---|
| [c8dfcd3] | 178 | // RAII object to regulate "save and restore" behaviour, e.g. | 
|---|
|  | 179 | // void Foo::bar() { | 
|---|
|  | 180 | //   ValueGuard<int> guard(var); // var is a member of type Foo | 
|---|
|  | 181 | //   var = ...; | 
|---|
|  | 182 | // } // var's original value is restored | 
|---|
|  | 183 | template< typename T > | 
|---|
|  | 184 | struct ValueGuard { | 
|---|
|  | 185 | T old; | 
|---|
|  | 186 | T& ref; | 
|---|
|  | 187 |  | 
|---|
|  | 188 | ValueGuard(T& inRef) : old(inRef), ref(inRef) {} | 
|---|
|  | 189 | ~ValueGuard() { ref = old; } | 
|---|
|  | 190 | }; | 
|---|
|  | 191 |  | 
|---|
| [134322e] | 192 | template< typename T > | 
|---|
|  | 193 | struct ValueGuardPtr { | 
|---|
|  | 194 | T old; | 
|---|
|  | 195 | T* ref; | 
|---|
|  | 196 |  | 
|---|
|  | 197 | ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {} | 
|---|
| [1b65595] | 198 | ValueGuardPtr(const ValueGuardPtr& other) = delete; | 
|---|
|  | 199 | ValueGuardPtr(ValueGuardPtr&& other) : old(other.old), ref(other.ref) { other.ref = nullptr; } | 
|---|
| [134322e] | 200 | ~ValueGuardPtr() { if( ref ) *ref = old; } | 
|---|
|  | 201 | }; | 
|---|
|  | 202 |  | 
|---|
| [234223f] | 203 | template< typename aT > | 
|---|
|  | 204 | struct FuncGuard { | 
|---|
|  | 205 | aT m_after; | 
|---|
|  | 206 |  | 
|---|
|  | 207 | template< typename bT > | 
|---|
|  | 208 | FuncGuard( bT before, aT after ) : m_after( after ) { | 
|---|
|  | 209 | before(); | 
|---|
|  | 210 | } | 
|---|
|  | 211 |  | 
|---|
|  | 212 | ~FuncGuard() { | 
|---|
|  | 213 | m_after(); | 
|---|
|  | 214 | } | 
|---|
|  | 215 | }; | 
|---|
|  | 216 |  | 
|---|
|  | 217 | template< typename bT, typename aT > | 
|---|
|  | 218 | FuncGuard<aT> makeFuncGuard( bT && before, aT && after ) { | 
|---|
|  | 219 | return FuncGuard<aT>( std::forward<bT>(before), std::forward<aT>(after) ); | 
|---|
|  | 220 | } | 
|---|
|  | 221 |  | 
|---|
| [134322e] | 222 | template< typename T > | 
|---|
|  | 223 | struct ValueGuardPtr< std::list< T > > { | 
|---|
|  | 224 | std::list< T > old; | 
|---|
|  | 225 | std::list< T >* ref; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) { | 
|---|
|  | 228 | if( ref ) { swap( *ref, old ); } | 
|---|
|  | 229 | } | 
|---|
|  | 230 | ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } } | 
|---|
|  | 231 | }; | 
|---|
|  | 232 |  | 
|---|
| [9dc31c10] | 233 | // ----------------------------------------------------------------------------- | 
|---|
| [f14d956] | 234 | // O(1) polymorphic integer ilog2, using clz, which returns the number of leading 0-bits, starting at the most | 
|---|
| [9dc31c10] | 235 | // significant bit (single instruction on x86) | 
|---|
|  | 236 |  | 
|---|
|  | 237 | template<typename T> | 
|---|
| [d28a03e] | 238 | inline | 
|---|
| [b6d7f44] | 239 | #if defined(__GNUC__) && __GNUC__ > 4 | 
|---|
| [d28a03e] | 240 | constexpr | 
|---|
|  | 241 | #endif | 
|---|
|  | 242 | T ilog2(const T & t) { | 
|---|
|  | 243 | if(std::is_integral<T>::value) { | 
|---|
| [9dc31c10] | 244 | const constexpr int r = sizeof(t) * __CHAR_BIT__ - 1; | 
|---|
| [d28a03e] | 245 | if( sizeof(T) == sizeof(unsigned       int) ) return r - __builtin_clz  ( t ); | 
|---|
|  | 246 | if( sizeof(T) == sizeof(unsigned      long) ) return r - __builtin_clzl ( t ); | 
|---|
|  | 247 | if( sizeof(T) == sizeof(unsigned long long) ) return r - __builtin_clzll( t ); | 
|---|
|  | 248 | } | 
|---|
|  | 249 | assert(false); | 
|---|
| [9dc31c10] | 250 | return -1; | 
|---|
| [01b8ccf1] | 251 | } // ilog2 | 
|---|
| [108f3cdb] | 252 |  | 
|---|
| [51587aa] | 253 | // Local Variables: // | 
|---|
|  | 254 | // tab-width: 4 // | 
|---|
|  | 255 | // mode: c++ // | 
|---|
|  | 256 | // compile-command: "make install" // | 
|---|
|  | 257 | // End: // | 
|---|