source: src/Common/utility.h@ 5b40f30

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 5b40f30 was 2871210, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

include file with keywords, fix type of label address expressions, fix computed goto to any expressions, generic types first attempt

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 : Thu Jul 2 18:04:41 2015
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
26template< typename T >
27static inline T * maybeClone( const T *orig ) {
28 if ( orig ) {
29 return orig->clone();
30 } else {
31 return 0;
32 } // if
33}
34
35template< typename T, typename U >
36static inline T * maybeBuild( const U *orig ) {
37 if ( orig ) {
38 return orig->build();
39 } else {
40 return 0;
41 } // if
42}
43
44template< typename Input_iterator >
45void 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
51template< typename Container >
52void deleteAll( Container &container ) {
53 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
54 delete *i;
55 } // for
56}
57
58template< typename Container >
59void 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 os << std::endl;
65 } // if
66 } // for
67}
68
69template< typename SrcContainer, typename DestContainer >
70void cloneAll( const SrcContainer &src, DestContainer &dest ) {
71 typename SrcContainer::const_iterator in = src.begin();
72 std::back_insert_iterator< DestContainer > out( dest );
73 while ( in != src.end() ) {
74 *out++ = (*in++)->clone();
75 } // while
76}
77
78template< typename Container >
79void assertAll( const Container &container ) {
80 int count = 0;
81 for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
82 if ( !(*i) ) {
83 std::cerr << count << " is null" << std::endl;
84 } // if
85 } // for
86}
87
88static inline std::string assign_strptr( const std::string *str ) {
89 if ( str == 0 ) {
90 return "";
91 } else {
92 std::string tmp;
93 tmp = *str;
94 delete str;
95 return tmp;
96 } // if
97}
98
99template< class T, typename ResultType, ResultType( T::* memfunc )() >
100ResultType dispatch( T *pT ) {
101 return (pT->*memfunc)();
102}
103
104template < typename T >
105std::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
110}
111
112template < typename T >
113std::list<T> flatten( std::list < std::list<T> > l) {
114 typedef std::list <T> Ts;
115
116 Ts ret;
117
118 switch ( l.size() ) {
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
128}
129
130template < typename T >
131std::string toString ( T value ) {
132 std::ostringstream os;
133 os << value; // << std::ends;
134 return os.str();
135}
136
137template< class Constructed, typename Arg >
138Constructed *ctor( Arg arg ) {
139 Constructed *c = new Constructed( arg );
140 return c;
141}
142
143template< class Constructed, typename Arg >
144Constructed ctor_noptr( Arg arg ) {
145 return Constructed( arg );
146}
147
148template< typename T >
149void replace( std::list< T > &org, typename std::list< T >::iterator pos, std::list< T > &with ) {
150 typename std::list< T >::iterator next = pos; advance( next, 1 );
151
152 //if ( next != org.end() ) {
153 org.erase( pos );
154 org.splice( next, with );
155 //}
156
157 return;
158}
159
160template< typename T1, typename T2 >
161T2 *cast_ptr( T1 *from ) {
162 return dynamic_cast< T2 * >( from );
163}
164
165template< class Exception, typename Arg >
166void inline assert_throw( bool pred, Arg arg ) {
167 if (pred) throw Exception( arg );
168}
169
170template< typename T >
171struct is_null_pointer {
172 bool operator()( const T *ptr ) { return ( ptr == 0 ); }
173};
174
175template< class InputIterator, class OutputIterator, class Predicate >
176void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
177 while ( begin++ != end )
178 if ( pred(*begin) ) *out++ = *begin;
179
180 return;
181}
182
183template< class InputIterator1, class InputIterator2, class OutputIterator >
184void zip( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out ) {
185 while ( b1 != e1 && b2 != e2 )
186 *out++ = std::pair<typename InputIterator1::value_type, typename InputIterator2::value_type>(*b1++, *b2++);
187}
188
189template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
190void zipWith( InputIterator1 b1, InputIterator1 e1, InputIterator2 b2, InputIterator2 e2, OutputIterator out, BinFunction func ) {
191 while ( b1 != e1 && b2 != e2 )
192 *out++ = func(*b1++, *b2++);
193}
194
195// it's nice to actually be able to increment iterators by an arbitrary amount
196template< typename Iterator >
197Iterator operator+(Iterator i, int inc) {
198 while ( inc > 0 ) {
199 ++i;
200 --inc;
201 }
202 return i;
203}
204
205#endif // _UTILITY_H
206
207// Local Variables: //
208// tab-width: 4 //
209// mode: c++ //
210// compile-command: "make install" //
211// End: //
Note: See TracBrowser for help on using the repository browser.