The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

275 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Holds a set of primitive values, storing them as a set of ranges.
  22. This container acts like an array, but can efficiently hold large contiguous
  23. ranges of values. It's quite a specialised class, mostly useful for things
  24. like keeping the set of selected rows in a listbox.
  25. The type used as a template parameter must be an integer type, such as int, short,
  26. int64, etc.
  27. */
  28. template <class Type>
  29. class SparseSet
  30. {
  31. public:
  32. //==============================================================================
  33. SparseSet() noexcept {}
  34. SparseSet (const SparseSet&) = default;
  35. SparseSet& operator= (const SparseSet&) = default;
  36. SparseSet (SparseSet&& other) noexcept : values (static_cast<Array<Type, DummyCriticalSection>&&> (other.values)) {}
  37. SparseSet& operator= (SparseSet&& other) noexcept { values = static_cast<Array<Type, DummyCriticalSection>&&> (other.values); return *this; }
  38. //==============================================================================
  39. /** Clears the set. */
  40. void clear()
  41. {
  42. values.clear();
  43. }
  44. /** Checks whether the set is empty.
  45. This is much quicker than using (size() == 0).
  46. */
  47. bool isEmpty() const noexcept
  48. {
  49. return values.isEmpty();
  50. }
  51. /** Returns the number of values in the set.
  52. Because of the way the data is stored, this method can take longer if there
  53. are a lot of items in the set. Use isEmpty() for a quick test of whether there
  54. are any items.
  55. */
  56. Type size() const noexcept
  57. {
  58. Type total = {};
  59. for (int i = 0; i < values.size(); i += 2)
  60. total += values.getUnchecked (i + 1) - values.getUnchecked (i);
  61. return total;
  62. }
  63. /** Returns one of the values in the set.
  64. @param index the index of the value to retrieve, in the range 0 to (size() - 1).
  65. @returns the value at this index, or 0 if it's out-of-range
  66. */
  67. Type operator[] (Type index) const noexcept
  68. {
  69. for (int i = 0; i < values.size(); i += 2)
  70. {
  71. auto start = values.getUnchecked (i);
  72. auto len = values.getUnchecked (i + 1) - start;
  73. if (index < len)
  74. return start + index;
  75. index -= len;
  76. }
  77. return Type();
  78. }
  79. /** Checks whether a particular value is in the set. */
  80. bool contains (Type valueToLookFor) const noexcept
  81. {
  82. for (int i = 0; i < values.size(); ++i)
  83. if (valueToLookFor < values.getUnchecked(i))
  84. return (i & 1) != 0;
  85. return false;
  86. }
  87. //==============================================================================
  88. /** Returns the number of contiguous blocks of values.
  89. @see getRange
  90. */
  91. int getNumRanges() const noexcept
  92. {
  93. return values.size() >> 1;
  94. }
  95. /** Returns one of the contiguous ranges of values stored.
  96. @param rangeIndex the index of the range to look up, between 0
  97. and (getNumRanges() - 1)
  98. @see getTotalRange
  99. */
  100. const Range<Type> getRange (int rangeIndex) const noexcept
  101. {
  102. if (isPositiveAndBelow (rangeIndex, getNumRanges()))
  103. return { values.getUnchecked (rangeIndex << 1),
  104. values.getUnchecked ((rangeIndex << 1) + 1) };
  105. return {};
  106. }
  107. /** Returns the range between the lowest and highest values in the set.
  108. @see getRange
  109. */
  110. Range<Type> getTotalRange() const noexcept
  111. {
  112. if (auto num = values.size())
  113. {
  114. jassert ((num & 1) == 0);
  115. return { values.getUnchecked (0), values.getUnchecked (num - 1) };
  116. }
  117. return {};
  118. }
  119. //==============================================================================
  120. /** Adds a range of contiguous values to the set.
  121. e.g. addRange (Range \<int\> (10, 14)) will add (10, 11, 12, 13) to the set.
  122. */
  123. void addRange (Range<Type> range)
  124. {
  125. if (! range.isEmpty())
  126. {
  127. removeRange (range);
  128. values.addUsingDefaultSort (range.getStart());
  129. values.addUsingDefaultSort (range.getEnd());
  130. simplify();
  131. }
  132. }
  133. /** Removes a range of values from the set.
  134. e.g. removeRange (Range\<int\> (10, 14)) will remove (10, 11, 12, 13) from the set.
  135. */
  136. void removeRange (Range<Type> rangeToRemove)
  137. {
  138. if (rangeToRemove.getLength() > 0
  139. && values.size() > 0
  140. && rangeToRemove.getStart() < values.getUnchecked (values.size() - 1)
  141. && values.getUnchecked(0) < rangeToRemove.getEnd())
  142. {
  143. bool onAtStart = contains (rangeToRemove.getStart() - 1);
  144. auto lastValue = jmin (rangeToRemove.getEnd(), values.getLast());
  145. bool onAtEnd = contains (lastValue);
  146. for (int i = values.size(); --i >= 0;)
  147. {
  148. if (values.getUnchecked(i) <= lastValue)
  149. {
  150. while (values.getUnchecked(i) >= rangeToRemove.getStart())
  151. {
  152. values.remove (i);
  153. if (--i < 0)
  154. break;
  155. }
  156. break;
  157. }
  158. }
  159. if (onAtStart) values.addUsingDefaultSort (rangeToRemove.getStart());
  160. if (onAtEnd) values.addUsingDefaultSort (lastValue);
  161. simplify();
  162. }
  163. }
  164. /** Does an XOR of the values in a given range. */
  165. void invertRange (Range<Type> range)
  166. {
  167. SparseSet newItems;
  168. newItems.addRange (range);
  169. for (int i = getNumRanges(); --i >= 0;)
  170. newItems.removeRange (getRange (i));
  171. removeRange (range);
  172. for (int i = newItems.getNumRanges(); --i >= 0;)
  173. addRange (newItems.getRange(i));
  174. }
  175. /** Checks whether any part of a given range overlaps any part of this set. */
  176. bool overlapsRange (Range<Type> range) const noexcept
  177. {
  178. if (range.getLength() > 0)
  179. {
  180. for (int i = getNumRanges(); --i >= 0;)
  181. {
  182. if (values.getUnchecked ((i << 1) + 1) <= range.getStart())
  183. return false;
  184. if (values.getUnchecked (i << 1) < range.getEnd())
  185. return true;
  186. }
  187. }
  188. return false;
  189. }
  190. /** Checks whether the whole of a given range is contained within this one. */
  191. bool containsRange (Range<Type> range) const noexcept
  192. {
  193. if (range.getLength() > 0)
  194. {
  195. for (int i = getNumRanges(); --i >= 0;)
  196. {
  197. if (values.getUnchecked ((i << 1) + 1) <= range.getStart())
  198. return false;
  199. if (values.getUnchecked (i << 1) <= range.getStart()
  200. && range.getEnd() <= values.getUnchecked ((i << 1) + 1))
  201. return true;
  202. }
  203. }
  204. return false;
  205. }
  206. //==============================================================================
  207. bool operator== (const SparseSet& other) const noexcept { return values == other.values; }
  208. bool operator!= (const SparseSet& other) const noexcept { return values != other.values; }
  209. private:
  210. //==============================================================================
  211. // alternating start/end values of ranges of values that are present.
  212. Array<Type, DummyCriticalSection> values;
  213. void simplify()
  214. {
  215. jassert ((values.size() & 1) == 0);
  216. for (int i = values.size(); --i > 0;)
  217. if (values.getUnchecked(i) == values.getUnchecked (i - 1))
  218. values.removeRange (--i, 2);
  219. }
  220. };
  221. } // namespace juce