source: src/GenPoly/ScopedSet.h@ 85dd381

ADT ast-experimental
Last change on this file since 85dd381 was 85dd381, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Fixed indentation in ScopedSet and moved the iterator details to the bottom of the file.

  • Property mode set to 100644
File size: 7.9 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 ScopeList scopes; ///< scoped list of sets
32public:
33 typedef typename Scope::key_type key_type;
34 typedef typename Scope::value_type value_type;
35 typedef typename ScopeList::size_type size_type;
36 typedef typename ScopeList::difference_type difference_type;
37 typedef typename Scope::reference reference;
38 typedef typename Scope::const_reference const_reference;
39 typedef typename Scope::pointer pointer;
40 typedef typename Scope::const_pointer const_pointer;
41
42 // Both iterator types are complete bidirectional iterators, see below.
43 class iterator;
44 class const_iterator;
45
46 /// Starts a new scope
47 void beginScope() {
48 Scope scope;
49 scopes.push_back(scope);
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
104template<typename Value>
105class ScopedSet<Value>::iterator :
106 public std::iterator< std::bidirectional_iterator_tag, value_type > {
107 friend class ScopedSet;
108 friend class const_iterator;
109 typedef typename std::set< Value >::iterator wrapped_iterator;
110 typedef typename std::vector< std::set< Value > > scope_list;
111 typedef typename scope_list::size_type size_type;
112
113 /// Checks if this iterator points to a valid item
114 bool is_valid() const {
115 return it != (*scopes)[i].end();
116 }
117
118 /// Increments on invalid
119 iterator& next_valid() {
120 if ( ! is_valid() ) { ++(*this); }
121 return *this;
122 }
123
124 /// Decrements on invalid
125 iterator& prev_valid() {
126 if ( ! is_valid() ) { --(*this); }
127 return *this;
128 }
129
130 iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
131 : scopes(&_scopes), it(_it), i(_i) {}
132public:
133 iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
134 iterator& operator= (const iterator &that) {
135 scopes = that.scopes; i = that.i; it = that.it;
136 return *this;
137 }
138
139 reference operator* () { return *it; }
140 pointer operator-> () { return it.operator->(); }
141
142 iterator& operator++ () {
143 if ( it == (*scopes)[i].end() ) {
144 if ( i == 0 ) return *this;
145 --i;
146 it = (*scopes)[i].begin();
147 } else {
148 ++it;
149 }
150 return next_valid();
151 }
152 iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
153
154 iterator& operator-- () {
155 // may fail if this is the begin iterator; allowed by STL spec
156 if ( it == (*scopes)[i].begin() ) {
157 ++i;
158 it = (*scopes)[i].end();
159 }
160 --it;
161 return prev_valid();
162 }
163 iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
164
165 bool operator== (const iterator &that) {
166 return scopes == that.scopes && i == that.i && it == that.it;
167 }
168 bool operator!= (const iterator &that) { return !( *this == that ); }
169
170 size_type get_level() const { return i; }
171
172private:
173 scope_list const *scopes;
174 wrapped_iterator it;
175 size_type i;
176};
177
178template<typename Value>
179class ScopedSet<Value>::const_iterator :
180 public std::iterator< std::bidirectional_iterator_tag, value_type > {
181 friend class ScopedSet;
182 typedef typename std::set< Value >::iterator wrapped_iterator;
183 typedef typename std::set< Value >::const_iterator wrapped_const_iterator;
184 typedef typename std::vector< std::set< Value > > scope_list;
185 typedef typename scope_list::size_type size_type;
186
187 /// Checks if this iterator points to a valid item
188 bool is_valid() const {
189 return it != (*scopes)[i].end();
190 }
191
192 /// Increments on invalid
193 const_iterator& next_valid() {
194 if ( ! is_valid() ) { ++(*this); }
195 return *this;
196 }
197
198 /// Decrements on invalid
199 const_iterator& prev_valid() {
200 if ( ! is_valid() ) { --(*this); }
201 return *this;
202 }
203
204 const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
205 : scopes(&_scopes), it(_it), i(_i) {}
206public:
207 const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
208 const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
209 const_iterator& operator= (const iterator &that) {
210 scopes = that.scopes; i = that.i; it = that.it;
211 return *this;
212 }
213 const_iterator& operator= (const const_iterator &that) {
214 scopes = that.scopes; i = that.i; it = that.it;
215 return *this;
216 }
217
218 const_reference operator* () { return *it; }
219 const_pointer operator-> () { return it.operator->(); }
220
221 const_iterator& operator++ () {
222 if ( it == (*scopes)[i].end() ) {
223 if ( i == 0 ) return *this;
224 --i;
225 it = (*scopes)[i].begin();
226 } else {
227 ++it;
228 }
229 return next_valid();
230 }
231 const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
232
233 const_iterator& operator-- () {
234 // may fail if this is the begin iterator; allowed by STL spec
235 if ( it == (*scopes)[i].begin() ) {
236 ++i;
237 it = (*scopes)[i].end();
238 }
239 --it;
240 return prev_valid();
241 }
242 const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
243
244 bool operator== (const const_iterator &that) {
245 return scopes == that.scopes && i == that.i && it == that.it;
246 }
247 bool operator!= (const const_iterator &that) { return !( *this == that ); }
248
249 size_type get_level() const { return i; }
250
251private:
252 scope_list const *scopes;
253 wrapped_const_iterator it;
254 size_type i;
255};
256
257} // namespace GenPoly
258
259// Local Variables: //
260// tab-width: 4 //
261// mode: c++ //
262// compile-command: "make install" //
263// End: //
Note: See TracBrowser for help on using the repository browser.