Changes in / [6fd8b0f:242f705]


Ignore:
Location:
src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/ScopedMap.h

    r6fd8b0f r242f705  
    2222#include <vector>
    2323
     24/// Default (empty) ScopedMap note type
     25struct EmptyNote {};
     26
    2427/// A map where the items are placed into nested scopes;
    25 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
    26 template<typename Key, typename Value>
     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
     30template<typename Key, typename Value, typename Note = EmptyNote>
    2731class ScopedMap {
    28         typedef std::map< Key, Value > Scope;
     32        typedef std::map< Key, Value > MapType;
     33        struct Scope {
     34                MapType map;
     35                Note note;     
     36        };
    2937        typedef std::vector< Scope > ScopeList;
    3038
    3139        ScopeList scopes; ///< scoped list of maps
    3240public:
    33         typedef typename Scope::key_type key_type;
    34         typedef typename Scope::mapped_type mapped_type;
    35         typedef typename Scope::value_type value_type;
     41        typedef typename MapType::key_type key_type;
     42        typedef typename MapType::mapped_type mapped_type;
     43        typedef typename MapType::value_type value_type;
    3644        typedef typename ScopeList::size_type size_type;
    3745        typedef typename ScopeList::difference_type difference_type;
    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;
     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;
    4250
    4351        class iterator : public std::iterator< std::bidirectional_iterator_tag,
     
    4553        friend class ScopedMap;
    4654        friend class const_iterator;
    47                 typedef typename std::map< Key, Value >::iterator wrapped_iterator;
    48                 typedef typename std::vector< std::map< Key, Value > > scope_list;
     55                typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     56                typedef typename ScopedMap::ScopeList scope_list;
    4957                typedef typename scope_list::size_type size_type;
    5058
    5159                /// Checks if this iterator points to a valid item
    5260                bool is_valid() const {
    53                         return it != (*scopes)[level].end();
     61                        return it != (*scopes)[level].map.end();
    5462                }
    5563
     
    7987
    8088                iterator& operator++ () {
    81                         if ( it == (*scopes)[level].end() ) {
     89                        if ( it == (*scopes)[level].map.end() ) {
    8290                                if ( level == 0 ) return *this;
    8391                                --level;
    84                                 it = (*scopes)[level].begin();
     92                                it = (*scopes)[level].map.begin();
    8593                        } else {
    8694                                ++it;
     
    92100                iterator& operator-- () {
    93101                        // may fail if this is the begin iterator; allowed by STL spec
    94                         if ( it == (*scopes)[level].begin() ) {
     102                        if ( it == (*scopes)[level].map.begin() ) {
    95103                                ++level;
    96                                 it = (*scopes)[level].end();
     104                                it = (*scopes)[level].map.end();
    97105                        }
    98106                        --it;
     
    107115
    108116                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; }
    109120
    110121        private:
     
    117128                                                     value_type > {
    118129        friend class ScopedMap;
    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;
     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;
    122133                typedef typename scope_list::size_type size_type;
    123134
    124135                /// Checks if this iterator points to a valid item
    125136                bool is_valid() const {
    126                         return it != (*scopes)[level].end();
     137                        return it != (*scopes)[level].map.end();
    127138                }
    128139
     
    157168
    158169                const_iterator& operator++ () {
    159                         if ( it == (*scopes)[level].end() ) {
     170                        if ( it == (*scopes)[level].map.end() ) {
    160171                                if ( level == 0 ) return *this;
    161172                                --level;
    162                                 it = (*scopes)[level].begin();
     173                                it = (*scopes)[level].map.begin();
    163174                        } else {
    164175                                ++it;
     
    170181                const_iterator& operator-- () {
    171182                        // may fail if this is the begin iterator; allowed by STL spec
    172                         if ( it == (*scopes)[level].begin() ) {
     183                        if ( it == (*scopes)[level].map.begin() ) {
    173184                                ++level;
    174                                 it = (*scopes)[level].end();
     185                                it = (*scopes)[level].map.end();
    175186                        }
    176187                        --it;
     
    185196
    186197                size_type get_level() const { return level; }
     198
     199                const Note& get_note() const { return (*scopes)[level].note; }
    187200
    188201        private:
     
    206219        ScopedMap() { beginScope(); }
    207220
    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); }
     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); }
    214227
    215228        /// Gets the index of the current scope (counted from 1)
    216229        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; }
    217234
    218235        /// Finds the given key in the outermost scope it occurs; returns end() for none such
    219236        iterator find( const Key &key ) {
    220237                for ( size_type i = scopes.size() - 1; ; --i ) {
    221                         typename Scope::iterator val = scopes[i].find( key );
    222                         if ( val != scopes[i].end() ) return iterator( scopes, val, i );
     238                        typename MapType::iterator val = scopes[i].map.find( key );
     239                        if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
    223240                        if ( i == 0 ) break;
    224241                }
     
    226243        }
    227244        const_iterator find( const Key &key ) const {
    228                         return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
     245                        return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) );
    229246        }
    230247
    231248        /// Finds the given key in the provided scope; returns end() for none such
    232249        iterator findAt( size_type scope, const Key& key ) {
    233                 typename Scope::iterator val = scopes[scope].find( key );
    234                 if ( val != scopes[scope].end() ) return iterator( scopes, val, scope );
     250                typename MapType::iterator val = scopes[scope].map.find( key );
     251                if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope );
    235252                return end();
    236253        }
    237254        const_iterator findAt( size_type scope, const Key& key ) const {
    238                 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) );
     255                return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) );
    239256        }
    240257
     
    243260                if ( it.level == 0 ) return end();
    244261                for ( size_type i = it.level - 1; ; --i ) {
    245                         typename Scope::iterator val = scopes[i].find( key );
    246                         if ( val != scopes[i].end() ) return iterator( scopes, val, i );
     262                        typename MapType::iterator val = scopes[i].map.find( key );
     263                        if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
    247264                        if ( i == 0 ) break;
    248265                }
     
    250267        }
    251268        const_iterator findNext( const_iterator &it, const Key &key ) const {
    252                         return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
     269                        return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) );
    253270        }
    254271
     
    256273        template< typename value_type_t >
    257274        std::pair< iterator, bool > insert( value_type_t&& value ) {
    258                 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );
     275                std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );
    259276                return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
    260277        }
     
    262279        template< typename value_type_t >
    263280        std::pair< iterator, bool > insert( iterator at, 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 ) );
     281                MapType& scope = (*at.scopes)[ at.level ].map;
     282                std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
    266283                return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
    267284        }
     
    272289        template< typename value_type_t >
    273290        std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
    274                 std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );
     291                std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );
    275292                return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
    276293        }
     
    288305
    289306        iterator erase( iterator pos ) {
    290                 Scope& scope = (*pos.scopes) [ pos.level ];
     307                MapType& scope = (*pos.scopes)[ pos.level ].map;
    291308                const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
    292309                iterator it( *pos.scopes, new_it, pos.level );
  • src/Parser/TypedefTable.h

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