MOS Source Code
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2#pragma once
3
4#include <mos/allocator.hpp>
5
6namespace mos
7{
8 template<typename T>
9 class list
10 {
11 public:
12 list() = default;
13 list(const list &other) = default;
14 list(list &&other) noexcept = default;
15 list &operator=(const list &other) = default;
16 list &operator=(list &&other) noexcept = default;
17 ~list() = default;
18
19 void push_back(const T &value)
20 {
21 if (head == nullptr)
22 {
23 head = mos::create<node>(value);
24 tail = head;
25 }
26 else
27 {
28 tail->next = mos::create<node>(value);
29 tail = tail->next;
30 }
31 }
32
33 void push_front(const T &value)
34 {
35 if (head == nullptr)
36 {
37 head = mos::create<node>(value);
38 tail = head;
39 }
40 else
41 {
42 node *new_head = mos::create<node>(value);
43 new_head->next = head;
44 head = new_head;
45 }
46 }
47
48 void pop_back()
49 {
50 if (head == nullptr)
51 return;
52
53 if (head == tail)
54 {
55 delete head;
56 head = nullptr;
57 tail = nullptr;
58 return;
59 }
60
61 node *current = head;
62 while (current->next != tail)
63 current = current->next;
64
65 delete tail;
66 tail = current;
67 tail->next = nullptr;
68 }
69
70 void pop_front()
71 {
72 if (head == nullptr)
73 return;
74
75 if (head == tail)
76 {
77 delete head;
78 head = nullptr;
79 tail = nullptr;
80 return;
81 }
82
83 node *new_head = head->next;
84 delete head;
85 head = new_head;
86 }
87
88 T &front()
89 {
90 return head->value;
91 }
92
93 T &back()
94 {
95 return tail->value;
96 }
97
98 bool empty() const
99 {
100 return head == nullptr;
101 }
102
103 private:
104 struct node : mos::NamedType<"List.Node">
105 {
107 node *next = nullptr;
108
109 node(const T &value) : value(value) {};
110 };
111
112 public:
114 {
115 public:
119
121 {
122 return current->value;
123 }
124
126 {
127 current = current->next;
128 return *this;
129 }
130
131 bool operator==(const iterator &other) const
132 {
133 return current == other.current;
134 }
135
136 bool operator!=(const iterator &other) const
137 {
138 return current != other.current;
139 }
140
141 private:
143 };
144
146 {
147 return iterator(head);
148 }
149
151 {
152 return iterator(nullptr);
153 }
154
155 const iterator begin() const
156 {
157 return iterator(head);
158 }
159
160 const iterator end() const
161 {
162 return iterator(nullptr);
163 }
164
165 const iterator cbegin() const
166 {
167 return iterator(head);
168 }
169
170 const iterator cend() const
171 {
172 return iterator(nullptr);
173 }
174
176 {
177 if (it.current == head)
178 {
179 pop_front();
180 return;
181 }
182
183 node *current = head;
184 while (current->next != it.current)
185 current = current->next;
186
187 current->next = it.current->next;
188 delete it.current;
189 }
190
191 void insert(iterator it, const T &value)
192 {
193 if (it.current == head)
194 {
195 push_front(value);
196 return;
197 }
198
199 node *current = head;
200 while (current->next != it.current)
201 current = current->next;
202
203 node *new_node = mos::create<node>(value);
204 new_node->next = current->next;
205 current->next = new_node;
206 }
207
208 void clear()
209 {
210 while (head != nullptr)
211 {
212 node *next = head->next;
213 delete head;
214 head = next;
215 }
216 }
217
218 private:
219 node *head = nullptr;
220 node *tail = nullptr;
221 };
222} // namespace mos
bool operator!=(const iterator &other) const
Definition list.hpp:136
bool operator==(const iterator &other) const
Definition list.hpp:131
iterator(node *current)
Definition list.hpp:116
iterator & operator++()
Definition list.hpp:125
void pop_back()
Definition list.hpp:48
void erase(iterator it)
Definition list.hpp:175
T & back()
Definition list.hpp:93
node * head
Definition list.hpp:219
const iterator end() const
Definition list.hpp:160
~list()=default
const iterator begin() const
Definition list.hpp:155
list(const list &other)=default
list()=default
void pop_front()
Definition list.hpp:70
list & operator=(list &&other) noexcept=default
void push_front(const T &value)
Definition list.hpp:33
bool empty() const
Definition list.hpp:98
void push_back(const T &value)
Definition list.hpp:19
void insert(iterator it, const T &value)
Definition list.hpp:191
const iterator cbegin() const
Definition list.hpp:165
list & operator=(const list &other)=default
T & front()
Definition list.hpp:88
node * tail
Definition list.hpp:220
list(list &&other) noexcept=default
void clear()
Definition list.hpp:208
iterator end()
Definition list.hpp:150
const iterator cend() const
Definition list.hpp:170
iterator begin()
Definition list.hpp:145
#define current
T * create(Args &&...args)
Definition allocator.hpp:10
node(const T &value)
Definition list.hpp:109
node * next
Definition list.hpp:107