Cleanup
[carla.git] / source / modules / water / containers / ElementComparator.h
blobc4c4f9873c19a9eb6436e3470f5892d5d792bc63
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #ifndef WATER_ELEMENTCOMPARATOR_H_INCLUDED
27 #define WATER_ELEMENTCOMPARATOR_H_INCLUDED
29 #include "../water.h"
31 namespace water {
33 /** This is an internal helper class which converts an ElementComparator style
34 class (using a "compareElements" method) into a class that's compatible with
35 std::sort (i.e. using an operator() to compare the elements)
37 template <typename ElementComparator>
38 struct SortFunctionConverter
40 SortFunctionConverter (ElementComparator& e) : comparator (e) {}
42 template <typename Type>
43 bool operator() (Type a, Type b) { return comparator.compareElements (a, b) < 0; }
45 private:
46 ElementComparator& comparator;
47 SortFunctionConverter& operator= (const SortFunctionConverter&) WATER_DELETED_FUNCTION;
50 //==============================================================================
51 /**
52 Sorts a range of elements in an array.
54 The comparator object that is passed-in must define a public method with the following
55 signature:
56 @code
57 int compareElements (ElementType first, ElementType second);
58 @endcode
60 ..and this method must return:
61 - a value of < 0 if the first comes before the second
62 - a value of 0 if the two objects are equivalent
63 - a value of > 0 if the second comes before the first
65 To improve performance, the compareElements() method can be declared as static or const.
67 @param comparator an object which defines a compareElements() method
68 @param array the array to sort
69 @param firstElement the index of the first element of the range to be sorted
70 @param lastElement the index of the last element in the range that needs
71 sorting (this is inclusive)
72 @param retainOrderOfEquivalentItems if true, the order of items that the
73 comparator deems the same will be maintained - this will be
74 a slower algorithm than if they are allowed to be moved around.
76 @see sortArrayRetainingOrder
78 template <class ElementType, class ElementComparator>
79 static void sortArray (ElementComparator& comparator,
80 ElementType* const array,
81 int firstElement,
82 int lastElement,
83 const bool retainOrderOfEquivalentItems)
85 SortFunctionConverter<ElementComparator> converter (comparator);
87 if (retainOrderOfEquivalentItems)
88 std::stable_sort (array + firstElement, array + lastElement + 1, converter);
89 else
90 std::sort (array + firstElement, array + lastElement + 1, converter);
94 //==============================================================================
95 /**
96 Searches a sorted array of elements, looking for the index at which a specified value
97 should be inserted for it to be in the correct order.
99 The comparator object that is passed-in must define a public method with the following
100 signature:
101 @code
102 int compareElements (ElementType first, ElementType second);
103 @endcode
105 ..and this method must return:
106 - a value of < 0 if the first comes before the second
107 - a value of 0 if the two objects are equivalent
108 - a value of > 0 if the second comes before the first
110 To improve performance, the compareElements() method can be declared as static or const.
112 @param comparator an object which defines a compareElements() method
113 @param array the array to search
114 @param newElement the value that is going to be inserted
115 @param firstElement the index of the first element to search
116 @param lastElement the index of the last element in the range (this is non-inclusive)
118 template <class ElementType, class ElementComparator>
119 static int findInsertIndexInSortedArray (ElementComparator& comparator,
120 ElementType* const array,
121 const ElementType newElement,
122 int firstElement,
123 int lastElement)
125 wassert (firstElement <= lastElement);
127 ignoreUnused (comparator); // if you pass in an object with a static compareElements() method, this
128 // avoids getting warning messages about the parameter being unused
130 while (firstElement < lastElement)
132 if (comparator.compareElements (newElement, array [firstElement]) == 0)
134 ++firstElement;
135 break;
137 else
139 const int halfway = (firstElement + lastElement) >> 1;
141 if (halfway == firstElement)
143 if (comparator.compareElements (newElement, array [halfway]) >= 0)
144 ++firstElement;
146 break;
148 else if (comparator.compareElements (newElement, array [halfway]) >= 0)
150 firstElement = halfway;
152 else
154 lastElement = halfway;
159 return firstElement;
162 //==============================================================================
164 A simple ElementComparator class that can be used to sort an array of
165 objects that support the '<' operator.
167 This will work for primitive types and objects that implement operator<().
169 Example: @code
170 Array <int> myArray;
171 DefaultElementComparator<int> sorter;
172 myArray.sort (sorter);
173 @endcode
175 @see ElementComparator
177 template <class ElementType>
178 class DefaultElementComparator
180 private:
181 typedef PARAMETER_TYPE (ElementType) ParameterType;
183 public:
184 static int compareElements (ParameterType first, ParameterType second)
186 return (first < second) ? -1 : ((second < first) ? 1 : 0);
192 #endif // WATER_ELEMENTCOMPARATOR_H_INCLUDED