| 1 | /* |
| 2 | * Copyright (C) 2004,2006 Dizzy |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | #ifndef INCLUDED_ELIST_TYPES |
| 19 | #define INCLUDED_ELIST_TYPES |
| 20 | |
| 21 | namespace pvpgn |
| 22 | { |
| 23 | |
| 24 | template<typename T> |
| 25 | class elist_node |
| 26 | { |
| 27 | public: |
| 28 | elist_node() : info_(0), prev_(this), next_(this) {} |
| 29 | ~elist_node() throw() { |
| 30 | remove(); |
| 31 | } |
| 32 | |
| 33 | void link_front(elist_node& where, T& info) { |
| 34 | info_ = &info; |
| 35 | where.next_->prev_ = this; |
| 36 | next_ = where.next_; |
| 37 | prev_ = &where; |
| 38 | where.next_ = this; |
| 39 | } |
| 40 | |
| 41 | void link_back(elist_node& where, T& info) { |
| 42 | info_ = &info; |
| 43 | where.prev_->next_ = this; |
| 44 | next_ = &where; |
| 45 | prev_ = where.prev_; |
| 46 | where.prev_ = this; |
| 47 | } |
| 48 | |
| 49 | void remove() { |
| 50 | /* remove ourselves from any link */ |
| 51 | prev_->next_ = next_; |
| 52 | next_->prev_ = prev_; |
| 53 | prev_ = next_ = this; |
| 54 | /* poison this to find out bugs a lot earlier */ |
| 55 | info_ = 0; |
| 56 | } |
| 57 | |
| 58 | T& info() const { return *info_; } |
| 59 | |
| 60 | elist_node* next() const { return next_; } |
| 61 | elist_node* prev() const { return prev_; } |
| 62 | |
| 63 | private: |
| 64 | T* info_; |
| 65 | elist_node *prev_, *next_; |
| 66 | |
| 67 | elist_node(const elist_node&); |
| 68 | elist_node& operator=(const elist_node&); |
| 69 | }; |
| 70 | |
| 71 | template<typename T> |
| 72 | class elist |
| 73 | { |
| 74 | public: |
| 75 | class iterator |
| 76 | { |
| 77 | public: |
| 78 | iterator() : ptr(0) {} |
| 79 | iterator(const iterator& orig) :ptr(orig.ptr) {} |
| 80 | ~iterator() throw() {} |
| 81 | |
| 82 | iterator& operator=(const iterator& i) { |
| 83 | ptr = i.ptr; |
| 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | bool operator==(const iterator& op) const { |
| 88 | return ptr == op.ptr; |
| 89 | } |
| 90 | bool operator!=(const iterator& op) const { |
| 91 | return ptr != op.ptr; |
| 92 | } |
| 93 | |
| 94 | iterator& operator++() { |
| 95 | ptr = ptr->next(); |
| 96 | return *this; |
| 97 | } |
| 98 | iterator operator++(int) { |
| 99 | elist_node<T>* save = ptr; |
| 100 | ptr = ptr->next(); |
| 101 | return iterator(save); |
| 102 | } |
| 103 | |
| 104 | T& operator*() const { |
| 105 | return ptr->info(); |
| 106 | } |
| 107 | |
| 108 | T* operator->() const { |
| 109 | return &ptr->info(); |
| 110 | } |
| 111 | |
| 112 | private: |
| 113 | elist_node<T> *ptr; |
| 114 | |
| 115 | explicit iterator(elist_node<T>* ptr_) : ptr(ptr_) {} |
| 116 | friend class elist; |
| 117 | }; |
| 118 | |
| 119 | explicit elist(elist_node<T> T::* node_) : node(node_) {} |
| 120 | ~elist() throw() { |
| 121 | clear(); |
| 122 | } |
| 123 | |
| 124 | void push_back(T& obj) { |
| 125 | (obj.*node).link_back(head, obj); |
| 126 | } |
| 127 | |
| 128 | void push_front(T& obj) { |
| 129 | (obj.*node).link_front(head, obj); |
| 130 | } |
| 131 | |
| 132 | void remove(T& obj) { |
| 133 | (obj.*node).remove(); |
| 134 | } |
| 135 | |
| 136 | void remove(iterator& it) { |
| 137 | it.ptr->remove(); |
| 138 | } |
| 139 | |
| 140 | void clear() { |
| 141 | while (!empty()) |
| 142 | head.prev()->remove(); |
| 143 | } |
| 144 | |
| 145 | bool empty() { |
| 146 | return &head == head.next(); |
| 147 | } |
| 148 | |
| 149 | T& front() const { return head.next()->info(); } |
| 150 | T& back() const { return head.prev()->info(); } |
| 151 | |
| 152 | iterator begin() { |
| 153 | return iterator(head.next()); |
| 154 | } |
| 155 | iterator end() { |
| 156 | return iterator(&head); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | /* the head of the list */ |
| 161 | elist_node<T> head; |
| 162 | |
| 163 | /* used to access the elist_node<T> of a given T */ |
| 164 | elist_node<T> T::* node; |
| 165 | |
| 166 | elist(const elist&); |
| 167 | elist& operator=(const elist&); |
| 168 | }; |
| 169 | |
| 170 | typedef struct elist_struct { |
| 171 | struct elist_struct *next, *prev; |
| 172 | } t_elist; |
| 173 | |
| 174 | /* hlist are single linked elists */ |
| 175 | typedef struct hlist_struct { |
| 176 | struct hlist_struct *next; |
| 177 | } t_hlist; |
| 178 | |
| 179 | } |
| 180 | |
| 181 | #endif /* INCLUDED_ELIST_TYPES */ |
| 182 | |
| 183 | #ifndef INCLUDED_ELIST_PROTOS |
| 184 | #define INCLUDED_ELIST_PROTOS |
| 185 | |
| 186 | #include <cstddef> |
| 187 | |
| 188 | /* access to it's members */ |
| 189 | #define elist_next(ptr) ((ptr)->next) |
| 190 | #define elist_prev(ptr) ((ptr)->prev) |
| 191 | |
| 192 | #define __elist_init(elist,val) { (elist)->next = (elist)->prev = (val); } |
| 193 | #define elist_init(elist) __elist_init(elist,elist) |
| 194 | #define DECLARE_ELIST_INIT(var) \ |
| 195 | t_elist var = { &var, &var } |
| 196 | |
| 197 | namespace pvpgn |
| 198 | { |
| 199 | |
| 200 | /* link an new node just after "where" */ |
| 201 | static inline void elist_add(t_elist *where, t_elist *what) |
| 202 | { |
| 203 | what->next = where->next; |
| 204 | where->next->prev = what; |
| 205 | what->prev = where; |
| 206 | where->next = what; |
| 207 | } |
| 208 | |
| 209 | /* link a new node just before "where" (usefull in creating queues) */ |
| 210 | static inline void elist_add_tail(t_elist *where, t_elist *what) |
| 211 | { |
| 212 | what->prev = where->prev; |
| 213 | where->prev->next = what; |
| 214 | what->next = where; |
| 215 | where->prev = what; |
| 216 | } |
| 217 | |
| 218 | /* unlink "what" from it's list */ |
| 219 | static inline void elist_del(t_elist *what) |
| 220 | { |
| 221 | what->next->prev = what->prev; |
| 222 | what->prev->next = what->next; |
| 223 | } |
| 224 | |
| 225 | /* finds out the container address by computing it from the list node |
| 226 | * address substracting the offset inside the container of the list node |
| 227 | * member */ |
| 228 | #define elist_entry(ptr,type,member) ((type*)(((char*)ptr)-offsetof(type,member))) |
| 229 | |
| 230 | /* DONT remove while traversing with this ! */ |
| 231 | #define elist_for_each(pos,head) \ |
| 232 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 233 | |
| 234 | #define elist_for_each_rev(pos,head) \ |
| 235 | for (pos = (head)->prev; pos != (head); pos = pos->prev) |
| 236 | |
| 237 | /* safe for removals while traversing */ |
| 238 | #define elist_for_each_safe(pos,head,save) \ |
| 239 | for (pos = (head)->next, save = pos->next; pos != (head); \ |
| 240 | pos = save, save = pos->next) |
| 241 | |
| 242 | #define elist_for_each_safe_rev(pos,head,save) \ |
| 243 | for (pos = (head)->prev, save = pos->prev; pos != (head); \ |
| 244 | pos = save, save = pos->prev) |
| 245 | |
| 246 | #define elist_empty(ptr) ((ptr)->next == (ptr)) |
| 247 | |
| 248 | #define hlist_next(ptr) elist_next(ptr) |
| 249 | |
| 250 | #define __hlist_init(hlist,val) { (hlist)->next = (val); } |
| 251 | #define hlist_init(hlist) __hlist_init(hlist,hlist) |
| 252 | #define DECLARE_HLIST_INIT(var) \ |
| 253 | t_hlist var = { &var }; |
| 254 | |
| 255 | /* link an new node just after "where" */ |
| 256 | static inline void hlist_add(t_hlist *where, t_hlist *what) |
| 257 | { |
| 258 | what->next = where->next; |
| 259 | where->next = what; |
| 260 | } |
| 261 | |
| 262 | /* unlink "what" from it's list, prev->next = what */ |
| 263 | static inline void hlist_del(t_hlist *what, t_hlist *prev) |
| 264 | { |
| 265 | prev->next = what->next; |
| 266 | } |
| 267 | |
| 268 | /* move "what" back in the list with one position, never NULL |
| 269 | * prev: the previous element in the list, never NULL |
| 270 | * prev2: the previous element of "prev", if NULL means "what" is first |
| 271 | */ |
| 272 | static inline void hlist_promote(t_hlist *what, t_hlist *prev, t_hlist *prev2) |
| 273 | { |
| 274 | if (prev2) { |
| 275 | prev->next = what->next; |
| 276 | prev2->next = what; |
| 277 | what->next = prev; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* finds out the container address by computing it from the list node |
| 282 | * address substracting the offset inside the container of the list node |
| 283 | * member */ |
| 284 | #define hlist_entry(ptr,type,member) elist_entry(ptr,type,member) |
| 285 | |
| 286 | /* DONT remove while traversing with this ! */ |
| 287 | #define hlist_for_each(pos,head) elist_for_each(pos,head) |
| 288 | |
| 289 | /* safe for removals while traversing */ |
| 290 | #define hlist_for_each_safe(pos,head,save) elist_for_each_safe(pos,head,save) |
| 291 | |
| 292 | #define hlist_empty(ptr) elist_empty(ptr) |
| 293 | |
| 294 | } |
| 295 | |
| 296 | #endif /* INCLUDED_ELIST_PROTOS */ |