8 #ifndef Sawyer_DistinctList_H
9 #define Sawyer_DistinctList_H
11 #include <boost/foreach.hpp>
13 #include <Sawyer/Map.h>
14 #include <Sawyer/Sawyer.h>
23 template<
class T,
class Cmp = std::less<T> >
27 typedef Cmp Comparator;
28 typedef std::list<Item> Items;
40 template<
class T2,
class Cmp2>
42 BOOST_FOREACH (
const T2 &item, other.
items())
47 template<
class T2,
class Cmp2>
50 BOOST_FOREACH (
const T2 &item, other.
items())
74 return position_.
size();
80 bool exists(
const Item &item)
const {
81 return position_.
exists(item);
88 if (!position_.
exists(item))
91 BOOST_FOREACH (
const Item &x, items_) {
105 throw std::runtime_error(
"front called on empty list");
106 return items_.front();
115 throw std::runtime_error(
"back called on empty list");
116 return items_.back();
124 if (found == position_.
nodes().end()) {
125 items_.push_front(item);
126 position_.
insert(item, items_.begin());
135 if (found == position_.
nodes().end()) {
136 items_.push_back(item);
137 position_.
insert(item, --items_.end());
147 throw std::runtime_error(
"popFront called on empty list");
148 Item item = items_.front();
150 position_.
erase(item);
160 throw std::runtime_error(
"popBack called on empty list");
161 Item item = items_.back();
163 position_.
erase(item);
172 if (found != position_.
nodes().end()) {
173 items_.erase(found->
value());
bool isEmpty() const
Determines whether list is empty.
Map & eraseAt(const NodeIterator &iter)
Remove a node by iterator.
void clear()
Clear the list.
DistinctList(const DistinctList< T2, Cmp2 > &other)
Copy-construct a list.
NodeIterator find(const Key &key)
Find a node by key.
void pushFront(const Item &item)
Insert item at front of list if distinct.
const Items & items() const
Return all items as a list.
bool isEmpty() const
Determines whether this container is empty.
Bidirectional iterator over key/value nodes.
void pushBack(const Item &item)
Insert item at back of list if distinct.
Item popBack()
Return and erase item at back of list.
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
size_t size() const
Number of nodes, keys, or values in this container.
const Item & back() const
Reference to item at back of list.
boost::iterator_range< NodeIterator > nodes()
Iterators for container nodes.
Value & value()
Value part of key/value node.
DistinctList & operator=(const DistinctList< T2, Cmp2 > &other)
Assign one list to another.
size_t size() const
Number of items in list.
A doubly-linked list of distinct items.
Name space for the entire library.
void erase(const Item &item)
Erase an item from the list.
DistinctList()
Construct an empty list.
bool exists(const Item &item) const
Determine if an item exists.
Item popFront()
Return and erase item at front of list.
bool exists(const Key &key) const
Determine if a key exists.
Map & erase(const Key &key)
Remove a node with specified key.
const Item & front() const
Reference to item at front of list.
Map & clear()
Remove all nodes.
size_t position(const Item &item) const
Determine the position of an item.