| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifndef _TVG_INLIST_H_ |
| 24 |
#define _TVG_INLIST_H_ |
| 25 |
|
| 26 |
namespace tvg { |
| 27 |
|
| 28 |
|
| 29 |
#define INLIST_ITEM(T) \ |
| 30 |
T* prev; \ |
| 31 |
T* next |
| 32 |
|
| 33 |
template<typename T> |
| 34 |
struct Inlist |
| 35 |
{ |
| 36 |
T* head = nullptr; |
| 37 |
T* tail = nullptr; |
| 38 |
|
| 39 |
void free() |
| 40 |
{ |
| 41 |
while (head) { |
| 42 |
auto t = head; |
| 43 |
head = t->next; |
| 44 |
delete(t); |
| 45 |
} |
| 46 |
head = tail = nullptr; |
| 47 |
} |
| 48 |
|
| 49 |
void back(T* element) |
| 50 |
{ |
| 51 |
if (tail) { |
| 52 |
tail->next = element; |
| 53 |
element->prev = tail; |
| 54 |
element->next = nullptr; |
| 55 |
tail = element; |
| 56 |
} else { |
| 57 |
head = tail = element; |
| 58 |
element->prev = nullptr; |
| 59 |
element->next = nullptr; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
void front(T* element) |
| 64 |
{ |
| 65 |
if (head) { |
| 66 |
head->prev = element; |
| 67 |
element->prev = nullptr; |
| 68 |
element->next = head; |
| 69 |
head = element; |
| 70 |
} else { |
| 71 |
head = tail = element; |
| 72 |
element->prev = nullptr; |
| 73 |
element->next = nullptr; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
T* back() |
| 78 |
{ |
| 79 |
if (!tail) return nullptr; |
| 80 |
auto t = tail; |
| 81 |
tail = t->prev; |
| 82 |
if (!tail) head = nullptr; |
| 83 |
return t; |
| 84 |
} |
| 85 |
|
| 86 |
T* front() |
| 87 |
{ |
| 88 |
if (!head) return nullptr; |
| 89 |
auto t = head; |
| 90 |
head = t->next; |
| 91 |
if (!head) tail = nullptr; |
| 92 |
return t; |
| 93 |
} |
| 94 |
|
| 95 |
void remove(T* element) |
| 96 |
{ |
| 97 |
if (element->prev) element->prev->next = element->next; |
| 98 |
if (element->next) element->next->prev = element->prev; |
| 99 |
if (element == head) head = element->next; |
| 100 |
if (element == tail) tail = element->prev; |
| 101 |
} |
| 102 |
|
| 103 |
bool empty() const |
| 104 |
{ |
| 105 |
return head ? false : true; |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
#endif |