Changes in / [c3e44e6a:a8615fd1]


Ignore:
Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/ScopedMap.h

    rc3e44e6a ra8615fd1  
    2222#include <vector>
    2323
    24 /// Default (empty) ScopedMap note type
    25 struct EmptyNote {};
    26 
    2724/// A map where the items are placed into nested scopes;
    28 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward.
    29 /// Scopes may be annotated with a value; the annotation defaults to empty
    30 template<typename Key, typename Value, typename Note = EmptyNote>
     25/// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
     26template<typename Key, typename Value>
    3127class ScopedMap {
    32         typedef std::map< Key, Value > MapType;
    33         struct Scope {
    34                 MapType map;
    35                 Note note;     
    36         };
     28        typedef std::map< Key, Value > Scope;
    3729        typedef std::vector< Scope > ScopeList;
    3830
    3931        ScopeList scopes; ///< scoped list of maps
    4032public:
    41         typedef typename MapType::key_type key_type;
    42         typedef typename MapType::mapped_type mapped_type;
    43         typedef typename MapType::value_type value_type;
     33        typedef typename Scope::key_type key_type;
     34        typedef typename Scope::mapped_type mapped_type;
     35        typedef typename Scope::value_type value_type;
    4436        typedef typename ScopeList::size_type size_type;
    4537        typedef typename ScopeList::difference_type difference_type;
    46         typedef typename MapType::reference reference;
    47         typedef typename MapType::const_reference const_reference;
    48         typedef typename MapType::pointer pointer;
    49         typedef typename MapType::const_pointer const_pointer;
     38        typedef typename Scope::reference reference;
     39        typedef typename Scope::const_reference const_reference;
     40        typedef typename Scope::pointer pointer;
     41        typedef typename Scope::const_pointer const_pointer;
    5042
    5143        class iterator : public std::iterator< std::bidirectional_iterator_tag,
     
    5345        friend class ScopedMap;
    5446        friend class const_iterator;
    55                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    56                 typedef typename ScopedMap::ScopeList scope_list;
     47                typedef typename std::map< Key, Value >::iterator wrapped_iterator;
     48                typedef typename std::vector< std::map< Key, Value > > scope_list;
    5749                typedef typename scope_list::size_type size_type;
    5850
    5951                /// Checks if this iterator points to a valid item
    6052                bool is_valid() const {
    61                         return it != (*scopes)[level].map.end();
     53                        return it != (*scopes)[level].end();
    6254                }
    6355
     
    8779
    8880                iterator& operator++ () {
    89                         if ( it == (*scopes)[level].map.end() ) {
     81                        if ( it == (*scopes)[level].end() ) {
    9082                                if ( level == 0 ) return *this;
    9183                                --level;
    92                                 it = (*scopes)[level].map.begin();
     84                                it = (*scopes)[level].begin();
    9385                        } else {
    9486                                ++it;
     
    10092                iterator& operator-- () {
    10193                        // may fail if this is the begin iterator; allowed by STL spec
    102                         if ( it == (*scopes)[level].map.begin() ) {
     94                        if ( it == (*scopes)[level].begin() ) {
    10395                                ++level;
    104                                 it = (*scopes)[level].map.end();
     96                                it = (*scopes)[level].end();
    10597                        }
    10698                        --it;
     
    115107
    116108                size_type get_level() const { return level; }
    117 
    118                 Note& get_note() { return (*scopes)[level].note; }
    119                 const Note& get_note() const { return (*scopes)[level].note; }
    120109
    121110        private:
     
    128117                                                     value_type > {
    129118        friend class ScopedMap;
    130                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    131                 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
    132                 typedef typename ScopedMap::ScopeList scope_list;
     119                typedef typename std::map< Key, Value >::iterator wrapped_iterator;
     120                typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator;
     121                typedef typename std::vector< std::map< Key, Value > > scope_list;
    133122                typedef typename scope_list::size_type size_type;
    134123
    135124                /// Checks if this iterator points to a valid item
    136125                bool is_valid() const {
    137                         return it != (*scopes)[level].map.end();
     126                        return it != (*scopes)[level].end();
    138127                }
    139128
     
    168157
    169158                const_iterator& operator++ () {
    170                         if ( it == (*scopes)[level].map.end() ) {
     159                        if ( it == (*scopes)[level].end() ) {
    171160                                if ( level == 0 ) return *this;
    172161                                --level;
    173                                 it = (*scopes)[level].map.begin();
     162                                it = (*scopes)[level].begin();
    174163                        } else {
    175164                                ++it;
     
    181170                const_iterator& operator-- () {
    182171                        // may fail if this is the begin iterator; allowed by STL spec
    183                         if ( it == (*scopes)[level].map.begin() ) {
     172                        if ( it == (*scopes)[level].begin() ) {
    184173                                ++level;
    185                                 it = (*scopes)[level].map.end();
     174                                it = (*scopes)[level].end();
    186175                        }
    187176                        --it;
     
    196185
    197186                size_type get_level() const { return level; }
    198 
    199                 const Note& get_note() const { return (*scopes)[level].note; }
    200187
    201188        private:
     
    219206        ScopedMap() { beginScope(); }
    220207
    221         iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
    222         const_iterator begin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
    223         const_iterator cbegin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); }
    224         iterator end() { return iterator(scopes, scopes[0].map.end(), 0); }
    225         const_iterator end() const { return const_iterator(scopes, scopes[0].map.end(), 0); }
    226         const_iterator cend() const { return const_iterator(scopes, scopes[0].map.end(), 0); }
     208        iterator begin() { return iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
     209        const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
     210        const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
     211        iterator end() { return iterator(scopes, scopes[0].end(), 0); }
     212        const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
     213        const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); }
    227214
    228215        /// Gets the index of the current scope (counted from 1)
    229216        size_type currentScope() const { return scopes.size() - 1; }
    230 
    231         /// Gets the note at the given scope
    232         Note& getNote( size_type i ) { return scopes[i].note; }
    233         const Note& getNote( size_type i ) const { return scopes[i].note; }
    234217
    235218        /// Finds the given key in the outermost scope it occurs; returns end() for none such
    236219        iterator find( const Key &key ) {
    237220                for ( size_type i = scopes.size() - 1; ; --i ) {
    238                         typename MapType::iterator val = scopes[i].map.find( key );
    239                         if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
     221                        typename Scope::iterator val = scopes[i].find( key );
     222                        if ( val != scopes[i].end() ) return iterator( scopes, val, i );
    240223                        if ( i == 0 ) break;
    241224                }
     
    243226        }
    244227        const_iterator find( const Key &key ) const {
    245                         return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) );
     228                        return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
    246229        }
    247230
    248231        /// Finds the given key in the provided scope; returns end() for none such
    249232        iterator findAt( size_type scope, const Key& key ) {
    250                 typename MapType::iterator val = scopes[scope].map.find( key );
    251                 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope );
     233                typename Scope::iterator val = scopes[scope].find( key );
     234                if ( val != scopes[scope].end() ) return iterator( scopes, val, scope );
    252235                return end();
    253236        }
    254237        const_iterator findAt( size_type scope, const Key& key ) const {
    255                 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) );
     238                return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) );
    256239        }
    257240
     
    260243                if ( it.level == 0 ) return end();
    261244                for ( size_type i = it.level - 1; ; --i ) {
    262                         typename MapType::iterator val = scopes[i].map.find( key );
    263                         if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
     245                        typename Scope::iterator val = scopes[i].find( key );
     246                        if ( val != scopes[i].end() ) return iterator( scopes, val, i );
    264247                        if ( i == 0 ) break;
    265248                }
     
    267250        }
    268251        const_iterator findNext( const_iterator &it, const Key &key ) const {
    269                         return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) );
     252                        return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
    270253        }
    271254
     
    273256        template< typename value_type_t >
    274257        std::pair< iterator, bool > insert( value_type_t&& value ) {
    275                 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );
     258                std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );
    276259                return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
    277260        }
     
    279262        template< typename value_type_t >
    280263        std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) {
    281                 MapType& scope = (*at.scopes)[ at.level ].map;
    282                 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
     264                Scope& scope = (*at.scopes) [ at.level ];
     265                std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
    283266                return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
    284267        }
     
    289272        template< typename value_type_t >
    290273        std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
    291                 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );
     274                std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );
    292275                return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
    293276        }
     
    305288
    306289        iterator erase( iterator pos ) {
    307                 MapType& scope = (*pos.scopes)[ pos.level ].map;
     290                Scope& scope = (*pos.scopes) [ pos.level ];
    308291                const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
    309292                iterator it( *pos.scopes, new_it, pos.level );
  • src/Parser/TypedefTable.h

    rc3e44e6a ra8615fd1  
    2323
    2424class TypedefTable {
    25         typedef ScopedMap< std::string, int, int > KindTable;
     25        typedef ScopedMap< std::string, int > KindTable;
    2626        KindTable kindTable;   
    2727        unsigned int level = 0;
Note: See TracChangeset for help on using the changeset viewer.