Changeset 6253fc3 for src/Common
- Timestamp:
- Jul 9, 2018, 3:10:05 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- c3e44e6
- Parents:
- 1319235
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ScopedMap.h
r1319235 r6253fc3 22 22 #include <vector> 23 23 24 /// Default (empty) ScopedMap note type 25 struct EmptyNote {}; 26 24 27 /// A map where the items are placed into nested scopes; 25 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward 26 template<typename Key, typename Value> 28 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward. 29 /// Scopes may be annotated with a value; the annotation defaults to empty 30 template<typename Key, typename Value, typename Note = EmptyNote> 27 31 class ScopedMap { 28 typedef std::map< Key, Value > Scope; 32 typedef std::map< Key, Value > MapType; 33 struct Scope { 34 MapType map; 35 Note note; 36 }; 29 37 typedef std::vector< Scope > ScopeList; 30 38 31 39 ScopeList scopes; ///< scoped list of maps 32 40 public: 33 typedef typename Scope::key_type key_type;34 typedef typename Scope::mapped_type mapped_type;35 typedef typename Scope::value_type value_type;41 typedef typename MapType::key_type key_type; 42 typedef typename MapType::mapped_type mapped_type; 43 typedef typename MapType::value_type value_type; 36 44 typedef typename ScopeList::size_type size_type; 37 45 typedef typename ScopeList::difference_type difference_type; 38 typedef typename Scope::reference reference;39 typedef typename Scope::const_reference const_reference;40 typedef typename Scope::pointer pointer;41 typedef typename Scope::const_pointer const_pointer;46 typedef typename MapType::reference reference; 47 typedef typename MapType::const_reference const_reference; 48 typedef typename MapType::pointer pointer; 49 typedef typename MapType::const_pointer const_pointer; 42 50 43 51 class iterator : public std::iterator< std::bidirectional_iterator_tag, … … 45 53 friend class ScopedMap; 46 54 friend class const_iterator; 47 typedef typename std::map< Key, Value >::iterator wrapped_iterator;48 typedef typename std::vector< std::map< Key, Value > >scope_list;55 typedef typename ScopedMap::MapType::iterator wrapped_iterator; 56 typedef typename ScopedMap::ScopeList scope_list; 49 57 typedef typename scope_list::size_type size_type; 50 58 51 59 /// Checks if this iterator points to a valid item 52 60 bool is_valid() const { 53 return it != (*scopes)[level]. end();61 return it != (*scopes)[level].map.end(); 54 62 } 55 63 … … 79 87 80 88 iterator& operator++ () { 81 if ( it == (*scopes)[level]. end() ) {89 if ( it == (*scopes)[level].map.end() ) { 82 90 if ( level == 0 ) return *this; 83 91 --level; 84 it = (*scopes)[level]. begin();92 it = (*scopes)[level].map.begin(); 85 93 } else { 86 94 ++it; … … 92 100 iterator& operator-- () { 93 101 // may fail if this is the begin iterator; allowed by STL spec 94 if ( it == (*scopes)[level]. begin() ) {102 if ( it == (*scopes)[level].map.begin() ) { 95 103 ++level; 96 it = (*scopes)[level]. end();104 it = (*scopes)[level].map.end(); 97 105 } 98 106 --it; … … 107 115 108 116 size_type get_level() const { return level; } 117 118 Note& get_note() { return (*scopes)[level].note; } 119 const Note& get_note() const { return (*scopes)[level].note; } 109 120 110 121 private: … … 117 128 value_type > { 118 129 friend class ScopedMap; 119 typedef typename std::map< Key, Value >::iterator wrapped_iterator;120 typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator;121 typedef typename std::vector< std::map< Key, Value > >scope_list;130 typedef typename ScopedMap::MapType::iterator wrapped_iterator; 131 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator; 132 typedef typename ScopedMap::ScopeList scope_list; 122 133 typedef typename scope_list::size_type size_type; 123 134 124 135 /// Checks if this iterator points to a valid item 125 136 bool is_valid() const { 126 return it != (*scopes)[level]. end();137 return it != (*scopes)[level].map.end(); 127 138 } 128 139 … … 157 168 158 169 const_iterator& operator++ () { 159 if ( it == (*scopes)[level]. end() ) {170 if ( it == (*scopes)[level].map.end() ) { 160 171 if ( level == 0 ) return *this; 161 172 --level; 162 it = (*scopes)[level]. begin();173 it = (*scopes)[level].map.begin(); 163 174 } else { 164 175 ++it; … … 170 181 const_iterator& operator-- () { 171 182 // may fail if this is the begin iterator; allowed by STL spec 172 if ( it == (*scopes)[level]. begin() ) {183 if ( it == (*scopes)[level].map.begin() ) { 173 184 ++level; 174 it = (*scopes)[level]. end();185 it = (*scopes)[level].map.end(); 175 186 } 176 187 --it; … … 185 196 186 197 size_type get_level() const { return level; } 198 199 const Note& get_note() const { return (*scopes)[level].note; } 187 200 188 201 private: … … 206 219 ScopedMap() { beginScope(); } 207 220 208 iterator begin() { return iterator(scopes, scopes.back(). begin(), currentScope()).next_valid(); }209 const_iterator begin() const { return const_iterator(scopes, scopes.back(). begin(), currentScope()).next_valid(); }210 const_iterator cbegin() const { return const_iterator(scopes, scopes.back(). begin(), currentScope()).next_valid(); }211 iterator end() { return iterator(scopes, scopes[0]. end(), 0); }212 const_iterator end() const { return const_iterator(scopes, scopes[0]. end(), 0); }213 const_iterator cend() const { return const_iterator(scopes, scopes[0]. end(), 0); }221 iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 222 const_iterator begin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 223 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 224 iterator end() { return iterator(scopes, scopes[0].map.end(), 0); } 225 const_iterator end() const { return const_iterator(scopes, scopes[0].map.end(), 0); } 226 const_iterator cend() const { return const_iterator(scopes, scopes[0].map.end(), 0); } 214 227 215 228 /// Gets the index of the current scope (counted from 1) 216 229 size_type currentScope() const { return scopes.size() - 1; } 230 231 /// Gets the note at the given scope 232 Note& getNote( size_type i ) { return scopes[i].note; } 233 const Note& getNote( size_type i ) const { return scopes[i].note; } 217 234 218 235 /// Finds the given key in the outermost scope it occurs; returns end() for none such 219 236 iterator find( const Key &key ) { 220 237 for ( size_type i = scopes.size() - 1; ; --i ) { 221 typename Scope::iterator val = scopes[i].find( key );222 if ( val != scopes[i]. end() ) return iterator( scopes, val, i );238 typename MapType::iterator val = scopes[i].map.find( key ); 239 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); 223 240 if ( i == 0 ) break; 224 241 } … … 226 243 } 227 244 const_iterator find( const Key &key ) const { 228 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );245 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) ); 229 246 } 230 247 231 248 /// Finds the given key in the provided scope; returns end() for none such 232 249 iterator findAt( size_type scope, const Key& key ) { 233 typename Scope::iterator val = scopes[scope].find( key );234 if ( val != scopes[scope]. end() ) return iterator( scopes, val, scope );250 typename MapType::iterator val = scopes[scope].map.find( key ); 251 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope ); 235 252 return end(); 236 253 } 237 254 const_iterator findAt( size_type scope, const Key& key ) const { 238 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) );255 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) ); 239 256 } 240 257 … … 243 260 if ( it.level == 0 ) return end(); 244 261 for ( size_type i = it.level - 1; ; --i ) { 245 typename Scope::iterator val = scopes[i].find( key );246 if ( val != scopes[i]. end() ) return iterator( scopes, val, i );262 typename MapType::iterator val = scopes[i].map.find( key ); 263 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); 247 264 if ( i == 0 ) break; 248 265 } … … 250 267 } 251 268 const_iterator findNext( const_iterator &it, const Key &key ) const { 252 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );269 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) ); 253 270 } 254 271 … … 256 273 template< typename value_type_t > 257 274 std::pair< iterator, bool > insert( value_type_t&& value ) { 258 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );275 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) ); 259 276 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); 260 277 } … … 262 279 template< typename value_type_t > 263 280 std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) { 264 Scope& scope = (*at.scopes) [ at.level ];265 std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );281 MapType& scope = (*at.scopes)[ at.level ].map; 282 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) ); 266 283 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); 267 284 } … … 272 289 template< typename value_type_t > 273 290 std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) { 274 std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );291 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) ); 275 292 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); 276 293 } … … 288 305 289 306 iterator erase( iterator pos ) { 290 Scope& scope = (*pos.scopes) [ pos.level ];307 MapType& scope = (*pos.scopes)[ pos.level ].map; 291 308 const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it ); 292 309 iterator it( *pos.scopes, new_it, pos.level );
Note: See TracChangeset
for help on using the changeset viewer.