Changeset 1f75e2d


Ignore:
Timestamp:
Sep 1, 2016, 3:29:10 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
5fda714
Parents:
e491159
Message:

fixed various issues in common

Location:
src/Common
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Common/ScopedMap.h

    re491159 r1f75e2d  
    5252                /// Checks if this iterator points to a valid item
    5353                bool is_valid() const {
    54                         return it != (*scopes)[i].end();
     54                        return it != (*scopes)[level].end();
    5555                }
    5656
     
    6767                }
    6868
    69                 iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type _i)
    70                         : scopes(&_scopes), it(_it), i(_i) {}
     69                iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type inLevel)
     70                        : scopes(&_scopes), it(_it), level(inLevel) {}
    7171        public:
    72                 iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
     72                iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
    7373                iterator& operator= (const iterator &that) {
    74                         scopes = that.scopes; i = that.i; it = that.it;
     74                        scopes = that.scopes; level = that.level; it = that.it;
    7575                        return *this;
    7676                }
     
    8080
    8181                iterator& operator++ () {
    82                         if ( it == (*scopes)[i].end() ) {
    83                                 if ( i == 0 ) return *this;
    84                                 --i;
    85                                 it = (*scopes)[i].begin();
     82                        if ( it == (*scopes)[level].end() ) {
     83                                if ( level == 0 ) return *this;
     84                                --level;
     85                                it = (*scopes)[level].begin();
    8686                        } else {
    8787                                ++it;
     
    9393                iterator& operator-- () {
    9494                        // may fail if this is the begin iterator; allowed by STL spec
    95                         if ( it == (*scopes)[i].begin() ) {
    96                                 ++i;
    97                                 it = (*scopes)[i].end();
     95                        if ( it == (*scopes)[level].begin() ) {
     96                                ++level;
     97                                it = (*scopes)[level].end();
    9898                        }
    9999                        --it;
     
    103103
    104104                bool operator== (const iterator &that) {
    105                         return scopes == that.scopes && i == that.i && it == that.it;
     105                        return scopes == that.scopes && level == that.level && it == that.it;
    106106                }
    107107                bool operator!= (const iterator &that) { return !( *this == that ); }
     108
     109                size_type get_level() const { return level; }
    108110
    109111        private:
    110112                scope_list *scopes;
    111113                wrapped_iterator it;
    112                 size_type i;
     114                size_type level;
    113115        };
    114116
     
    123125                /// Checks if this iterator points to a valid item
    124126                bool is_valid() const {
    125                         return it != (*scopes)[i].end();
     127                        return it != (*scopes)[level].end();
    126128                }
    127129
     
    138140                }
    139141
    140                 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
    141                         : scopes(&_scopes), it(_it), i(_i) {}
     142                const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type inLevel)
     143                        : scopes(&_scopes), it(_it), level(inLevel) {}
    142144        public:
    143                 const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
    144                 const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
     145                const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
     146                const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {}
    145147                const_iterator& operator= (const iterator &that) {
    146                         scopes = that.scopes; i = that.i; it = that.it;
     148                        scopes = that.scopes; level = that.level; it = that.it;
    147149                        return *this;
    148150                }
    149151                const_iterator& operator= (const const_iterator &that) {
    150                         scopes = that.scopes; i = that.i; it = that.it;
     152                        scopes = that.scopes; level = that.level; it = that.it;
    151153                        return *this;
    152154                }
     
    156158
    157159                const_iterator& operator++ () {
    158                         if ( it == (*scopes)[i].end() ) {
    159                                 if ( i == 0 ) return *this;
    160                                 --i;
    161                                 it = (*scopes)[i].begin();
     160                        if ( it == (*scopes)[level].end() ) {
     161                                if ( level == 0 ) return *this;
     162                                --level;
     163                                it = (*scopes)[level].begin();
    162164                        } else {
    163165                                ++it;
     
    169171                const_iterator& operator-- () {
    170172                        // may fail if this is the begin iterator; allowed by STL spec
    171                         if ( it == (*scopes)[i].begin() ) {
    172                                 ++i;
    173                                 it = (*scopes)[i].end();
     173                        if ( it == (*scopes)[level].begin() ) {
     174                                ++level;
     175                                it = (*scopes)[level].end();
    174176                        }
    175177                        --it;
     
    179181
    180182                bool operator== (const const_iterator &that) {
    181                         return scopes == that.scopes && i == that.i && it == that.it;
     183                        return scopes == that.scopes && level == that.level && it == that.it;
    182184                }
    183185                bool operator!= (const const_iterator &that) { return !( *this == that ); }
     186
     187                size_type get_level() const { return level; }
    184188
    185189        private:
    186190                scope_list const *scopes;
    187191                wrapped_const_iterator it;
    188                 size_type i;
     192                size_type level;
    189193        };
    190194
     
    203207        ScopedMap() { beginScope(); }
    204208
    205         iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    206         const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    207         const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     209        iterator begin() { return iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
     210        const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
     211        const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); }
    208212        iterator end() { return iterator(scopes, scopes[0].end(), 0); }
    209213        const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
     
    211215
    212216        /// Gets the index of the current scope (counted from 1)
    213         size_type currentScope() const { return scopes.size(); }
     217        size_type currentScope() const { return scopes.size() - 1; }
    214218
    215219        /// Finds the given key in the outermost scope it occurs; returns end() for none such
     
    228232        /// Finds the given key in the outermost scope inside the given scope where it occurs
    229233        iterator findNext( const_iterator &it, const Key &key ) {
    230                 if ( it.i == 0 ) return end();
    231                 for ( size_type i = it.i - 1; ; --i ) {
     234                if ( it.level == 0 ) return end();
     235                for ( size_type i = it.level - 1; ; --i ) {
    232236                        typename Scope::iterator val = scopes[i].find( key );
    233237                        if ( val != scopes[i].end() ) return iterator( scopes, val, i );
     
    241245
    242246        /// Inserts the given key-value pair into the outermost scope
    243         std::pair< iterator, bool > insert( const value_type &value ) {
    244                 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value );
    245                 return std::make_pair( iterator(scopes, res.first, scopes.size()-1), res.second );
    246         }
    247 
    248         std::pair< iterator, bool > insert( value_type &&value ) {
    249                 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::move( value ) );
     247        template< typename value_type_t >
     248        std::pair< iterator, bool > insert( value_type_t&& value ) {
     249                std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );
    250250                return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
    251251        }
    252252
    253         std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); }
    254         std::pair< iterator, bool > insert( const Key &key, Value &&value ) { return insert( std::make_pair( key, std::move( value ) ) ); }
     253        template< typename value_type_t >
     254        std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) {
     255                Scope& scope = (*at.scopes) [ at.level ];
     256                std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
     257                return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
     258        }
     259
     260        template< typename value_t >
     261        std::pair< iterator, bool > insert( const Key &key, value_t&& value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
     262
     263        template< typename value_type_t >
     264        std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
     265                std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );
     266                return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
     267        }
    255268
    256269        Value& operator[] ( const Key &key ) {
     
    261274
    262275        iterator erase( iterator pos ) {
    263                 Scope& scope = (*pos.scopes) [ pos.i ];
     276                Scope& scope = (*pos.scopes) [ pos.level ];
    264277                const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
    265                 iterator it( *pos.scopes, new_it, pos.i );
     278                iterator it( *pos.scopes, new_it, pos.level );
    266279                return it.next_valid();
    267280        }
  • src/Common/utility.h

    re491159 r1f75e2d  
    262262};
    263263
     264template< typename ThisType >
     265std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance;
     266
    264267#endif // _UTILITY_H
    265268
Note: See TracChangeset for help on using the changeset viewer.