source: src/GenPoly/ScopedMap.h @ 89173242

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 89173242 was 0531b5d, checked in by Aaron Moss <a3moss@…>, 8 years ago

Fixed ScopedMap?'s const find, refactored InstantionMap? to use ScopedMap?

  • Property mode set to 100644
File size: 7.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 : Aaron B. Moss
12// Last Modified On : Wed Dec 2 11:37:00 2015
13// Update Count     : 1
14//
15
16#ifndef _SCOPEDMAP_H
17#define _SCOPEDMAP_H
18
19#include <cassert>
20#include <iterator>
21#include <map>
22#include <utility>
23#include <vector>
24
25namespace GenPoly {
26        /// A map where the items are placed into nested scopes;
27        /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
28        template<typename Key, typename Value>
29        class ScopedMap {
30                typedef std::map< Key, Value > Scope;
31                typedef std::vector< Scope > ScopeList;
32
33                ScopeList scopes; ///< scoped list of maps
34        public:
35                typedef typename Scope::key_type key_type;
36                typedef typename Scope::mapped_type mapped_type;
37                typedef typename Scope::value_type value_type;
38                typedef typename ScopeList::size_type size_type;
39                typedef typename ScopeList::difference_type difference_type;
40                typedef typename Scope::reference reference;
41                typedef typename Scope::const_reference const_reference;
42                typedef typename Scope::pointer pointer;
43                typedef typename Scope::const_pointer const_pointer;
44               
45                class iterator : public std::iterator< std::bidirectional_iterator_tag,
46                                                       value_type > {
47                friend class ScopedMap;
48                friend class const_iterator;
49                        typedef typename std::map< Key, Value >::iterator wrapped_iterator;
50                        typedef typename std::vector< std::map< Key, Value > > scope_list;
51                        typedef typename scope_list::size_type size_type;
52
53                        iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
54                                : scopes(&_scopes), it(_it), i(_i) {}
55                public:
56                        iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
57                        iterator& operator= (const iterator &that) {
58                                scopes = that.scopes; i = that.i; it = that.it;
59                                return *this;
60                        }
61                       
62                        reference operator* () { return *it; }
63                        pointer operator-> () { return it.operator->(); }
64
65                        iterator& operator++ () {
66                                if ( it == (*scopes)[i].end() ) {
67                                        if ( i == 0 ) return *this;
68                                        --i;
69                                        it = (*scopes)[i].begin();
70                                        return *this;
71                                }
72                                ++it;
73                                return *this;
74                        }
75                        iterator& operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
76
77                        iterator& operator-- () {
78                                // may fail if this is the begin iterator; allowed by STL spec
79                                if ( it == (*scopes)[i].begin() ) {
80                                        ++i;
81                                        it = (*scopes)[i].end();
82                                }
83                                --it;
84                                return *this;
85                        }
86                        iterator& operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
87
88                        bool operator== (const iterator &that) {
89                                return scopes == that.scopes && i == that.i && it == that.it;
90                        }
91                        bool operator!= (const iterator &that) { return !( *this == that ); }
92
93                private:
94                        scope_list const *scopes;
95                        wrapped_iterator it;
96                        size_type i;
97                };
98
99                class const_iterator : public std::iterator< std::bidirectional_iterator_tag,
100                                                             value_type > {
101                friend class ScopedMap;
102                        typedef typename std::map< Key, Value >::iterator wrapped_iterator;
103                        typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator;
104                        typedef typename std::vector< std::map< Key, Value > > scope_list;
105                        typedef typename scope_list::size_type size_type;
106
107                        const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
108                                : scopes(&_scopes), it(_it), i(_i) {}
109                public:
110                        const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
111                        const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
112                        const_iterator& operator= (const iterator &that) {
113                                scopes = that.scopes; i = that.i; it = that.it;
114                                return *this;
115                        }
116                        const_iterator& operator= (const const_iterator &that) {
117                                scopes = that.scopes; i = that.i; it = that.it;
118                                return *this;
119                        }
120
121                        const_reference operator* () { return *it; }
122                        const_pointer operator-> () { return it.operator->(); }
123
124                        const_iterator& operator++ () {
125                                if ( it == (*scopes)[i].end() ) {
126                                        if ( i == 0 ) return *this;
127                                        --i;
128                                        it = (*scopes)[i].begin();
129                                        return *this;
130                                }
131                                ++it;
132                                return *this;
133                        }
134                        const_iterator& operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
135
136                        const_iterator& operator-- () {
137                                // may fail if this is the begin iterator; allowed by STL spec
138                                if ( it == (*scopes)[i].begin() ) {
139                                        ++i;
140                                        it = (*scopes)[i].end();
141                                }
142                                --it;
143                                return *this;
144                        }
145                        const_iterator& operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
146
147                        bool operator== (const const_iterator &that) {
148                                return scopes == that.scopes && i == that.i && it == that.it;
149                        }
150                        bool operator!= (const const_iterator &that) { return !( *this == that ); }
151
152                private:
153                        scope_list const *scopes;
154                        wrapped_const_iterator it;
155                        size_type i;
156                };
157               
158                /// Starts a new scope
159                void beginScope() {
160                        Scope scope;
161                        scopes.push_back(scope);
162                }
163
164                /// Ends a scope; invalidates any iterators pointing to elements of that scope
165                void endScope() {
166                        scopes.pop_back();
167                        assert( ! scopes.empty() );
168                }
169
170                /// Default constructor initializes with one scope
171                ScopedMap() { beginScope(); }
172
173                iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1); }
174                const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
175                const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1); }
176                iterator end() { return iterator(scopes, scopes[0].end(), 0); }
177                const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
178                const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); }
179
180                /// Gets the index of the current scope (counted from 1)
181                size_type currentScope() const { return scopes.size(); }
182
183                /// Finds the given key in the outermost scope it occurs; returns end() for none such
184                iterator find( const Key &key ) {
185                        for ( size_type i = scopes.size() - 1; ; --i ) {
186                                typename Scope::iterator val = scopes[i].find( key );
187                                if ( val != scopes[i].end() ) return iterator( scopes, val, i );
188                                if ( i == 0 ) break;
189                        }
190                        return end();
191                }
192                const_iterator find( const Key &key ) const {
193                                return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );
194                }
195               
196                /// Finds the given key in the outermost scope inside the given scope where it occurs
197                iterator findNext( const_iterator &it, const Key &key ) {
198                        if ( it.i == 0 ) return end();
199                        for ( size_type i = it.i - 1; ; --i ) {
200                                typename Scope::iterator val = scopes[i].find( key );
201                                if ( val != scopes[i].end() ) return iterator( scopes, val, i );
202                                if ( i == 0 ) break;
203                        }
204                        return end();
205                }
206                const_iterator findNext( const_iterator &it, const Key &key ) const {
207                                return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );
208                }
209
210                /// Inserts the given key-value pair into the outermost scope
211                std::pair< iterator, bool > insert( const value_type &value ) {
212                        std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value );
213                        return std::make_pair( iterator(scopes, res.first, scopes.size()-1), res.second );
214                }
215                std::pair< iterator, bool > insert( const Key &key, const Value &value ) { return insert( std::make_pair( key, value ) ); }
216
217                Value& operator[] ( const Key &key ) {
218                        iterator slot = find( key );
219                        if ( slot != end() ) return slot->second;
220                        return insert( key, Value() ).first->second;
221                }
222        };
223} // namespace GenPoly
224
225#endif // _SCOPEDMAP_H
226
227// Local Variables: //
228// tab-width: 4 //
229// mode: c++ //
230// compile-command: "make install" //
231// End: //
Note: See TracBrowser for help on using the repository browser.