source: src/GenPoly/ScopedSet.h @ 7f1be01

Last change on this file since 7f1be01 was 7f1be01, checked in by Andrew Beach <ajbeach@…>, 11 months ago

Combined some fixes from the variaus scoped containers. Reducing redeclarations, internal type names now use one consistent naming scheme (it is different from the public naming convention), a whitespace fix and shrinking a few functions.

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