LMMS
Loading...
Searching...
No Matches
ElementComparator.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_ELEMENTCOMPARATOR_H_INCLUDED
27#define WATER_ELEMENTCOMPARATOR_H_INCLUDED
28
29#include "../water.h"
30
31namespace water {
32
37template <typename ElementComparator>
39{
40 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
41
42 template <typename Type>
43 bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
44
45private:
46 ElementComparator& comparator;
48};
49
50//==============================================================================
78template <class ElementType, class ElementComparator>
79static void sortArray (ElementComparator& comparator,
80 ElementType* const array,
81 int firstElement,
82 int lastElement,
83 const bool retainOrderOfEquivalentItems)
84{
85 SortFunctionConverter<ElementComparator> converter (comparator);
86
87 if (retainOrderOfEquivalentItems)
88 std::stable_sort (array + firstElement, array + lastElement + 1, converter);
89 else
90 std::sort (array + firstElement, array + lastElement + 1, converter);
91}
92
93
94//==============================================================================
118template <class ElementType, class ElementComparator>
119static int findInsertIndexInSortedArray (ElementComparator& comparator,
120 ElementType* const array,
121 const ElementType newElement,
122 int firstElement,
123 int lastElement)
124{
125 wassert (firstElement <= lastElement);
126
127 ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
128 // avoids getting warning messages about the parameter being unused
129
130 while (firstElement < lastElement)
131 {
132 if (comparator.compareElements (newElement, array [firstElement]) == 0)
133 {
134 ++firstElement;
135 break;
136 }
137 else
138 {
139 const int halfway = (firstElement + lastElement) >> 1;
140
141 if (halfway == firstElement)
142 {
143 if (comparator.compareElements (newElement, array [halfway]) >= 0)
144 ++firstElement;
145
146 break;
147 }
148 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
149 {
150 firstElement = halfway;
151 }
152 else
153 {
154 lastElement = halfway;
155 }
156 }
157 }
158
159 return firstElement;
160}
161
162//==============================================================================
177template <class ElementType>
179{
180private:
181 typedef PARAMETER_TYPE (ElementType) ParameterType;
182
183public:
184 static int compareElements (ParameterType first, ParameterType second)
185 {
186 return (first < second) ? -1 : ((second < first) ? 1 : 0);
187 }
188};
189
190}
191
192#endif // WATER_ELEMENTCOMPARATOR_H_INCLUDED
uint8_t a
Definition Spc_Cpu.h:141
Definition ElementComparator.h:179
static int compareElements(ParameterType first, ParameterType second)
Definition ElementComparator.h:184
typedef PARAMETER_TYPE(ElementType) ParameterType
* e
Definition inflate.c:1404
ParameterType
Definition CarlaBackend.h:763
#define wassert(expression)
#define WATER_DELETED_FUNCTION
Definition AudioSampleBuffer.h:33
static int findInsertIndexInSortedArray(ElementComparator &comparator, ElementType *const array, const ElementType newElement, int firstElement, int lastElement)
Definition ElementComparator.h:119
void ignoreUnused(const Type1 &) noexcept
Definition MathsFunctions.h:237
static void sortArray(ElementComparator &comparator, ElementType *const array, int firstElement, int lastElement, const bool retainOrderOfEquivalentItems)
Definition ElementComparator.h:79
Definition ElementComparator.h:39
ElementComparator & comparator
Definition ElementComparator.h:46
SortFunctionConverter(ElementComparator &e)
Definition ElementComparator.h:40
SortFunctionConverter & operator=(const SortFunctionConverter &) WATER_DELETED_FUNCTION
bool operator()(Type a, Type b)
Definition ElementComparator.h:43
b
Definition crypt.c:628