source: src/Common/ScopedMap.h @ e62802f

ADTast-experimental
Last change on this file since e62802f was ccb29b4, checked in by Andrew Beach <ajbeach@…>, 17 months ago

This should fix the issues with building with older gcc versions.

  • Property mode set to 100644
File size: 11.5 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
[df00c78]12// Last Modified On : Tue Feb 15 08:41:28 2022
13// Update Count     : 5
[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>
[df00c78]38                Scope(N && n) : map(), note(std::forward<N>(n)) {}
[98a2b1dc]39
[4612bb0]40                Scope() = default;
[df00c78]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
[db9d7a9]48        /// Scoped list of maps.
49        ScopeList scopes;
[e491159]50public:
[6253fc3]51        typedef typename MapType::key_type key_type;
52        typedef typename MapType::mapped_type mapped_type;
53        typedef typename MapType::value_type value_type;
[e491159]54        typedef typename ScopeList::size_type size_type;
55        typedef typename ScopeList::difference_type difference_type;
[6253fc3]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;
[e491159]60
[98a2b1dc]61        // Both iterator types are complete bidrectional iterators, see below.
62        class iterator;
63        class const_iterator;
[e491159]64
65        /// Starts a new scope
66        void beginScope() {
67                scopes.emplace_back();
68        }
69
[4612bb0]70        // Starts a new scope with the given note
71        template<typename N>
[df00c78]72        void beginScope( N && n ) {
[4612bb0]73                scopes.emplace_back( std::forward<N>(n) );
74        }
75
[e491159]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
[4612bb0]83        ScopedMap() : scopes() { beginScope(); }
84
85        /// Constructs with a given note on the outermost scope
86        template<typename N>
[df00c78]87        ScopedMap( N && n ) : scopes() { beginScope(std::forward<N>(n)); }
[e491159]88
[6253fc3]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); }
[e491159]95
96        /// Gets the index of the current scope (counted from 1)
[1f75e2d]97        size_type currentScope() const { return scopes.size() - 1; }
[e491159]98
[6253fc3]99        /// Gets the note at the given scope
[df00c78]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; }
[6253fc3]104
[e491159]105        /// Finds the given key in the outermost scope it occurs; returns end() for none such
[df00c78]106        iterator find( const Key & key ) {
[e491159]107                for ( size_type i = scopes.size() - 1; ; --i ) {
[6253fc3]108                        typename MapType::iterator val = scopes[i].map.find( key );
109                        if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
[e491159]110                        if ( i == 0 ) break;
111                }
112                return end();
113        }
[df00c78]114        const_iterator find( const Key & key ) const {
[6253fc3]115                        return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) );
[e491159]116        }
117
[e58dfb9]118        /// Finds the given key in the provided scope; returns end() for none such
[df00c78]119        iterator findAt( size_type scope, const Key & key ) {
[6253fc3]120                typename MapType::iterator val = scopes[scope].map.find( key );
121                if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope );
[e58dfb9]122                return end();
123        }
[df00c78]124        const_iterator findAt( size_type scope, const Key & key ) const {
[6253fc3]125                return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) );
[e58dfb9]126        }
127
[e491159]128        /// Finds the given key in the outermost scope inside the given scope where it occurs
[df00c78]129        iterator findNext( const_iterator & it, const Key & key ) {
[1f75e2d]130                if ( it.level == 0 ) return end();
131                for ( size_type i = it.level - 1; ; --i ) {
[6253fc3]132                        typename MapType::iterator val = scopes[i].map.find( key );
133                        if ( val != scopes[i].map.end() ) return iterator( scopes, val, i );
[e491159]134                        if ( i == 0 ) break;
135                }
136                return end();
137        }
[df00c78]138        const_iterator findNext( const_iterator & it, const Key & key ) const {
[6253fc3]139                        return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) );
[e491159]140        }
141
142        /// Inserts the given key-value pair into the outermost scope
[1f75e2d]143        template< typename value_type_t >
[df00c78]144        std::pair< iterator, bool > insert( value_type_t && value ) {
[6253fc3]145                std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) );
[1f75e2d]146                return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) );
[e491159]147        }
148
[1f75e2d]149        template< typename value_t >
[df00c78]150        std::pair< iterator, bool > insert( const Key & key, value_t && value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
[1f75e2d]151
152        template< typename value_type_t >
[df00c78]153        std::pair< iterator, bool > insertAt( size_type scope, value_type_t && value ) {
[6253fc3]154                std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) );
[1f75e2d]155                return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) );
156        }
[e491159]157
[b12c036]158        template< typename value_t >
[df00c78]159        std::pair< iterator, bool > insertAt( size_type scope, const Key & key, value_t && value ) {
[b12c036]160                return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) );
161        }
162
[df00c78]163        Value & operator[] ( const Key & key ) {
[e491159]164                iterator slot = find( key );
165                if ( slot != end() ) return slot->second;
166                return insert( key, Value() ).first->second;
167        }
168
[21a2a7d]169        /// Erases element with key in the innermost scope that has it.
170        size_type erase( const Key & key ) {
171                for ( auto it = scopes.rbegin() ; it != scopes.rend() ; ++it ) {
[ccb29b4]172                        size_type i = it->map.erase( key );
173                        if ( 0 != i ) return i;
[21a2a7d]174                }
175                return 0;
176        }
[e491159]177
[df00c78]178        size_type count( const Key & key ) const {
[e491159]179                size_type c = 0;
180                auto it = find( key );
181                auto end = cend();
182
183                while(it != end) {
184                        c++;
185                        it = findNext(it, key);
186                }
187
188                return c;
189        }
[e9b5043]190
191        bool contains( const Key & key ) const {
192                return find( key ) != cend();
193        }
[e491159]194};
195
[98a2b1dc]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
[e491159]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.