-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmystl_list.hpp
265 lines (211 loc) · 6.24 KB
/
mystl_list.hpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#ifndef _MYSTL_LIST_H
#define _MYSTL_LIST_H
#include "mystl_iterator_base.hpp"
#include "mystl_algobase.hpp"
namespace numb {
template <typename _Tp>
struct _List_node {
_Tp _data;
_List_node* _next;
_List_node* _prev;
_List_node() : _data(_Tp()), _next(NULL), _prev(NULL) {}
explicit _List_node(_Tp _x, _List_node* _p = NULL, _List_node* _n = NULL)
: _data(_x), _next(_n), _prev(_p) {}
};
template <typename _Tp>
struct _List_iterator {
_List_node<_Tp>* _M_node;
typedef _List_iterator<_Tp> _Self;
typedef _List_node<_Tp> _Node;
typedef ptrdiff_t difference_type;
typedef Bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Tp* pointer;
typedef _Tp& reference;
_List_iterator() : _M_node() {}
explicit _List_iterator(_List_node<_Tp>* _x) : _M_node(_x) {}
reference operator*() const { return _M_node->_data; }
pointer operator->() const { return __Addressof(_M_node->_data); }
_Self& operator++() {
_M_node = _M_node->_next;
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_M_node = _M_node->_next;
return _tmp;
}
_Self& operator--() {
_M_node = _M_node->_prev;
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_M_node = _M_node->_prev;
return _tmp;
}
bool operator==(const _Self& _x) const { return _M_node == _x._M_node; }
bool operator!=(const _Self& _x) const { return _M_node != _x._M_node; }
};
template <typename _Tp>
struct _List_const_iterator {
_List_node<_Tp>* _M_node;
typedef _List_const_iterator<_Tp> _Self;
typedef _List_node<_Tp> _Node;
typedef ptrdiff_t difference_type;
typedef Bidirectional_iterator_tag iterator_category;
typedef _Tp value_type;
typedef _Tp* pointer;
typedef _Tp& reference;
_List_const_iterator() : _M_node() {}
explicit _List_const_iterator(_List_node<_Tp>* _x) : _M_node(_x) {}
reference operator*() const { return _M_node->_data; }
pointer operator->() const { return __Addressof(_M_node->_data); }
_Self& operator++() {
_M_node = _M_node->_next;
return *this;
}
_Self operator++(int) {
_Self _tmp = *this;
_M_node = _M_node->_next;
return _tmp;
}
_Self& operator--() {
_M_node = _M_node->_prev;
return *this;
}
_Self operator--(int) {
_Self _tmp = *this;
_M_node = _M_node->_prev;
return _tmp;
}
bool operator==(const _Self& _x) const { return _M_node == _x._M_node; }
bool operator!=(const _Self& _x) const { return _M_node != _x._M_node; }
};
template <typename _Val>
inline bool operator==(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y) {
return __x._M_node == __y._M_node;
}
template <typename _Val>
inline bool operator!=(const _List_iterator<_Val>& __x,
const _List_const_iterator<_Val>& __y) {
return __x._M_node != __y._M_node;
}
template <typename _Tp>
class List {
public:
typedef _Tp value_type;
typedef _Tp* pointer;
typedef const _Tp* const_pointer;
typedef _Tp& reference;
typedef const _Tp& const_reference;
typedef _List_iterator<_Tp> Iterator;
typedef _List_const_iterator<_Tp> Const_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:
typedef _List_node<_Tp> _Node;
_Node* _head;
size_type _size;
public:
List() : _head(new _Node), _size(0) {
_head->_prev = _head;
_head->_next = _head;
_head->_data = value_type();
}
~List() { _M_clear(); }
List(const List& _rhs) : _head(new _Node), _size(0) {
_head->_prev = _head;
_head->_next = _head;
_head->_data = value_type();
for (Const_iterator _iter = _rhs.begin(); _iter != _rhs.end();
++_iter)
push_back(*_iter);
}
List& operator=(const List& rhs) {
List _copy = rhs;
swap(_copy);
return *this;
}
void swap(const List& rhs) {
using numb::Swap;
Swap(_head, rhs._head);
Swap(_size, rhs._size);
}
Iterator begin() throw() { return Iterator(_head->_next); }
Iterator end() throw() { return Iterator(_head); }
Const_iterator begin() const throw() {
return Const_iterator(_head->_next);
}
Const_iterator end() const throw() { return Const_iterator(_head); }
reference front() { return *begin(); }
const_reference front() const { return *begin(); }
reference back() {
Iterator _tmp = end();
--_tmp;
return *_tmp;
}
const_reference back() const {
Iterator _tmp = end();
--_tmp;
return *_tmp;
}
size_type size() const { return _size; }
bool empty() const { return _size == 0; }
void push_front(const value_type& _x) {
_Node* cur = new _Node(_x, _head, _head->_next);
_head->_next->_prev = cur;
_head->_next = cur;
_size++;
}
value_type pop_front() {
if (empty()) throw;
value_type _tmp = *begin();
_M_erase(begin());
return _tmp;
}
void push_back(const value_type& _x) {
_Node* cur = new _Node(_x, _head->_prev, _head);
_head->_prev->_next = cur;
_head->_prev = cur;
_size++;
}
value_type pop_back() {
if (empty()) throw;
value_type _tmp = *end();
_M_erase(--end());
return _tmp;
}
value_type erase(Iterator _it) {
value_type _tmp = *_it;
_M_erase(_it);
return _tmp;
}
value_type erase(difference_type _n) {
Iterator _it = begin();
Advance(_it, _n);
value_type _tmp = *_it;
_M_erase(_it);
return _tmp;
}
protected:
void _M_clear() {
_Node* _q = _head->_next;
_Node* _p;
while (_q != _head) {
_p = _q->_next;
delete _q;
_q = _p;
}
delete _head;
}
void _M_erase(Iterator _iter) {
_iter._M_node->_prev->_next = _iter._M_node->_next;
_iter._M_node->_next->_prev = _iter._M_node->_prev;
delete _iter._M_node;
--_size;
}
};
}
#endif