Changeset 70a06f6 for src/GenPoly/ScopedMap.h
- Timestamp:
- Apr 14, 2016, 4:13:10 PM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 356189a
- Parents:
- db4ecc5 (diff), 37f0da8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/GenPoly/ScopedMap.h
rdb4ecc5 r70a06f6 17 17 #define _SCOPEDMAP_H 18 18 19 #include <cassert> 19 20 #include <iterator> 20 21 #include <map> … … 50 51 typedef typename scope_list::size_type size_type; 51 52 53 /// Checks if this iterator points to a valid item 54 bool is_valid() const { 55 return it != (*scopes)[i].end(); 56 } 57 58 /// Increments on invalid 59 iterator& next_valid() { 60 if ( ! is_valid() ) { ++(*this); } 61 return *this; 62 } 63 64 /// Decrements on invalid 65 iterator& prev_valid() { 66 if ( ! is_valid() ) { --(*this); } 67 return *this; 68 } 69 52 70 iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i) 53 71 : scopes(&_scopes), it(_it), i(_i) {} … … 67 85 --i; 68 86 it = (*scopes)[i].begin(); 69 return *this;70 }71 ++it;72 return *this;87 } else { 88 ++it; 89 } 90 return next_valid(); 73 91 } 74 92 iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } … … 81 99 } 82 100 --it; 83 return *this;101 return prev_valid(); 84 102 } 85 103 iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; } … … 104 122 typedef typename scope_list::size_type size_type; 105 123 124 /// Checks if this iterator points to a valid item 125 bool is_valid() const { 126 return it != (*scopes)[i].end(); 127 } 128 129 /// Increments on invalid 130 const_iterator& next_valid() { 131 if ( ! is_valid() ) { ++(*this); } 132 return *this; 133 } 134 135 /// Decrements on invalid 136 const_iterator& prev_valid() { 137 if ( ! is_valid() ) { --(*this); } 138 return *this; 139 } 140 106 141 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i) 107 142 : scopes(&_scopes), it(_it), i(_i) {} … … 126 161 --i; 127 162 it = (*scopes)[i].begin(); 128 return *this;129 }130 ++it;131 return *this;163 } else { 164 ++it; 165 } 166 return next_valid(); 132 167 } 133 168 const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } … … 140 175 } 141 176 --it; 142 return *this;177 return prev_valid(); 143 178 } 144 179 const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } … … 164 199 void endScope() { 165 200 scopes.pop_back(); 201 assert( ! scopes.empty() ); 166 202 } 167 203 … … 169 205 ScopedMap() { beginScope(); } 170 206 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) ; }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(); } 174 210 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 175 211 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } … … 188 224 return end(); 189 225 } 190 const_iterator find( const Key &key ) const { return const_iterator( find( key ) ); } 226 const_iterator find( const Key &key ) const { 227 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) ); 228 } 191 229 192 230 /// Finds the given key in the outermost scope inside the given scope where it occurs … … 200 238 return end(); 201 239 } 202 const_iterator findNext( const_iterator &it, const Key &key ) const { return const_iterator( findNext( it, key ) ); } 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 } 203 243 204 244 /// Inserts the given key-value pair into the outermost scope … … 208 248 } 209 249 std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); } 210 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 } 211 256 }; 212 257 } // namespace GenPoly
Note: See TracChangeset
for help on using the changeset viewer.