1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
#ifndef SLANG_CORE_LINKED_LIST_H
#define SLANG_CORE_LINKED_LIST_H
#include "slang-allocator.h"
#include "slang-list.h"
#include "slang.h"
#include <type_traits>
namespace Slang
{
template<typename T>
class LinkedList;
template<typename T>
class LinkedNode
{
template<typename T1>
friend class LinkedList;
private:
LinkedNode<T>* prev = nullptr;
LinkedNode<T>* next = nullptr;
LinkedList<T>* list;
public:
T value;
LinkedNode(LinkedList<T>* lnk)
: list(lnk){};
LinkedNode<T>* getPrevious() { return prev; };
LinkedNode<T>* getNext() { return next; };
const LinkedNode<T>* getNext() const { return next; };
LinkedNode<T>* insertAfter(const T& nData)
{
LinkedNode<T>* n = new LinkedNode<T>(list);
n->value = nData;
n->prev = this;
n->next = this->next;
LinkedNode<T>* npp = n->next;
if (npp)
{
npp->prev = n;
}
next = n;
if (!n->next)
list->tail = n;
list->count++;
return n;
};
LinkedNode<T>* insertBefore(const T& nData)
{
LinkedNode<T>* n = new LinkedNode<T>(list);
n->value = nData;
n->prev = prev;
n->next = this;
prev = n;
LinkedNode<T>* npp = n->prev;
if (npp)
npp->next = n;
if (!n->prev)
list->head = n;
list->count++;
return n;
};
void removeAndDelete()
{
if (prev)
prev->next = next;
if (next)
next->prev = prev;
list->count--;
if (list->head == this)
{
list->head = next;
}
if (list->tail == this)
{
list->tail = prev;
}
delete this;
}
};
template<typename T>
class LinkedList
{
template<typename T1>
friend class LinkedNode;
private:
LinkedNode<T>*head, *tail;
int count;
public:
template<bool Const>
class GenIterator
{
public:
using Node = std::conditional_t<Const, const LinkedNode<T>, LinkedNode<T>>;
Node *current, *next;
void setCurrent(Node* cur)
{
current = cur;
if (current)
next = current->getNext();
else
next = nullptr;
}
GenIterator() { current = next = nullptr; }
GenIterator(Node* cur) { setCurrent(cur); }
std::conditional_t<Const, const T&, T&> operator*() const { return current->value; }
GenIterator& operator++()
{
setCurrent(next);
return *this;
}
GenIterator operator++(int)
{
GenIterator rs = *this;
setCurrent(next);
return rs;
}
bool operator!=(const GenIterator& iter) const { return current != iter.current; }
bool operator==(const GenIterator& iter) const { return current == iter.current; }
};
using Iterator = GenIterator<false>;
Iterator begin() { return Iterator(head); }
Iterator end() { return Iterator(0); }
using ConstIterator = GenIterator<true>;
ConstIterator begin() const { return ConstIterator(head); }
ConstIterator end() const { return ConstIterator(0); }
public:
LinkedList()
: head(0), tail(0), count(0)
{
}
~LinkedList() { clear(); }
LinkedList(const LinkedList<T>& link)
: head(0), tail(0), count(0)
{
this->operator=(link);
}
LinkedList(LinkedList<T>&& link)
: head(0), tail(0), count(0)
{
this->operator=(_Move(link));
}
LinkedList<T>& operator=(LinkedList<T>&& link)
{
if (head != 0)
clear();
head = link.head;
tail = link.tail;
count = link.count;
link.head = 0;
link.tail = 0;
link.count = 0;
for (auto node = head; node; node = node->getNext())
node->list = this;
return *this;
}
LinkedList<T>& operator=(const LinkedList<T>& link)
{
if (head != nullptr)
clear();
auto p = link.head;
while (p)
{
addLast(p->value);
p = p->getNext();
}
return *this;
}
template<typename IteratorFunc>
void forEach(const IteratorFunc& f)
{
auto p = head;
while (p)
{
f(p->value);
p = p->getNext();
}
}
LinkedNode<T>* getNode(int x)
{
LinkedNode<T>* pCur = head;
for (int i = 0; i < x; i++)
{
if (pCur)
pCur = pCur->next;
else
SLANG_UNEXPECTED("Index out of range");
}
return pCur;
};
LinkedNode<T>* find(const T& fData)
{
for (LinkedNode<T>* pCur = head; pCur; pCur = pCur->next)
{
if (pCur->value == fData)
return pCur;
}
return nullptr;
};
LinkedNode<T>* getFirstNode() const { return head; };
T& getFirst() const
{
if (!head)
SLANG_UNEXPECTED("LinkedList: index out of range.");
return head->value;
}
T& getLast() const
{
if (!tail)
SLANG_UNEXPECTED("LinkedList: index out of range.");
return tail->value;
}
LinkedNode<T>* getLastNode() const { return tail; };
LinkedNode<T>* addLast(const T& nData)
{
LinkedNode<T>* n = new LinkedNode<T>(this);
n->value = nData;
n->prev = tail;
if (tail)
tail->next = n;
n->next = 0;
tail = n;
if (!head)
head = n;
count++;
return n;
};
// Insert a blank node
LinkedNode<T>* addLast()
{
LinkedNode<T>* n = new LinkedNode<T>(this);
n->prev = tail;
if (tail)
tail->next = n;
n->next = 0;
tail = n;
if (!head)
head = n;
count++;
return n;
};
LinkedNode<T>* addFirst(const T& nData)
{
LinkedNode<T>* n = new LinkedNode<T>(this);
n->value = nData;
addFirst(n);
count++;
return n;
};
void addFirst(LinkedNode<T>* n)
{
n->prev = 0;
n->next = head;
if (head)
head->prev = n;
head = n;
if (!tail)
tail = n;
}
void removeFromList(LinkedNode<T>* n)
{
LinkedNode<T>*n1, *n2 = 0;
n1 = n->prev;
n2 = n->next;
if (n1)
n1->next = n2;
else
head = n2;
if (n2)
n2->prev = n1;
else
tail = n1;
n->prev = nullptr;
n->next = nullptr;
}
void removeAndDelete(LinkedNode<T>* n, int Count = 1)
{
LinkedNode<T>*cur, *next;
cur = n;
int numDeleted = 0;
for (int i = 0; i < Count; i++)
{
next = cur->next;
removeFromList(cur);
delete cur;
cur = next;
numDeleted++;
if (cur == 0)
break;
}
count -= numDeleted;
}
void clear()
{
for (LinkedNode<T>* n = head; n;)
{
LinkedNode<T>* tmp = n->next;
delete n;
n = tmp;
}
head = 0;
tail = 0;
count = 0;
}
List<T> toList() const
{
List<T> rs;
rs.Reserve(count);
for (auto& item : *this)
{
rs.add(item);
}
return rs;
}
int getCount() const { return count; }
};
} // namespace Slang
#endif
|