Changes in src/GenPoly/ScopedMap.h [5ba653c:933667d]
- File:
-
- 1 edited
-
src/GenPoly/ScopedMap.h (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/ScopedMap.h
r5ba653c r933667d 17 17 #define _SCOPEDMAP_H 18 18 19 #include <cassert>20 19 #include <iterator> 21 20 #include <map> … … 51 50 typedef typename scope_list::size_type size_type; 52 51 53 /// Checks if this iterator points to a valid item54 bool is_valid() const {55 return it != (*scopes)[i].end();56 }57 58 /// Increments on invalid59 iterator& next_valid() {60 if ( ! is_valid() ) { ++(*this); }61 return *this;62 }63 64 /// Decrements on invalid65 iterator& prev_valid() {66 if ( ! is_valid() ) { --(*this); }67 return *this;68 }69 70 52 iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i) 71 53 : scopes(&_scopes), it(_it), i(_i) {} … … 85 67 --i; 86 68 it = (*scopes)[i].begin(); 87 } else {88 ++it;89 }90 return next_valid();69 return *this; 70 } 71 ++it; 72 return *this; 91 73 } 92 74 iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } … … 99 81 } 100 82 --it; 101 return prev_valid();83 return *this; 102 84 } 103 85 iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; } … … 122 104 typedef typename scope_list::size_type size_type; 123 105 124 /// Checks if this iterator points to a valid item125 bool is_valid() const {126 return it != (*scopes)[i].end();127 }128 129 /// Increments on invalid130 const_iterator& next_valid() {131 if ( ! is_valid() ) { ++(*this); }132 return *this;133 }134 135 /// Decrements on invalid136 const_iterator& prev_valid() {137 if ( ! is_valid() ) { --(*this); }138 return *this;139 }140 141 106 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i) 142 107 : scopes(&_scopes), it(_it), i(_i) {} … … 161 126 --i; 162 127 it = (*scopes)[i].begin(); 163 } else {164 ++it;165 }166 return next_valid();128 return *this; 129 } 130 ++it; 131 return *this; 167 132 } 168 133 const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } … … 175 140 } 176 141 --it; 177 return prev_valid();142 return *this; 178 143 } 179 144 const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } … … 199 164 void endScope() { 200 165 scopes.pop_back(); 201 assert( ! scopes.empty() );202 166 } 203 167 … … 205 169 ScopedMap() { beginScope(); } 206 170 207 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1) .next_valid(); }208 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) .next_valid(); }209 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1) .next_valid(); }171 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); } 172 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); } 173 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); } 210 174 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 211 175 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } … … 224 188 return end(); 225 189 } 226 const_iterator find( const Key &key ) const { 227 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) ); 228 } 190 const_iterator find( const Key &key ) const { return const_iterator( find( key ) ); } 229 191 230 192 /// Finds the given key in the outermost scope inside the given scope where it occurs … … 238 200 return end(); 239 201 } 240 const_iterator findNext( const_iterator &it, const Key &key ) const { 241 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) ); 242 } 202 const_iterator findNext( const_iterator &it, const Key &key ) const { return const_iterator( findNext( it, key ) ); } 243 203 244 204 /// Inserts the given key-value pair into the outermost scope … … 248 208 } 249 209 std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); } 250 251 Value& operator[] ( const Key &key ) { 252 iterator slot = find( key ); 253 if ( slot != end() ) return slot->second; 254 return insert( key, Value() ).first->second; 255 } 210 256 211 }; 257 212 } // namespace GenPoly
Note:
See TracChangeset
for help on using the changeset viewer.