2 #include <ToolchefsSTL/Allocators/Allocator.h>
14 Node(): data(), _prev(
nullptr), _next(
nullptr) {}
16 Node(
const T& value) : data(value), _prev(
nullptr), _next(
nullptr) {}
18 template <
typename... Args>
19 Node(Args&&... args): data(std::forward<Args>(args)...), _prev(
nullptr), _next(
nullptr) {}
29 template <
class Item,
class ItemType,
bool Reverse>
37 for (
size_t i=0; i < value; ++i)
41 if (node && node->_prev)
46 if (node && node->_next)
56 for (
size_t i = 0; i < value; ++i)
60 if (node && node->_next)
65 if (node && node->_prev)
75 m_ptr = m_ptr ? m_ptr->_prev :
nullptr;
77 m_ptr = m_ptr ? m_ptr->_next :
nullptr;
84 m_ptr = m_ptr ? m_ptr->_next :
nullptr;
86 m_ptr = m_ptr ? m_ptr->_prev :
nullptr;
90 bool operator ==(
const ListIterator& rhs)
const {
return m_ptr == rhs.m_ptr; }
91 bool operator !=(
const ListIterator& rhs)
const {
return m_ptr != rhs.m_ptr; }
92 ItemType& operator *()
const {
return m_ptr->data; }
93 ItemType* operator->()
const {
return &m_ptr->data; }
98 typedef T& reference_type;
106 explicit List(
Allocator* allocator = defaultAllocator()) noexcept;
112 List& operator=(const
List& rhs) noexcept;
116 List& operator=(
List&& rhs) noexcept;
118 void clear() noexcept;
120 size_t size() const noexcept;
122 size_t length() const noexcept;
137 const T&
back() const noexcept;
140 const T& front() const noexcept;
143 void push_back(const T& element) noexcept;
145 void pop_back() noexcept;
147 template <typename... Args>
148 T& emplace_back(Args&&... args)
150 Node* value =
static_cast<Node*
>(m_allocator->malloc(
sizeof(
Node)));
151 new (value)
Node(std::forward<Args>(args)...);
158 m_tail->_next = value;
159 value->_prev = m_tail;
164 template <
typename... Args>
165 T& emplace_front(Args&&... args)
167 Node* value =
static_cast<Node*
>(m_allocator->malloc(
sizeof(Node)));
168 new (value) Node(std::forward<Args>(args)...);
176 value->_next = m_head;
184 void pop_front() noexcept;
186 inline
bool empty() noexcept {
return m_head ==
nullptr; }
188 iterator insert(const_iterator pos,
const T& value) noexcept;
190 iterator insert(iterator pos,
const T& value) noexcept;
192 template <
typename... Args>
193 iterator emplace(const_iterator pos, Args&&... args) noexcept
196 if (pos == cend() || !pos.m_ptr)
return end();
197 Node* node =
const_cast<Node*
>(pos.m_ptr);
199 Node* new_node =
static_cast<Node*
>(m_allocator->malloc(
sizeof(Node)));
200 new (new_node) Node(std::forward<Args>(args)...);
201 Node* prevNode = node->_prev;
202 node->_prev = new_node;
203 new_node->_next = node;
206 prevNode->_next = new_node;
207 new_node->_prev = prevNode;
209 return iterator(new_node);
212 template <
typename... Args>
213 iterator emplace(iterator pos, Args&&... args) noexcept
216 if (pos == end() || !pos.m_ptr)
return end();
217 Node* node = pos.m_ptr;
219 Node* new_node =
static_cast<Node*
>(m_allocator->malloc(
sizeof(Node)));
220 new (new_node) Node(std::forward<Args>(args)...);
221 Node* prevNode = node->_prev;
222 node->_prev = new_node;
223 new_node->_next = node;
226 prevNode->_next = new_node;
227 new_node->_prev = prevNode;
229 return iterator(new_node);
232 iterator erase(const_iterator pos) noexcept;
234 iterator erase(iterator pos) noexcept;
236 iterator find(
const T& element) noexcept;
238 const_iterator find(
const T& element)
const noexcept;
243 Allocator* m_allocator;
247 #include "List.impl.h"