Changeset 1f75e2d
- Timestamp:
- Sep 1, 2016, 3:29:10 PM (8 years ago)
- 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:
- 5fda7143
- Parents:
- e491159
- Location:
- src/Common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ScopedMap.h
re491159 r1f75e2d 52 52 /// Checks if this iterator points to a valid item 53 53 bool is_valid() const { 54 return it != (*scopes)[ i].end();54 return it != (*scopes)[level].end(); 55 55 } 56 56 … … 67 67 } 68 68 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) {} 71 71 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) {} 73 73 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; 75 75 return *this; 76 76 } … … 80 80 81 81 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(); 86 86 } else { 87 87 ++it; … … 93 93 iterator& operator-- () { 94 94 // 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(); 98 98 } 99 99 --it; … … 103 103 104 104 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; 106 106 } 107 107 bool operator!= (const iterator &that) { return !( *this == that ); } 108 109 size_type get_level() const { return level; } 108 110 109 111 private: 110 112 scope_list *scopes; 111 113 wrapped_iterator it; 112 size_type i;114 size_type level; 113 115 }; 114 116 … … 123 125 /// Checks if this iterator points to a valid item 124 126 bool is_valid() const { 125 return it != (*scopes)[ i].end();127 return it != (*scopes)[level].end(); 126 128 } 127 129 … … 138 140 } 139 141 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) {} 142 144 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) {} 145 147 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; 147 149 return *this; 148 150 } 149 151 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; 151 153 return *this; 152 154 } … … 156 158 157 159 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(); 162 164 } else { 163 165 ++it; … … 169 171 const_iterator& operator-- () { 170 172 // 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(); 174 176 } 175 177 --it; … … 179 181 180 182 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; 182 184 } 183 185 bool operator!= (const const_iterator &that) { return !( *this == that ); } 186 187 size_type get_level() const { return level; } 184 188 185 189 private: 186 190 scope_list const *scopes; 187 191 wrapped_const_iterator it; 188 size_type i;192 size_type level; 189 193 }; 190 194 … … 203 207 ScopedMap() { beginScope(); } 204 208 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(); } 208 212 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 209 213 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } … … 211 215 212 216 /// 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; } 214 218 215 219 /// Finds the given key in the outermost scope it occurs; returns end() for none such … … 228 232 /// Finds the given key in the outermost scope inside the given scope where it occurs 229 233 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 ) { 232 236 typename Scope::iterator val = scopes[i].find( key ); 233 237 if ( val != scopes[i].end() ) return iterator( scopes, val, i ); … … 241 245 242 246 /// 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 ) ); 250 250 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); 251 251 } 252 252 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 } 255 268 256 269 Value& operator[] ( const Key &key ) { … … 261 274 262 275 iterator erase( iterator pos ) { 263 Scope& scope = (*pos.scopes) [ pos. i];276 Scope& scope = (*pos.scopes) [ pos.level ]; 264 277 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 ); 266 279 return it.next_valid(); 267 280 } -
src/Common/utility.h
re491159 r1f75e2d 262 262 }; 263 263 264 template< typename ThisType > 265 std::weak_ptr<ThisType> RefCountSingleton<ThisType>::global_instance; 266 264 267 #endif // _UTILITY_H 265 268
Note: See TracChangeset
for help on using the changeset viewer.