Skip to content

Commit 56e4b2a

Browse files
committed
[container] Implement DoublyLinkedList as std::list
1 parent 5681d1d commit 56e4b2a

File tree

3 files changed

+65
-534
lines changed

3 files changed

+65
-534
lines changed

src/modm/container/doubly_linked_list.hpp

Lines changed: 65 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Copyright (c) 2012, Niklas Hauser
55
* Copyright (c) 2013, Sascha Schade
66
* Copyright (c) 2014, Daniel Krebs
7+
* Copyright (c) 2023, Christopher Durand
78
*
89
* This file is part of the modm project.
910
*
@@ -16,9 +17,7 @@
1617
#ifndef MODM_DOUBLY_LINKED_LIST_HPP
1718
#define MODM_DOUBLY_LINKED_LIST_HPP
1819

19-
#include <stdint.h>
20-
#include <modm/utils/allocator.hpp>
21-
#include <iterator>
20+
#include <list>
2221

2322
namespace modm
2423
{
@@ -30,177 +29,120 @@ namespace modm
3029
* \author Fabian Greif
3130
* \ingroup modm_container
3231
*/
33-
template <typename T, typename Allocator = allocator::Dynamic<T> >
32+
template <typename T, typename Allocator = std::allocator<T>>
3433
class DoublyLinkedList
3534
{
3635
public:
37-
DoublyLinkedList(const Allocator& allocator = Allocator());
36+
using const_iterator = std::list<T>::const_iterator;
37+
using iterator = std::list<T>::iterator;
3838

39-
~DoublyLinkedList();
39+
DoublyLinkedList(const Allocator& allocator = Allocator())
40+
: data_{std::move(allocator)}
41+
{}
4042

4143
/// 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+
}
4449

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
5151
std::size_t
52-
getSize() const;
52+
getSize() const
53+
{
54+
return data_.size();
55+
}
5356

5457
/// Insert in front
5558
bool
56-
prepend(const T& value);
59+
prepend(const T& value)
60+
{
61+
data_.push_front(value);
62+
return true;
63+
}
5764

5865
/// Insert at the end of the list
5966
void
60-
append(const T& value);
67+
append(const T& value)
68+
{
69+
data_.push_back(value);
70+
}
6171

6272
/// Remove the first entry
6373
void
64-
removeFront();
74+
removeFront()
75+
{
76+
data_.pop_front();
77+
}
6578

6679
void
67-
removeBack();
80+
removeBack()
81+
{
82+
data_.pop_back();
83+
}
6884

6985
/**
7086
* \return the first node in the list
7187
*/
72-
inline const T&
73-
getFront() const;
88+
const T&
89+
getFront() const
90+
{
91+
return data_.front();
92+
}
7493

7594
/**
7695
* \return the last node in the list
7796
*/
78-
inline const T&
79-
getBack() const;
80-
81-
protected:
82-
struct Node
97+
const T&
98+
getBack() const
8399
{
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+
}
100102

101103
public:
102-
#pragma GCC diagnostic push
103-
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
104104
/**
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
176106
* list. Iteration is done in ordinary element order.
177107
*/
178108
iterator
179-
begin();
109+
begin()
110+
{
111+
return data_.begin();
112+
}
180113

181114
/**
182115
* Returns a read-only (constant) iterator that points to the
183116
* first element in the list. Iteration is done in ordinary
184117
* element order.
185118
*/
186119
const_iterator
187-
begin() const;
120+
begin() const
121+
{
122+
return data_.begin();
123+
}
188124

189125
/**
190126
* Returns a read/write iterator that points one past the last
191127
* element in the list. Iteration is done in ordinary element
192128
* order.
193129
*/
194130
iterator
195-
end();
131+
end()
132+
{
133+
return data_.end();
134+
}
196135

197136
/**
198137
* Returns a read-only (constant) iterator that points one past
199138
* the last element in the list. Iteration is done in ordinary
200139
* element order.
201140
*/
202141
const_iterator
203-
end() const;
142+
end() const
143+
{
144+
return data_.end();
145+
}
204146

205147
/**
206148
* Deletes element pointed to by iterator and returns an iterator
@@ -209,20 +151,11 @@ namespace modm
209151
* Warning: you must not use the iterator after calling erase()
210152
*/
211153
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+
}
222158
};
223159
}
224160

225-
#include "doubly_linked_list_impl.hpp"
226-
#include "doubly_linked_list_iterator_impl.hpp"
227-
228161
#endif // MODM_DOUBLY_LINKED_LIST_HPP

0 commit comments

Comments
 (0)