-
Notifications
You must be signed in to change notification settings - Fork 4
/
list.h
113 lines (86 loc) · 1.98 KB
/
list.h
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
#ifndef LITE__LIST_H
#define LITE__LIST_H
#include <lace/do_not_copy.h>
#include "link.h"
#include <cassert>
#include <cstddef>
#include <algorithm>
namespace lite {
template <class X>
struct list_link {
typedef list_link type;
template <class T, typename list_link<T>::type T::*L>
friend class list;
bool bound() const { return p.p; }
private:
link<X> n, p;
};
template <class T, typename list_link<T>::type T::*L>
class list : public lace::do_not_copy {
public:
list() : head(NULL) { }
~list() { assert(empty()); }
bool empty() const { return !head; }
list & enlist(T* t, T* n = NULL) {
assert(!(t->*L).bound());
assert(!n || (n->*L).bound());
(t->*L).n.p = (t->*L).p.p = t;
if (empty()) {
assert(!n);
head = t;
} else {
if (n == head)
head = t;
else if (!n)
n = head;
T* p = (n->*L).p.p;
std::swap((t->*L).n.p, (p->*L).n.p);
std::swap((t->*L).p.p, (n->*L).p.p);
}
assert((t->*L).bound());
assert(!empty());
return *this;
}
T* delist(T* t) {
assert((t->*L).bound());
T* n = (t->*L).n.p;
T* p = (t->*L).p.p;
if (t == n) {
assert(head == t);
assert(p == n);
head = NULL;
} else {
if (head == t)
head = n;
std::swap((t->*L).n.p, (p->*L).n.p);
std::swap((t->*L).p.p, (n->*L).p.p);
}
(t->*L).n.p = (t->*L).p.p = NULL;
assert(!(t->*L).bound());
return t;
}
T* first() const { return head; }
T* last() const { return head ? (head->*L).p.p : NULL; }
T* next(const T* t) const {
assert((t->*L).bound());
return (t->*L).n.guarded(head);
}
T* previous(const T* t) const {
assert((t->*L).bound());
return (t->*L).p.qualified(t != head);
}
T* prograde() {
assert(!empty());
head = (head->*L).n.p;
return head;
}
T* retrograde() {
assert(!empty());
head = (head->*L).p.p;
return head;
}
private:
T * head;
};
} // namespace lite
#endif//LITE__LIST