source: src/Common/ScopedMap.h@ e9b5043

ADT ast-experimental stuck-waitfor-destruct
Last change on this file since e9b5043 was e9b5043, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Added contains to some of our containers. Also changed some code to use the new method.

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