4
4
* Copyright (c) 2012, Niklas Hauser
5
5
* Copyright (c) 2013, Sascha Schade
6
6
* Copyright (c) 2014, Daniel Krebs
7
+ * Copyright (c) 2023, Christopher Durand
7
8
*
8
9
* This file is part of the modm project.
9
10
*
16
17
#ifndef MODM_DOUBLY_LINKED_LIST_HPP
17
18
#define MODM_DOUBLY_LINKED_LIST_HPP
18
19
19
- #include < stdint.h>
20
- #include < modm/utils/allocator.hpp>
21
- #include < iterator>
20
+ #include < list>
22
21
23
22
namespace modm
24
23
{
@@ -30,177 +29,120 @@ namespace modm
30
29
* \author Fabian Greif
31
30
* \ingroup modm_container
32
31
*/
33
- template <typename T, typename Allocator = allocator::Dynamic <T> >
32
+ template <typename T, typename Allocator = std::allocator <T>>
34
33
class DoublyLinkedList
35
34
{
36
35
public:
37
- DoublyLinkedList (const Allocator& allocator = Allocator());
36
+ using const_iterator = std::list<T>::const_iterator;
37
+ using iterator = std::list<T>::iterator;
38
38
39
- ~DoublyLinkedList ();
39
+ DoublyLinkedList (const Allocator& allocator = Allocator())
40
+ : data_{std::move (allocator)}
41
+ {}
40
42
41
43
// / check if there are any nodes in the list
42
- inline bool
43
- isEmpty () const ;
44
+ bool
45
+ isEmpty () const
46
+ {
47
+ return data_.empty ();
48
+ }
44
49
45
- /* *
46
- * \brief Get number of items in the list
47
- *
48
- * Very slow for a long list as it needs to iterate through all
49
- * items in the list.
50
- */
50
+ // / Get number of items in the list
51
51
std::size_t
52
- getSize () const ;
52
+ getSize () const
53
+ {
54
+ return data_.size ();
55
+ }
53
56
54
57
// / Insert in front
55
58
bool
56
- prepend (const T& value);
59
+ prepend (const T& value)
60
+ {
61
+ data_.push_front (value);
62
+ return true ;
63
+ }
57
64
58
65
// / Insert at the end of the list
59
66
void
60
- append (const T& value);
67
+ append (const T& value)
68
+ {
69
+ data_.push_back (value);
70
+ }
61
71
62
72
// / Remove the first entry
63
73
void
64
- removeFront ();
74
+ removeFront ()
75
+ {
76
+ data_.pop_front ();
77
+ }
65
78
66
79
void
67
- removeBack ();
80
+ removeBack ()
81
+ {
82
+ data_.pop_back ();
83
+ }
68
84
69
85
/* *
70
86
* \return the first node in the list
71
87
*/
72
- inline const T&
73
- getFront () const ;
88
+ const T&
89
+ getFront () const
90
+ {
91
+ return data_.front ();
92
+ }
74
93
75
94
/* *
76
95
* \return the last node in the list
77
96
*/
78
- inline const T&
79
- getBack () const ;
80
-
81
- protected:
82
- struct Node
97
+ const T&
98
+ getBack () const
83
99
{
84
- T value;
85
-
86
- Node *previous;
87
- Node *next;
88
- };
89
-
90
- // The stored instance is not actually of type Allocator. Instead we
91
- // rebind the type to Allocator<Node<T>>. Node<T> is not the same
92
- // size as T (it's one pointer larger), and specializations on T may go
93
- // unused because Node<T> is being bound instead.
94
- typedef typename Allocator::template rebind< Node >::other NodeAllocator;
95
-
96
- NodeAllocator nodeAllocator;
97
-
98
- Node *front;
99
- Node *back;
100
+ return data_.back ();
101
+ }
100
102
101
103
public:
102
- #pragma GCC diagnostic push
103
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
104
104
/* *
105
- * \brief Forward iterator
106
- *
107
- * \todo decrement operator doesn't work correctly
108
- */
109
- class iterator : public std ::iterator<std::forward_iterator_tag, T>
110
- {
111
- friend class DoublyLinkedList ;
112
- friend class const_iterator ;
113
-
114
- public:
115
- // / Default constructor
116
- iterator ();
117
- iterator (const iterator& other);
118
-
119
- iterator& operator = (const iterator& other);
120
- iterator& operator ++ ();
121
- iterator& operator -- ();
122
- bool operator == (const iterator& other) const ;
123
- bool operator != (const iterator& other) const ;
124
- T& operator * ();
125
- T* operator -> ();
126
-
127
- private:
128
- iterator (Node* node);
129
-
130
- Node* node;
131
- };
132
-
133
- /* *
134
- * \brief forward const iterator
135
- *
136
- * \todo decrement operator doesn't work correctly
137
- */
138
- class const_iterator : public std ::iterator<std::forward_iterator_tag, T>
139
- {
140
- friend class DoublyLinkedList ;
141
-
142
- public:
143
- // / Default constructor
144
- const_iterator ();
145
-
146
- /* *
147
- * \brief Copy constructor
148
- *
149
- * Used to convert a normal iterator to a const iterator.
150
- * The other way is not possible.
151
- */
152
- const_iterator (const iterator& other);
153
-
154
- /* *
155
- * \brief Copy constructor
156
- */
157
- const_iterator (const const_iterator& other);
158
-
159
- const_iterator& operator = (const const_iterator& other);
160
- const_iterator& operator ++ ();
161
- const_iterator& operator -- ();
162
- bool operator == (const const_iterator& other) const ;
163
- bool operator != (const const_iterator& other) const ;
164
- const T& operator * () const ;
165
- const T* operator -> () const ;
166
-
167
- private:
168
- const_iterator (const Node* node);
169
-
170
- const Node* node;
171
- };
172
- #pragma GCC diagnostic pop
173
-
174
- /* *
175
- * Returns a read/write iterator that points to the first element in the
105
+ * Returns a read/write iterator that points to the first element in the
176
106
* list. Iteration is done in ordinary element order.
177
107
*/
178
108
iterator
179
- begin ();
109
+ begin ()
110
+ {
111
+ return data_.begin ();
112
+ }
180
113
181
114
/* *
182
115
* Returns a read-only (constant) iterator that points to the
183
116
* first element in the list. Iteration is done in ordinary
184
117
* element order.
185
118
*/
186
119
const_iterator
187
- begin () const ;
120
+ begin () const
121
+ {
122
+ return data_.begin ();
123
+ }
188
124
189
125
/* *
190
126
* Returns a read/write iterator that points one past the last
191
127
* element in the list. Iteration is done in ordinary element
192
128
* order.
193
129
*/
194
130
iterator
195
- end ();
131
+ end ()
132
+ {
133
+ return data_.end ();
134
+ }
196
135
197
136
/* *
198
137
* Returns a read-only (constant) iterator that points one past
199
138
* the last element in the list. Iteration is done in ordinary
200
139
* element order.
201
140
*/
202
141
const_iterator
203
- end () const ;
142
+ end () const
143
+ {
144
+ return data_.end ();
145
+ }
204
146
205
147
/* *
206
148
* Deletes element pointed to by iterator and returns an iterator
@@ -209,20 +151,11 @@ namespace modm
209
151
* Warning: you must not use the iterator after calling erase()
210
152
*/
211
153
iterator
212
- erase (iterator position);
213
-
214
- private:
215
- friend class const_iterator ;
216
- friend class iterator ;
217
-
218
- DoublyLinkedList (const DoublyLinkedList& other);
219
-
220
- DoublyLinkedList&
221
- operator = (const DoublyLinkedList& other);
154
+ erase (iterator position)
155
+ {
156
+ return data_.erase (position);
157
+ }
222
158
};
223
159
}
224
160
225
- #include " doubly_linked_list_impl.hpp"
226
- #include " doubly_linked_list_iterator_impl.hpp"
227
-
228
161
#endif // MODM_DOUBLY_LINKED_LIST_HPP
0 commit comments