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