Changeset 13de4478 for src/ResolvExpr/typeops.h
- Timestamp:
- Apr 23, 2024, 1:37:17 PM (4 months ago)
- Branches:
- master
- Children:
- 4a3eb1c
- Parents:
- 15215f02
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/typeops.h
r15215f02 r13de4478 21 21 22 22 namespace ResolvExpr { 23 class TypeEnvironment;24 23 25 // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by 26 // picking one element out of each set 27 template< typename InputIterator, typename OutputIterator > 28 void combos( InputIterator begin, InputIterator end, OutputIterator out ) { 29 typedef typename InputIterator::value_type SetType; 30 typedef typename std::vector< typename SetType::value_type > ListType; 24 class TypeEnvironment; 31 25 32 if ( begin == end ) { 33 *out++ = ListType(); 34 return; 35 } // if 26 // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by 27 // picking one element out of each set 28 template< typename InputIterator, typename OutputIterator > 29 void combos( InputIterator begin, InputIterator end, OutputIterator out ) { 30 typedef typename InputIterator::value_type SetType; 31 typedef typename std::vector< typename SetType::value_type > ListType; 36 32 37 InputIterator current = begin; 38 begin++; 33 if ( begin == end ) { 34 *out++ = ListType(); 35 return; 36 } // if 39 37 40 std::vector< ListType > recursiveResult;41 combos( begin, end, back_inserter( recursiveResult ) );38 InputIterator current = begin; 39 begin++; 42 40 43 for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { 44 ListType result; 45 std::back_insert_iterator< ListType > inserter = back_inserter( result ); 46 *inserter++ = j; 47 std::copy( i.begin(), i.end(), inserter ); 48 *out++ = result; 41 std::vector< ListType > recursiveResult; 42 combos( begin, end, back_inserter( recursiveResult ) ); 43 44 for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { 45 ListType result; 46 std::back_insert_iterator< ListType > inserter = back_inserter( result ); 47 *inserter++ = j; 48 std::copy( i.begin(), i.end(), inserter ); 49 *out++ = result; 50 } 51 } 52 53 /// Flatten tuple type into existing list of types. 54 inline void flatten( 55 const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out 56 ) { 57 if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { 58 for ( const ast::Type * t : tupleType->types ) { 59 flatten( t, out ); 49 60 } 61 } else { 62 out.emplace_back( type ); 63 } 64 } 65 66 /// Flatten tuple type into list of types. 67 inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { 68 std::vector< ast::ptr< ast::Type > > out; 69 out.reserve( type->size() ); 70 flatten( type, out ); 71 return out; 72 } 73 74 template< typename Iter > 75 const ast::Type * tupleFromTypes( Iter crnt, Iter end ) { 76 std::vector< ast::ptr< ast::Type > > types; 77 while ( crnt != end ) { 78 // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure 79 // that this results in a flat tuple 80 flatten( *crnt, types ); 81 82 ++crnt; 50 83 } 51 84 52 /// flatten tuple type into existing list of types 53 inline void flatten( 54 const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out 55 ) { 56 if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { 57 for ( const ast::Type * t : tupleType->types ) { 58 flatten( t, out ); 59 } 60 } else { 61 out.emplace_back( type ); 62 } 63 } 85 return new ast::TupleType( std::move(types) ); 86 } 64 87 65 /// flatten tuple type into list of types 66 inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { 67 std::vector< ast::ptr< ast::Type > > out; 68 out.reserve( type->size() ); 69 flatten( type, out ); 70 return out; 71 } 88 inline const ast::Type * tupleFromTypes( 89 const std::vector< ast::ptr< ast::Type > > & tys 90 ) { 91 return tupleFromTypes( tys.begin(), tys.end() ); 92 } 72 93 73 template< typename Iter >74 const ast::Type * tupleFromTypes( Iter crnt, Iter end ) {75 std::vector< ast::ptr< ast::Type > > types;76 while ( crnt != end ) {77 // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure78 // that this results in a flat tuple79 flatten( *crnt, types );80 81 ++crnt;82 }83 84 return new ast::TupleType{ std::move(types) };85 }86 87 inline const ast::Type * tupleFromTypes(88 const std::vector< ast::ptr< ast::Type > > & tys89 ) {90 return tupleFromTypes( tys.begin(), tys.end() );91 }92 94 } // namespace ResolvExpr 93 95
Note: See TracChangeset
for help on using the changeset viewer.