Changeset 5c14030 for src/Common


Ignore:
Timestamp:
Jul 11, 2018, 4:26:22 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env
Children:
d318a18
Parents:
184557e
git-author:
Aaron Moss <a3moss@…> (07/11/18 16:10:36)
git-committer:
Aaron Moss <a3moss@…> (07/11/18 16:26:22)
Message:

Persistent-array environment compiles

Location:
src/Common
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Common/GC.h

    r184557e r5c14030  
    6969        bool mark;                     ///< The current collection's mark bit
    7070        unsigned g;                    ///< The current number generation in use
     71
     72        /// Trace a traceable object (enabled by SFINAE)
     73        template<typename T>
     74        static inline auto do_trace(const GC& gc, T& obj, int) -> decltype(gc << obj, void()) {
     75                gc << obj;
     76        }
     77
     78        /// Do not trace an untraceable object
     79        template<typename T>
     80        static inline auto do_trace(const GC&, T&, long) -> void {}
     81
     82        /// Base case for maybe_trace
     83        void maybe_trace() const {}
     84
     85public:
     86        /// Trace any objects that are traceable
     87        template<typename T, typename... Args>
     88        void maybe_trace(T& obj, Args&... args) const {
     89                // uses SFINAE trick to select proper overload; prefers actually tracing version
     90                // (because of int->long conversion), but will fall back to non-tracing
     91                do_trace(*this, obj, 0);
     92                maybe_trace(args...);
     93        }
    7194};
    7295
  • src/Common/PersistentDisjointSet.h

    r184557e r5c14030  
    129129        }
    130130
     131        /// reset as base
     132        void reset_as_base() {
     133                assertf( mode == BASE, "can only reset_as_base() on BASE" );
     134                as<Base>().~Base();
     135        }
     136
    131137        /// Non-initializing constructor; should call init() before use
    132138        PersistentDisjointSet( Mode m ) : data(), mode(m) {}
     
    165171                        case BASE: {
    166172                                for (const auto& entry : as<Base>()) {
    167                                         gc << entry.first;
     173                                        gc.maybe_trace( entry.first );
    168174                                }
    169175                                return;
     
    171177                        case ADD: case REM: {
    172178                                const Add& self = as<Add>();
    173                                 gc << self.base << self.root;
     179                                gc << self.base;
     180                                gc.maybe_trace( self.root );
    174181                                return;
    175182                        }
    176183                        case ADDTO: case REMFROM: {
    177184                                const AddTo& self = as<AddTo>();
    178                                 gc << self.base << self.root << self.child;
     185                                gc << self.base;
     186                                gc.maybe_trace( self.root, self.child );
    179187                                return;
    180188                        }
     
    210218                // take map out of base
    211219                Base base_map = base->take_as<Base>();
    212                 base->reset();
     220                base->reset_as_base();
    213221
    214222                // switch base to inverse of self and mutate base map
     
    331339                // transfer map to new node
    332340                Self* ret = new Self{ BASE, take_as<Base>() };
    333                 reset();
     341                reset_as_base();
    334342
    335343                // set self to REM node
     
    352360                // transfer map to new node
    353361                Self* ret = new Self{ BASE, take_as<Base>() };
    354                 reset();
     362                reset_as_base();
    355363
    356364                // find set nodes
     
    449457        /// `f` should take members by const reference or copy
    450458        template<typename F>
    451         void apply_to_class(Elm i, F&& f) const {
     459        void for_class(Elm i, F&& f) const {
    452460                const Base& self = rerooted();
    453461
  • src/Common/PersistentMap.h

    r184557e r5c14030  
    139139                        case BASE: {
    140140                                for (const auto& entry : as<Base>()) {
    141                                         gc << entry.first << entry.second;
     141                                        gc.maybe_trace( entry.first, entry.second );
    142142                                }
    143143                                return;
     
    145145                        case REM: {
    146146                                const Rem& self = as<Rem>();
    147                                 gc << self.base << self.key;
     147                                gc << self.base;
     148                                gc.maybe_trace( self.key );
    148149                                return;
    149150                        }
    150151                        case INS: case UPD: {
    151152                                const Ins& self = as<Ins>();
    152                                 gc << self.base << self.key << self.val;
     153                                gc << self.base;
     154                                gc.maybe_trace( self.key, self.val );
    153155                                return;
    154156                        }
     
    424426        }
    425427
     428        /// Applies the function `f` to all elements of the map.
     429        /// `f` should take two parameters, `const Key&` and `const Val&`.
     430        template<typename F>
     431        void for_each(F&& f) const {
     432                for ( const auto& entry : rerooted() ) { f( entry.first, entry.second ); }
     433        }
     434
    426435        /// Applies the function `f` to all elements of the map, returning a pointer to the updated map.
    427436        /// `f` should take two parameters, `const Key&` and `Val&`, returning option<Val> filled with
     
    429438        /// NOTE: when porting to C++17, this should work fine with std::optional
    430439        template<typename F>
    431         Self* apply_to_all(F&& f) {
     440        Self* mutate_each(F&& f) {
    432441                // reset to root and exit early if no change
    433442                if ( rerooted().empty() ) return this;
     
    454463                return next_edit;
    455464        }
    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         }
    463465};
    464466
  • src/Common/option.h

    r184557e r5c14030  
    1616#pragma once
    1717
     18#include <cassert>
    1819#include <functional>
    1920#include <type_traits>
    2021#include <utility>
    21 
    22 #include "debug.h"
    2322
    2423using std::move;
     
    121120
    122121    /// Get contained value (checked)
    123     T& value() & { assume(filled, "checked get failed"); return get(); }
    124     const T& value() const& { assume(filled, "checked get failed"); return get(); }
    125     T&& value() && { assume(filled, "checked get failed"); return move(get()); }
    126     const T&& value() const&& { assume(filled, "checked get failed"); return move(get()); }
     122    T& value() & { assertf(filled, "checked get failed"); return get(); }
     123    const T& value() const& { assertf(filled, "checked get failed"); return get(); }
     124    T&& value() && { assertf(filled, "checked get failed"); return move(get()); }
     125    const T&& value() const&& { assertf(filled, "checked get failed"); return move(get()); }
    127126
    128127    /// Get contained or default value
Note: See TracChangeset for help on using the changeset viewer.