Changes in src/Common/ScopedMap.h [6dba8755:df00c78]
- File:
-
- 1 edited
-
src/Common/ScopedMap.h (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ScopedMap.h
r6dba8755 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( size_type i ) { return scopes[i].note; } 252 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; } 253 254 254 255 /// Finds the given key in the outermost scope it occurs; returns end() for none such 255 iterator find( const Key & key ) {256 iterator find( const Key & key ) { 256 257 for ( size_type i = scopes.size() - 1; ; --i ) { 257 258 typename MapType::iterator val = scopes[i].map.find( key ); … … 261 262 return end(); 262 263 } 263 const_iterator find( const Key & key ) const {264 const_iterator find( const Key & key ) const { 264 265 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) ); 265 266 } 266 267 267 268 /// Finds the given key in the provided scope; returns end() for none such 268 iterator findAt( size_type scope, const Key & key ) {269 iterator findAt( size_type scope, const Key & key ) { 269 270 typename MapType::iterator val = scopes[scope].map.find( key ); 270 271 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope ); 271 272 return end(); 272 273 } 273 const_iterator findAt( size_type scope, const Key & key ) const {274 const_iterator findAt( size_type scope, const Key & key ) const { 274 275 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) ); 275 276 } 276 277 277 278 /// Finds the given key in the outermost scope inside the given scope where it occurs 278 iterator findNext( const_iterator & it, const Key &key ) {279 iterator findNext( const_iterator & it, const Key & key ) { 279 280 if ( it.level == 0 ) return end(); 280 281 for ( size_type i = it.level - 1; ; --i ) { … … 285 286 return end(); 286 287 } 287 const_iterator findNext( const_iterator & it, const Key &key ) const {288 const_iterator findNext( const_iterator & it, const Key & key ) const { 288 289 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) ); 289 290 } … … 291 292 /// Inserts the given key-value pair into the outermost scope 292 293 template< typename value_type_t > 293 std::pair< iterator, bool > insert( value_type_t && value ) {294 std::pair< iterator, bool > insert( value_type_t && value ) { 294 295 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) ); 295 296 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); … … 297 298 298 299 template< typename value_type_t > 299 std::pair< iterator, bool > insert( iterator at, value_type_t && value ) {300 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; 301 302 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) ); 302 303 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); … … 304 305 305 306 template< typename value_t > 306 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 ) ) ); } 307 308 308 309 template< typename value_type_t > 309 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 ) { 310 311 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) ); 311 312 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); … … 313 314 314 315 template< typename value_t > 315 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 ) { 316 317 return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) ); 317 318 } 318 319 319 Value & operator[] ( const Key &key ) {320 Value & operator[] ( const Key & key ) { 320 321 iterator slot = find( key ); 321 322 if ( slot != end() ) return slot->second; … … 324 325 325 326 iterator erase( iterator pos ) { 326 MapType & scope = (*pos.scopes)[ pos.level ].map;327 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 ); 328 329 iterator it( *pos.scopes, new_it, pos.level ); 329 330 return it.next_valid(); 330 331 } 331 332 332 size_type count( const Key & key ) const {333 size_type count( const Key & key ) const { 333 334 size_type c = 0; 334 335 auto it = find( key ); … … 342 343 return c; 343 344 } 344 345 345 }; 346 346
Note:
See TracChangeset
for help on using the changeset viewer.