[e491159] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // ScopedMap.h -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Wed Dec 2 11:37:00 2015 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[df00c78] | 12 | // Last Modified On : Tue Feb 15 08:41:28 2022 |
---|
| 13 | // Update Count : 5 |
---|
[e491159] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[e491159] | 17 | |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <iterator> |
---|
| 20 | #include <map> |
---|
| 21 | #include <utility> |
---|
| 22 | #include <vector> |
---|
| 23 | |
---|
[6253fc3] | 24 | /// Default (empty) ScopedMap note type |
---|
| 25 | struct EmptyNote {}; |
---|
| 26 | |
---|
[e491159] | 27 | /// A map where the items are placed into nested scopes; |
---|
[6253fc3] | 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> |
---|
[e491159] | 31 | class ScopedMap { |
---|
[6253fc3] | 32 | typedef std::map< Key, Value > MapType; |
---|
| 33 | struct Scope { |
---|
| 34 | MapType map; |
---|
[4612bb0] | 35 | Note note; |
---|
| 36 | |
---|
| 37 | template<typename N> |
---|
[df00c78] | 38 | Scope(N && n) : map(), note(std::forward<N>(n)) {} |
---|
[98a2b1dc] | 39 | |
---|
[4612bb0] | 40 | Scope() = default; |
---|
[df00c78] | 41 | Scope(const Scope &) = default; |
---|
| 42 | Scope(Scope &&) = default; |
---|
| 43 | Scope & operator= (const Scope &) = default; |
---|
| 44 | Scope & operator= (Scope &&) = default; |
---|
[6253fc3] | 45 | }; |
---|
[e491159] | 46 | typedef std::vector< Scope > ScopeList; |
---|
| 47 | |
---|
[db9d7a9] | 48 | /// Scoped list of maps. |
---|
| 49 | ScopeList scopes; |
---|
[e491159] | 50 | public: |
---|
[6253fc3] | 51 | typedef typename MapType::key_type key_type; |
---|
| 52 | typedef typename MapType::mapped_type mapped_type; |
---|
| 53 | typedef typename MapType::value_type value_type; |
---|
[e491159] | 54 | typedef typename ScopeList::size_type size_type; |
---|
| 55 | typedef typename ScopeList::difference_type difference_type; |
---|
[6253fc3] | 56 | typedef typename MapType::reference reference; |
---|
| 57 | typedef typename MapType::const_reference const_reference; |
---|
| 58 | typedef typename MapType::pointer pointer; |
---|
| 59 | typedef typename MapType::const_pointer const_pointer; |
---|
[e491159] | 60 | |
---|
[98a2b1dc] | 61 | // Both iterator types are complete bidrectional iterators, see below. |
---|
| 62 | class iterator; |
---|
| 63 | class const_iterator; |
---|
[e491159] | 64 | |
---|
| 65 | /// Starts a new scope |
---|
| 66 | void beginScope() { |
---|
| 67 | scopes.emplace_back(); |
---|
| 68 | } |
---|
| 69 | |
---|
[4612bb0] | 70 | // Starts a new scope with the given note |
---|
| 71 | template<typename N> |
---|
[df00c78] | 72 | void beginScope( N && n ) { |
---|
[4612bb0] | 73 | scopes.emplace_back( std::forward<N>(n) ); |
---|
| 74 | } |
---|
| 75 | |
---|
[e491159] | 76 | /// Ends a scope; invalidates any iterators pointing to elements of that scope |
---|
| 77 | void endScope() { |
---|
| 78 | scopes.pop_back(); |
---|
| 79 | assert( ! scopes.empty() ); |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /// Default constructor initializes with one scope |
---|
[4612bb0] | 83 | ScopedMap() : scopes() { beginScope(); } |
---|
| 84 | |
---|
| 85 | /// Constructs with a given note on the outermost scope |
---|
| 86 | template<typename N> |
---|
[df00c78] | 87 | ScopedMap( N && n ) : scopes() { beginScope(std::forward<N>(n)); } |
---|
[e491159] | 88 | |
---|
[6253fc3] | 89 | iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } |
---|
| 90 | const_iterator begin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } |
---|
| 91 | const_iterator cbegin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } |
---|
| 92 | iterator end() { return iterator(scopes, scopes[0].map.end(), 0); } |
---|
| 93 | const_iterator end() const { return const_iterator(scopes, scopes[0].map.end(), 0); } |
---|
| 94 | const_iterator cend() const { return const_iterator(scopes, scopes[0].map.end(), 0); } |
---|
[e491159] | 95 | |
---|
| 96 | /// Gets the index of the current scope (counted from 1) |
---|
[1f75e2d] | 97 | size_type currentScope() const { return scopes.size() - 1; } |
---|
[e491159] | 98 | |
---|
[6253fc3] | 99 | /// Gets the note at the given scope |
---|
[df00c78] | 100 | Note & getNote() { return scopes.back().note; } |
---|
| 101 | const Note & getNote() const { return scopes.back().note; } |
---|
| 102 | Note & getNote( size_type i ) { return scopes[i].note; } |
---|
| 103 | const Note & getNote( size_type i ) const { return scopes[i].note; } |
---|
[6253fc3] | 104 | |
---|
[e491159] | 105 | /// Finds the given key in the outermost scope it occurs; returns end() for none such |
---|
[df00c78] | 106 | iterator find( const Key & key ) { |
---|
[e491159] | 107 | for ( size_type i = scopes.size() - 1; ; --i ) { |
---|
[6253fc3] | 108 | typename MapType::iterator val = scopes[i].map.find( key ); |
---|
| 109 | if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); |
---|
[e491159] | 110 | if ( i == 0 ) break; |
---|
| 111 | } |
---|
| 112 | return end(); |
---|
| 113 | } |
---|
[df00c78] | 114 | const_iterator find( const Key & key ) const { |
---|
[6253fc3] | 115 | return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) ); |
---|
[e491159] | 116 | } |
---|
| 117 | |
---|
[e58dfb9] | 118 | /// Finds the given key in the provided scope; returns end() for none such |
---|
[df00c78] | 119 | iterator findAt( size_type scope, const Key & key ) { |
---|
[6253fc3] | 120 | typename MapType::iterator val = scopes[scope].map.find( key ); |
---|
| 121 | if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope ); |
---|
[e58dfb9] | 122 | return end(); |
---|
| 123 | } |
---|
[df00c78] | 124 | const_iterator findAt( size_type scope, const Key & key ) const { |
---|
[6253fc3] | 125 | return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) ); |
---|
[e58dfb9] | 126 | } |
---|
| 127 | |
---|
[e491159] | 128 | /// Finds the given key in the outermost scope inside the given scope where it occurs |
---|
[df00c78] | 129 | iterator findNext( const_iterator & it, const Key & key ) { |
---|
[1f75e2d] | 130 | if ( it.level == 0 ) return end(); |
---|
| 131 | for ( size_type i = it.level - 1; ; --i ) { |
---|
[6253fc3] | 132 | typename MapType::iterator val = scopes[i].map.find( key ); |
---|
| 133 | if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); |
---|
[e491159] | 134 | if ( i == 0 ) break; |
---|
| 135 | } |
---|
| 136 | return end(); |
---|
| 137 | } |
---|
[df00c78] | 138 | const_iterator findNext( const_iterator & it, const Key & key ) const { |
---|
[6253fc3] | 139 | return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) ); |
---|
[e491159] | 140 | } |
---|
| 141 | |
---|
| 142 | /// Inserts the given key-value pair into the outermost scope |
---|
[1f75e2d] | 143 | template< typename value_type_t > |
---|
[df00c78] | 144 | std::pair< iterator, bool > insert( value_type_t && value ) { |
---|
[6253fc3] | 145 | std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) ); |
---|
[1f75e2d] | 146 | return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); |
---|
[e491159] | 147 | } |
---|
| 148 | |
---|
[1f75e2d] | 149 | template< typename value_t > |
---|
[df00c78] | 150 | std::pair< iterator, bool > insert( const Key & key, value_t && value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); } |
---|
[1f75e2d] | 151 | |
---|
| 152 | template< typename value_type_t > |
---|
[df00c78] | 153 | std::pair< iterator, bool > insertAt( size_type scope, value_type_t && value ) { |
---|
[6253fc3] | 154 | std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) ); |
---|
[1f75e2d] | 155 | return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); |
---|
| 156 | } |
---|
[e491159] | 157 | |
---|
[b12c036] | 158 | template< typename value_t > |
---|
[df00c78] | 159 | std::pair< iterator, bool > insertAt( size_type scope, const Key & key, value_t && value ) { |
---|
[b12c036] | 160 | return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) ); |
---|
| 161 | } |
---|
| 162 | |
---|
[df00c78] | 163 | Value & operator[] ( const Key & key ) { |
---|
[e491159] | 164 | iterator slot = find( key ); |
---|
| 165 | if ( slot != end() ) return slot->second; |
---|
| 166 | return insert( key, Value() ).first->second; |
---|
| 167 | } |
---|
| 168 | |
---|
[21a2a7d] | 169 | /// Erases element with key in the innermost scope that has it. |
---|
| 170 | size_type erase( const Key & key ) { |
---|
| 171 | for ( auto it = scopes.rbegin() ; it != scopes.rend() ; ++it ) { |
---|
[ccb29b4] | 172 | size_type i = it->map.erase( key ); |
---|
| 173 | if ( 0 != i ) return i; |
---|
[21a2a7d] | 174 | } |
---|
| 175 | return 0; |
---|
| 176 | } |
---|
[e491159] | 177 | |
---|
[df00c78] | 178 | size_type count( const Key & key ) const { |
---|
[e491159] | 179 | size_type c = 0; |
---|
| 180 | auto it = find( key ); |
---|
| 181 | auto end = cend(); |
---|
| 182 | |
---|
| 183 | while(it != end) { |
---|
| 184 | c++; |
---|
| 185 | it = findNext(it, key); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | return c; |
---|
| 189 | } |
---|
[e9b5043] | 190 | |
---|
| 191 | bool contains( const Key & key ) const { |
---|
| 192 | return find( key ) != cend(); |
---|
| 193 | } |
---|
[e491159] | 194 | }; |
---|
| 195 | |
---|
[98a2b1dc] | 196 | template<typename Key, typename Value, typename Note> |
---|
| 197 | class ScopedMap<Key, Value, Note>::iterator : |
---|
| 198 | public std::iterator< std::bidirectional_iterator_tag, value_type > { |
---|
| 199 | friend class ScopedMap; |
---|
| 200 | friend class const_iterator; |
---|
[7f1be01] | 201 | typedef typename MapType::iterator wrapped_iterator; |
---|
| 202 | typedef typename ScopeList::size_type size_type; |
---|
[98a2b1dc] | 203 | |
---|
| 204 | /// Checks if this iterator points to a valid item |
---|
| 205 | bool is_valid() const { |
---|
| 206 | return it != (*scopes)[level].map.end(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /// Increments on invalid |
---|
| 210 | iterator & next_valid() { |
---|
| 211 | if ( ! is_valid() ) { ++(*this); } |
---|
| 212 | return *this; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | /// Decrements on invalid |
---|
| 216 | iterator & prev_valid() { |
---|
| 217 | if ( ! is_valid() ) { --(*this); } |
---|
| 218 | return *this; |
---|
| 219 | } |
---|
| 220 | |
---|
[7f1be01] | 221 | iterator(ScopeList & _scopes, const wrapped_iterator & _it, size_type inLevel) |
---|
[98a2b1dc] | 222 | : scopes(&_scopes), it(_it), level(inLevel) {} |
---|
| 223 | public: |
---|
| 224 | iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
| 225 | iterator & operator= (const iterator & that) { |
---|
| 226 | scopes = that.scopes; level = that.level; it = that.it; |
---|
| 227 | return *this; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | reference operator* () { return *it; } |
---|
| 231 | pointer operator-> () const { return it.operator->(); } |
---|
| 232 | |
---|
| 233 | iterator & operator++ () { |
---|
| 234 | if ( it == (*scopes)[level].map.end() ) { |
---|
| 235 | if ( level == 0 ) return *this; |
---|
| 236 | --level; |
---|
| 237 | it = (*scopes)[level].map.begin(); |
---|
| 238 | } else { |
---|
| 239 | ++it; |
---|
| 240 | } |
---|
| 241 | return next_valid(); |
---|
| 242 | } |
---|
| 243 | iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } |
---|
| 244 | |
---|
| 245 | iterator & operator-- () { |
---|
| 246 | // may fail if this is the begin iterator; allowed by STL spec |
---|
| 247 | if ( it == (*scopes)[level].map.begin() ) { |
---|
| 248 | ++level; |
---|
| 249 | it = (*scopes)[level].map.end(); |
---|
| 250 | } |
---|
| 251 | --it; |
---|
| 252 | return prev_valid(); |
---|
| 253 | } |
---|
| 254 | iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; } |
---|
| 255 | |
---|
| 256 | bool operator== (const iterator & that) const { |
---|
| 257 | return scopes == that.scopes && level == that.level && it == that.it; |
---|
| 258 | } |
---|
| 259 | bool operator!= (const iterator & that) const { return !( *this == that ); } |
---|
| 260 | |
---|
| 261 | size_type get_level() const { return level; } |
---|
| 262 | |
---|
| 263 | Note & get_note() { return (*scopes)[level].note; } |
---|
| 264 | const Note & get_note() const { return (*scopes)[level].note; } |
---|
| 265 | |
---|
| 266 | private: |
---|
[7f1be01] | 267 | ScopeList *scopes; |
---|
[98a2b1dc] | 268 | wrapped_iterator it; |
---|
| 269 | size_type level; |
---|
| 270 | }; |
---|
| 271 | |
---|
| 272 | template<typename Key, typename Value, typename Note> |
---|
| 273 | class ScopedMap<Key, Value, Note>::const_iterator : |
---|
| 274 | public std::iterator< std::bidirectional_iterator_tag, value_type > { |
---|
| 275 | friend class ScopedMap; |
---|
| 276 | typedef typename ScopedMap::MapType::iterator wrapped_iterator; |
---|
| 277 | typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator; |
---|
| 278 | typedef typename ScopedMap::ScopeList scope_list; |
---|
| 279 | typedef typename scope_list::size_type size_type; |
---|
| 280 | |
---|
| 281 | /// Checks if this iterator points to a valid item |
---|
| 282 | bool is_valid() const { |
---|
| 283 | return it != (*scopes)[level].map.end(); |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /// Increments on invalid |
---|
| 287 | const_iterator & next_valid() { |
---|
| 288 | if ( ! is_valid() ) { ++(*this); } |
---|
| 289 | return *this; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | /// Decrements on invalid |
---|
| 293 | const_iterator & prev_valid() { |
---|
| 294 | if ( ! is_valid() ) { --(*this); } |
---|
| 295 | return *this; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel) |
---|
| 299 | : scopes(&_scopes), it(_it), level(inLevel) {} |
---|
| 300 | public: |
---|
| 301 | const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
| 302 | const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
| 303 | const_iterator & operator= (const iterator & that) { |
---|
| 304 | scopes = that.scopes; level = that.level; it = that.it; |
---|
| 305 | return *this; |
---|
| 306 | } |
---|
| 307 | const_iterator & operator= (const const_iterator & that) { |
---|
| 308 | scopes = that.scopes; level = that.level; it = that.it; |
---|
| 309 | return *this; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | const_reference operator* () { return *it; } |
---|
| 313 | const_pointer operator-> () { return it.operator->(); } |
---|
| 314 | |
---|
| 315 | const_iterator & operator++ () { |
---|
| 316 | if ( it == (*scopes)[level].map.end() ) { |
---|
| 317 | if ( level == 0 ) return *this; |
---|
| 318 | --level; |
---|
| 319 | it = (*scopes)[level].map.begin(); |
---|
| 320 | } else { |
---|
| 321 | ++it; |
---|
| 322 | } |
---|
| 323 | return next_valid(); |
---|
| 324 | } |
---|
| 325 | const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } |
---|
| 326 | |
---|
| 327 | const_iterator & operator-- () { |
---|
| 328 | // may fail if this is the begin iterator; allowed by STL spec |
---|
| 329 | if ( it == (*scopes)[level].map.begin() ) { |
---|
| 330 | ++level; |
---|
| 331 | it = (*scopes)[level].map.end(); |
---|
| 332 | } |
---|
| 333 | --it; |
---|
| 334 | return prev_valid(); |
---|
| 335 | } |
---|
| 336 | const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } |
---|
| 337 | |
---|
| 338 | bool operator== (const const_iterator & that) const { |
---|
| 339 | return scopes == that.scopes && level == that.level && it == that.it; |
---|
| 340 | } |
---|
| 341 | bool operator!= (const const_iterator & that) const { return !( *this == that ); } |
---|
| 342 | |
---|
| 343 | size_type get_level() const { return level; } |
---|
| 344 | |
---|
| 345 | const Note & get_note() const { return (*scopes)[level].note; } |
---|
| 346 | |
---|
| 347 | private: |
---|
| 348 | scope_list const *scopes; |
---|
| 349 | wrapped_const_iterator it; |
---|
| 350 | size_type level; |
---|
| 351 | }; |
---|
| 352 | |
---|
[e491159] | 353 | // Local Variables: // |
---|
| 354 | // tab-width: 4 // |
---|
| 355 | // mode: c++ // |
---|
| 356 | // compile-command: "make install" // |
---|
| 357 | // End: // |
---|