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 | // utility.h --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Mon May 18 07:44:20 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Wed Jun 8 17:33:59 2016
|
---|
13 | // Update Count : 22
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef _UTILITY_H
|
---|
17 | #define _UTILITY_H
|
---|
18 |
|
---|
19 | #include <cctype>
|
---|
20 | #include <algorithm>
|
---|
21 | #include <iostream>
|
---|
22 | #include <iterator>
|
---|
23 | #include <list>
|
---|
24 | #include <memory>
|
---|
25 | #include <sstream>
|
---|
26 | #include <string>
|
---|
27 |
|
---|
28 | template< typename T >
|
---|
29 | static inline T * maybeClone( const T *orig ) {
|
---|
30 | if ( orig ) {
|
---|
31 | return orig->clone();
|
---|
32 | } else {
|
---|
33 | return 0;
|
---|
34 | } // if
|
---|
35 | }
|
---|
36 |
|
---|
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 |
|
---|
48 | template< typename T, typename U >
|
---|
49 | static inline T * maybeBuild( const U *orig ) {
|
---|
50 | return maybeBuild_t<T,U>::doit(orig);
|
---|
51 | }
|
---|
52 |
|
---|
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 |
|
---|
60 | template< typename Input_iterator >
|
---|
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
|
---|
65 | }
|
---|
66 |
|
---|
67 | template< typename Container >
|
---|
68 | void deleteAll( Container &container ) {
|
---|
69 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
70 | delete *i;
|
---|
71 | } // for
|
---|
72 | }
|
---|
73 |
|
---|
74 | template< typename Container >
|
---|
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 ) {
|
---|
78 | os << std::string( indent, ' ' );
|
---|
79 | (*i)->print( os, indent + 2 );
|
---|
80 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
81 | os << std::endl;
|
---|
82 | } // if
|
---|
83 | } // for
|
---|
84 | }
|
---|
85 |
|
---|
86 | template< typename SrcContainer, typename DestContainer >
|
---|
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
|
---|
93 | }
|
---|
94 |
|
---|
95 | template< typename Container >
|
---|
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 |
|
---|
105 | static inline std::string assign_strptr( const std::string *str ) {
|
---|
106 | if ( str == 0 ) {
|
---|
107 | return "";
|
---|
108 | } else {
|
---|
109 | std::string tmp;
|
---|
110 | tmp = *str;
|
---|
111 | delete str;
|
---|
112 | return tmp;
|
---|
113 | } // if
|
---|
114 | }
|
---|
115 |
|
---|
116 | template < typename T >
|
---|
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
|
---|
122 | }
|
---|
123 |
|
---|
124 | template < typename T >
|
---|
125 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
126 | typedef std::list <T> Ts;
|
---|
127 |
|
---|
128 | Ts ret;
|
---|
129 |
|
---|
130 | switch ( l.size() ) {
|
---|
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
|
---|
140 | }
|
---|
141 |
|
---|
142 | template < typename T >
|
---|
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 ) {
|
---|
155 | std::ostringstream os;
|
---|
156 | toString_single( os, params... );
|
---|
157 | return os.str();
|
---|
158 | }
|
---|
159 |
|
---|
160 | template< typename T >
|
---|
161 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
---|
162 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
---|
163 |
|
---|
164 | //if ( next != org.end() ) {
|
---|
165 | org.erase( pos );
|
---|
166 | org.splice( next, with );
|
---|
167 | //}
|
---|
168 |
|
---|
169 | return;
|
---|
170 | }
|
---|
171 |
|
---|
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)...);
|
---|
175 | }
|
---|
176 |
|
---|
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);
|
---|
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 ) {
|
---|
184 | while ( b1 != e1 && b2 != e2 )
|
---|
185 | *out++ = func(*b1++, *b2++);
|
---|
186 | }
|
---|
187 |
|
---|
188 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
---|
189 | template< class InputIt, class Distance >
|
---|
190 | InputIt operator+( InputIt it, Distance n ) {
|
---|
191 | advance(it, n);
|
---|
192 | return it;
|
---|
193 | }
|
---|
194 |
|
---|
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 |
|
---|
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 |
|
---|
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 |
|
---|
232 | template< typename ThisType >
|
---|
233 | std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
|
---|
234 |
|
---|
235 | // -----------------------------------------------------------------------------
|
---|
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 |
|
---|
250 | // -----------------------------------------------------------------------------
|
---|
251 | // Helper struct and function to support
|
---|
252 | // for ( val : reverseIterate( container ) ) {}
|
---|
253 | // syntax to have a for each that iterates backwards
|
---|
254 |
|
---|
255 | template< typename T >
|
---|
256 | struct reverseIterate_t {
|
---|
257 | T& ref;
|
---|
258 |
|
---|
259 | reverseIterate_t( T & ref ) : ref(ref) {}
|
---|
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 >
|
---|
267 | reverseIterate_t< T > reverseIterate( T & ref ) {
|
---|
268 | return reverseIterate_t< T >( ref );
|
---|
269 | }
|
---|
270 |
|
---|
271 | #endif // _UTILITY_H
|
---|
272 |
|
---|
273 | // Local Variables: //
|
---|
274 | // tab-width: 4 //
|
---|
275 | // mode: c++ //
|
---|
276 | // compile-command: "make install" //
|
---|
277 | // End: //
|
---|