LMMS
Loading...
Searching...
No Matches
juce_ElementComparator.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
15
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
19
20 ==============================================================================
21*/
22
23namespace juce
24{
25
26#ifndef DOXYGEN
27
34template <typename ElementComparator>
36{
37 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
39
40 template <typename Type>
41 bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
42
43private:
44 ElementComparator& comparator;
45
46 SortFunctionConverter& operator= (const SortFunctionConverter&) = delete;
47};
48
49#endif
50
51
52//==============================================================================
80template <class ElementType, class ElementComparator>
81static void sortArray (ElementComparator& comparator,
82 ElementType* const array,
83 int firstElement,
84 int lastElement,
85 const bool retainOrderOfEquivalentItems)
86{
87 jassert (firstElement >= 0);
88
89 if (lastElement > firstElement)
90 {
91 SortFunctionConverter<ElementComparator> converter (comparator);
92
93 if (retainOrderOfEquivalentItems)
94 std::stable_sort (array + firstElement, array + lastElement + 1, converter);
95 else
96 std::sort (array + firstElement, array + lastElement + 1, converter);
97 }
98}
99
100
101//==============================================================================
125template <class ElementType, class ElementComparator>
126static int findInsertIndexInSortedArray (ElementComparator& comparator,
127 ElementType* const array,
128 const ElementType newElement,
129 int firstElement,
130 int lastElement)
131{
132 jassert (firstElement <= lastElement);
133
134 ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
135 // avoids getting warning messages about the parameter being unused
136
137 while (firstElement < lastElement)
138 {
139 if (comparator.compareElements (newElement, array [firstElement]) == 0)
140 {
141 ++firstElement;
142 break;
143 }
144 else
145 {
146 const int halfway = (firstElement + lastElement) >> 1;
147
148 if (halfway == firstElement)
149 {
150 if (comparator.compareElements (newElement, array [halfway]) >= 0)
151 ++firstElement;
152
153 break;
154 }
155 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
156 {
157 firstElement = halfway;
158 }
159 else
160 {
161 lastElement = halfway;
162 }
163 }
164 }
165
166 return firstElement;
167}
168
169//==============================================================================
186template <class ElementType>
188{
189private:
191
192public:
193 static int compareElements (ParameterType first, ParameterType second)
194 {
195 return (first < second) ? -1 : ((second < first) ? 1 : 0);
196 }
197};
198
199} // namespace juce
uint8_t a
Definition Spc_Cpu.h:141
Definition juce_ElementComparator.h:188
typename TypeHelpers::ParameterType< ElementType >::type ParameterType
Definition juce_ElementComparator.h:190
static int compareElements(ParameterType first, ParameterType second)
Definition juce_ElementComparator.h:193
* e
Definition inflate.c:1404
#define jassert(expression)
Definition carla_juce.cpp:31
static int findInsertIndexInSortedArray(ElementComparator &comparator, ElementType *const array, const ElementType newElement, int firstElement, int lastElement)
Definition juce_ElementComparator.h:126
static void sortArray(ElementComparator &comparator, ElementType *const array, int firstElement, int lastElement, const bool retainOrderOfEquivalentItems)
Definition juce_ElementComparator.h:81
void ignoreUnused(Types &&...) noexcept
Definition juce_MathsFunctions.h:333
Definition juce_ElementComparator.h:36
SortFunctionConverter(ElementComparator &e)
Definition juce_ElementComparator.h:37
ElementComparator & comparator
Definition juce_ElementComparator.h:44
SortFunctionConverter(const SortFunctionConverter &)=default
const Type & type
Definition juce_MathsFunctions.h:632
b
Definition crypt.c:628