forked from sunziping2016/MemoryLeakDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_list.h
200 lines (184 loc) · 3.99 KB
/
my_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
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
#ifndef MY_LIST_H_INCLUDE
#define MY_LIST_H_INCLUDE
#include "memory_leak_detection.h"
#include <utility>
#include <iterator>
template<typename ValueType>
class MyListNode
{
public:
MyListNode<ValueType> *prev, *next;
ValueType data;
// No copy
MyListNode(const MyListNode &orig) = delete;
MyListNode & operator = (const MyListNode &orig) = delete;
MyListNode(): prev(nullptr), next(nullptr), data() {}
MyListNode(MyListNode &&orig): prev(orig.prev), next(orig.next), data(std::move(orig.data)) {}
MyListNode(const ValueType &orig): prev(nullptr), next(nullptr), data(orig) {}
MyListNode(ValueType &&orig): prev(nullptr), next(nullptr), data(std::move(orig)) {}
~MyListNode() {}
};
template<typename ValueType>
class MyList;
template<typename ValueType>
class MyListIterator : std::iterator<std::bidirectional_iterator_tag, ValueType>
{
friend class MyList<ValueType>;
public:
typedef ValueType value_type;
typedef MyListNode<value_type> node_type;
typedef MyListIterator<value_type> iterator;
typedef const MyListIterator<value_type> const_iterator;
MyListIterator(node_type *node): node(node) {}
MyListIterator(const iterator &other): node(other.node) {}
MyListIterator &operator = (const iterator &other)
{
node = other.node;
return *this;
}
~MyListIterator() {}
void swap(iterator &other)
{
node_type *temp = other.node;
other.node = node;
node = temp;
}
ValueType &operator * ()
{
return node->data;
}
ValueType &operator -> ()
{
return &node->data;
}
const ValueType &operator * () const
{
return node->data;
}
const ValueType &operator -> () const
{
return &node->data;
}
bool operator == (const iterator &other) const
{
return node == other.node;
}
bool operator != (const iterator &other) const
{
return !(*this == other);
}
iterator &operator ++ ()
{
node = node->next;
return *this;
}
const iterator &operator ++ (int)
{
const iterator temp(*this);
node = node->next;
return temp;
}
iterator &operator -- ()
{
node = node->prev;
return *this;
}
const iterator &operator -- (int)
{
const iterator temp(*this);
node = node->prev;
return temp;
}
private:
MyListNode<ValueType> *node;
};
#include <iostream>
template<typename ValueType>
class MyList
{
public:
typedef ValueType value_type;
typedef MyListNode<value_type> node_type;
typedef MyListIterator<value_type> iterator;
typedef const MyListIterator<value_type> const_iterator;
MyList(const MyList &orig) = delete;
MyList & operator = (const MyList &orig) = delete;
MyList()
{
root = new node_type();
root->next = root->prev = root;
}
~MyList()
{
clear();
delete root;
}
void clear()
{
while(root->next != root)
remove(root->next);
}
void remove(iterator iter)
{
remove(iter.node);
}
void push_back(const value_type &value)
{
insert(root, new node_type(value));
}
void insert(iterator iter, const value_type &value)
{
insert(iter->node, new node_type(value));
}
iterator begin()
{
return iterator(root->next);
}
iterator end()
{
return iterator(root);
}
const_iterator begin() const
{
return iterator(root->next);
}
const_iterator end() const
{
return iterator(root);
}
template<typename Func = std::less<value_type> >
void sort(Func func = Func())
{
node_type *temp;
for (node_type *outer = root; outer->prev != root->next; outer = outer->prev)
for (node_type *inner = root->next; inner->next != outer; inner = inner->next) {
if (!func(inner->data, inner->next->data)) {
temp = inner->next;
inner->prev->next = temp;
temp->next->prev = inner;
inner->next = temp->next;
temp->prev = inner->prev;
inner->prev = temp;
temp->next = inner;
inner = temp;
}
}
}
protected:
void remove(node_type *node)
{
node->prev->next = node->next;
node->next->prev = node->prev;
delete node;
}
void insert(node_type *node, node_type *newnode)
{
node->prev->next = newnode;
newnode->prev = node->prev;
node->prev = newnode;
newnode->next = node;
}
private:
node_type *root;
};
#endif