source: src/Common/ScopedMap.h@ 30159e5

ADT ast-experimental pthread-emulation
Last change on this file since 30159e5 was 22f94a4, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'master' into new-ast

  • Property mode set to 100644
File size: 11.7 KB
RevLine 
[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
25struct 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
30template<typename Key, typename Value, typename Note = EmptyNote>
[e491159]31class 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
49public:
[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; }
[6dba8755]95 pointer operator-> () const { return it.operator->(); }
[e491159]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
[e0e9a0b]251 Note& getNote() { return scopes.back().note; }
252 const Note& getNote() const { return scopes.back().note; }
[6253fc3]253 Note& getNote( size_type i ) { return scopes[i].note; }
254 const Note& getNote( size_type i ) const { return scopes[i].note; }
255
[e491159]256 /// Finds the given key in the outermost scope it occurs; returns end() for none such
257 iterator find( const Key &key ) {
258 for ( size_type i = scopes.size() - 1; ; --i ) {
[6253fc3]259 typename MapType::iterator val = scopes[i].map.find( key );
260 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
[e491159]261 if ( i == 0 ) break;
262 }
263 return end();
264 }
265 const_iterator find( const Key &key ) const {
[6253fc3]266 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) );
[e491159]267 }
268
[e58dfb9]269 /// Finds the given key in the provided scope; returns end() for none such
270 iterator findAt( size_type scope, const Key& key ) {
[6253fc3]271 typename MapType::iterator val = scopes[scope].map.find( key );
272 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope );
[e58dfb9]273 return end();
274 }
275 const_iterator findAt( size_type scope, const Key& key ) const {
[6253fc3]276 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) );
[e58dfb9]277 }
278
[e491159]279 /// Finds the given key in the outermost scope inside the given scope where it occurs
280 iterator findNext( const_iterator &it, const Key &key ) {
[1f75e2d]281 if ( it.level == 0 ) return end();
282 for ( size_type i = it.level - 1; ; --i ) {
[6253fc3]283 typename MapType::iterator val = scopes[i].map.find( key );
284 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
[e491159]285 if ( i == 0 ) break;
286 }
287 return end();
288 }
289 const_iterator findNext( const_iterator &it, const Key &key ) const {
[6253fc3]290 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) );
[e491159]291 }
292
293 /// Inserts the given key-value pair into the outermost scope
[1f75e2d]294 template< typename value_type_t >
295 std::pair< iterator, bool > insert( value_type_t&& value ) {
[6253fc3]296 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );
[1f75e2d]297 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
[e491159]298 }
299
[1f75e2d]300 template< typename value_type_t >
301 std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) {
[6253fc3]302 MapType& scope = (*at.scopes)[ at.level ].map;
303 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
[1f75e2d]304 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
[e491159]305 }
306
[1f75e2d]307 template< typename value_t >
308 std::pair< iterator, bool > insert( const Key &key, value_t&& value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
309
310 template< typename value_type_t >
311 std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) {
[6253fc3]312 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );
[1f75e2d]313 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
314 }
[e491159]315
[b12c036]316 template< typename value_t >
317 std::pair< iterator, bool > insertAt( size_type scope, const Key& key, value_t&& value ) {
318 return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) );
319 }
320
[e491159]321 Value& operator[] ( const Key &key ) {
322 iterator slot = find( key );
323 if ( slot != end() ) return slot->second;
324 return insert( key, Value() ).first->second;
325 }
326
327 iterator erase( iterator pos ) {
[6253fc3]328 MapType& scope = (*pos.scopes)[ pos.level ].map;
[e491159]329 const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it );
[1f75e2d]330 iterator it( *pos.scopes, new_it, pos.level );
[e491159]331 return it.next_valid();
332 }
333
334 size_type count( const Key &key ) const {
335 size_type c = 0;
336 auto it = find( key );
337 auto end = cend();
338
339 while(it != end) {
340 c++;
341 it = findNext(it, key);
342 }
343
344 return c;
345 }
346
347};
348
349// Local Variables: //
350// tab-width: 4 //
351// mode: c++ //
352// compile-command: "make install" //
353// End: //
Note: See TracBrowser for help on using the repository browser.