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 : Rob Schluntz
|
---|
12 | // Last Modified On : Thu Apr 28 13:18:24 2016
|
---|
13 | // Update Count : 16
|
---|
14 | //
|
---|
15 |
|
---|
16 | #ifndef _UTILITY_H
|
---|
17 | #define _UTILITY_H
|
---|
18 |
|
---|
19 | #include <iostream>
|
---|
20 | #include <sstream>
|
---|
21 | #include <iterator>
|
---|
22 | #include <string>
|
---|
23 | #include <cctype>
|
---|
24 | #include <list>
|
---|
25 |
|
---|
26 | template< typename T >
|
---|
27 | static inline T * maybeClone( const T *orig ) {
|
---|
28 | if ( orig ) {
|
---|
29 | return orig->clone();
|
---|
30 | } else {
|
---|
31 | return 0;
|
---|
32 | } // if
|
---|
33 | }
|
---|
34 |
|
---|
35 | template< typename T, typename U >
|
---|
36 | static inline T * maybeBuild( const U *orig ) {
|
---|
37 | if ( orig ) {
|
---|
38 | return orig->build();
|
---|
39 | } else {
|
---|
40 | return 0;
|
---|
41 | } // if
|
---|
42 | }
|
---|
43 |
|
---|
44 | template< typename Input_iterator >
|
---|
45 | void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
|
---|
46 | for ( Input_iterator i = begin; i != end; ++i ) {
|
---|
47 | os << name_array[ *i ] << ' ';
|
---|
48 | } // for
|
---|
49 | }
|
---|
50 |
|
---|
51 | template< typename Container >
|
---|
52 | void deleteAll( Container &container ) {
|
---|
53 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
54 | delete *i;
|
---|
55 | } // for
|
---|
56 | }
|
---|
57 |
|
---|
58 | template< typename Container >
|
---|
59 | void printAll( const Container &container, std::ostream &os, int indent = 0 ) {
|
---|
60 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
61 | if ( *i ) {
|
---|
62 | os << std::string( indent, ' ' );
|
---|
63 | (*i)->print( os, indent + 2 );
|
---|
64 | // need an endl after each element because it's not easy to know when each individual item should end
|
---|
65 | os << std::endl;
|
---|
66 | } // if
|
---|
67 | } // for
|
---|
68 | }
|
---|
69 |
|
---|
70 | template< typename SrcContainer, typename DestContainer >
|
---|
71 | void cloneAll( const SrcContainer &src, DestContainer &dest ) {
|
---|
72 | typename SrcContainer::const_iterator in = src.begin();
|
---|
73 | std::back_insert_iterator< DestContainer > out( dest );
|
---|
74 | while ( in != src.end() ) {
|
---|
75 | *out++ = (*in++)->clone();
|
---|
76 | } // while
|
---|
77 | }
|
---|
78 |
|
---|
79 | template< typename Container >
|
---|
80 | void assertAll( const Container &container ) {
|
---|
81 | int count = 0;
|
---|
82 | for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
|
---|
83 | if ( !(*i) ) {
|
---|
84 | std::cerr << count << " is null" << std::endl;
|
---|
85 | } // if
|
---|
86 | } // for
|
---|
87 | }
|
---|
88 |
|
---|
89 | static inline std::string assign_strptr( const std::string *str ) {
|
---|
90 | if ( str == 0 ) {
|
---|
91 | return "";
|
---|
92 | } else {
|
---|
93 | std::string tmp;
|
---|
94 | tmp = *str;
|
---|
95 | delete str;
|
---|
96 | return tmp;
|
---|
97 | } // if
|
---|
98 | }
|
---|
99 |
|
---|
100 | template< class T, typename ResultType, ResultType( T::* memfunc )() >
|
---|
101 | ResultType dispatch( T *pT ) {
|
---|
102 | return (pT->*memfunc)();
|
---|
103 | }
|
---|
104 |
|
---|
105 | template < typename T >
|
---|
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
|
---|
111 | }
|
---|
112 |
|
---|
113 | template < typename T >
|
---|
114 | std::list<T> flatten( std::list < std::list<T> > l) {
|
---|
115 | typedef std::list <T> Ts;
|
---|
116 |
|
---|
117 | Ts ret;
|
---|
118 |
|
---|
119 | switch ( l.size() ) {
|
---|
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
|
---|
129 | }
|
---|
130 |
|
---|
131 | template < typename T >
|
---|
132 | std::string toString ( T value ) {
|
---|
133 | std::ostringstream os;
|
---|
134 | os << value; // << std::ends;
|
---|
135 | return os.str();
|
---|
136 | }
|
---|
137 |
|
---|
138 | template< class Constructed, typename Arg >
|
---|
139 | Constructed *ctor( Arg arg ) {
|
---|
140 | Constructed *c = new Constructed( arg );
|
---|
141 | return c;
|
---|
142 | }
|
---|
143 |
|
---|
144 | template< class Constructed, typename Arg >
|
---|
145 | Constructed ctor_noptr( Arg arg ) {
|
---|
146 | return Constructed( arg );
|
---|
147 | }
|
---|
148 |
|
---|
149 | template< typename T >
|
---|
150 | void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
|
---|
151 | typename std::list< T >::iterator next = pos; advance( next, 1 );
|
---|
152 |
|
---|
153 | //if ( next != org.end() ) {
|
---|
154 | org.erase( pos );
|
---|
155 | org.splice( next, with );
|
---|
156 | //}
|
---|
157 |
|
---|
158 | return;
|
---|
159 | }
|
---|
160 |
|
---|
161 | template< typename T1, typename T2 >
|
---|
162 | T2 *cast_ptr( T1 *from ) {
|
---|
163 | return dynamic_cast< T2 * >( from );
|
---|
164 | }
|
---|
165 |
|
---|
166 | template< class Exception, typename Arg >
|
---|
167 | void inline assert_throw( bool pred, Arg arg ) {
|
---|
168 | if (pred) throw Exception( arg );
|
---|
169 | }
|
---|
170 |
|
---|
171 | template< typename T >
|
---|
172 | struct is_null_pointer {
|
---|
173 | bool operator()( const T *ptr ) { return ( ptr == 0 ); }
|
---|
174 | };
|
---|
175 |
|
---|
176 | template< class InputIterator, class OutputIterator, class Predicate >
|
---|
177 | void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
|
---|
178 | while ( begin++ != end )
|
---|
179 | if ( pred(*begin) ) *out++ = *begin;
|
---|
180 |
|
---|
181 | return;
|
---|
182 | }
|
---|
183 |
|
---|
184 | template< class InputIterator1, class InputIterator2, class OutputIterator >
|
---|
185 | void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
|
---|
186 | while ( b1 != e1 && b2 != e2 )
|
---|
187 | *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
|
---|
188 | }
|
---|
189 |
|
---|
190 | template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
|
---|
191 | void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
|
---|
192 | while ( b1 != e1 && b2 != e2 )
|
---|
193 | *out++ = func(*b1++, *b2++);
|
---|
194 | }
|
---|
195 |
|
---|
196 | // it's nice to actually be able to increment iterators by an arbitrary amount
|
---|
197 | template< typename Iterator >
|
---|
198 | Iterator operator+(Iterator i, int inc) {
|
---|
199 | while ( inc > 0 ) {
|
---|
200 | ++i;
|
---|
201 | --inc;
|
---|
202 | }
|
---|
203 | return i;
|
---|
204 | }
|
---|
205 |
|
---|
206 | #endif // _UTILITY_H
|
---|
207 |
|
---|
208 | // Local Variables: //
|
---|
209 | // tab-width: 4 //
|
---|
210 | // mode: c++ //
|
---|
211 | // compile-command: "make install" //
|
---|
212 | // End: //
|
---|