LMMS
Loading...
Searching...
No Matches
LinkedListPointer.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_LINKEDLISTPOINTER_H_INCLUDED
27#define WATER_LINKEDLISTPOINTER_H_INCLUDED
28
29#include "../water.h"
30
31namespace water {
32
33//==============================================================================
60template <class ObjectType>
62{
63public:
64 //==============================================================================
67 : item (nullptr)
68 {
69 }
70
72 explicit LinkedListPointer (ObjectType* const headItem) noexcept
73 : item (headItem)
74 {
75 }
76
78 LinkedListPointer& operator= (ObjectType* const newItem) noexcept
79 {
80 item = newItem;
81 return *this;
82 }
83
84 //==============================================================================
86 inline operator ObjectType*() const noexcept
87 {
88 return item;
89 }
90
92 inline ObjectType* get() const noexcept
93 {
94 return item;
95 }
96
105 {
106 LinkedListPointer* l = this;
107
108 while (l->item != nullptr)
109 l = &(l->item->nextListItem);
110
111 return *l;
112 }
113
119 {
120 int total = 0;
121
122 for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
123 ++total;
124
125 return total;
126 }
127
132 LinkedListPointer& operator[] (int index) noexcept
133 {
134 LinkedListPointer* l = this;
135
136 while (--index >= 0 && l->item != nullptr)
137 l = &(l->item->nextListItem);
138
139 return *l;
140 }
141
146 const LinkedListPointer& operator[] (int index) const noexcept
147 {
148 const LinkedListPointer* l = this;
149
150 while (--index >= 0 && l->item != nullptr)
151 l = &(l->item->nextListItem);
152
153 return *l;
154 }
155
157 bool contains (const ObjectType* const itemToLookFor) const noexcept
158 {
159 for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
160 if (itemToLookFor == i)
161 return true;
162
163 return false;
164 }
165
166 //==============================================================================
170 void insertNext (ObjectType* const newItem)
171 {
172 wassert (newItem != nullptr);
173 wassert (newItem->nextListItem == nullptr);
174 newItem->nextListItem = item;
175 item = newItem;
176 }
177
182 void insertAtIndex (int index, ObjectType* newItem)
183 {
184 wassert (newItem != nullptr);
185 LinkedListPointer* l = this;
186
187 while (index != 0 && l->item != nullptr)
188 {
189 l = &(l->item->nextListItem);
190 --index;
191 }
192
193 l->insertNext (newItem);
194 }
195
199 ObjectType* replaceNext (ObjectType* const newItem) noexcept
200 {
201 wassert (newItem != nullptr);
202 wassert (newItem->nextListItem == nullptr);
203
204 ObjectType* const oldItem = item;
205 item = newItem;
206 item->nextListItem = oldItem->nextListItem.item;
207 oldItem->nextListItem.item = nullptr;
208 return oldItem;
209 }
210
217 void append (ObjectType* const newItem)
218 {
219 getLast().item = newItem;
220 }
221
227 {
228 LinkedListPointer* insertPoint = this;
229
230 for (ObjectType* i = other.item; i != nullptr; i = i->nextListItem)
231 {
232 insertPoint->insertNext (new ObjectType (*i));
233 insertPoint = &(insertPoint->item->nextListItem);
234 }
235 }
236
241 ObjectType* removeNext() noexcept
242 {
243 ObjectType* const oldItem = item;
244
245 if (oldItem != nullptr)
246 {
247 item = oldItem->nextListItem;
248 oldItem->nextListItem.item = nullptr;
249 }
250
251 return oldItem;
252 }
253
257 void remove (ObjectType* const itemToRemove)
258 {
259 if (LinkedListPointer* const l = findPointerTo (itemToRemove))
260 l->removeNext();
261 }
262
267 {
268 while (item != nullptr)
269 {
270 ObjectType* const oldItem = item;
271 item = oldItem->nextListItem;
272 delete oldItem;
273 }
274 }
275
280 LinkedListPointer* findPointerTo (ObjectType* const itemToLookFor) noexcept
281 {
282 LinkedListPointer* l = this;
283
284 while (l->item != nullptr)
285 {
286 if (l->item == itemToLookFor)
287 return l;
288
289 l = &(l->item->nextListItem);
290 }
291
292 return nullptr;
293 }
294
299 void copyToArray (ObjectType** destArray) const noexcept
300 {
301 wassert (destArray != nullptr);
302
303 for (ObjectType* i = item; i != nullptr; i = i->nextListItem)
304 *destArray++ = i;
305 }
306
308 void swapWith (LinkedListPointer& other) noexcept
309 {
310 std::swap (item, other.item);
311 }
312
313 //==============================================================================
322 {
323 public:
327 : endOfList (&endOfListPointer)
328 {
329 // This can only be used to add to the end of a list.
330 wassert (endOfListPointer.item == nullptr);
331 }
332
334 void append (ObjectType* const newItem) noexcept
335 {
336 *endOfList = newItem;
337 endOfList = &(newItem->nextListItem);
338 }
339
340 private:
342
344 };
345
346private:
347 //==============================================================================
348 ObjectType* item;
349
351};
352
353}
354
355#endif // WATER_LINKEDLISTPOINTER_H_INCLUDED
#define CARLA_DECLARE_NON_COPYABLE(ClassName)
Definition CarlaDefines.h:242
#define noexcept
Definition DistrhoDefines.h:72
void append(ObjectType *const newItem) noexcept
Definition LinkedListPointer.h:334
Appender(LinkedListPointer &endOfListPointer) noexcept
Definition LinkedListPointer.h:326
LinkedListPointer * endOfList
Definition LinkedListPointer.h:341
void deleteAll()
Definition LinkedListPointer.h:266
void insertAtIndex(int index, ObjectType *newItem)
Definition LinkedListPointer.h:182
void copyToArray(ObjectType **destArray) const noexcept
Definition LinkedListPointer.h:299
void append(ObjectType *const newItem)
Definition LinkedListPointer.h:217
void swapWith(LinkedListPointer &other) noexcept
Definition LinkedListPointer.h:308
bool contains(const ObjectType *const itemToLookFor) const noexcept
Definition LinkedListPointer.h:157
LinkedListPointer & operator=(ObjectType *const newItem) noexcept
Definition LinkedListPointer.h:78
void addCopyOfList(const LinkedListPointer &other)
Definition LinkedListPointer.h:226
ObjectType * removeNext() noexcept
Definition LinkedListPointer.h:241
ObjectType * get() const noexcept
Definition LinkedListPointer.h:92
LinkedListPointer & getLast() noexcept
Definition LinkedListPointer.h:104
ObjectType * replaceNext(ObjectType *const newItem) noexcept
Definition LinkedListPointer.h:199
void remove(ObjectType *const itemToRemove)
Definition LinkedListPointer.h:257
LinkedListPointer & operator[](int index) noexcept
Definition LinkedListPointer.h:132
ObjectType * item
Definition LinkedListPointer.h:348
LinkedListPointer(ObjectType *const headItem) noexcept
Definition LinkedListPointer.h:72
LinkedListPointer() noexcept
Definition LinkedListPointer.h:66
void insertNext(ObjectType *const newItem)
Definition LinkedListPointer.h:170
LinkedListPointer * findPointerTo(ObjectType *const itemToLookFor) noexcept
Definition LinkedListPointer.h:280
int size() const noexcept
Definition LinkedListPointer.h:118
int * l
Definition inflate.c:1579
register unsigned i
Definition inflate.c:1575
#define wassert(expression)
Definition AudioSampleBuffer.h:33
#define const
Definition zconf.h:137