Ignore:
Timestamp:
Oct 18, 2018, 4:26:11 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
Children:
2fd9f24
Parents:
2c187378
Message:

rational.cfa passes deferred resolution pass now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/ResolveAssertions.cc

    r2c187378 rfbecee5  
    1818#include <cassert>                // for assertf
    1919#include <list>                   // for list
     20#include <unordered_map>          // for unordered_map
    2021#include <utility>                // for move
    2122#include <vector>                 // for vector
     
    2324#include "Alternative.h"          // for Alternative, AssertionItem
    2425#include "Common/FilterCombos.h"  // for filterCombos
     26#include "Common/utility.h"       // for sort_mins
    2527#include "SymTab/Indexer.h"       // for Indexer
    2628#include "TypeEnvironment.h"      // for TypeEnvironment, etc.
     
    125127        };
    126128
     129        /// Comparator for CandidateEnvMerger outputs that sums their costs and caches the stored
     130        /// sums
     131        struct CandidateCost {
     132                using Element = CandidateEnvMerger::OutType;
     133        private:
     134                using Memo = std::unordered_map<const Element*, Cost>;
     135                mutable Memo cache;              ///< cache of element costs
     136                const SymTab::Indexer& indexer;  ///< Indexer for costing
     137
     138        public:
     139                CandidateCost( const SymTab::Indexer& indexer ) : indexer(indexer) {}
     140
     141                /// reports the cost of an element
     142                Cost get( const Element& x ) const {
     143                        Memo::const_iterator it = cache.find( &x );
     144                        if ( it == cache.end() ) {
     145                                Cost k = Cost::zero;
     146                                for ( const auto& assn : x.assns ) {
     147                                        k += computeConversionCost(
     148                                                assn.match.adjType, assn.decl->get_type(), indexer, x.env );
     149                                }
     150                                it = cache.emplace_hint( it, &x, k );
     151                        }
     152                        return it->second;
     153                }
     154               
     155                /// compares elements by cost
     156                bool operator() ( const Element& a, const Element& b ) const {
     157                        return get( a ) < get( b );
     158                }
     159        };
     160
    127161        /// Flag for state iteration
    128162        enum IterateFlag { IterateState };
     
    275309                                        }
    276310                                } else {
    277                                         // resolve deferred assertions by mutual compatibility
     311                                        // resolve deferred assertions by mutual compatibility and sort by cost
    278312                                        std::vector<CandidateEnvMerger::OutType> compatible = filterCombos(
    279313                                                resn.deferred,
    280314                                                CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } );
     315                                        auto lmin = sort_mins( compatible.begin(), compatible.end(),
     316                                                CandidateCost{resn.indexer} );
    281317                                       
    282                                         for ( auto& compat : compatible ) {
     318                                        // for ( auto& compat : compatible ) {
     319                                        for ( auto it = compatible.begin(); it != lmin ; ++it ) {
     320                                                auto& compat = *it;
    283321                                                ResnState new_resn = resn;
    284322
Note: See TracChangeset for help on using the changeset viewer.