source: src/Common/utility.h@ 1e8bbac9

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 1e8bbac9 was 2e30d47, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed gcc-7 build

  • Property mode set to 100644
File size: 12.0 KB
RevLine 
[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
[21f0aa8]11// Last Modified By : Andrew Beach
12// Last Modified On : Thr Aug 17 11:38:00 2017
13// Update Count : 34
[51587aa]14//
[01aeade]15
[6b0b624]16#pragma once
[51b73452]17
[e491159]18#include <cctype>
[940bba3]19#include <algorithm>
[2e30d47]20#include <functional>
[51b73452]21#include <iostream>
22#include <iterator>
23#include <list>
[e491159]24#include <memory>
25#include <sstream>
26#include <string>
[55a68c3]27#include <type_traits>
[51b73452]28
[294647b]29#include <cassert>
[be9288a]30
[50377a4]31#include "Common/Indenter.h"
32
[51b73452]33template< typename T >
[01aeade]34static inline T * maybeClone( const T *orig ) {
35 if ( orig ) {
36 return orig->clone();
37 } else {
38 return 0;
39 } // if
[51b73452]40}
41
[a7741435]42template< typename T, typename U >
[e04ef3a]43struct maybeBuild_t {
44 static T * doit( const U *orig ) {
45 if ( orig ) {
46 return orig->build();
47 } else {
48 return 0;
49 } // if
50 }
51};
52
[51b73452]53template< typename T, typename U >
[01aeade]54static inline T * maybeBuild( const U *orig ) {
[e04ef3a]55 return maybeBuild_t<T,U>::doit(orig);
[51b73452]56}
57
[7ecbb7e]58template< typename T, typename U >
59static inline T * maybeMoveBuild( const U *orig ) {
60 T* ret = maybeBuild<T>(orig);
61 delete orig;
62 return ret;
63}
64
[51b73452]65template< typename Input_iterator >
[01aeade]66void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
67 for ( Input_iterator i = begin; i != end; ++i ) {
68 os << name_array[ *i ] << ' ';
69 } // for
[51b73452]70}
71
72template< typename Container >
[01aeade]73void deleteAll( Container &container ) {
74 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
75 delete *i;
76 } // for
[51b73452]77}
78
79template< typename Container >
[50377a4]80void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
[01aeade]81 for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
82 if ( *i ) {
[50377a4]83 os << indent;
84 (*i)->print( os, indent );
[60089f4]85 // need an endl after each element because it's not easy to know when each individual item should end
[01aeade]86 os << std::endl;
87 } // if
88 } // for
[51b73452]89}
90
91template< typename SrcContainer, typename DestContainer >
[01aeade]92void cloneAll( const SrcContainer &src, DestContainer &dest ) {
93 typename SrcContainer::const_iterator in = src.begin();
94 std::back_insert_iterator< DestContainer > out( dest );
95 while ( in != src.end() ) {
96 *out++ = (*in++)->clone();
97 } // while
[51b73452]98}
99
100template< typename Container >
[01aeade]101void assertAll( const Container &container ) {
102 int count = 0;
103 for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
104 if ( !(*i) ) {
105 std::cerr << count << " is null" << std::endl;
106 } // if
107 } // for
108}
109
[51b73452]110template < typename T >
[01aeade]111std::list<T> tail( std::list<T> l ) {
112 if ( ! l.empty() ) {
113 std::list<T> ret(++(l.begin()), l.end());
114 return ret;
115 } // if
[51b73452]116}
117
118template < typename T >
119std::list<T> flatten( std::list < std::list<T> > l) {
[01aeade]120 typedef std::list <T> Ts;
[51b73452]121
[01aeade]122 Ts ret;
[51b73452]123
[a08ba92]124 switch ( l.size() ) {
[01aeade]125 case 0:
126 return ret;
127 case 1:
128 return l.front();
129 default:
130 ret = flatten(tail(l));
131 ret.insert(ret.begin(), l.front().begin(), l.front().end());
132 return ret;
133 } // switch
[51b73452]134}
135
[60089f4]136template < typename T >
[2298f728]137void toString_single( std::ostream & os, const T & value ) {
[79970ed]138 os << value;
139}
140
141template < typename T, typename... Params >
[2298f728]142void toString_single( std::ostream & os, const T & value, const Params & ... params ) {
[79970ed]143 os << value;
144 toString_single( os, params ... );
145}
146
147template < typename ... Params >
[2298f728]148std::string toString( const Params & ... params ) {
[5f2f2d7]149 std::ostringstream os;
[79970ed]150 toString_single( os, params... );
[5f2f2d7]151 return os.str();
[51b73452]152}
153
[3c13c03]154// replace element of list with all elements of another list
[51b73452]155template< typename T >
156void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
[01aeade]157 typename std::list< T >::iterator next = pos; advance( next, 1 );
[51b73452]158
[01aeade]159 //if ( next != org.end() ) {
[a08ba92]160 org.erase( pos );
161 org.splice( next, with );
162 //}
[51b73452]163
[01aeade]164 return;
[51b73452]165}
166
[3c13c03]167// replace range of a list with a single element
168template< typename T >
169void replace( std::list< T > &org, typename std::list< T >::iterator begin, typename std::list< T >::iterator end, const T & with ) {
170 org.insert( begin, with );
171 org.erase( begin, end );
172}
173
[940bba3]174template< typename... Args >
175auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
176 return std::copy_if(std::forward<Args>(args)...);
[51b73452]177}
178
[2bf9c37]179template <typename E, typename UnaryPredicate, template< typename, typename...> class Container, typename... Args >
180void filter( Container< E *, Args... > & container, UnaryPredicate pred, bool doDelete ) {
181 auto i = begin( container );
182 while ( i != end( container ) ) {
183 auto it = next( i );
184 if ( pred( *i ) ) {
185 if ( doDelete ) {
186 delete *i;
187 } // if
188 container.erase( i );
189 } // if
190 i = it;
191 } // while
192}
193
[940bba3]194template< typename... Args >
195auto zip(Args&&... args) -> decltype(zipWith(std::forward<Args>(args)..., std::make_pair)) {
196 return zipWith(std::forward<Args>(args)..., std::make_pair);
[51b73452]197}
198
199template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
200void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
[01aeade]201 while ( b1 != e1 && b2 != e2 )
202 *out++ = func(*b1++, *b2++);
[51b73452]203}
204
[d939274]205// it's nice to actually be able to increment iterators by an arbitrary amount
[940bba3]206template< class InputIt, class Distance >
207InputIt operator+( InputIt it, Distance n ) {
208 advance(it, n);
209 return it;
[d939274]210}
211
[79970ed]212template< typename T >
213void warn_single( const T & arg ) {
214 std::cerr << arg << std::endl;
215}
216
217template< typename T, typename... Params >
218void warn_single(const T & arg, const Params & ... params ) {
219 std::cerr << arg;
220 warn_single( params... );
221}
222
223template< typename... Params >
224void warn( const Params & ... params ) {
225 std::cerr << "Warning: ";
226 warn_single( params... );
227}
228
[bc3127d]229/// determines if `pref` is a prefix of `str`
230static inline bool isPrefix( const std::string & str, const std::string & pref ) {
231 if ( pref.size() > str.size() ) return false;
232 auto its = std::mismatch( pref.begin(), pref.end(), str.begin() );
233 return its.first == pref.end();
234}
235
[940bba3]236// -----------------------------------------------------------------------------
237// Ref Counted Singleton class
238// Objects that inherit from this class will have at most one reference to it
239// but if all references die, the object will be deleted.
240
[e491159]241template< typename ThisType >
242class RefCountSingleton {
243 public:
244 static std::shared_ptr<ThisType> get() {
245 if( global_instance.expired() ) {
246 std::shared_ptr<ThisType> new_instance = std::make_shared<ThisType>();
247 global_instance = new_instance;
248 return std::move(new_instance);
249 }
250 return global_instance.lock();
251 }
252 private:
253 static std::weak_ptr<ThisType> global_instance;
254};
255
[1f75e2d]256template< typename ThisType >
257std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
258
[940bba3]259// -----------------------------------------------------------------------------
[c8dfcd3]260// RAII object to regulate "save and restore" behaviour, e.g.
261// void Foo::bar() {
262// ValueGuard<int> guard(var); // var is a member of type Foo
263// var = ...;
264// } // var's original value is restored
265template< typename T >
266struct ValueGuard {
267 T old;
268 T& ref;
269
270 ValueGuard(T& inRef) : old(inRef), ref(inRef) {}
271 ~ValueGuard() { ref = old; }
272};
273
[134322e]274template< typename T >
275struct ValueGuardPtr {
276 T old;
277 T* ref;
278
279 ValueGuardPtr(T * inRef) : old( inRef ? *inRef : T() ), ref(inRef) {}
280 ~ValueGuardPtr() { if( ref ) *ref = old; }
281};
282
[234223f]283template< typename aT >
284struct FuncGuard {
285 aT m_after;
286
287 template< typename bT >
288 FuncGuard( bT before, aT after ) : m_after( after ) {
289 before();
290 }
291
292 ~FuncGuard() {
293 m_after();
294 }
295};
296
297template< typename bT, typename aT >
298FuncGuard<aT> makeFuncGuard( bT && before, aT && after ) {
299 return FuncGuard<aT>( std::forward<bT>(before), std::forward<aT>(after) );
300}
301
[134322e]302template< typename T >
303struct ValueGuardPtr< std::list< T > > {
304 std::list< T > old;
305 std::list< T >* ref;
306
307 ValueGuardPtr( std::list< T > * inRef) : old(), ref(inRef) {
308 if( ref ) { swap( *ref, old ); }
309 }
310 ~ValueGuardPtr() { if( ref ) { swap( *ref, old ); } }
311};
312
[940bba3]313// -----------------------------------------------------------------------------
314// Helper struct and function to support
315// for ( val : reverseIterate( container ) ) {}
316// syntax to have a for each that iterates backwards
317
[44f6341]318template< typename T >
[4a9ccc3]319struct reverse_iterate_t {
[44f6341]320 T& ref;
321
[4a9ccc3]322 reverse_iterate_t( T & ref ) : ref(ref) {}
[44f6341]323
324 typedef typename T::reverse_iterator iterator;
325 iterator begin() { return ref.rbegin(); }
326 iterator end() { return ref.rend(); }
327};
328
329template< typename T >
[4a9ccc3]330reverse_iterate_t< T > reverseIterate( T & ref ) {
331 return reverse_iterate_t< T >( ref );
[44f6341]332}
333
[3ad7978]334template< typename OutType, typename Range, typename Functor >
335OutType map_range( const Range& range, Functor&& functor ) {
336 OutType out;
337
338 std::transform(
339 begin( range ),
340 end( range ),
341 std::back_inserter( out ),
342 std::forward< Functor >( functor )
343 );
344
345 return out;
346}
347
[4a9ccc3]348// -----------------------------------------------------------------------------
349// Helper struct and function to support
350// for ( val : group_iterate( container1, container2, ... ) ) {}
351// syntax to have a for each that iterates multiple containers of the same length
[55a68c3]352// TODO: update to use variadic arguments
[4a9ccc3]353
354template< typename T1, typename T2 >
355struct group_iterate_t {
[50377a4]356private:
357 std::tuple<T1, T2> args;
358public:
[6d267ca]359 group_iterate_t( bool skipBoundsCheck, const T1 & v1, const T2 & v2 ) : args(v1, v2) {
[3c09d47]360 assertf(skipBoundsCheck || v1.size() == v2.size(), "group iteration requires containers of the same size: <%zd, %zd>.", v1.size(), v2.size());
[4a9ccc3]361 };
362
[50377a4]363 typedef std::tuple<decltype(*std::get<0>(args).begin()), decltype(*std::get<1>(args).begin())> value_type;
364 typedef decltype(std::get<0>(args).begin()) T1Iter;
365 typedef decltype(std::get<1>(args).begin()) T2Iter;
366
[4a9ccc3]367 struct iterator {
368 typedef std::tuple<T1Iter, T2Iter> IterTuple;
369 IterTuple it;
370 iterator( T1Iter i1, T2Iter i2 ) : it( i1, i2 ) {}
371 iterator operator++() {
372 return iterator( ++std::get<0>(it), ++std::get<1>(it) );
373 }
374 bool operator!=( const iterator &other ) const { return it != other.it; }
[55a68c3]375 value_type operator*() const { return std::tie( *std::get<0>(it), *std::get<1>(it) ); }
[4a9ccc3]376 };
[50377a4]377
[4a9ccc3]378 iterator begin() { return iterator( std::get<0>(args).begin(), std::get<1>(args).begin() ); }
379 iterator end() { return iterator( std::get<0>(args).end(), std::get<1>(args).end() ); }
380};
381
[6d267ca]382/// performs bounds check to ensure that all arguments are of the same length.
[4a9ccc3]383template< typename... Args >
[55a68c3]384group_iterate_t<Args...> group_iterate( Args &&... args ) {
[6d267ca]385 return group_iterate_t<Args...>(false, std::forward<Args>( args )...);
386}
387
388/// does not perform a bounds check - requires user to ensure that iteration terminates when appropriate.
389template< typename... Args >
390group_iterate_t<Args...> unsafe_group_iterate( Args &&... args ) {
391 return group_iterate_t<Args...>(true, std::forward<Args>( args )...);
[4a9ccc3]392}
[294647b]393
[108f3cdb]394// -----------------------------------------------------------------------------
395// Helper struct and function to support
396// for ( val : lazy_map( container1, f ) ) {}
397// syntax to have a for each that iterates a container, mapping each element by applying f
398template< typename T, typename Func >
399struct lambda_iterate_t {
[4639b0d]400 const T & ref;
401 std::function<Func> f;
[108f3cdb]402
403 struct iterator {
404 typedef decltype(begin(ref)) Iter;
405 Iter it;
[4639b0d]406 std::function<Func> f;
407 iterator( Iter it, std::function<Func> f ) : it(it), f(f) {}
[108f3cdb]408 iterator & operator++() {
409 ++it; return *this;
410 }
411 bool operator!=( const iterator &other ) const { return it != other.it; }
412 auto operator*() const -> decltype(f(*it)) { return f(*it); }
413 };
414
[4639b0d]415 lambda_iterate_t( const T & ref, std::function<Func> f ) : ref(ref), f(f) {}
[108f3cdb]416
417 auto begin() const -> decltype(iterator(std::begin(ref), f)) { return iterator(std::begin(ref), f); }
418 auto end() const -> decltype(iterator(std::end(ref), f)) { return iterator(std::end(ref), f); }
419};
420
421template< typename... Args >
[4639b0d]422lambda_iterate_t<Args...> lazy_map( const Args &... args ) {
423 return lambda_iterate_t<Args...>( args...);
[108f3cdb]424}
425
426
427
[51587aa]428// Local Variables: //
429// tab-width: 4 //
430// mode: c++ //
431// compile-command: "make install" //
432// End: //
Note: See TracBrowser for help on using the repository browser.