Changes in / [c3e44e6a:a8615fd1]
- Location:
- src
- Files:
-
- 2 edited
-
Common/ScopedMap.h (modified) (17 diffs)
-
Parser/TypedefTable.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
src/Common/ScopedMap.h
rc3e44e6a ra8615fd1 22 22 #include <vector> 23 23 24 /// Default (empty) ScopedMap note type25 struct EmptyNote {};26 27 24 /// A map where the items are placed into nested scopes; 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> 25 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward 26 template<typename Key, typename Value> 31 27 class ScopedMap { 32 typedef std::map< Key, Value > MapType; 33 struct Scope { 34 MapType map; 35 Note note; 36 }; 28 typedef std::map< Key, Value > Scope; 37 29 typedef std::vector< Scope > ScopeList; 38 30 39 31 ScopeList scopes; ///< scoped list of maps 40 32 public: 41 typedef typename MapType::key_type key_type;42 typedef typename MapType::mapped_type mapped_type;43 typedef typename MapType::value_type value_type;33 typedef typename Scope::key_type key_type; 34 typedef typename Scope::mapped_type mapped_type; 35 typedef typename Scope::value_type value_type; 44 36 typedef typename ScopeList::size_type size_type; 45 37 typedef typename ScopeList::difference_type difference_type; 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;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; 50 42 51 43 class iterator : public std::iterator< std::bidirectional_iterator_tag, … … 53 45 friend class ScopedMap; 54 46 friend class const_iterator; 55 typedef typename ScopedMap::MapType::iterator wrapped_iterator;56 typedef typename ScopedMap::ScopeListscope_list;47 typedef typename std::map< Key, Value >::iterator wrapped_iterator; 48 typedef typename std::vector< std::map< Key, Value > > scope_list; 57 49 typedef typename scope_list::size_type size_type; 58 50 59 51 /// Checks if this iterator points to a valid item 60 52 bool is_valid() const { 61 return it != (*scopes)[level]. map.end();53 return it != (*scopes)[level].end(); 62 54 } 63 55 … … 87 79 88 80 iterator& operator++ () { 89 if ( it == (*scopes)[level]. map.end() ) {81 if ( it == (*scopes)[level].end() ) { 90 82 if ( level == 0 ) return *this; 91 83 --level; 92 it = (*scopes)[level]. map.begin();84 it = (*scopes)[level].begin(); 93 85 } else { 94 86 ++it; … … 100 92 iterator& operator-- () { 101 93 // may fail if this is the begin iterator; allowed by STL spec 102 if ( it == (*scopes)[level]. map.begin() ) {94 if ( it == (*scopes)[level].begin() ) { 103 95 ++level; 104 it = (*scopes)[level]. map.end();96 it = (*scopes)[level].end(); 105 97 } 106 98 --it; … … 115 107 116 108 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; }120 109 121 110 private: … … 128 117 value_type > { 129 118 friend class ScopedMap; 130 typedef typename ScopedMap::MapType::iterator wrapped_iterator;131 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;132 typedef typename ScopedMap::ScopeListscope_list;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; 133 122 typedef typename scope_list::size_type size_type; 134 123 135 124 /// Checks if this iterator points to a valid item 136 125 bool is_valid() const { 137 return it != (*scopes)[level]. map.end();126 return it != (*scopes)[level].end(); 138 127 } 139 128 … … 168 157 169 158 const_iterator& operator++ () { 170 if ( it == (*scopes)[level]. map.end() ) {159 if ( it == (*scopes)[level].end() ) { 171 160 if ( level == 0 ) return *this; 172 161 --level; 173 it = (*scopes)[level]. map.begin();162 it = (*scopes)[level].begin(); 174 163 } else { 175 164 ++it; … … 181 170 const_iterator& operator-- () { 182 171 // may fail if this is the begin iterator; allowed by STL spec 183 if ( it == (*scopes)[level]. map.begin() ) {172 if ( it == (*scopes)[level].begin() ) { 184 173 ++level; 185 it = (*scopes)[level]. map.end();174 it = (*scopes)[level].end(); 186 175 } 187 176 --it; … … 196 185 197 186 size_type get_level() const { return level; } 198 199 const Note& get_note() const { return (*scopes)[level].note; }200 187 201 188 private: … … 219 206 ScopedMap() { beginScope(); } 220 207 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); }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); } 227 214 228 215 /// Gets the index of the current scope (counted from 1) 229 216 size_type currentScope() const { return scopes.size() - 1; } 230 231 /// Gets the note at the given scope232 Note& getNote( size_type i ) { return scopes[i].note; }233 const Note& getNote( size_type i ) const { return scopes[i].note; }234 217 235 218 /// Finds the given key in the outermost scope it occurs; returns end() for none such 236 219 iterator find( const Key &key ) { 237 220 for ( size_type i = scopes.size() - 1; ; --i ) { 238 typename MapType::iterator val = scopes[i].map.find( key );239 if ( val != scopes[i]. map.end() ) return iterator( scopes, val, i );221 typename Scope::iterator val = scopes[i].find( key ); 222 if ( val != scopes[i].end() ) return iterator( scopes, val, i ); 240 223 if ( i == 0 ) break; 241 224 } … … 243 226 } 244 227 const_iterator find( const Key &key ) const { 245 return const_iterator( const_cast< ScopedMap< Key, Value , Note>* >(this)->find( key ) );228 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) ); 246 229 } 247 230 248 231 /// Finds the given key in the provided scope; returns end() for none such 249 232 iterator findAt( size_type scope, const Key& key ) { 250 typename MapType::iterator val = scopes[scope].map.find( key );251 if ( val != scopes[scope]. map.end() ) return iterator( scopes, val, scope );233 typename Scope::iterator val = scopes[scope].find( key ); 234 if ( val != scopes[scope].end() ) return iterator( scopes, val, scope ); 252 235 return end(); 253 236 } 254 237 const_iterator findAt( size_type scope, const Key& key ) const { 255 return const_iterator( const_cast< ScopedMap< Key, Value , Note>* >(this)->findAt( scope, key ) );238 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) ); 256 239 } 257 240 … … 260 243 if ( it.level == 0 ) return end(); 261 244 for ( size_type i = it.level - 1; ; --i ) { 262 typename MapType::iterator val = scopes[i].map.find( key );263 if ( val != scopes[i]. map.end() ) return iterator( scopes, val, i );245 typename Scope::iterator val = scopes[i].find( key ); 246 if ( val != scopes[i].end() ) return iterator( scopes, val, i ); 264 247 if ( i == 0 ) break; 265 248 } … … 267 250 } 268 251 const_iterator findNext( const_iterator &it, const Key &key ) const { 269 return const_iterator( const_cast< ScopedMap< Key, Value , Note>* >(this)->findNext( it, key ) );252 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) ); 270 253 } 271 254 … … 273 256 template< typename value_type_t > 274 257 std::pair< iterator, bool > insert( value_type_t&& value ) { 275 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );258 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) ); 276 259 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); 277 260 } … … 279 262 template< typename value_type_t > 280 263 std::pair< iterator, bool > insert( iterator at, 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 ) );264 Scope& scope = (*at.scopes) [ at.level ]; 265 std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) ); 283 266 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); 284 267 } … … 289 272 template< typename value_type_t > 290 273 std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) { 291 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );274 std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) ); 292 275 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); 293 276 } … … 305 288 306 289 iterator erase( iterator pos ) { 307 MapType& scope = (*pos.scopes)[ pos.level ].map;290 Scope& scope = (*pos.scopes) [ pos.level ]; 308 291 const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it ); 309 292 iterator it( *pos.scopes, new_it, pos.level ); -
src/Parser/TypedefTable.h
rc3e44e6a ra8615fd1 23 23 24 24 class TypedefTable { 25 typedef ScopedMap< std::string, int , int> KindTable;25 typedef ScopedMap< std::string, int > KindTable; 26 26 KindTable kindTable; 27 27 unsigned int level = 0;
Note:
See TracChangeset
for help on using the changeset viewer.