Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ElementComparator.h 7.1KB

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