Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r5bf685f r9feb34b  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed Jan 17 14:40:00 2024
    13 // Update Count     : 54
     12// Last Modified On : Fri Feb 17 15:25:00 2023
     13// Update Count     : 53
    1414//
    1515
     
    1717
    1818#include <cassert>
     19#include <cctype>
    1920#include <algorithm>
     21#include <iostream>
    2022#include <list>
     23#include <memory>
    2124#include <string>
    2225#include <type_traits>
    2326#include <vector>
     27#include <cstring>                                                                              // memcmp
     28
     29#include "Common/Indenter.h"
     30
     31class Expression;
     32
     33/// bring std::move into global scope
     34using std::move;
    2435
    2536/// partner to move that copies any copyable type
    2637template<typename T>
    2738T copy( const T & x ) { return x; }
     39
     40template< typename T >
     41static inline T * maybeClone( const T *orig ) {
     42        if ( orig ) {
     43                return orig->clone();
     44        } else {
     45                return 0;
     46        } // if
     47}
     48
     49template< typename Input_iterator >
     50void printEnums( Input_iterator begin, Input_iterator end, const char * const *name_array, std::ostream &os ) {
     51        for ( Input_iterator i = begin; i != end; ++i ) {
     52                os << name_array[ *i ] << ' ';
     53        } // for
     54}
     55
     56template< typename Container >
     57void deleteAll( const Container &container ) {
     58        for ( const auto &i : container ) {
     59                delete i;
     60        } // for
     61}
     62
     63template< typename Container >
     64void printAll( const Container &container, std::ostream &os, Indenter indent = {} ) {
     65        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
     66                if ( *i ) {
     67                        os << indent;
     68                        (*i)->print( os, indent );
     69                        // need an endl after each element because it's not easy to know when each individual item should end
     70                        os << std::endl;
     71                } // if
     72        } // for
     73}
     74
     75template< typename SrcContainer, typename DestContainer >
     76void cloneAll( const SrcContainer &src, DestContainer &dest ) {
     77        typename SrcContainer::const_iterator in = src.begin();
     78        std::back_insert_iterator< DestContainer > out( dest );
     79        while ( in != src.end() ) {
     80                *out++ = (*in++)->clone();
     81        } // while
     82}
     83
     84template< typename SrcContainer, typename DestContainer, typename Predicate >
     85void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) {
     86        std::back_insert_iterator< DestContainer > out( dest );
     87        for ( auto x : src ) {
     88                if ( pred(x) ) {
     89                        *out++ = x->clone();
     90                }
     91        } // while
     92}
     93
     94template< typename Container >
     95void assertAll( const Container &container ) {
     96        int count = 0;
     97        for ( typename Container::const_iterator i = container.begin(); i != container.end(); ++i ) {
     98                if ( !(*i) ) {
     99                        std::cerr << count << " is null" << std::endl;
     100                } // if
     101        } // for
     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}
    28129
    29130/// Splice src onto the end of dst, clearing src
     
    42143}
    43144
    44 /// Remove elements that match pred from the container.
     145template< typename... Args >
     146auto filter(Args&&... args) -> decltype(std::copy_if(std::forward<Args>(args)...)) {
     147  return std::copy_if(std::forward<Args>(args)...);
     148}
     149
     150template <typename E, typename UnaryPredicate, template< typename, typename...> class Container, typename... Args >
     151void filter( Container< E *, Args... > & container, UnaryPredicate pred, bool doDelete ) {
     152        auto i = begin( container );
     153        while ( i != end( container ) ) {
     154                auto it = next( i );
     155                if ( pred( *i ) ) {
     156                        if ( doDelete ) {
     157                                delete *i;
     158                        } // if
     159                        container.erase( i );
     160                } // if
     161                i = it;
     162        } // while
     163}
     164
    45165template<typename Container, typename Pred>
    46166void erase_if( Container & cont, Pred && pred ) {
Note: See TracChangeset for help on using the changeset viewer.