@@ -40,7 +40,6 @@ 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;
@@ -87,6 +86,7 @@ class LinkedList {
87
86
88
87
LinkedList (OnRemove onRemove) : _root(nullptr ), _last(nullptr ), _onRemove(onRemove) {}
89
88
~LinkedList () { free (); }
89
+
90
90
void add (T t){
91
91
auto it = new ItemType (std::move (t));
92
92
if (!_root){
@@ -97,91 +97,84 @@ class LinkedList {
97
97
}
98
98
_last = it;
99
99
}
100
+
100
101
T& front () const {
101
102
return _root->value ();
102
103
}
103
104
104
105
bool isEmpty () const {
105
106
return _root == nullptr ;
106
107
}
108
+
107
109
size_t length () const {
108
110
size_t i = 0 ;
109
- auto it = _root;
110
- while (it){
111
- i++;
112
- it = it->next ;
113
- }
111
+ for (auto it = _root; it != nullptr ; it = it->next ) { ++i; };
114
112
return i;
115
113
}
116
- size_t count_if (Predicate predicate) const {
114
+
115
+ template <typename Predicate>
116
+ size_t count_if (const Predicate& predicate) const {
117
117
size_t i = 0 ;
118
- auto it = _root;
119
- while (it){
120
- if (!predicate){
121
- i++;
122
- }
123
- else if (predicate (it->value ())) {
118
+ for (auto it = _root; it != nullptr ; it = it->next ) {
119
+ if (predicate (it->value ())) {
124
120
i++;
125
121
}
126
- it = it->next ;
127
122
}
128
123
return i;
129
124
}
125
+
130
126
const T* nth (size_t N) const {
131
127
size_t i = 0 ;
132
- auto it = _root;
133
- while (it){
134
- if (i++ == N)
135
- return &(it->value ());
136
- it = it->next ;
137
- }
128
+ for (auto it = _root; it != nullptr ; it = it->next ) {
129
+ if (i++ == N) return &(it->value ());
130
+ };
138
131
return nullptr ;
139
132
}
133
+
140
134
bool remove (const T& t){
141
- auto it = _root;
142
- auto pit = decltype (it) { nullptr };
143
- while (it){
135
+ auto pit = (ItemType*) nullptr ;
136
+ for (auto it = _root; it != nullptr ; pit = it, it = it->next ) {
144
137
if (it->value () == t){
145
138
_remove (pit, it);
146
139
return true ;
147
140
}
148
- pit = it;
149
- it = it->next ;
150
141
}
151
142
return false ;
152
143
}
153
- bool remove_first (Predicate predicate){
154
- auto it = _root;
155
- auto pit = decltype (it) { nullptr };
156
- while (it){
144
+
145
+ template <typename Predicate>
146
+ bool remove_first (const Predicate& predicate){
147
+ auto pit = (ItemType*) nullptr ;
148
+ for (auto it = _root; it != nullptr ; pit = it, it = it->next ) {
157
149
if (predicate (it->value ())){
158
150
_remove (pit, it);
159
151
return true ;
160
152
}
161
- pit = it;
162
- it = it->next ;
163
153
}
164
154
return false ;
165
155
}
166
- bool remove (const ConstIterator& t, const ConstIterator& where = ConstIterator(nullptr )) {
156
+
157
+ bool remove (const ConstIterator& t, const ConstIterator& where) {
167
158
if (where._node ) {
168
159
if ((where._node ->next ) != t._node ) return false ;
169
160
_remove (where._node , t._node );
170
161
return true ;
162
+ } else {
163
+ return remove (t);
171
164
}
165
+ }
172
166
173
- auto it = _root;
174
- auto pit = decltype (it) { nullptr } ;
175
- while (it) {
167
+ bool remove ( const ConstIterator& t) {
168
+ auto pit = (ItemType*) nullptr ;
169
+ for ( auto it = _root; it != nullptr ; pit = it, it = it-> next ) {
176
170
if (it == t._node ){
177
171
_remove (pit, it);
178
172
return true ;
179
173
}
180
- pit = it;
181
- it = it->next ;
182
174
}
183
175
return false ;
184
- }
176
+ }
177
+
185
178
void free (){
186
179
while (_root != nullptr ){
187
180
auto it = _root;
0 commit comments