Ignore:
Timestamp:
Jul 6, 2018, 5:10:14 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env
Children:
5c14030
Parents:
b21c77a
Message:

First draft of persistent-hash-based TypeEnvironment?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PersistentMap.h

    rb21c77a r184557e  
    114114        }
    115115
     116        /// Non-initializing constructor; should call init() before use
     117        PersistentMap( Mode m ) : data(), mode(m) {}
     118
    116119        PersistentMap( Mode m, Base&& b ) : data(), mode(m) {
    117120                assertf(m == BASE, "invalid mode");
     
    420423                }
    421424        }
     425
     426        /// Applies the function `f` to all elements of the map, returning a pointer to the updated map.
     427        /// `f` should take two parameters, `const Key&` and `Val&`, returning option<Val> filled with
     428        /// the previous value if mutated, an empty option<Val> otherwise.
     429        /// NOTE: when porting to C++17, this should work fine with std::optional
     430        template<typename F>
     431        Self* apply_to_all(F&& f) {
     432                // reset to root and exit early if no change
     433                if ( rerooted().empty() ) return this;
     434
     435                // remove map from self
     436                Base base_map = take_as<Base>();
     437                reset_as_base();
     438
     439                // apply all edits
     440                Self* next_edit = this;
     441                for ( auto& entry : base_map ) {
     442                        auto res = f( entry.first, entry.second );
     443                        if ( res ) {
     444                                // entry has been mutated; reset next_edit node as mutation
     445                                Self* new_node = new Self{ BASE };
     446                                next_edit->init<Ins>( new_node, entry.first, *std::move(res) );
     447                                next_edit->mode = UPD;
     448                                next_edit = new_node;
     449                        }
     450                }
     451
     452                // set map into final node and return
     453                next_edit->init<Base>( std::move(base_map) );
     454                return next_edit;
     455        }
     456
     457        /// Applies the function `f` to all elements of the map.
     458        /// `f` should take two parameters, `const Key&` and `const Val&`.
     459        template<typename F>
     460        void apply_to_all(F&& f) const {
     461                for ( const auto& entry : rerooted() ) { f( entry.first, entry.second ); }
     462        }
    422463};
    423464
Note: See TracChangeset for help on using the changeset viewer.