1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#pragma once
namespace os {
/**
* Defines a set implemented as a doubly-linked list within its elements. The elements are
* required to contain "next" and "prev" pointers of the same type as the template parameter to
* this class. This object is designed to keep track of a number of objects, and has therefore
* no support for multiple keys with the same "key". The key used in this set is the object
* pointer itself. The "prev" and "next" members are assumed to be initialized to zero at
* startup. Use the "SetMember" class to get this automatically. The iterators in the set are
* designed to manage removal of the current item. Eg, iterator a = foo.begin(); foo.erase(*a);
* a++; is legal. Insertions during iterations will however fail in some cases.
*
* This implementation needs to be thread safe when a thread iterates through a set in the
* process of being modified. It is enough to be able to iterate through the entire list between
* any two machine instructions (disregarding any data visibility issues) since this is done by
* the GC when a thread to be scanned has been stopped at an arbitrary point. If iteration
* happens during insertion or removal, the node being modified may or may not be included in
* the iteration, but all other elements need to be visited by the iteration.
*/
template <class T>
class InlineSet {
public:
// Create.
InlineSet() : first(null), last(null), size(0) {}
// Destroy.
~InlineSet() {
clear();
}
// Clear contents. Not thread safe.
void clear() {
T *current = first;
while (current) {
T *next = current->next;
current->prev = null;
current->next = null;
current = next;
}
first = last = null;
size = 0;
}
// Add an element to the set. It is assumed that the element has not previously been added
// to another set. This function is thread safe wrt iterations through the set, ie. an
// iteration through the set between any two machine instructions in this function will
// succeed, yielding all elements in the set, possibly except for the newly inserted
// element.
void insert(T *item) {
++size;
dbg_assert(item->prev == null, L"The node being inserted is already in a set.");
dbg_assert(item->next == null, L"The node being inserted is already in a set.");
atomicWrite(item->prev, last);
atomicWrite(item->next, (T *)null);
if (last)
atomicWrite(last->next, item);
if (!first)
atomicWrite(first, item);
atomicWrite(last, item);
}
// Remove an element from the set. This function is thread safe wrt iterations through the
// set, ie. an iteration through the set between any two machine instructions in this
// function will succeed, yielding all elements in the set, possibly except for the element
// to be removed.
void erase(T *item) {
dbg_assert(size > 0, L"Trying to erase elements in an empty set.");
--size;
if (item == first)
atomicWrite(first, item->next);
if (item == last)
atomicWrite(last, item->prev);
if (item->prev)
atomicWrite(item->prev->next, item->next);
if (item->next)
atomicWrite(item->next->prev, item->prev);
item->prev = item->next = null;
}
// Is the set empty?
inline bool empty() const {
return atomicRead(first) == null && atomicRead(last) == null;
}
// Any elements in the set?
inline bool any() const {
return !empty();
}
// How many elements?
inline nat count() const {
return size;
}
// Iterators...
class iterator {
private:
friend class InlineSet<T>;
iterator(T *at) : at(at), next(null) { if (at) next = atomicRead(at->next); }
T *at;
T *next;
public:
inline T *operator *() const {
return at;
}
inline T *operator ->() const {
return at;
}
inline operator T *() const {
return at;
}
inline iterator &operator ++() {
at = next;
if (next)
next = atomicRead(next->next);
return *this;
}
inline iterator operator ++(int) {
iterator t = *this;
at = next;
if (next)
next = atomicRead(next->next);
return t;
}
inline bool operator ==(const iterator &other) const {
return at == other.at;
}
inline bool operator !=(const iterator &other) const {
return at != other.at;
}
typedef std::forward_iterator_tag iterator_category;
typedef nat difference_type;
typedef T *value_type;
typedef T *pointer;
typedef T &reference;
};
inline iterator begin() const {
return iterator(atomicRead(first));
}
inline iterator end() const {
return iterator(null);
}
// See if "item" is contained within this set. This operation is slow (linear time).
bool contains(T *item) const {
for (iterator current = begin(); current != end(); ++current)
if (*current == item)
return true;
return false;
}
private:
// Disallow copying.
InlineSet(const InlineSet &o);
InlineSet &operator =(const InlineSet &o);
// Data.
T *first;
T *last;
nat size;
};
/**
* A simple class you can derive your data from to get a protected, correctly initialized prev
* and next member in your class.
*/
template <class T>
class SetMember {
public:
SetMember() : prev(null), next(null) {}
~SetMember() {
// make sure we're not currently inside a set!
dbg_assert(prev == null, L"Trying to remove a node inside a InlineSet!");
dbg_assert(next == null, L"Trying to remove a node inside a InlineSet!");
}
// Check if the type is in a set.
bool inASet() const {
return next != null || prev != null;
}
private:
friend class InlineSet<T>;
T *prev;
T *next;
};
}
|