OpenShot Audio Library | OpenShotAudio: juce_ElementComparator.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.2

Loading...
Searching...
No Matches
juce_ElementComparator.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
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>
35 struct SortFunctionConverter
36{
37 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
38
39 template <typename Type>
40 bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
41
42private:
43 ElementComparator& comparator;
44 SortFunctionConverter& operator= (const SortFunctionConverter&) = delete;
45};
46
47#endif
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 jassert (firstElement >= 0);
86
87 if (lastElement > firstElement)
88 {
89 SortFunctionConverter<ElementComparator> converter (comparator);
90
91 if (retainOrderOfEquivalentItems)
92 std::stable_sort (array + firstElement, array + lastElement + 1, converter);
93 else
94 std::sort (array + firstElement, array + lastElement + 1, converter);
95 }
96}
97
98
99//==============================================================================
123template <class ElementType, class ElementComparator>
124static int findInsertIndexInSortedArray (ElementComparator& comparator,
125 ElementType* const array,
126 const ElementType newElement,
127 int firstElement,
128 int lastElement)
129{
130 jassert (firstElement <= lastElement);
131
132 ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
133 // avoids getting warning messages about the parameter being unused
134
135 while (firstElement < lastElement)
136 {
137 if (comparator.compareElements (newElement, array [firstElement]) == 0)
138 {
139 ++firstElement;
140 break;
141 }
142 else
143 {
144 const int halfway = (firstElement + lastElement) >> 1;
145
146 if (halfway == firstElement)
147 {
148 if (comparator.compareElements (newElement, array [halfway]) >= 0)
149 ++firstElement;
150
151 break;
152 }
153 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
154 {
155 firstElement = halfway;
156 }
157 else
158 {
159 lastElement = halfway;
160 }
161 }
162 }
163
164 return firstElement;
165}
166
167//==============================================================================
184template <class ElementType>
185 class DefaultElementComparator
186{
187private:
188 using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
189
190public:
191 static int compareElements (ParameterType first, ParameterType second)
192 {
193 return (first < second) ? -1 : ((second < first) ? 1 : 0);
194 }
195};
196
197} // namespace juce
juce::DefaultElementComparator
Definition: juce_ElementComparator.h:186
juce::SortFunctionConverter
Definition: juce_ElementComparator.h:36