source: src/Common/ScopedMap.h @ 98a2b1dc

ADTast-experimental
Last change on this file since 98a2b1dc was 98a2b1dc, checked in by Andrew Beach <ajbeach@…>, 18 months ago

Updated ScopedMap? to use the layout of ErasableScopedMap?; putting the iterator details at the end of the file.

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