Changeset b067d9b for src/Common/utility.h
- Timestamp:
- Oct 29, 2019, 4:01:24 PM (6 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/utility.h
r7951100 rb067d9b 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun May 6 22:24:16 201813 // Update Count : 4 012 // Last Modified On : Wed Jul 24 14:28:19 2019 13 // Update Count : 41 14 14 // 15 15 16 16 #pragma once 17 17 18 #include <cassert> 18 19 #include <cctype> 19 20 #include <algorithm> … … 26 27 #include <string> 27 28 #include <type_traits> 28 29 #include < cassert>29 #include <utility> 30 #include <vector> 30 31 31 32 #include "Common/Indenter.h" 33 34 class Expression; 35 36 /// bring std::move into global scope 37 using std::move; 38 39 /// partner to move that copies any copyable type 40 template<typename T> 41 T copy( const T & x ) { return x; } 32 42 33 43 template< typename T > … … 71 81 72 82 template< typename Container > 73 void deleteAll( Container &container ) {74 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i) {75 delete *i;83 void deleteAll( const Container &container ) { 84 for ( const auto &i : container ) { 85 delete i; 76 86 } // for 77 87 } … … 142 152 return ret; 143 153 } // switch 154 } 155 156 /// Splice src onto the end of dst, clearing src 157 template< typename T > 158 void 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 165 template< typename T > 166 void spliceBegin( std::vector< T > & dst, std::vector< T > & src ) { 167 splice( src, dst ); 168 dst.swap( src ); 144 169 } 145 170 … … 456 481 } // ilog2 457 482 483 // ----------------------------------------------------------------------------- 484 /// evaluates expr as a long long int. If second is false, expr could not be evaluated 485 std::pair<long long int, bool> eval(const Expression * expr); 486 487 namespace ast { 488 class Expr; 489 } 490 491 std::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. 497 template<typename Iter, typename Compare> 498 Iter 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 516 template<typename Iter, typename Compare> 517 inline 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 522 template<typename Iter> 523 inline Iter sort_mins( Iter begin, Iter end ) { 524 return sort_mins( begin, end, std::less<typename std::iterator_traits<Iter>::value_type>{} ); 525 } 458 526 459 527 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.