LMMS
Loading...
Searching...
No Matches
SortedSet.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 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_SORTEDSET_H_INCLUDED
27#define WATER_SORTEDSET_H_INCLUDED
28
29#include "../water.h"
30
31namespace water {
32
33//==============================================================================
55template <class ElementType>
57{
58public:
59 //==============================================================================
62 {
63 }
64
68 SortedSet (const SortedSet& other)
69 : data (other.data)
70 {
71 }
72
75 {
76 }
77
81 SortedSet& operator= (const SortedSet& other) noexcept
82 {
83 data = other.data;
84 return *this;
85 }
86
87 //==============================================================================
92 bool operator== (const SortedSet<ElementType>& other) const noexcept
93 {
94 return data == other.data;
95 }
96
101 bool operator!= (const SortedSet<ElementType>& other) const noexcept
102 {
103 return ! operator== (other);
104 }
105
106 //==============================================================================
116 {
117 data.clear();
118 }
119
124 {
125 data.clearQuick();
126 }
127
128 //==============================================================================
130 inline int size() const noexcept
131 {
132 return data.size();
133 }
134
136 inline bool isEmpty() const noexcept
137 {
138 return size() == 0;
139 }
140
152 inline ElementType operator[] (const int index) const noexcept
153 {
154 return data [index];
155 }
156
165 inline ElementType getUnchecked (const int index) const noexcept
166 {
167 return data.getUnchecked (index);
168 }
169
178 inline ElementType& getReference (const int index) const noexcept
179 {
180 return data.getReference (index);
181 }
182
186 inline ElementType getFirst() const noexcept
187 {
188 return data.getFirst();
189 }
190
194 inline ElementType getLast() const noexcept
195 {
196 return data.getLast();
197 }
198
199 //==============================================================================
203 inline ElementType* begin() const noexcept
204 {
205 return data.begin();
206 }
207
211 inline ElementType* end() const noexcept
212 {
213 return data.end();
214 }
215
216 //==============================================================================
225 int indexOf (const ElementType& elementToLookFor) const noexcept
226 {
227 int s = 0;
228 int e = data.size();
229
230 for (;;)
231 {
232 if (s >= e)
233 return -1;
234
235 if (elementToLookFor == data.getReference (s))
236 return s;
237
238 const int halfway = (s + e) / 2;
239
240 if (halfway == s)
241 return -1;
242
243 if (elementToLookFor < data.getReference (halfway))
244 e = halfway;
245 else
246 s = halfway;
247 }
248 }
249
255 bool contains (const ElementType& elementToLookFor) const noexcept
256 {
257 return indexOf (elementToLookFor) >= 0;
258 }
259
260 //==============================================================================
272 bool add (const ElementType& newElement) noexcept
273 {
274 int s = 0;
275 int e = data.size();
276
277 while (s < e)
278 {
279 ElementType& elem = data.getReference (s);
280 if (newElement == elem)
281 {
282 elem = newElement; // force an update in case operator== permits differences.
283 return false;
284 }
285
286 const int halfway = (s + e) / 2;
287 const bool isBeforeHalfway = (newElement < data.getReference (halfway));
288
289 if (halfway == s)
290 {
291 if (! isBeforeHalfway)
292 ++s;
293
294 break;
295 }
296
297 if (isBeforeHalfway)
298 e = halfway;
299 else
300 s = halfway;
301 }
302
303 data.insert (s, newElement);
304 return true;
305 }
306
313 void addArray (const ElementType* elementsToAdd,
314 int numElementsToAdd) noexcept
315 {
316 while (--numElementsToAdd >= 0)
317 add (*elementsToAdd++);
318 }
319
329 template <class OtherSetType>
330 void addSet (const OtherSetType& setToAddFrom,
331 int startIndex = 0,
332 int numElementsToAdd = -1) noexcept
333 {
334 wassert (this != &setToAddFrom);
335
336 if (this != &setToAddFrom)
337 {
338 if (startIndex < 0)
339 {
341 startIndex = 0;
342 }
343
344 if (numElementsToAdd < 0 || startIndex + numElementsToAdd > setToAddFrom.size())
345 numElementsToAdd = setToAddFrom.size() - startIndex;
346
347 if (numElementsToAdd > 0)
348 addArray (&setToAddFrom.data.getReference (startIndex), numElementsToAdd);
349 }
350 }
351
352 //==============================================================================
362 ElementType remove (const int indexToRemove) noexcept
363 {
364 return data.removeAndReturn (indexToRemove);
365 }
366
374 void removeValue (const ElementType valueToRemove) noexcept
375 {
376 data.remove (indexOf (valueToRemove));
377 }
378
384 template <class OtherSetType>
385 void removeValuesIn (const OtherSetType& otherSet) noexcept
386 {
387 if (this == &otherSet)
388 {
389 clear();
390 }
391 else if (otherSet.size() > 0)
392 {
393 for (int i = data.size(); --i >= 0;)
394 if (otherSet.contains (data.getReference (i)))
395 remove (i);
396 }
397 }
398
406 template <class OtherSetType>
407 void removeValuesNotIn (const OtherSetType& otherSet) noexcept
408 {
409 if (this != &otherSet)
410 {
411 if (otherSet.size() <= 0)
412 {
413 clear();
414 }
415 else
416 {
417 for (int i = data.size(); --i >= 0;)
418 if (! otherSet.contains (data.getReference (i)))
419 remove (i);
420 }
421 }
422 }
423
429 template <class OtherSetType>
430 void swapWith (OtherSetType& otherSet) noexcept
431 {
432 data.swapWith (otherSet.data);
433 }
434
435 //==============================================================================
443 {
444 data.minimiseStorageOverheads();
445 }
446
453 void ensureStorageAllocated (const int minNumElements)
454 {
455 data.ensureStorageAllocated (minNumElements);
456 }
457
458
459private:
460 //==============================================================================
462};
463
464}
465
466#endif // WATER_SORTEDSET_H_INCLUDED
#define noexcept
Definition DistrhoDefines.h:72
Definition Array.h:57
bool contains(const ElementType &elementToLookFor) const noexcept
Definition SortedSet.h:255
ElementType getUnchecked(const int index) const noexcept
Definition SortedSet.h:165
~SortedSet() noexcept
Definition SortedSet.h:74
void removeValuesIn(const OtherSetType &otherSet) noexcept
Definition SortedSet.h:385
void swapWith(OtherSetType &otherSet) noexcept
Definition SortedSet.h:430
void ensureStorageAllocated(const int minNumElements)
Definition SortedSet.h:453
ElementType remove(const int indexToRemove) noexcept
Definition SortedSet.h:362
ElementType * begin() const noexcept
Definition SortedSet.h:203
ElementType operator[](const int index) const noexcept
Definition SortedSet.h:152
void clear() noexcept
Definition SortedSet.h:115
ElementType getLast() const noexcept
Definition SortedSet.h:194
void clearQuick() noexcept
Definition SortedSet.h:123
void removeValue(const ElementType valueToRemove) noexcept
Definition SortedSet.h:374
ElementType * end() const noexcept
Definition SortedSet.h:211
bool operator==(const SortedSet< ElementType > &other) const noexcept
Definition SortedSet.h:92
SortedSet() noexcept
Definition SortedSet.h:61
bool operator!=(const SortedSet< ElementType > &other) const noexcept
Definition SortedSet.h:101
bool add(const ElementType &newElement) noexcept
Definition SortedSet.h:272
int size() const noexcept
Definition SortedSet.h:130
void minimiseStorageOverheads() noexcept
Definition SortedSet.h:442
SortedSet & operator=(const SortedSet &other) noexcept
Definition SortedSet.h:81
Array< ElementType > data
Definition SortedSet.h:461
int indexOf(const ElementType &elementToLookFor) const noexcept
Definition SortedSet.h:225
void removeValuesNotIn(const OtherSetType &otherSet) noexcept
Definition SortedSet.h:407
ElementType & getReference(const int index) const noexcept
Definition SortedSet.h:178
bool isEmpty() const noexcept
Definition SortedSet.h:136
ElementType getFirst() const noexcept
Definition SortedSet.h:186
void addSet(const OtherSetType &setToAddFrom, int startIndex=0, int numElementsToAdd=-1) noexcept
Definition SortedSet.h:330
SortedSet(const SortedSet &other)
Definition SortedSet.h:68
void addArray(const ElementType *elementsToAdd, int numElementsToAdd) noexcept
Definition SortedSet.h:313
* e
Definition inflate.c:1404
register unsigned i
Definition inflate.c:1575
unsigned s
Definition inflate.c:1555
#define wassertfalse
#define wassert(expression)
Definition AudioSampleBuffer.h:33
#define const
Definition zconf.h:137