Ignore:
Timestamp:
Feb 3, 2023, 1:28:43 PM (18 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
2f61765
Parents:
8a97248 (diff), db9d7a9 (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/ScopedMap.h

    r8a97248 r2125443a  
    3737                template<typename N>
    3838                Scope(N && n) : map(), note(std::forward<N>(n)) {}
    39                
     39
    4040                Scope() = default;
    4141                Scope(const Scope &) = default;
     
    4646        typedef std::vector< Scope > ScopeList;
    4747
    48         ScopeList scopes; ///< scoped list of maps
     48        /// Scoped list of maps.
     49        ScopeList scopes;
    4950public:
    5051        typedef typename MapType::key_type key_type;
     
    5859        typedef typename MapType::const_pointer const_pointer;
    5960
    60         class iterator : public std::iterator< std::bidirectional_iterator_tag, value_type > {
    61         friend class ScopedMap;
    62         friend class const_iterator;
    63                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    64                 typedef typename ScopedMap::ScopeList scope_list;
    65                 typedef typename scope_list::size_type size_type;
    66 
    67                 /// Checks if this iterator points to a valid item
    68                 bool is_valid() const {
    69                         return it != (*scopes)[level].map.end();
    70                 }
    71 
    72                 /// Increments on invalid
    73                 iterator & next_valid() {
    74                         if ( ! is_valid() ) { ++(*this); }
    75                         return *this;
    76                 }
    77 
    78                 /// Decrements on invalid
    79                 iterator & prev_valid() {
    80                         if ( ! is_valid() ) { --(*this); }
    81                         return *this;
    82                 }
    83 
    84                 iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel)
    85                         : scopes(&_scopes), it(_it), level(inLevel) {}
    86         public:
    87                 iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    88                 iterator & operator= (const iterator & that) {
    89                         scopes = that.scopes; level = that.level; it = that.it;
    90                         return *this;
    91                 }
    92 
    93                 reference operator* () { return *it; }
    94                 pointer operator-> () const { return it.operator->(); }
    95 
    96                 iterator & operator++ () {
    97                         if ( it == (*scopes)[level].map.end() ) {
    98                                 if ( level == 0 ) return *this;
    99                                 --level;
    100                                 it = (*scopes)[level].map.begin();
    101                         } else {
    102                                 ++it;
    103                         }
    104                         return next_valid();
    105                 }
    106                 iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
    107 
    108                 iterator & operator-- () {
    109                         // may fail if this is the begin iterator; allowed by STL spec
    110                         if ( it == (*scopes)[level].map.begin() ) {
    111                                 ++level;
    112                                 it = (*scopes)[level].map.end();
    113                         }
    114                         --it;
    115                         return prev_valid();
    116                 }
    117                 iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
    118 
    119                 bool operator== (const iterator & that) const {
    120                         return scopes == that.scopes && level == that.level && it == that.it;
    121                 }
    122                 bool operator!= (const iterator & that) const { return !( *this == that ); }
    123 
    124                 size_type get_level() const { return level; }
    125 
    126                 Note & get_note() { return (*scopes)[level].note; }
    127                 const Note & get_note() const { return (*scopes)[level].note; }
    128 
    129         private:
    130                 scope_list *scopes;
    131                 wrapped_iterator it;
    132                 size_type level;
    133         };
    134 
    135         class const_iterator : public std::iterator< std::bidirectional_iterator_tag,
    136                                                      value_type > {
    137         friend class ScopedMap;
    138                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    139                 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
    140                 typedef typename ScopedMap::ScopeList scope_list;
    141                 typedef typename scope_list::size_type size_type;
    142 
    143                 /// Checks if this iterator points to a valid item
    144                 bool is_valid() const {
    145                         return it != (*scopes)[level].map.end();
    146                 }
    147 
    148                 /// Increments on invalid
    149                 const_iterator & next_valid() {
    150                         if ( ! is_valid() ) { ++(*this); }
    151                         return *this;
    152                 }
    153 
    154                 /// Decrements on invalid
    155                 const_iterator & prev_valid() {
    156                         if ( ! is_valid() ) { --(*this); }
    157                         return *this;
    158                 }
    159 
    160                 const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel)
    161                         : scopes(&_scopes), it(_it), level(inLevel) {}
    162         public:
    163                 const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    164                 const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    165                 const_iterator & operator= (const iterator & that) {
    166                         scopes = that.scopes; level = that.level; it = that.it;
    167                         return *this;
    168                 }
    169                 const_iterator & operator= (const const_iterator & that) {
    170                         scopes = that.scopes; level = that.level; it = that.it;
    171                         return *this;
    172                 }
    173 
    174                 const_reference operator* () { return *it; }
    175                 const_pointer operator-> () { return it.operator->(); }
    176 
    177                 const_iterator & operator++ () {
    178                         if ( it == (*scopes)[level].map.end() ) {
    179                                 if ( level == 0 ) return *this;
    180                                 --level;
    181                                 it = (*scopes)[level].map.begin();
    182                         } else {
    183                                 ++it;
    184                         }
    185                         return next_valid();
    186                 }
    187                 const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
    188 
    189                 const_iterator & operator-- () {
    190                         // may fail if this is the begin iterator; allowed by STL spec
    191                         if ( it == (*scopes)[level].map.begin() ) {
    192                                 ++level;
    193                                 it = (*scopes)[level].map.end();
    194                         }
    195                         --it;
    196                         return prev_valid();
    197                 }
    198                 const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
    199 
    200                 bool operator== (const const_iterator & that) const {
    201                         return scopes == that.scopes && level == that.level && it == that.it;
    202                 }
    203                 bool operator!= (const const_iterator & that) const { return !( *this == that ); }
    204 
    205                 size_type get_level() const { return level; }
    206 
    207                 const Note & get_note() const { return (*scopes)[level].note; }
    208 
    209         private:
    210                 scope_list const *scopes;
    211                 wrapped_const_iterator it;
    212                 size_type level;
    213         };
     61        // Both iterator types are complete bidrectional iterators, see below.
     62        class iterator;
     63        class const_iterator;
    21464
    21565        /// Starts a new scope
     
    297147        }
    298148
    299         template< typename value_type_t >
    300         std::pair< iterator, bool > insert( iterator at, value_type_t && value ) {
    301                 MapType & scope = (*at.scopes)[ at.level ].map;
    302                 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
    303                 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
    304         }
    305 
    306149        template< typename value_t >
    307150        std::pair< iterator, bool > insert( const Key & key, value_t && value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
     
    324167        }
    325168
    326         iterator erase( iterator pos ) {
    327                 MapType & scope = (*pos.scopes)[ pos.level ].map;
    328                 const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it );
    329                 iterator it( *pos.scopes, new_it, pos.level );
    330                 return it.next_valid();
    331         }
     169        iterator erase( iterator pos );
    332170
    333171        size_type count( const Key & key ) const {
     
    344182        }
    345183};
     184
     185template<typename Key, typename Value, typename Note>
     186class ScopedMap<Key, Value, Note>::iterator :
     187                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     188        friend class ScopedMap;
     189        friend class const_iterator;
     190        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     191        typedef typename ScopedMap::ScopeList scope_list;
     192        typedef typename scope_list::size_type size_type;
     193
     194        /// Checks if this iterator points to a valid item
     195        bool is_valid() const {
     196                return it != (*scopes)[level].map.end();
     197        }
     198
     199        /// Increments on invalid
     200        iterator & next_valid() {
     201                if ( ! is_valid() ) { ++(*this); }
     202                return *this;
     203        }
     204
     205        /// Decrements on invalid
     206        iterator & prev_valid() {
     207                if ( ! is_valid() ) { --(*this); }
     208                return *this;
     209        }
     210
     211        iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel)
     212                : scopes(&_scopes), it(_it), level(inLevel) {}
     213public:
     214        iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     215        iterator & operator= (const iterator & that) {
     216                scopes = that.scopes; level = that.level; it = that.it;
     217                return *this;
     218        }
     219
     220        reference operator* () { return *it; }
     221        pointer operator-> () const { return it.operator->(); }
     222
     223        iterator & operator++ () {
     224                if ( it == (*scopes)[level].map.end() ) {
     225                        if ( level == 0 ) return *this;
     226                        --level;
     227                        it = (*scopes)[level].map.begin();
     228                } else {
     229                        ++it;
     230                }
     231                return next_valid();
     232        }
     233        iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     234
     235        iterator & operator-- () {
     236                // may fail if this is the begin iterator; allowed by STL spec
     237                if ( it == (*scopes)[level].map.begin() ) {
     238                        ++level;
     239                        it = (*scopes)[level].map.end();
     240                }
     241                --it;
     242                return prev_valid();
     243        }
     244        iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     245
     246        bool operator== (const iterator & that) const {
     247                return scopes == that.scopes && level == that.level && it == that.it;
     248        }
     249        bool operator!= (const iterator & that) const { return !( *this == that ); }
     250
     251        size_type get_level() const { return level; }
     252
     253        Note & get_note() { return (*scopes)[level].note; }
     254        const Note & get_note() const { return (*scopes)[level].note; }
     255
     256private:
     257        scope_list *scopes;
     258        wrapped_iterator it;
     259        size_type level;
     260};
     261
     262template<typename Key, typename Value, typename Note>
     263class ScopedMap<Key, Value, Note>::const_iterator :
     264                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     265        friend class ScopedMap;
     266        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     267        typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
     268        typedef typename ScopedMap::ScopeList scope_list;
     269        typedef typename scope_list::size_type size_type;
     270
     271        /// Checks if this iterator points to a valid item
     272        bool is_valid() const {
     273                return it != (*scopes)[level].map.end();
     274        }
     275
     276        /// Increments on invalid
     277        const_iterator & next_valid() {
     278                if ( ! is_valid() ) { ++(*this); }
     279                return *this;
     280        }
     281
     282        /// Decrements on invalid
     283        const_iterator & prev_valid() {
     284                if ( ! is_valid() ) { --(*this); }
     285                return *this;
     286        }
     287
     288        const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel)
     289                : scopes(&_scopes), it(_it), level(inLevel) {}
     290public:
     291        const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     292        const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     293        const_iterator & operator= (const iterator & that) {
     294                scopes = that.scopes; level = that.level; it = that.it;
     295                return *this;
     296        }
     297        const_iterator & operator= (const const_iterator & that) {
     298                scopes = that.scopes; level = that.level; it = that.it;
     299                return *this;
     300        }
     301
     302        const_reference operator* () { return *it; }
     303        const_pointer operator-> () { return it.operator->(); }
     304
     305        const_iterator & operator++ () {
     306                if ( it == (*scopes)[level].map.end() ) {
     307                        if ( level == 0 ) return *this;
     308                        --level;
     309                        it = (*scopes)[level].map.begin();
     310                } else {
     311                        ++it;
     312                }
     313                return next_valid();
     314        }
     315        const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     316
     317        const_iterator & operator-- () {
     318                // may fail if this is the begin iterator; allowed by STL spec
     319                if ( it == (*scopes)[level].map.begin() ) {
     320                        ++level;
     321                        it = (*scopes)[level].map.end();
     322                }
     323                --it;
     324                return prev_valid();
     325        }
     326        const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     327
     328        bool operator== (const const_iterator & that) const {
     329                return scopes == that.scopes && level == that.level && it == that.it;
     330        }
     331        bool operator!= (const const_iterator & that) const { return !( *this == that ); }
     332
     333        size_type get_level() const { return level; }
     334
     335        const Note & get_note() const { return (*scopes)[level].note; }
     336
     337private:
     338        scope_list const *scopes;
     339        wrapped_const_iterator it;
     340        size_type level;
     341};
     342
     343template<typename Key, typename Value, typename Note>
     344typename ScopedMap<Key, Value, Note>::iterator
     345                ScopedMap<Key, Value, Note>::erase( iterator pos ) {
     346        MapType & scope = (*pos.scopes)[ pos.level ].map;
     347        const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it );
     348        iterator it( *pos.scopes, new_it, pos.level );
     349        return it.next_valid();
     350}
    346351
    347352// Local Variables: //
Note: See TracChangeset for help on using the changeset viewer.