Ignore:
Timestamp:
Feb 5, 2023, 11:42:15 AM (3 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
8fa77eb
Parents:
9ef5516 (diff), 35d1de5 (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

    r9ef5516 r6d2af204  
    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();
     169        /// Erases element with key in the innermost scope that has it.
     170        size_type erase( const Key & key ) {
     171                for ( auto it = scopes.rbegin() ; it != scopes.rend() ; ++it ) {
     172                        if ( size_type i = it->map.erase( key ) ; 0 != i ) return i;
     173                }
     174                return 0;
    331175        }
    332176
     
    343187                return c;
    344188        }
     189
     190        bool contains( const Key & key ) const {
     191                return find( key ) != cend();
     192        }
     193};
     194
     195template<typename Key, typename Value, typename Note>
     196class ScopedMap<Key, Value, Note>::iterator :
     197                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     198        friend class ScopedMap;
     199        friend class const_iterator;
     200        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     201        typedef typename ScopedMap::ScopeList scope_list;
     202        typedef typename scope_list::size_type size_type;
     203
     204        /// Checks if this iterator points to a valid item
     205        bool is_valid() const {
     206                return it != (*scopes)[level].map.end();
     207        }
     208
     209        /// Increments on invalid
     210        iterator & next_valid() {
     211                if ( ! is_valid() ) { ++(*this); }
     212                return *this;
     213        }
     214
     215        /// Decrements on invalid
     216        iterator & prev_valid() {
     217                if ( ! is_valid() ) { --(*this); }
     218                return *this;
     219        }
     220
     221        iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel)
     222                : scopes(&_scopes), it(_it), level(inLevel) {}
     223public:
     224        iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     225        iterator & operator= (const iterator & that) {
     226                scopes = that.scopes; level = that.level; it = that.it;
     227                return *this;
     228        }
     229
     230        reference operator* () { return *it; }
     231        pointer operator-> () const { return it.operator->(); }
     232
     233        iterator & operator++ () {
     234                if ( it == (*scopes)[level].map.end() ) {
     235                        if ( level == 0 ) return *this;
     236                        --level;
     237                        it = (*scopes)[level].map.begin();
     238                } else {
     239                        ++it;
     240                }
     241                return next_valid();
     242        }
     243        iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     244
     245        iterator & operator-- () {
     246                // may fail if this is the begin iterator; allowed by STL spec
     247                if ( it == (*scopes)[level].map.begin() ) {
     248                        ++level;
     249                        it = (*scopes)[level].map.end();
     250                }
     251                --it;
     252                return prev_valid();
     253        }
     254        iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     255
     256        bool operator== (const iterator & that) const {
     257                return scopes == that.scopes && level == that.level && it == that.it;
     258        }
     259        bool operator!= (const iterator & that) const { return !( *this == that ); }
     260
     261        size_type get_level() const { return level; }
     262
     263        Note & get_note() { return (*scopes)[level].note; }
     264        const Note & get_note() const { return (*scopes)[level].note; }
     265
     266private:
     267        scope_list *scopes;
     268        wrapped_iterator it;
     269        size_type level;
     270};
     271
     272template<typename Key, typename Value, typename Note>
     273class ScopedMap<Key, Value, Note>::const_iterator :
     274                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     275        friend class ScopedMap;
     276        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     277        typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
     278        typedef typename ScopedMap::ScopeList scope_list;
     279        typedef typename scope_list::size_type size_type;
     280
     281        /// Checks if this iterator points to a valid item
     282        bool is_valid() const {
     283                return it != (*scopes)[level].map.end();
     284        }
     285
     286        /// Increments on invalid
     287        const_iterator & next_valid() {
     288                if ( ! is_valid() ) { ++(*this); }
     289                return *this;
     290        }
     291
     292        /// Decrements on invalid
     293        const_iterator & prev_valid() {
     294                if ( ! is_valid() ) { --(*this); }
     295                return *this;
     296        }
     297
     298        const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel)
     299                : scopes(&_scopes), it(_it), level(inLevel) {}
     300public:
     301        const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     302        const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     303        const_iterator & operator= (const iterator & that) {
     304                scopes = that.scopes; level = that.level; it = that.it;
     305                return *this;
     306        }
     307        const_iterator & operator= (const const_iterator & that) {
     308                scopes = that.scopes; level = that.level; it = that.it;
     309                return *this;
     310        }
     311
     312        const_reference operator* () { return *it; }
     313        const_pointer operator-> () { return it.operator->(); }
     314
     315        const_iterator & operator++ () {
     316                if ( it == (*scopes)[level].map.end() ) {
     317                        if ( level == 0 ) return *this;
     318                        --level;
     319                        it = (*scopes)[level].map.begin();
     320                } else {
     321                        ++it;
     322                }
     323                return next_valid();
     324        }
     325        const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     326
     327        const_iterator & operator-- () {
     328                // may fail if this is the begin iterator; allowed by STL spec
     329                if ( it == (*scopes)[level].map.begin() ) {
     330                        ++level;
     331                        it = (*scopes)[level].map.end();
     332                }
     333                --it;
     334                return prev_valid();
     335        }
     336        const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     337
     338        bool operator== (const const_iterator & that) const {
     339                return scopes == that.scopes && level == that.level && it == that.it;
     340        }
     341        bool operator!= (const const_iterator & that) const { return !( *this == that ); }
     342
     343        size_type get_level() const { return level; }
     344
     345        const Note & get_note() const { return (*scopes)[level].note; }
     346
     347private:
     348        scope_list const *scopes;
     349        wrapped_const_iterator it;
     350        size_type level;
    345351};
    346352
Note: See TracChangeset for help on using the changeset viewer.