[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 |
---|
| 12 | // Last Modified On : Fri Jul 21 22:18:24 2017 |
---|
| 13 | // Update Count : 2 |
---|
[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 | |
---|
| 24 | /// 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> |
---|
| 27 | class ScopedMap { |
---|
| 28 | typedef std::map< Key, Value > Scope; |
---|
| 29 | typedef std::vector< Scope > ScopeList; |
---|
| 30 | |
---|
| 31 | ScopeList scopes; ///< scoped list of maps |
---|
| 32 | 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; |
---|
| 36 | typedef typename ScopeList::size_type size_type; |
---|
| 37 | 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; |
---|
| 42 | |
---|
| 43 | class iterator : public std::iterator< std::bidirectional_iterator_tag, |
---|
| 44 | value_type > { |
---|
| 45 | friend class ScopedMap; |
---|
| 46 | 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; |
---|
| 49 | typedef typename scope_list::size_type size_type; |
---|
| 50 | |
---|
| 51 | /// Checks if this iterator points to a valid item |
---|
| 52 | bool is_valid() const { |
---|
[1f75e2d] | 53 | return it != (*scopes)[level].end(); |
---|
[e491159] | 54 | } |
---|
| 55 | |
---|
| 56 | /// Increments on invalid |
---|
| 57 | iterator& next_valid() { |
---|
| 58 | if ( ! is_valid() ) { ++(*this); } |
---|
| 59 | return *this; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /// Decrements on invalid |
---|
| 63 | iterator& prev_valid() { |
---|
| 64 | if ( ! is_valid() ) { --(*this); } |
---|
| 65 | return *this; |
---|
| 66 | } |
---|
| 67 | |
---|
[1f75e2d] | 68 | iterator(scope_list &_scopes, const wrapped_iterator &_it, size_type inLevel) |
---|
| 69 | : scopes(&_scopes), it(_it), level(inLevel) {} |
---|
[e491159] | 70 | public: |
---|
[1f75e2d] | 71 | iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
[e491159] | 72 | iterator& operator= (const iterator &that) { |
---|
[1f75e2d] | 73 | scopes = that.scopes; level = that.level; it = that.it; |
---|
[e491159] | 74 | return *this; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | reference operator* () { return *it; } |
---|
| 78 | pointer operator-> () { return it.operator->(); } |
---|
| 79 | |
---|
| 80 | iterator& operator++ () { |
---|
[1f75e2d] | 81 | if ( it == (*scopes)[level].end() ) { |
---|
| 82 | if ( level == 0 ) return *this; |
---|
| 83 | --level; |
---|
| 84 | it = (*scopes)[level].begin(); |
---|
[e491159] | 85 | } else { |
---|
| 86 | ++it; |
---|
| 87 | } |
---|
| 88 | return next_valid(); |
---|
| 89 | } |
---|
| 90 | iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } |
---|
| 91 | |
---|
| 92 | iterator& operator-- () { |
---|
| 93 | // may fail if this is the begin iterator; allowed by STL spec |
---|
[1f75e2d] | 94 | if ( it == (*scopes)[level].begin() ) { |
---|
| 95 | ++level; |
---|
| 96 | it = (*scopes)[level].end(); |
---|
[e491159] | 97 | } |
---|
| 98 | --it; |
---|
| 99 | return prev_valid(); |
---|
| 100 | } |
---|
| 101 | iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; } |
---|
| 102 | |
---|
[fa2de95] | 103 | bool operator== (const iterator &that) const { |
---|
[1f75e2d] | 104 | return scopes == that.scopes && level == that.level && it == that.it; |
---|
[e491159] | 105 | } |
---|
[fa2de95] | 106 | bool operator!= (const iterator &that) const { return !( *this == that ); } |
---|
[e491159] | 107 | |
---|
[1f75e2d] | 108 | size_type get_level() const { return level; } |
---|
| 109 | |
---|
[e491159] | 110 | private: |
---|
| 111 | scope_list *scopes; |
---|
| 112 | wrapped_iterator it; |
---|
[1f75e2d] | 113 | size_type level; |
---|
[e491159] | 114 | }; |
---|
| 115 | |
---|
| 116 | class const_iterator : public std::iterator< std::bidirectional_iterator_tag, |
---|
| 117 | value_type > { |
---|
| 118 | 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; |
---|
| 122 | typedef typename scope_list::size_type size_type; |
---|
| 123 | |
---|
| 124 | /// Checks if this iterator points to a valid item |
---|
| 125 | bool is_valid() const { |
---|
[1f75e2d] | 126 | return it != (*scopes)[level].end(); |
---|
[e491159] | 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 | |
---|
[1f75e2d] | 141 | const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type inLevel) |
---|
| 142 | : scopes(&_scopes), it(_it), level(inLevel) {} |
---|
[e491159] | 143 | public: |
---|
[1f75e2d] | 144 | const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
| 145 | const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), level(that.level) {} |
---|
[e491159] | 146 | const_iterator& operator= (const iterator &that) { |
---|
[1f75e2d] | 147 | scopes = that.scopes; level = that.level; it = that.it; |
---|
[e491159] | 148 | return *this; |
---|
| 149 | } |
---|
| 150 | const_iterator& operator= (const const_iterator &that) { |
---|
[1f75e2d] | 151 | scopes = that.scopes; level = that.level; it = that.it; |
---|
[e491159] | 152 | return *this; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | const_reference operator* () { return *it; } |
---|
| 156 | const_pointer operator-> () { return it.operator->(); } |
---|
| 157 | |
---|
| 158 | const_iterator& operator++ () { |
---|
[1f75e2d] | 159 | if ( it == (*scopes)[level].end() ) { |
---|
| 160 | if ( level == 0 ) return *this; |
---|
| 161 | --level; |
---|
| 162 | it = (*scopes)[level].begin(); |
---|
[e491159] | 163 | } else { |
---|
| 164 | ++it; |
---|
| 165 | } |
---|
| 166 | return next_valid(); |
---|
| 167 | } |
---|
| 168 | const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } |
---|
| 169 | |
---|
| 170 | const_iterator& operator-- () { |
---|
| 171 | // may fail if this is the begin iterator; allowed by STL spec |
---|
[1f75e2d] | 172 | if ( it == (*scopes)[level].begin() ) { |
---|
| 173 | ++level; |
---|
| 174 | it = (*scopes)[level].end(); |
---|
[e491159] | 175 | } |
---|
| 176 | --it; |
---|
| 177 | return prev_valid(); |
---|
| 178 | } |
---|
| 179 | const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } |
---|
| 180 | |
---|
[fa2de95] | 181 | bool operator== (const const_iterator &that) const { |
---|
[1f75e2d] | 182 | return scopes == that.scopes && level == that.level && it == that.it; |
---|
[e491159] | 183 | } |
---|
[fa2de95] | 184 | bool operator!= (const const_iterator &that) const { return !( *this == that ); } |
---|
[e491159] | 185 | |
---|
[1f75e2d] | 186 | size_type get_level() const { return level; } |
---|
| 187 | |
---|
[e491159] | 188 | private: |
---|
| 189 | scope_list const *scopes; |
---|
| 190 | wrapped_const_iterator it; |
---|
[1f75e2d] | 191 | size_type level; |
---|
[e491159] | 192 | }; |
---|
| 193 | |
---|
| 194 | /// Starts a new scope |
---|
| 195 | void beginScope() { |
---|
| 196 | scopes.emplace_back(); |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | /// Ends a scope; invalidates any iterators pointing to elements of that scope |
---|
| 200 | void endScope() { |
---|
| 201 | scopes.pop_back(); |
---|
| 202 | assert( ! scopes.empty() ); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /// Default constructor initializes with one scope |
---|
| 206 | ScopedMap() { beginScope(); } |
---|
| 207 | |
---|
[1f75e2d] | 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(); } |
---|
[e491159] | 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); } |
---|
| 214 | |
---|
| 215 | /// Gets the index of the current scope (counted from 1) |
---|
[1f75e2d] | 216 | size_type currentScope() const { return scopes.size() - 1; } |
---|
[e491159] | 217 | |
---|
| 218 | /// Finds the given key in the outermost scope it occurs; returns end() for none such |
---|
| 219 | iterator find( const Key &key ) { |
---|
| 220 | 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 ); |
---|
| 223 | if ( i == 0 ) break; |
---|
| 224 | } |
---|
| 225 | return end(); |
---|
| 226 | } |
---|
| 227 | const_iterator find( const Key &key ) const { |
---|
| 228 | return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) ); |
---|
| 229 | } |
---|
| 230 | |
---|
[e58dfb9] | 231 | /// Finds the given key in the provided scope; returns end() for none such |
---|
| 232 | 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 ); |
---|
| 235 | return end(); |
---|
| 236 | } |
---|
| 237 | const_iterator findAt( size_type scope, const Key& key ) const { |
---|
| 238 | return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) ); |
---|
| 239 | } |
---|
| 240 | |
---|
[e491159] | 241 | /// Finds the given key in the outermost scope inside the given scope where it occurs |
---|
| 242 | iterator findNext( const_iterator &it, const Key &key ) { |
---|
[1f75e2d] | 243 | if ( it.level == 0 ) return end(); |
---|
| 244 | for ( size_type i = it.level - 1; ; --i ) { |
---|
[e491159] | 245 | typename Scope::iterator val = scopes[i].find( key ); |
---|
| 246 | if ( val != scopes[i].end() ) return iterator( scopes, val, i ); |
---|
| 247 | if ( i == 0 ) break; |
---|
| 248 | } |
---|
| 249 | return end(); |
---|
| 250 | } |
---|
| 251 | const_iterator findNext( const_iterator &it, const Key &key ) const { |
---|
| 252 | return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) ); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /// Inserts the given key-value pair into the outermost scope |
---|
[1f75e2d] | 256 | template< typename value_type_t > |
---|
| 257 | 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 ) ); |
---|
| 259 | return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); |
---|
[e491159] | 260 | } |
---|
| 261 | |
---|
[1f75e2d] | 262 | template< typename value_type_t > |
---|
| 263 | 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 ) ); |
---|
| 266 | return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); |
---|
[e491159] | 267 | } |
---|
| 268 | |
---|
[1f75e2d] | 269 | template< typename value_t > |
---|
| 270 | std::pair< iterator, bool > insert( const Key &key, value_t&& value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); } |
---|
| 271 | |
---|
| 272 | template< typename value_type_t > |
---|
| 273 | 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 ) ); |
---|
| 275 | return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); |
---|
| 276 | } |
---|
[e491159] | 277 | |
---|
| 278 | Value& operator[] ( const Key &key ) { |
---|
| 279 | iterator slot = find( key ); |
---|
| 280 | if ( slot != end() ) return slot->second; |
---|
| 281 | return insert( key, Value() ).first->second; |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | iterator erase( iterator pos ) { |
---|
[1f75e2d] | 285 | Scope& scope = (*pos.scopes) [ pos.level ]; |
---|
[e491159] | 286 | const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it ); |
---|
[1f75e2d] | 287 | iterator it( *pos.scopes, new_it, pos.level ); |
---|
[e491159] | 288 | return it.next_valid(); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | size_type count( const Key &key ) const { |
---|
| 292 | size_type c = 0; |
---|
| 293 | auto it = find( key ); |
---|
| 294 | auto end = cend(); |
---|
| 295 | |
---|
| 296 | while(it != end) { |
---|
| 297 | c++; |
---|
| 298 | it = findNext(it, key); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | return c; |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | }; |
---|
| 305 | |
---|
| 306 | // Local Variables: // |
---|
| 307 | // tab-width: 4 // |
---|
| 308 | // mode: c++ // |
---|
| 309 | // compile-command: "make install" // |
---|
| 310 | // End: // |
---|