Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/ScopedMap.h

    r5ba653c r933667d  
    1717#define _SCOPEDMAP_H
    1818
    19 #include <cassert>
    2019#include <iterator>
    2120#include <map>
     
    5150                        typedef typename scope_list::size_type size_type;
    5251
    53                         /// Checks if this iterator points to a valid item
    54                         bool is_valid() const {
    55                                 return it != (*scopes)[i].end();
    56                         }
    57 
    58                         /// Increments on invalid
    59                         iterator& next_valid() {
    60                                 if ( ! is_valid() ) { ++(*this); }
    61                                 return *this;
    62                         }
    63 
    64                         /// Decrements on invalid
    65                         iterator& prev_valid() {
    66                                 if ( ! is_valid() ) { --(*this); }
    67                                 return *this;
    68                         }
    69 
    7052                        iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
    7153                                : scopes(&_scopes), it(_it), i(_i) {}
     
    8567                                        --i;
    8668                                        it = (*scopes)[i].begin();
    87                                 } else {
    88                                         ++it;
    89                                 }
    90                                 return next_valid();
     69                                        return *this;
     70                                }
     71                                ++it;
     72                                return *this;
    9173                        }
    9274                        iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     
    9981                                }
    10082                                --it;
    101                                 return prev_valid();
     83                                return *this;
    10284                        }
    10385                        iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     
    122104                        typedef typename scope_list::size_type size_type;
    123105
    124                         /// Checks if this iterator points to a valid item
    125                         bool is_valid() const {
    126                                 return it != (*scopes)[i].end();
    127                         }
    128 
    129                         /// Increments on invalid
    130                         const_iterator& next_valid() {
    131                                 if ( ! is_valid() ) { ++(*this); }
    132                                 return *this;
    133                         }
    134 
    135                         /// Decrements on invalid
    136                         const_iterator& prev_valid() {
    137                                 if ( ! is_valid() ) { --(*this); }
    138                                 return *this;
    139                         }
    140 
    141106                        const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
    142107                                : scopes(&_scopes), it(_it), i(_i) {}
     
    161126                                        --i;
    162127                                        it = (*scopes)[i].begin();
    163                                 } else {
    164                                         ++it;
    165                                 }
    166                                 return next_valid();
     128                                        return *this;
     129                                }
     130                                ++it;
     131                                return *this;
    167132                        }
    168133                        const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     
    175140                                }
    176141                                --it;
    177                                 return prev_valid();
     142                                return *this;
    178143                        }
    179144                        const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     
    199164                void endScope() {
    200165                        scopes.pop_back();
    201                         assert( ! scopes.empty() );
    202166                }
    203167
     
    205169                ScopedMap() { beginScope(); }
    206170
    207                 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    208                 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    209                 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     171                iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
     172                const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
     173                const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
    210174                iterator end() { return iterator(scopes, scopes[0].end(), 0); }
    211175                const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
     
    224188                        return end();
    225189                }
    226                 const_iterator find( const Key &key ) const {
    227                                 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
    228                 }
     190                const_iterator find( const Key &key ) const { return const_iterator( find( key ) ); }
    229191               
    230192                /// Finds the given key in the outermost scope inside the given scope where it occurs
     
    238200                        return end();
    239201                }
    240                 const_iterator findNext( const_iterator &it, const Key &key ) const {
    241                                 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
    242                 }
     202                const_iterator findNext( const_iterator &it, const Key &key ) const { return const_iterator( findNext( it, key ) ); }
    243203
    244204                /// Inserts the given key-value pair into the outermost scope
     
    248208                }
    249209                std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); }
    250 
    251                 Value& operator[] ( const Key &key ) {
    252                         iterator slot = find( key );
    253                         if ( slot != end() ) return slot->second;
    254                         return insert( key, Value() ).first->second;
    255                 }
     210               
    256211        };
    257212} // namespace GenPoly
Note: See TracChangeset for help on using the changeset viewer.