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