21 #ifndef __EDELIB_LIST_H__
22 #define __EDELIB_LIST_H__
34 ListNode() : value(0), next(0), prev(0) { }
39 typedef ListNode NodeType;
42 ListIterator(NodeType* n) : node(n) { }
43 ListIterator() : node(0) { }
45 T& operator*(
void)
const {
46 E_ASSERT(node != 0 &&
"Bad code! Access to zero node!!!");
47 E_ASSERT(node->value != 0 &&
"Bad code! Dereferencing NULL value!!!");
48 return *(T*)node->value;
51 T* operator->(
void)
const {
52 E_ASSERT(node != 0 &&
"Bad code! Access to zero node!!!");
53 E_ASSERT(node->value != 0 &&
"Bad code! Dereferencing NULL value!!!");
54 return (T*)node->value;
57 bool operator!=(
const ListIterator& other)
const {
return node != other.node; }
58 bool operator==(
const ListIterator& other)
const {
return node == other.node; }
59 ListIterator& operator++(
void) { node = node->next;
return *
this; }
60 ListIterator& operator--(
void) { node = node->prev;
return *
this; }
63 #ifndef USE_SMALL_LIST
65 struct ListConstIterator {
66 typedef ListNode NodeType;
69 ListConstIterator(NodeType* n) : node(n) { }
70 ListConstIterator() : node(0) { }
73 ListConstIterator(
const ListIterator<T>& i) : node(i.node) { }
75 const T& operator*(
void)
const {
76 E_ASSERT(node != 0 &&
"Bad code! Access to zero node!!!");
77 E_ASSERT(node->value != 0 &&
"Bad code! Dereferencing NULL value!!!");
78 return *(T*)node->value;
81 const T* operator->(
void)
const {
82 E_ASSERT(node != 0 &&
"Bad code! Access to zero node!!!");
83 E_ASSERT(node->value != 0 &&
"Bad code! Dereferencing NULL value!!!");
84 return (T*)node->value;
87 bool operator!=(
const ListConstIterator& other)
const {
return node != other.node; }
88 bool operator==(
const ListConstIterator& other)
const {
return node == other.node; }
89 ListConstIterator& operator++(
void) { node = node->next;
return *
this; }
90 ListConstIterator& operator--(
void) { node = node->prev;
return *
this; }
159 template <
typename T>
163 typedef unsigned int size_type;
166 typedef ListNode Node;
167 typedef bool (SortCmp)(
const T& val1,
const T& val2);
174 static bool default_sort_cmp(
const T& val1,
const T& val2) {
return val1 < val2; }
176 Node* merge_nodes(Node* a, Node* b, SortCmp* cmp) {
181 while(a != 0 && b != 0) {
183 if(cmp(*(T*)a->value, *(T*)b->value)) {
205 Node* mergesort(Node* c, SortCmp* cmp) {
212 while((b != 0) && (b->next != 0)) {
219 return merge_nodes(mergesort(a, cmp), mergesort(b, cmp), cmp);
244 typedef ListIterator<T> iterator;
245 #ifndef USE_SMALL_LIST
246 typedef ListConstIterator<T> const_iterator;
248 typedef ListIterator<T> const_iterator;
267 E_ASSERT(sz == 0 &&
"Internal error! size() != 0, but list is empty !?!!");
271 Node* p = tail->next;
295 iterator
insert(iterator it,
const T& val) {
297 Node* tmp =
new Node;
298 tmp->value =
new T(val);
303 tail->next = tail->prev = tmp;
304 tmp->next = tmp->prev = tail;
307 tmp->prev = it.node->prev;
308 it.node->prev->next = tmp;
313 return iterator(tmp);
324 E_ASSERT(it.node != tail &&
"Bad code! erase() on end()!!!");
327 it.node->prev->next = it.node->next;
328 it.node->next->prev = it.node->prev;
330 iterator ret(it.node);
334 delete (T*)it.node->value;
355 iterator
begin(
void) {
return iterator(tail ? tail->next : 0); }
360 const_iterator
begin(
void)
const {
return const_iterator(tail ? tail->next : 0); }
367 iterator
end(
void) {
return iterator(tail); }
374 const_iterator
end(
void)
const {
return const_iterator(tail); }
379 T&
front(
void) {
return *(begin()); }
384 const T&
front(
void)
const {
return *(begin()); }
389 T&
back(
void) {
return *(--end()); }
394 const T&
back(
void)
const {
return *(--end()); }
399 size_type
size(
void)
const {
return sz; }
404 bool empty(
void)
const {
return sz == 0; }
410 if(size() != other.
size())
413 iterator it = begin(), it_end = end(), it2 = other.
begin();
414 for(; it != it_end; ++it, ++it2) {
431 void sort(SortCmp* cmp = default_sort_cmp) {
436 tail->prev->next = 0;
438 Node* nn = mergesort(tail->next, cmp);
463 #ifndef NO_LIST_SPECIALIZATION
466 template class list<void*>;
467 template class ListIterator<void*>;
469 template <
typename T>
470 struct ListIterator<T*> {
472 ListIterator<void*> impl;
476 operator ListIterator<void*> () {
return impl; }
478 ListIterator(
const ListIterator<void*>& b) : impl(b) { }
479 typedef ListNode NodeType;
482 ListIterator(NodeType* n) : impl(n) { }
484 T* operator*(
void)
const {
return (T*)*impl; }
486 bool operator!=(
const ListIterator& other)
const {
return impl != other.impl; }
487 bool operator==(
const ListIterator& other)
const {
return impl == other.impl; }
489 ListIterator& operator++(
void) { ++impl;
return *
this; }
490 ListIterator& operator--(
void) { --impl;
return *
this; }
493 template <
typename T>
497 static bool default_sort_cmp(
const T* val1,
const T* val2) {
return *val1 < *val2; }
500 typedef unsigned int size_type;
502 typedef T* value_type;
503 typedef const value_type& const_reference;
504 typedef value_type& reference;
505 typedef value_type* pointer;
506 typedef const value_type* const_pointer;
508 typedef bool (SortCmp)(const_reference val1, const_reference val2);
510 typedef ListIterator<T*> iterator;
511 typedef ListIterator<T*> const_iterator;
513 void clear(
void) { impl.clear(); }
515 iterator insert(iterator it, const_reference val) {
return impl.insert(it, val); }
516 iterator erase(iterator it) {
return impl.erase(it); }
518 void push_back(const_reference val) { impl.push_back((
void*)val); }
519 void push_front(const_reference val) { impl.push_front((
void*)val); }
521 iterator begin(
void) {
return impl.begin(); }
522 const_iterator begin(
void)
const {
return impl.begin(); }
524 iterator end(
void) {
return impl.end(); }
525 const_iterator end(
void)
const {
return impl.end(); }
527 pointer front(
void) {
return impl.front(); }
528 const_pointer front(
void)
const {
return impl.front(); }
530 pointer back(
void) {
return impl.back(); }
531 const_pointer back(
void)
const {
return impl.back(); }
533 size_type size(
void)
const {
return impl.size(); }
534 bool empty(
void)
const {
return impl.empty(); }
536 bool operator==(list<T*>& other) {
return impl.operator==(other); }
537 bool operator!=(list<T*>& other) {
return impl.operator!=(other); }
540 void sort(SortCmp* cmp) { impl.sort((
bool (*)(
void*
const&,
void*
const&)) cmp); }
543 #endif // NO_LIST_SPECIALIZATION
const T & back(void) const
Definition: List.h:394
iterator begin(void)
Definition: List.h:355
#define E_ASSERT(expr)
Definition: Debug.h:117
Linked list class.
Definition: List.h:160
void sort(SortCmp *cmp=default_sort_cmp)
Definition: List.h:431
iterator insert(iterator it, const T &val)
Definition: List.h:295
T & back(void)
Definition: List.h:389
void clear(void)
Definition: List.h:265
bool operator==(const String &str1, const char *str2)
Definition: String.h:353
const T & front(void) const
Definition: List.h:384
bool operator!=(list< T > &other)
Definition: List.h:425
iterator erase(iterator it)
Definition: List.h:322
~list()
Definition: List.h:260
void push_back(const T &val)
Definition: List.h:344
list()
Definition: List.h:255
#define E_DISABLE_CLASS_COPY(klass)
Definition: edelib-global.h:161
iterator end(void)
Definition: List.h:367
const_iterator begin(void) const
Definition: List.h:360
size_type size(void) const
Definition: List.h:399
void push_front(const T &val)
Definition: List.h:350
bool operator==(list< T > &other)
Definition: List.h:409
T & front(void)
Definition: List.h:379
bool operator!=(const String &str1, const char *str2)
Definition: String.h:359
bool empty(void) const
Definition: List.h:404
const_iterator end(void) const
Definition: List.h:374