openshot-audio  0.1.2
juce_ElementComparator.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_ELEMENTCOMPARATOR_H_INCLUDED
30 #define JUCE_ELEMENTCOMPARATOR_H_INCLUDED
31 
32 #ifndef DOXYGEN
33 
38 template <typename ElementComparator>
40 {
41  SortFunctionConverter (ElementComparator& e) : comparator (e) {}
42 
43  template <typename Type>
44  bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
45 
46 private:
47  ElementComparator& comparator;
49 };
50 
51 #endif
52 
53 
54 //==============================================================================
82 template <class ElementType, class ElementComparator>
83 static void sortArray (ElementComparator& comparator,
84  ElementType* const array,
85  int firstElement,
86  int lastElement,
87  const bool retainOrderOfEquivalentItems)
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 //==============================================================================
122 template <class ElementType, class ElementComparator>
123 static int findInsertIndexInSortedArray (ElementComparator& comparator,
124  ElementType* const array,
125  const ElementType newElement,
126  int firstElement,
127  int lastElement)
128 {
129  jassert (firstElement <= lastElement);
130 
131  (void) comparator; // if you pass in an object with a static compareElements() method, this
132  // avoids getting warning messages about the parameter being unused
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 //==============================================================================
181 template <class ElementType>
183 {
184 private:
185  typedef PARAMETER_TYPE (ElementType) ParameterType;
186 
187 public:
188  static int compareElements (ParameterType first, ParameterType second)
189  {
190  return (first < second) ? -1 : ((second < first) ? 1 : 0);
191  }
192 };
193 
194 
195 #endif // JUCE_ELEMENTCOMPARATOR_H_INCLUDED
static int compareElements(ParameterType first, ParameterType second)
Definition: juce_ElementComparator.h:188
bool operator()(Type a, Type b)
Definition: juce_ElementComparator.h:44
#define JUCE_DELETED_FUNCTION
Definition: juce_CompilerSupport.h:133
Definition: juce_ElementComparator.h:39
#define jassert(a)
Definition: juce_PlatformDefs.h:146
#define PARAMETER_TYPE(a)
Definition: juce_ElementComparator.h:182
SortFunctionConverter(ElementComparator &e)
Definition: juce_ElementComparator.h:41