source: src/Common/utility.h @ d5f1cfc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d5f1cfc was 60089f4, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

fix printing in CommaExpr?, CompoundStmt?, ForStmt?, etc.

  • Property mode set to 100644
File size: 5.2 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 : 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
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                        // 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
70template< typename SrcContainer, typename DestContainer >
71void 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
79template< typename Container >
80void 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
89static 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
100template< class T, typename ResultType, ResultType( T::* memfunc )() >
101ResultType dispatch( T *pT ) {
102        return (pT->*memfunc)();
103}
104
105template < typename T >
106std::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
113template < typename T >
114std::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
131template < typename T >
132std::string toString ( T value ) {
133        std::ostringstream os;
134        os << value; // << std::ends;
135        return os.str();
136}
137
138template< class Constructed, typename Arg >
139Constructed *ctor( Arg arg ) {
140        Constructed *c = new Constructed( arg );
141        return c;
142}
143
144template< class Constructed, typename Arg >
145Constructed ctor_noptr( Arg arg ) {
146        return Constructed( arg );
147}
148
149template< typename T >
150void 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
161template< typename T1, typename T2 >
162T2 *cast_ptr( T1 *from ) {
163        return dynamic_cast< T2 * >( from );
164}
165
166template< class Exception, typename Arg >
167void inline assert_throw( bool pred, Arg arg ) {
168        if (pred) throw Exception( arg );
169}
170
171template< typename T >
172struct is_null_pointer {
173        bool operator()( const T *ptr ) { return ( ptr == 0 ); }
174};
175
176template< class InputIterator, class OutputIterator, class Predicate >
177void filter(InputIterator begin, InputIterator end, OutputIterator out, Predicate pred) {
178        while ( begin++ != end )
179                if ( pred(*begin) ) *out++ = *begin;
180
181        return;
182}
183
184template< class InputIterator1, class InputIterator2, class OutputIterator >
185void 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
190template< class InputIterator1, class InputIterator2, class OutputIterator, class BinFunction >
191void 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
197template< typename Iterator >
198Iterator 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: //
Note: See TracBrowser for help on using the repository browser.