ROSE 0.11.145.147
WorkLists.h
1// FIXME[Robb Matzke 2020-03-08]: this is generic support stuff and should be moved to roseSupport directory.
2
3#ifndef ROSE_WorkLists_H
4#define ROSE_WorkLists_H
5
6#include <boost/logic/tribool.hpp>
7
8#include <algorithm>
9#include <cassert>
10#include <list>
11#include <map>
12#include <set>
13#include <vector>
14
63template<typename T, class Compare = std::less<T> >
64class WorkList {
65public:
66 typedef T value_type;
67 typedef Compare key_compare;
68
69 explicit WorkList(bool check_uniqueness=false): nitems_(0), use_map_(check_uniqueness) {}
70
72 bool empty() const { return 0 == nitems_; }
73
75 size_t size() const { return nitems_; }
76
78 void clear() { list_.clear(); map_.clear(); }
79
81 bool exists(const T &item) const {
82 return use_map_ ? map_.find(item)!=map_.end() : std::find(list_.begin(), list_.end(), item)!=list_.end();
83 }
84
87 bool unshift(const T&, boost::tribool check_uniqueness=boost::indeterminate);
88
92 template<class Iterator>
93 size_t unshift(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate);
94
96 T shift();
97
100 bool push(const T&, boost::tribool check_uniqueness=boost::logic::indeterminate);
101
105 template<class Iterator>
106 size_t push(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate);
107
109 T pop();
110
115 const T& front() const;
116 const T& back() const;
120 // These are here for compatibility with another WorkList API
122
126 void add(const T& item) { push(item); }
127 void add(const std::vector<T> &items) { push(items.begin(), items.end()); }
128 void add(const std::set<T> &items) { push(items.begin(), items.end()); }
132 T take() { return shift(); }
133
134private:
135 void removed(const T&);
136 std::list<T> list_;
137 std::map<T, size_t, key_compare> map_;
138 size_t nitems_;
139 bool use_map_;
140};
141
146template<typename T, class Compare = std::less<T> >
147class WorkListUnique: public WorkList<T, Compare> {
148public:
150};
151
156template<typename T, class Compare = std::less<T> >
157class WorkListNonUnique: public WorkList<T, Compare> {
158public:
160};
161
162/*******************************************************************************************************************************
163 * Template member functions
164 *******************************************************************************************************************************/
165
166template<typename T, class Compare>
167bool
168WorkList<T, Compare>::unshift(const T &item, boost::tribool check_uniqueness)
169{
170 if (indeterminate(check_uniqueness))
171 check_uniqueness = use_map_;
172 if (use_map_) {
173 std::pair<typename std::map<T, size_t, Compare>::iterator, bool> found = map_.insert(std::pair<T, size_t>(item, 1));
174 if (check_uniqueness && !found.second)
175 return false;
176 if (!found.second)
177 ++found.first->second;
178 } else if (check_uniqueness) {
179 if (std::find(list_.begin(), list_.end(), item)!=list_.end())
180 return false;
181 }
182 list_.push_front(item);
183 ++nitems_;
184 return true;
185}
186
187template<typename T, class Compare>
188template<class Iterator>
189size_t
190WorkList<T, Compare>::unshift(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness)
191{
192 size_t retval = 0;
193 for (Iterator i=begin; i!=end; ++i) {
194 if (unshift(*i, check_uniqueness))
195 ++retval;
196 }
197 return retval;
198}
199
200template<typename T, class Compare>
201bool
202WorkList<T, Compare>::push(const T &item, boost::tribool check_uniqueness)
203{
204 if (indeterminate(check_uniqueness))
205 check_uniqueness = use_map_;
206 if (use_map_) {
207 std::pair<typename std::map<T, size_t, Compare>::iterator, bool> found = map_.insert(std::make_pair(item, 1));
208 if (check_uniqueness && !found.second)
209 return false;
210 if (!found.second)
211 ++found.first->second;
212 } else if (check_uniqueness) {
213 if (std::find(list_.begin(), list_.end(), item)!=list_.end())
214 return false;
215 }
216 list_.push_back(item);
217 ++nitems_;
218 return true;
219}
220
221template<typename T, class Compare>
222template<class Iterator>
223size_t
224WorkList<T, Compare>::push(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness)
225{
226 size_t retval = 0;
227 for (Iterator i=begin; i!=end; ++i) {
228 if (push(*i, check_uniqueness))
229 ++retval;
230 }
231 return retval;
232}
233
234template<typename T, class Compare>
235T
237{
238 assert(!empty());
239 T item = list_.front();
240 list_.pop_front();
241 removed(item);
242 return item;
243}
244
245template<typename T, class Compare>
246T
248{
249 assert(!empty());
250 T item = list_.back();
251 list_.pop_back();
252 removed(item);
253 return item;
254}
255
256template<typename T, class Compare>
257const T&
259{
260 assert(!empty());
261 return list_.front();
262}
263
264template<typename T, class Compare>
265const T&
267{
268 assert(!empty());
269 return list_.back();
270}
271
272template<typename T, class Compare>
273void
275{
276 if (use_map_) {
277 typename std::map<T, size_t>::iterator found = map_.find(item);
278 assert(found!=map_.end());
279 if (found->second > 1) {
280 --found->second;
281 } else {
282 map_.erase(found);
283 }
284 }
285 --nitems_;
286}
287
288#endif
A version of WorkList that does not check for uniqueness by default.
Definition WorkLists.h:157
A version of WorkList that checks for uniqueness by default.
Definition WorkLists.h:147
List of things to work on.
Definition WorkLists.h:64
T pop()
Remove an item from the back of the work list.
Definition WorkLists.h:247
void add(const T &item)
Adds an item(s) to the end of the queue.
Definition WorkLists.h:126
const T & back() const
Returns the object at the front/back of the list without removing it.
Definition WorkLists.h:266
size_t push(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate)
Adds multiple items to the back of the work list.
Definition WorkLists.h:224
size_t unshift(const Iterator &begin, const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate)
Adds multiple items to the front of the work list.
Definition WorkLists.h:190
bool push(const T &, boost::tribool check_uniqueness=boost::logic::indeterminate)
Add an item to the back of the work list.
Definition WorkLists.h:202
bool exists(const T &item) const
Return true if the specified item is already on the work list.
Definition WorkLists.h:81
bool unshift(const T &, boost::tribool check_uniqueness=boost::indeterminate)
Add an item to the front of the work list.
Definition WorkLists.h:168
size_t size() const
Returns the number of items in the work list.
Definition WorkLists.h:75
const T & front() const
Returns the object at the front/back of the list without removing it.
Definition WorkLists.h:258
void add(const std::vector< T > &items)
Adds an item(s) to the end of the queue.
Definition WorkLists.h:127
T take()
Remove and return an item from the front of the work list.
Definition WorkLists.h:132
bool empty() const
Returns true if this work list is empty.
Definition WorkLists.h:72
void clear()
Reset the list to an empty state.
Definition WorkLists.h:78
void add(const std::set< T > &items)
Adds an item(s) to the end of the queue.
Definition WorkLists.h:128
T shift()
Remove and return the item from the front of the work list.
Definition WorkLists.h:236