1 | // SPDX-License-Identifier: GPL-3.0-or-later |
2 | #pragma once |
3 | |
4 | #include <mos/allocator.hpp> |
5 | |
6 | namespace 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 | { |
106 | T value; |
107 | node *next = nullptr; |
108 | |
109 | node(const T &value) : value(value) {}; |
110 | }; |
111 | |
112 | public: |
113 | class iterator |
114 | { |
115 | public: |
116 | iterator(node *current) : current(current) |
117 | { |
118 | } |
119 | |
120 | T &operator*() |
121 | { |
122 | return current->value; |
123 | } |
124 | |
125 | iterator &operator++() |
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: |
142 | node *current; |
143 | }; |
144 | |
145 | iterator begin() |
146 | { |
147 | return iterator(head); |
148 | } |
149 | |
150 | iterator end() |
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 | |
175 | void erase(iterator it) |
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 |
223 | |