Changeset df00c78 for src/Common
- Timestamp:
- Aug 8, 2022, 5:03:15 PM (2 years ago)
- Branches:
- ADT, ast-experimental, master, pthread-emulation
- Children:
- 66406f3
- Parents:
- ba48a9b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ScopedMap.h
rba48a9b rdf00c78 10 10 // Created On : Wed Dec 2 11:37:00 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon May 21 15:22:40 201813 // Update Count : 312 // Last Modified On : Tue Feb 15 08:41:28 2022 13 // Update Count : 5 14 14 // 15 15 … … 36 36 37 37 template<typename N> 38 Scope(N && n) : map(), note(std::forward<N>(n)) {}38 Scope(N && n) : map(), note(std::forward<N>(n)) {} 39 39 40 40 Scope() = default; 41 Scope(const Scope &) = default;42 Scope(Scope &&) = default;43 Scope & operator= (const Scope&) = default;44 Scope & operator= (Scope&&) = default;41 Scope(const Scope &) = default; 42 Scope(Scope &&) = default; 43 Scope & operator= (const Scope &) = default; 44 Scope & operator= (Scope &&) = default; 45 45 }; 46 46 typedef std::vector< Scope > ScopeList; … … 58 58 typedef typename MapType::const_pointer const_pointer; 59 59 60 class iterator : public std::iterator< std::bidirectional_iterator_tag, 61 value_type > { 60 class iterator : public std::iterator< std::bidirectional_iterator_tag, value_type > { 62 61 friend class ScopedMap; 63 62 friend class const_iterator; … … 72 71 73 72 /// Increments on invalid 74 iterator & next_valid() {73 iterator & next_valid() { 75 74 if ( ! is_valid() ) { ++(*this); } 76 75 return *this; … … 78 77 79 78 /// Decrements on invalid 80 iterator & prev_valid() {79 iterator & prev_valid() { 81 80 if ( ! is_valid() ) { --(*this); } 82 81 return *this; 83 82 } 84 83 85 iterator(scope_list & _scopes, const wrapped_iterator &_it, size_type inLevel)84 iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel) 86 85 : scopes(&_scopes), it(_it), level(inLevel) {} 87 86 public: 88 iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}89 iterator & operator= (const iterator &that) {87 iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} 88 iterator & operator= (const iterator & that) { 90 89 scopes = that.scopes; level = that.level; it = that.it; 91 90 return *this; … … 95 94 pointer operator-> () const { return it.operator->(); } 96 95 97 iterator & operator++ () {96 iterator & operator++ () { 98 97 if ( it == (*scopes)[level].map.end() ) { 99 98 if ( level == 0 ) return *this; … … 107 106 iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } 108 107 109 iterator & operator-- () {108 iterator & operator-- () { 110 109 // may fail if this is the begin iterator; allowed by STL spec 111 110 if ( it == (*scopes)[level].map.begin() ) { … … 118 117 iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; } 119 118 120 bool operator== (const iterator & that) const {119 bool operator== (const iterator & that) const { 121 120 return scopes == that.scopes && level == that.level && it == that.it; 122 121 } 123 bool operator!= (const iterator & that) const { return !( *this == that ); }122 bool operator!= (const iterator & that) const { return !( *this == that ); } 124 123 125 124 size_type get_level() const { return level; } 126 125 127 Note & get_note() { return (*scopes)[level].note; }128 const Note & get_note() const { return (*scopes)[level].note; }126 Note & get_note() { return (*scopes)[level].note; } 127 const Note & get_note() const { return (*scopes)[level].note; } 129 128 130 129 private: … … 148 147 149 148 /// Increments on invalid 150 const_iterator & next_valid() {149 const_iterator & next_valid() { 151 150 if ( ! is_valid() ) { ++(*this); } 152 151 return *this; … … 154 153 155 154 /// Decrements on invalid 156 const_iterator & prev_valid() {155 const_iterator & prev_valid() { 157 156 if ( ! is_valid() ) { --(*this); } 158 157 return *this; 159 158 } 160 159 161 const_iterator(scope_list const & _scopes, const wrapped_const_iterator &_it, size_type inLevel)160 const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel) 162 161 : scopes(&_scopes), it(_it), level(inLevel) {} 163 162 public: 164 const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}165 const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}166 const_iterator & operator= (const iterator &that) {163 const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} 164 const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} 165 const_iterator & operator= (const iterator & that) { 167 166 scopes = that.scopes; level = that.level; it = that.it; 168 167 return *this; 169 168 } 170 const_iterator & operator= (const const_iterator &that) {169 const_iterator & operator= (const const_iterator & that) { 171 170 scopes = that.scopes; level = that.level; it = that.it; 172 171 return *this; … … 176 175 const_pointer operator-> () { return it.operator->(); } 177 176 178 const_iterator & operator++ () {177 const_iterator & operator++ () { 179 178 if ( it == (*scopes)[level].map.end() ) { 180 179 if ( level == 0 ) return *this; … … 188 187 const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } 189 188 190 const_iterator & operator-- () {189 const_iterator & operator-- () { 191 190 // may fail if this is the begin iterator; allowed by STL spec 192 191 if ( it == (*scopes)[level].map.begin() ) { … … 199 198 const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } 200 199 201 bool operator== (const const_iterator & that) const {200 bool operator== (const const_iterator & that) const { 202 201 return scopes == that.scopes && level == that.level && it == that.it; 203 202 } 204 bool operator!= (const const_iterator & that) const { return !( *this == that ); }203 bool operator!= (const const_iterator & that) const { return !( *this == that ); } 205 204 206 205 size_type get_level() const { return level; } 207 206 208 const Note & get_note() const { return (*scopes)[level].note; }207 const Note & get_note() const { return (*scopes)[level].note; } 209 208 210 209 private: … … 221 220 // Starts a new scope with the given note 222 221 template<typename N> 223 void beginScope( N && n ) {222 void beginScope( N && n ) { 224 223 scopes.emplace_back( std::forward<N>(n) ); 225 224 } … … 236 235 /// Constructs with a given note on the outermost scope 237 236 template<typename N> 238 ScopedMap( N && n ) : scopes() { beginScope(std::forward<N>(n)); }237 ScopedMap( N && n ) : scopes() { beginScope(std::forward<N>(n)); } 239 238 240 239 iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } … … 249 248 250 249 /// Gets the note at the given scope 251 Note & getNote() { return scopes.back().note; }252 const Note & getNote() const { return scopes.back().note; }253 Note & getNote( size_type i ) { return scopes[i].note; }254 const Note & getNote( size_type i ) const { return scopes[i].note; }250 Note & getNote() { return scopes.back().note; } 251 const Note & getNote() const { return scopes.back().note; } 252 Note & getNote( size_type i ) { return scopes[i].note; } 253 const Note & getNote( size_type i ) const { return scopes[i].note; } 255 254 256 255 /// Finds the given key in the outermost scope it occurs; returns end() for none such 257 iterator find( const Key & key ) {256 iterator find( const Key & key ) { 258 257 for ( size_type i = scopes.size() - 1; ; --i ) { 259 258 typename MapType::iterator val = scopes[i].map.find( key ); … … 263 262 return end(); 264 263 } 265 const_iterator find( const Key & key ) const {264 const_iterator find( const Key & key ) const { 266 265 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) ); 267 266 } 268 267 269 268 /// Finds the given key in the provided scope; returns end() for none such 270 iterator findAt( size_type scope, const Key & key ) {269 iterator findAt( size_type scope, const Key & key ) { 271 270 typename MapType::iterator val = scopes[scope].map.find( key ); 272 271 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope ); 273 272 return end(); 274 273 } 275 const_iterator findAt( size_type scope, const Key & key ) const {274 const_iterator findAt( size_type scope, const Key & key ) const { 276 275 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) ); 277 276 } 278 277 279 278 /// Finds the given key in the outermost scope inside the given scope where it occurs 280 iterator findNext( const_iterator & it, const Key &key ) {279 iterator findNext( const_iterator & it, const Key & key ) { 281 280 if ( it.level == 0 ) return end(); 282 281 for ( size_type i = it.level - 1; ; --i ) { … … 287 286 return end(); 288 287 } 289 const_iterator findNext( const_iterator & it, const Key &key ) const {288 const_iterator findNext( const_iterator & it, const Key & key ) const { 290 289 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) ); 291 290 } … … 293 292 /// Inserts the given key-value pair into the outermost scope 294 293 template< typename value_type_t > 295 std::pair< iterator, bool > insert( value_type_t && value ) {294 std::pair< iterator, bool > insert( value_type_t && value ) { 296 295 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) ); 297 296 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); … … 299 298 300 299 template< typename value_type_t > 301 std::pair< iterator, bool > insert( iterator at, value_type_t && value ) {302 MapType & scope = (*at.scopes)[ at.level ].map;300 std::pair< iterator, bool > insert( iterator at, value_type_t && value ) { 301 MapType & scope = (*at.scopes)[ at.level ].map; 303 302 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) ); 304 303 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); … … 306 305 307 306 template< typename value_t > 308 std::pair< iterator, bool > insert( const Key & key, value_t&& value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }307 std::pair< iterator, bool > insert( const Key & key, value_t && value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); } 309 308 310 309 template< typename value_type_t > 311 std::pair< iterator, bool > insertAt( size_type scope, value_type_t && value ) {310 std::pair< iterator, bool > insertAt( size_type scope, value_type_t && value ) { 312 311 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) ); 313 312 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); … … 315 314 316 315 template< typename value_t > 317 std::pair< iterator, bool > insertAt( size_type scope, const Key & key, value_t&& value ) {316 std::pair< iterator, bool > insertAt( size_type scope, const Key & key, value_t && value ) { 318 317 return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) ); 319 318 } 320 319 321 Value & operator[] ( const Key &key ) {320 Value & operator[] ( const Key & key ) { 322 321 iterator slot = find( key ); 323 322 if ( slot != end() ) return slot->second; … … 326 325 327 326 iterator erase( iterator pos ) { 328 MapType & scope = (*pos.scopes)[ pos.level ].map;329 const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it );327 MapType & scope = (*pos.scopes)[ pos.level ].map; 328 const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it ); 330 329 iterator it( *pos.scopes, new_it, pos.level ); 331 330 return it.next_valid(); 332 331 } 333 332 334 size_type count( const Key & key ) const {333 size_type count( const Key & key ) const { 335 334 size_type c = 0; 336 335 auto it = find( key ); … … 344 343 return c; 345 344 } 346 347 345 }; 348 346
Note: See TracChangeset
for help on using the changeset viewer.