3#ifndef ROSE_WorkLists_H
4#define ROSE_WorkLists_H
6#include <boost/logic/tribool.hpp>
63template<
typename T,
class Compare = std::less<T> >
67 typedef Compare key_compare;
69 explicit WorkList(
bool check_uniqueness=
false): nitems_(0), use_map_(check_uniqueness) {}
72 bool empty()
const {
return 0 == nitems_; }
75 size_t size()
const {
return nitems_; }
78 void clear() { list_.clear(); map_.clear(); }
82 return use_map_ ? map_.find(item)!=map_.end() : std::find(list_.begin(), list_.end(), item)!=list_.end();
87 bool unshift(
const T&, boost::tribool check_uniqueness=boost::indeterminate);
92 template<
class Iterator>
93 size_t unshift(
const Iterator &begin,
const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate);
100 bool push(
const T&, boost::tribool check_uniqueness=boost::logic::indeterminate);
105 template<
class Iterator>
106 size_t push(
const Iterator &begin,
const Iterator &end, boost::tribool check_uniqueness=boost::indeterminate);
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()); }
135 void removed(
const T&);
137 std::map<T, size_t, key_compare> map_;
146template<
typename T,
class Compare = std::less<T> >
156template<
typename T,
class Compare = std::less<T> >
166template<
typename T,
class Compare>
170 if (indeterminate(check_uniqueness))
171 check_uniqueness = 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)
177 ++found.first->second;
178 }
else if (check_uniqueness) {
179 if (std::find(list_.begin(), list_.end(), item)!=list_.end())
182 list_.push_front(item);
187template<
typename T,
class Compare>
188template<
class Iterator>
193 for (Iterator i=begin; i!=end; ++i) {
194 if (unshift(*i, check_uniqueness))
200template<
typename T,
class Compare>
204 if (indeterminate(check_uniqueness))
205 check_uniqueness = 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)
211 ++found.first->second;
212 }
else if (check_uniqueness) {
213 if (std::find(list_.begin(), list_.end(), item)!=list_.end())
216 list_.push_back(item);
221template<
typename T,
class Compare>
222template<
class Iterator>
227 for (Iterator i=begin; i!=end; ++i) {
228 if (push(*i, check_uniqueness))
234template<
typename T,
class Compare>
239 T item = list_.front();
245template<
typename T,
class Compare>
250 T item = list_.back();
256template<
typename T,
class Compare>
261 return list_.front();
264template<
typename T,
class Compare>
272template<
typename T,
class Compare>
277 typename std::map<T, size_t>::iterator found = map_.find(item);
278 assert(found!=map_.end());
279 if (found->second > 1) {
A version of WorkList that does not check for uniqueness by default.
A version of WorkList that checks for uniqueness by default.
List of things to work on.
T pop()
Remove an item from the back of the work list.
void add(const T &item)
Adds an item(s) to the end of the queue.
const T & back() const
Returns the object at the front/back of the list without removing it.
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.
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.
bool push(const T &, boost::tribool check_uniqueness=boost::logic::indeterminate)
Add an item to the back of the work list.
bool exists(const T &item) const
Return true if the specified item is already on the work list.
bool unshift(const T &, boost::tribool check_uniqueness=boost::indeterminate)
Add an item to the front of the work list.
size_t size() const
Returns the number of items in the work list.
const T & front() const
Returns the object at the front/back of the list without removing it.
void add(const std::vector< T > &items)
Adds an item(s) to the end of the queue.
T take()
Remove and return an item from the front of the work list.
bool empty() const
Returns true if this work list is empty.
void clear()
Reset the list to an empty state.
void add(const std::set< T > &items)
Adds an item(s) to the end of the queue.
T shift()
Remove and return the item from the front of the work list.