OpenShot Audio Library | OpenShotAudio 0.4.0
juce_ElementComparator.h
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 ([[maybe_unused]] ElementComparator& comparator,
127 ElementType* const array,
128 const ElementType newElement,
129 int firstElement,
130 int lastElement)
131{
132 jassert (firstElement <= lastElement);
133
134 while (firstElement < lastElement)
135 {
136 if (comparator.compareElements (newElement, array [firstElement]) == 0)
137 {
138 ++firstElement;
139 break;
140 }
141 else
142 {
143 const int halfway = (firstElement + lastElement) >> 1;
144
145 if (halfway == firstElement)
146 {
147 if (comparator.compareElements (newElement, array [halfway]) >= 0)
148 ++firstElement;
149
150 break;
151 }
152 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
153 {
154 firstElement = halfway;
155 }
156 else
157 {
158 lastElement = halfway;
159 }
160 }
161 }
162
163 return firstElement;
164}
165
166//==============================================================================
183template <class ElementType>
185{
186private:
187 using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
188
189public:
190 static int compareElements (ParameterType first, ParameterType second)
191 {
192 return (first < second) ? -1 : ((second < first) ? 1 : 0);
193 }
194};
195
196} // namespace juce