Ignore:
Timestamp:
Oct 29, 2019, 4:01:24 PM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
773db65, 9421f3d8
Parents:
7951100 (diff), 8364209 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/utility.h

    r7951100 rb067d9b  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sun May  6 22:24:16 2018
    13 // Update Count     : 40
     12// Last Modified On : Wed Jul 24 14:28:19 2019
     13// Update Count     : 41
    1414//
    1515
    1616#pragma once
    1717
     18#include <cassert>
    1819#include <cctype>
    1920#include <algorithm>
     
    2627#include <string>
    2728#include <type_traits>
    28 
    29 #include <cassert>
     29#include <utility>
     30#include <vector>
    3031
    3132#include "Common/Indenter.h"
     33
     34class Expression;
     35
     36/// bring std::move into global scope
     37using std::move;
     38
     39/// partner to move that copies any copyable type
     40template<typename T>
     41T copy( const T & x ) { return x; }
    3242
    3343template< typename T >
     
    7181
    7282template< typename Container >
    73 void deleteAll( Container &container ) {
    74         for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
    75                 delete *i;
     83void deleteAll( const Container &container ) {
     84        for ( const auto &i : container ) {
     85                delete i;
    7686        } // for
    7787}
     
    142152                return ret;
    143153        } // switch
     154}
     155
     156/// Splice src onto the end of dst, clearing src
     157template< typename T >
     158void splice( std::vector< T > & dst, std::vector< T > & src ) {
     159        dst.reserve( dst.size() + src.size() );
     160        for ( T & x : src ) { dst.emplace_back( std::move( x ) ); }
     161        src.clear();
     162}
     163
     164/// Splice src onto the begining of dst, clearing src
     165template< typename T >
     166void spliceBegin( std::vector< T > & dst, std::vector< T > & src ) {
     167        splice( src, dst );
     168        dst.swap( src );
    144169}
    145170
     
    456481} // ilog2
    457482
     483// -----------------------------------------------------------------------------
     484/// evaluates expr as a long long int. If second is false, expr could not be evaluated
     485std::pair<long long int, bool> eval(const Expression * expr);
     486
     487namespace ast {
     488        class Expr;
     489}
     490
     491std::pair<long long int, bool> eval(const ast::Expr * expr);
     492
     493// -----------------------------------------------------------------------------
     494/// Reorders the input range in-place so that the minimal-value elements according to the
     495/// comparator are in front;
     496/// returns the iterator after the last minimal-value element.
     497template<typename Iter, typename Compare>
     498Iter sort_mins( Iter begin, Iter end, Compare& lt ) {
     499        if ( begin == end ) return end;
     500
     501        Iter min_pos = begin;
     502        for ( Iter i = begin + 1; i != end; ++i ) {
     503                if ( lt( *i, *min_pos ) ) {
     504                        // new minimum cost; swap into first position
     505                        min_pos = begin;
     506                        std::iter_swap( min_pos, i );
     507                } else if ( ! lt( *min_pos, *i ) ) {
     508                        // duplicate minimum cost; swap into next minimum position
     509                        ++min_pos;
     510                        std::iter_swap( min_pos, i );
     511                }
     512        }
     513        return ++min_pos;
     514}
     515
     516template<typename Iter, typename Compare>
     517inline Iter sort_mins( Iter begin, Iter end, Compare&& lt ) {
     518        return sort_mins( begin, end, lt );
     519}
     520
     521/// sort_mins defaulted to use std::less
     522template<typename Iter>
     523inline Iter sort_mins( Iter begin, Iter end ) {
     524        return sort_mins( begin, end, std::less<typename std::iterator_traits<Iter>::value_type>{} );
     525}
    458526
    459527// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.