OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
23 namespace juce
24{
25
26#ifndef DOXYGEN
27
34 template <typename ElementComparator>
35 struct SortFunctionConverter
36{
37 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
38 SortFunctionConverter (const SortFunctionConverter&) = default;
39
40 template <typename Type>
41 bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
42
43 private:
44 ElementComparator& comparator;
45
46 SortFunctionConverter& operator= (const SortFunctionConverter&) = delete;
47};
48
49#endif
50
51
52//==============================================================================
80 template <class ElementType, class ElementComparator>
81 static 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//==============================================================================
125 template <class ElementType, class ElementComparator>
126 static 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//==============================================================================
183 template <class ElementType>
184 class DefaultElementComparator
185{
186 private:
187 using ParameterType = typename TypeHelpers::ParameterType<ElementType>::type;
188
189 public:
190 static int compareElements (ParameterType first, ParameterType second)
191 {
192 return (first < second) ? -1 : ((second < first) ? 1 : 0);
193 }
194};
195
196} // namespace juce
juce::DefaultElementComparator
Definition juce_ElementComparator.h:185
juce::SortFunctionConverter
Definition juce_ElementComparator.h:36