@@ -40,26 +40,18 @@ class LinkedList {
40
40
public:
41
41
typedef Item<T> ItemType;
42
42
typedef std::function<void (const T&)> OnRemove;
43
- typedef std::function<bool (const T&)> Predicate;
44
43
private:
45
44
ItemType* _root;
46
45
ItemType* _last;
47
46
OnRemove _onRemove;
48
47
49
48
class Iterator {
50
49
ItemType* _node;
51
- ItemType* _nextNode = nullptr ;
52
50
friend class LinkedList ;
53
51
public:
54
- Iterator (ItemType* current = nullptr ) : _node(current) {
55
- _nextNode = _node != nullptr ? _node->next : nullptr ;
56
- }
57
- Iterator (const Iterator& i) : _node(i._node) {
58
- _nextNode = _node != nullptr ? _node->next : nullptr ;
59
- }
52
+ Iterator (ItemType* current = nullptr ) : _node(current) {};
60
53
Iterator& operator ++() {
61
- _node = _nextNode;
62
- _nextNode = _node != nullptr ? _node->next : nullptr ;
54
+ if (_node) _node = _node->next ;
63
55
return *this ;
64
56
}
65
57
bool operator != (const Iterator& i) const { return _node != i._node ; }
@@ -68,23 +60,16 @@ class LinkedList {
68
60
};
69
61
70
62
void _remove (ItemType* pit, ItemType* it) {
71
- if (pit == nullptr ){ // item is root
72
- _root = _root->next ;
73
- if (_root == nullptr ) {
74
- _last = nullptr ;
75
- }
76
- } else {
77
- pit->next = it->next ;
78
- if (it == _last) {
79
- _last = pit;
80
- }
81
- }
82
-
83
- if (_onRemove) {
84
- _onRemove (it->value ());
85
- }
86
-
87
- delete it;
63
+ auto * next = pit ? &pit->next : &_root;
64
+ *next = it->next ;
65
+ if (_last == it) {
66
+ _last = pit;
67
+ }
68
+
69
+ if (_onRemove) {
70
+ _onRemove (it->value ());
71
+ }
72
+ delete it;
88
73
}
89
74
90
75
public:
@@ -94,6 +79,7 @@ class LinkedList {
94
79
95
80
LinkedList (OnRemove onRemove) : _root(nullptr ), _last(nullptr ), _onRemove(onRemove) {}
96
81
~LinkedList () { free (); }
82
+
97
83
void add (T t){
98
84
auto it = new ItemType (std::move (t));
99
85
if (!_root){
@@ -104,102 +90,88 @@ class LinkedList {
104
90
}
105
91
_last = it;
106
92
}
93
+
107
94
T& front () const {
108
95
return _root->value ();
109
96
}
110
97
111
98
bool isEmpty () const {
112
99
return _root == nullptr ;
113
100
}
101
+
114
102
size_t length () const {
115
103
size_t i = 0 ;
116
- auto it = _root;
117
- while (it){
118
- i++;
119
- it = it->next ;
120
- }
104
+ for (auto it = _root; it != nullptr ; it = it->next ) { ++i; };
121
105
return i;
122
106
}
123
- size_t count_if (Predicate predicate) const {
107
+
108
+ template <typename Predicate>
109
+ size_t count_if (const Predicate& predicate) const {
124
110
size_t i = 0 ;
125
- auto it = _root;
126
- while (it){
127
- if (!predicate){
111
+ for (auto it = _root; it != nullptr ; it = it->next ) {
112
+ if (predicate (it->value ())) {
128
113
i++;
129
114
}
130
- else if (predicate (it->value ())) {
131
- i++;
132
- }
133
- it = it->next ;
134
115
}
135
116
return i;
136
117
}
118
+
137
119
const T* nth (size_t N) const {
138
120
size_t i = 0 ;
139
- auto it = _root;
140
- while (it){
141
- if (i++ == N)
142
- return &(it->value ());
143
- it = it->next ;
144
- }
121
+ for (auto it = _root; it != nullptr ; it = it->next ) {
122
+ if (i++ == N) return &(it->value ());
123
+ };
145
124
return nullptr ;
146
125
}
126
+
147
127
bool remove (const T& t){
148
- auto it = _root;
149
- auto pit = decltype (it) { nullptr };
150
- while (it){
128
+ auto pit = (ItemType*) nullptr ;
129
+ for (auto it = _root; it != nullptr ; pit = it, it = it->next ) {
151
130
if (it->value () == t){
152
131
_remove (pit, it);
153
132
return true ;
154
133
}
155
- pit = it;
156
- it = it->next ;
157
134
}
158
135
return false ;
159
136
}
160
- bool remove_first (Predicate predicate){
161
- auto it = _root;
162
- auto pit = decltype (it) { nullptr };
163
- while (it){
137
+
138
+ template <typename Predicate>
139
+ bool remove_first (const Predicate& predicate){
140
+ auto pit = (ItemType*) nullptr ;
141
+ for (auto it = _root; it != nullptr ; pit = it, it = it->next ) {
164
142
if (predicate (it->value ())){
165
143
_remove (pit, it);
166
144
return true ;
167
145
}
168
- pit = it;
169
- it = it->next ;
170
146
}
171
147
return false ;
172
148
}
173
- bool remove (const ConstIterator& t, const ConstIterator& where = ConstIterator(nullptr )) {
149
+
150
+ bool remove (const ConstIterator& t, const ConstIterator& where) {
174
151
if (where._node ) {
175
- if ((where._nextNode ) != t._node ) return false ;
152
+ if ((where._node -> next ) != t._node ) return false ;
176
153
_remove (where._node , t._node );
177
154
return true ;
155
+ } else {
156
+ return remove (t);
178
157
}
158
+ }
179
159
180
- auto it = _root;
181
- auto pit = decltype (it) { nullptr } ;
182
- while (it) {
160
+ bool remove ( const ConstIterator& t) {
161
+ auto pit = (ItemType*) nullptr ;
162
+ for ( auto it = _root; it != nullptr ; pit = it, it = it-> next ) {
183
163
if (it == t._node ){
184
164
_remove (pit, it);
185
165
return true ;
186
166
}
187
- pit = it;
188
- it = it->next ;
189
167
}
190
168
return false ;
191
- }
169
+ }
170
+
192
171
void free (){
193
172
while (_root != nullptr ){
194
- auto it = _root;
195
- _root = _root->next ;
196
- if (_onRemove) {
197
- _onRemove (it->value ());
198
- }
199
- delete it;
173
+ _remove (nullptr , _root);
200
174
}
201
- _root = nullptr ;
202
- _last = nullptr ;
203
175
}
204
176
};
205
177
0 commit comments