EHS
Loading...
Searching...
No Matches
LinkedList.h
Go to the documentation of this file.
1#pragma once
2
3#include "EHS.h"
4#include "Log.h"
5#include "Link.h"
6
7namespace ehs
8{
9 template<typename T, typename N = UInt_64>
11 {
12 private:
13 Link<T>* start;
14 Link<T>* end;
15 N size;
16
17 public:
19 {
20 delete start;
21 }
22
24 : size(0), start(nullptr), end(nullptr)
25 {
26 }
27
29 : start(nullptr), end(nullptr), size(list.size)
30 {
31 const Link<T>* rLast = list.start;
32 Link<T>* last = new Link<T>(rLast->value);
33 start = last;
34
35 while (rLast->child)
36 {
37 last->child = new Link<T>(rLast->child->value);
38 last = last->child;
39 rLast = rLast->child;
40 }
41
42 end = last;
43 }
44
45 LinkedList(LinkedList&& list) noexcept
46 : start(list.start), end(list.end), size(list.size)
47 {
48 list.start = nullptr;
49 list.end = nullptr;
50 list.size = {};
51 }
52
54 {
55 if (this == &list)
56 return *this;
57
58 const Link<T>* rLast = list.start;
59 Link<T>* last = new Link<T>(rLast->value);
60 start = last;
61
62 while (rLast->child)
63 {
64 last->child = new Link<T>(rLast->child->value);
65 last = last->child;
66 rLast = rLast->child;
67 }
68
69 end = last;
70 size = list.size;
71
72 return *this;
73 }
74
76 {
77 if (this == &list)
78 return *this;
79
80 start = list.start;
81 end = list.end;
82 size = list.size;
83
84 list.start = nullptr;
85 list.end = nullptr;
86 list.size = {};
87
88 return *this;
89 }
90
91 const Link<T>* operator[](const N index) const
92 {
93 const Link<T>* result = start;
94
95 for (N i = 0; i != index; ++i)
96 result = result->child;
97
98 return result;
99 }
100
101 Link<T>* operator[](const N index)
102 {
103 Link<T>* result = start;
104
105 for (N i = 0; i != index; ++i)
106 result = result->child;
107
108 return result;
109 }
110
111 T& Insert(const N index, const T value)
112 {
113 if (index && index == size - 1)
114 {
115 end->child = new Link<T>(value);
116 end = end->child;
117 ++size;
118 return end->value;
119 }
120 else if (index)
121 {
122 Link<T>* hierarchy = start;
123 for (N i = 0; i != index - 1; ++i)
124 hierarchy = hierarchy->child;
125
126 hierarchy->child = new Link<T>(value, hierarchy->child);
127 ++size;
128 return hierarchy->child->value;
129 }
130 else
131 {
132 start = new Link<T>(value, start);
133 ++size;
134 return start->value;
135 }
136 }
137
138 T Remove(const N index)
139 {
140 if (index && index == size - 1)
141 {
142 Link<T>* hierarchy = start;
143 while (hierarchy->child->child)
144 hierarchy = hierarchy->child;
145
146 T result = end->value;
147 delete end;
148 end = hierarchy;
149 --size;
150 return result;
151 }
152 else if (index)
153 {
154 Link<T>* hierarchy = start;
155 for (N i = 0; i != index - 1; ++i)
156 hierarchy = hierarchy->child;
157
158 Link<T>* tmp = hierarchy->child;
159 T result = tmp->value;
160 hierarchy->child = hierarchy->child->child;
161 tmp->child = nullptr;
162 delete tmp;
163 return result;
164 }
165 else
166 {
167 Link<T>* tmp = start;
168 T result = tmp->value;
169 start = start->child;
170
171 if (--size)
172 tmp->child = nullptr;
173 else
174 end = nullptr;
175
176 delete tmp;
177
178 return result;
179 }
180 }
181
182 T& Push(const T value)
183 {
184 if (size)
185 {
186 end->child = new Link<T>(value);
187 end = end->child;
188 ++size;
189 return end->value;
190 }
191 else
192 {
193 start = new Link<T>(value);
194 end = start;
195 size = 1;
196 return start->value;
197 }
198 }
199
200 T Pop()
201 {
202 if (size == 1)
203 {
204 T result = start->value;
205 delete start;
206 start = nullptr;
207 end = nullptr;
208 size = 0;
209 return result;
210 }
211 if (size > 1)
212 {
213 Link<T>* hierarchy = start;
214 while (hierarchy->child->child)
215 hierarchy = hierarchy->child;
216
217 T result = hierarchy->child->value;
218 delete hierarchy->child;
219 hierarchy->child = nullptr;
220 end = hierarchy;
221 return result;
222 }
223 else
224 return {};
225 }
226
227 void Clear()
228 {
229 delete start;
230 start = nullptr;
231 end = nullptr;
232 size = 0;
233 }
234
235 N Size() const
236 {
237 return size;
238 }
239 };
240}
const Link< T > * operator[](const N index) const
Definition LinkedList.h:91
T & Push(const T value)
Definition LinkedList.h:182
LinkedList(const LinkedList &list)
Definition LinkedList.h:28
N Size() const
Definition LinkedList.h:235
Link< T > * operator[](const N index)
Definition LinkedList.h:101
~LinkedList()
Definition LinkedList.h:18
T & Insert(const N index, const T value)
Definition LinkedList.h:111
T Remove(const N index)
Definition LinkedList.h:138
LinkedList & operator=(LinkedList &&list) noexcept
Definition LinkedList.h:75
void Clear()
Definition LinkedList.h:227
LinkedList & operator=(const LinkedList &list)
Definition LinkedList.h:53
T Pop()
Definition LinkedList.h:200
LinkedList()
Definition LinkedList.h:23
LinkedList(LinkedList &&list) noexcept
Definition LinkedList.h:45
Definition Anchor.h:6