[bfae637] | 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 | // |
---|
[5f225f5] | 7 | // ErasableScopedMap.hpp -- A map that supports scoping and erasing elements. |
---|
[bfae637] | 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 : Sat Jul 22 09:23:24 2017 |
---|
| 13 | // Update Count : 2 |
---|
[bfae637] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[bfae637] | 17 | |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <iterator> |
---|
| 20 | #include <map> |
---|
| 21 | #include <utility> |
---|
| 22 | #include <vector> |
---|
| 23 | |
---|
| 24 | namespace GenPoly { |
---|
| 25 | |
---|
[c1fb3903] | 26 | /// A map where the items are placed into nested scopes. |
---|
| 27 | /// Inserted items are placed into the innermost scope, lookup looks from the |
---|
| 28 | /// innermost scope outward. Erasing a key means that find() will no longer |
---|
| 29 | /// report any instance of the key in a scope further out, but the erasure |
---|
| 30 | /// itself is scoped. Key erasure works by inserting a sentinal value into |
---|
| 31 | /// the value field, and thus only works for Value types where a meaningful |
---|
| 32 | /// sentinal can be chosen. |
---|
| 33 | template<typename Key, typename Value> |
---|
| 34 | class ErasableScopedMap { |
---|
| 35 | typedef std::map< Key, Value > Scope; |
---|
| 36 | typedef std::vector< Scope > ScopeList; |
---|
| 37 | |
---|
| 38 | /// Scoped list of maps. |
---|
| 39 | ScopeList scopes; |
---|
| 40 | /// Sentinal value for erased keys. |
---|
| 41 | Value erased; |
---|
| 42 | public: |
---|
| 43 | typedef typename Scope::key_type key_type; |
---|
| 44 | typedef typename Scope::mapped_type mapped_type; |
---|
| 45 | typedef typename Scope::value_type value_type; |
---|
| 46 | typedef typename ScopeList::size_type size_type; |
---|
| 47 | typedef typename ScopeList::difference_type difference_type; |
---|
| 48 | typedef typename Scope::reference reference; |
---|
| 49 | typedef typename Scope::const_reference const_reference; |
---|
| 50 | typedef typename Scope::pointer pointer; |
---|
| 51 | typedef typename Scope::const_pointer const_pointer; |
---|
| 52 | |
---|
[98a2b1dc] | 53 | // Both iterator types are complete bidirectional iterators, see below. |
---|
[c1fb3903] | 54 | class iterator; |
---|
| 55 | class const_iterator; |
---|
| 56 | |
---|
| 57 | /// Starts a new scope |
---|
| 58 | void beginScope() { |
---|
[7f1be01] | 59 | scopes.emplace_back(); |
---|
[c1fb3903] | 60 | } |
---|
| 61 | |
---|
| 62 | /// Ends a scope; invalidates any iterators pointing to elements of that scope |
---|
| 63 | void endScope() { |
---|
| 64 | scopes.pop_back(); |
---|
| 65 | assert( ! scopes.empty() ); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /// Default constructor initializes with one scope |
---|
| 69 | ErasableScopedMap( const Value &erased_ ) : erased( erased_ ) { beginScope(); } |
---|
| 70 | |
---|
| 71 | iterator begin() { return iterator(*this, scopes.back().begin(), scopes.size()-1).next_valid(); } |
---|
| 72 | const_iterator begin() const { return const_iterator(*this, scopes.back().begin(), scopes.size()-1).next_valid(); } |
---|
| 73 | const_iterator cbegin() const { return const_iterator(*this, scopes.back().begin(), scopes.size()-1).next_valid(); } |
---|
| 74 | iterator end() { return iterator(*this, scopes[0].end(), 0); } |
---|
| 75 | const_iterator end() const { return const_iterator(*this, scopes[0].end(), 0); } |
---|
| 76 | const_iterator cend() const { return const_iterator(*this, scopes[0].end(), 0); } |
---|
| 77 | |
---|
| 78 | /// Gets the index of the current scope (counted from 1) |
---|
| 79 | size_type currentScope() const { return scopes.size(); } |
---|
| 80 | |
---|
| 81 | /// Finds the given key in the outermost scope it occurs; returns end() for none such |
---|
| 82 | iterator find( const Key &key ) { |
---|
| 83 | for ( size_type i = scopes.size() - 1; ; --i ) { |
---|
| 84 | typename Scope::iterator val = scopes[i].find( key ); |
---|
| 85 | if ( val != scopes[i].end() ) { |
---|
| 86 | return val->second == erased ? end() : iterator( *this, val, i ); |
---|
[bfae637] | 87 | } |
---|
[c1fb3903] | 88 | if ( i == 0 ) break; |
---|
[bfae637] | 89 | } |
---|
[c1fb3903] | 90 | return end(); |
---|
| 91 | } |
---|
| 92 | const_iterator find( const Key &key ) const { |
---|
| 93 | return const_iterator( const_cast< ErasableScopedMap< Key, Value >* >(this)->find( key ) ); |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | /// Finds the given key in the outermost scope inside the given scope where it occurs |
---|
| 97 | iterator findNext( const_iterator &it, const Key &key ) { |
---|
| 98 | if ( it.i == 0 ) return end(); |
---|
| 99 | for ( size_type i = it.i - 1; ; --i ) { |
---|
| 100 | typename Scope::iterator val = scopes[i].find( key ); |
---|
| 101 | if ( val != scopes[i].end() ) { |
---|
| 102 | return val->second == erased ? end() : iterator( *this, val, i ); |
---|
[bfae637] | 103 | } |
---|
[c1fb3903] | 104 | if ( i == 0 ) break; |
---|
[bfae637] | 105 | } |
---|
[c1fb3903] | 106 | return end(); |
---|
| 107 | } |
---|
| 108 | const_iterator findNext( const_iterator &it, const Key &key ) const { |
---|
| 109 | return const_iterator( const_cast< ErasableScopedMap< Key, Value >* >(this)->findNext( it, key ) ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | /// Inserts the given key-value pair into the outermost scope |
---|
| 113 | std::pair< iterator, bool > insert( const value_type &value ) { |
---|
| 114 | std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value ); |
---|
| 115 | return std::make_pair( iterator(*this, res.first, scopes.size()-1), res.second ); |
---|
| 116 | } |
---|
| 117 | std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); } |
---|
| 118 | |
---|
[e9b5043] | 119 | Value& operator[] ( const Key &key ) { |
---|
| 120 | iterator slot = find( key ); |
---|
| 121 | if ( slot != end() ) return slot->second; |
---|
| 122 | return insert( key, Value() ).first->second; |
---|
| 123 | } |
---|
| 124 | |
---|
[c1fb3903] | 125 | /// Marks the given element as erased from this scope inward; returns 1 for erased an element, 0 otherwise |
---|
| 126 | size_type erase( const Key &key ) { |
---|
| 127 | typename Scope::iterator val = scopes.back().find( key ); |
---|
| 128 | if ( val != scopes.back().end() ) { |
---|
| 129 | val->second = erased; |
---|
| 130 | return 1; |
---|
| 131 | } else { |
---|
| 132 | scopes.back().insert( val, std::make_pair( key, erased ) ); |
---|
| 133 | return 0; |
---|
[bfae637] | 134 | } |
---|
[c1fb3903] | 135 | } |
---|
| 136 | |
---|
[e9b5043] | 137 | bool contains( const Key & key ) const { |
---|
| 138 | return find( key ) != cend(); |
---|
[c1fb3903] | 139 | } |
---|
| 140 | }; |
---|
| 141 | |
---|
| 142 | template<typename Key, typename Value> |
---|
| 143 | class ErasableScopedMap<Key, Value>::iterator : |
---|
| 144 | public std::iterator< std::bidirectional_iterator_tag, value_type > { |
---|
| 145 | friend class ErasableScopedMap; |
---|
[7f1be01] | 146 | typedef typename Scope::iterator wrapped_iterator; |
---|
| 147 | typedef typename ScopeList::size_type size_type; |
---|
[c1fb3903] | 148 | |
---|
| 149 | /// Checks if this iterator points to a valid item |
---|
| 150 | bool is_valid() const { |
---|
| 151 | return it != map->scopes[i].end() && it->second != map->erased; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /// Increments on invalid |
---|
| 155 | iterator& next_valid() { |
---|
| 156 | if ( ! is_valid() ) { ++(*this); } |
---|
| 157 | return *this; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /// Decrements on invalid |
---|
| 161 | iterator& prev_valid() { |
---|
| 162 | if ( ! is_valid() ) { --(*this); } |
---|
| 163 | return *this; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | iterator(ErasableScopedMap< Key, Value > const &_map, const wrapped_iterator &_it, size_type _i) |
---|
| 167 | : map(&_map), it(_it), i(_i) {} |
---|
| 168 | |
---|
| 169 | public: |
---|
| 170 | iterator(const iterator &that) : map(that.map), it(that.it), i(that.i) {} |
---|
| 171 | iterator& operator= (const iterator &that) { |
---|
| 172 | map = that.map; i = that.i; it = that.it; |
---|
| 173 | return *this; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | reference operator* () { return *it; } |
---|
| 177 | pointer operator-> () { return it.operator->(); } |
---|
| 178 | |
---|
| 179 | iterator& operator++ () { |
---|
| 180 | if ( it == map->scopes[i].end() ) { |
---|
| 181 | if ( i == 0 ) return *this; |
---|
| 182 | --i; |
---|
| 183 | it = map->scopes[i].begin(); |
---|
| 184 | } else { |
---|
| 185 | ++it; |
---|
[bfae637] | 186 | } |
---|
[c1fb3903] | 187 | return next_valid(); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; } |
---|
[bfae637] | 191 | |
---|
[c1fb3903] | 192 | iterator& operator-- () { |
---|
| 193 | // may fail if this is the begin iterator; allowed by STL spec |
---|
| 194 | if ( it == map->scopes[i].begin() ) { |
---|
| 195 | ++i; |
---|
| 196 | it = map->scopes[i].end(); |
---|
[bfae637] | 197 | } |
---|
[c1fb3903] | 198 | --it; |
---|
| 199 | return prev_valid(); |
---|
| 200 | } |
---|
| 201 | iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; } |
---|
| 202 | |
---|
| 203 | bool operator== (const iterator &that) { |
---|
| 204 | return map == that.map && i == that.i && it == that.it; |
---|
| 205 | } |
---|
| 206 | bool operator!= (const iterator &that) { return !( *this == that ); } |
---|
| 207 | |
---|
| 208 | private: |
---|
| 209 | ErasableScopedMap< Key, Value > const *map; |
---|
| 210 | wrapped_iterator it; |
---|
| 211 | size_type i; |
---|
| 212 | }; |
---|
| 213 | |
---|
| 214 | template<typename Key, typename Value> |
---|
| 215 | class ErasableScopedMap<Key, Value>::const_iterator : |
---|
| 216 | public std::iterator< std::bidirectional_iterator_tag, value_type > { |
---|
| 217 | friend class ErasableScopedMap; |
---|
| 218 | typedef typename std::map< Key, Value >::iterator wrapped_iterator; |
---|
| 219 | typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator; |
---|
| 220 | typedef typename std::vector< std::map< Key, Value > > scope_list; |
---|
| 221 | typedef typename scope_list::size_type size_type; |
---|
| 222 | |
---|
| 223 | /// Checks if this iterator points to a valid item |
---|
| 224 | bool is_valid() const { |
---|
| 225 | return it != map->scopes[i].end() && it->second != map->erased; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /// Increments on invalid |
---|
| 229 | const_iterator& next_valid() { |
---|
| 230 | if ( ! is_valid() ) { ++(*this); } |
---|
| 231 | return *this; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /// Decrements on invalid |
---|
| 235 | const_iterator& prev_valid() { |
---|
| 236 | if ( ! is_valid() ) { --(*this); } |
---|
| 237 | return *this; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | const_iterator(ErasableScopedMap< Key, Value > const &_map, const wrapped_const_iterator &_it, size_type _i) |
---|
| 241 | : map(&_map), it(_it), i(_i) {} |
---|
| 242 | public: |
---|
| 243 | const_iterator(const iterator &that) : map(that.map), it(that.it), i(that.i) {} |
---|
| 244 | const_iterator(const const_iterator &that) : map(that.map), it(that.it), i(that.i) {} |
---|
| 245 | const_iterator& operator= (const iterator &that) { |
---|
| 246 | map = that.map; i = that.i; it = that.it; |
---|
| 247 | return *this; |
---|
| 248 | } |
---|
| 249 | const_iterator& operator= (const const_iterator &that) { |
---|
| 250 | map = that.map; i = that.i; it = that.it; |
---|
| 251 | return *this; |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | const_reference operator* () { return *it; } |
---|
| 255 | const_pointer operator-> () { return it.operator->(); } |
---|
| 256 | |
---|
| 257 | const_iterator& operator++ () { |
---|
| 258 | if ( it == map->scopes[i].end() ) { |
---|
| 259 | if ( i == 0 ) return *this; |
---|
| 260 | --i; |
---|
| 261 | it = map->scopes[i].begin(); |
---|
| 262 | } else { |
---|
| 263 | ++it; |
---|
[bfae637] | 264 | } |
---|
[c1fb3903] | 265 | return next_valid(); |
---|
| 266 | } |
---|
| 267 | const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; } |
---|
| 268 | |
---|
| 269 | const_iterator& operator-- () { |
---|
| 270 | // may fail if this is the begin iterator; allowed by STL spec |
---|
| 271 | if ( it == map->scopes[i].begin() ) { |
---|
| 272 | ++i; |
---|
| 273 | it = map->scopes[i].end(); |
---|
[bfae637] | 274 | } |
---|
[c1fb3903] | 275 | --it; |
---|
| 276 | return prev_valid(); |
---|
| 277 | } |
---|
| 278 | const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; } |
---|
| 279 | |
---|
| 280 | bool operator== (const const_iterator &that) { |
---|
| 281 | return map == that.map && i == that.i && it == that.it; |
---|
| 282 | } |
---|
| 283 | bool operator!= (const const_iterator &that) { return !( *this == that ); } |
---|
| 284 | |
---|
| 285 | private: |
---|
| 286 | ErasableScopedMap< Key, Value > const *map; |
---|
| 287 | wrapped_const_iterator it; |
---|
| 288 | size_type i; |
---|
| 289 | }; |
---|
| 290 | |
---|
[bfae637] | 291 | } // namespace GenPoly |
---|
| 292 | |
---|
| 293 | // Local Variables: // |
---|
| 294 | // tab-width: 4 // |
---|
| 295 | // mode: c++ // |
---|
| 296 | // compile-command: "make install" // |
---|
[6b0b624] | 297 | // End: // |
---|