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.

288 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. #pragma once
  18. //==============================================================================
  19. /**
  20. Holds a set of primitive values, storing them as a set of ranges.
  21. This container acts like an array, but can efficiently hold large contiguous
  22. ranges of values. It's quite a specialised class, mostly useful for things
  23. like keeping the set of selected rows in a listbox.
  24. The type used as a template parameter must be an integer type, such as int, short,
  25. int64, etc.
  26. */
  27. template <class Type>
  28. class SparseSet
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a new empty set. */
  33. SparseSet()
  34. {
  35. }
  36. /** Creates a copy of another SparseSet. */
  37. SparseSet (const SparseSet<Type>& other)
  38. : values (other.values)
  39. {
  40. }
  41. //==============================================================================
  42. /** Clears the set. */
  43. void clear()
  44. {
  45. values.clear();
  46. }
  47. /** Checks whether the set is empty.
  48. This is much quicker than using (size() == 0).
  49. */
  50. bool isEmpty() const noexcept
  51. {
  52. return values.size() == 0;
  53. }
  54. /** Returns the number of values in the set.
  55. Because of the way the data is stored, this method can take longer if there
  56. are a lot of items in the set. Use isEmpty() for a quick test of whether there
  57. are any items.
  58. */
  59. Type size() const
  60. {
  61. Type total (0);
  62. for (int i = 0; i < values.size(); i += 2)
  63. total += values.getUnchecked (i + 1) - values.getUnchecked (i);
  64. return total;
  65. }
  66. /** Returns one of the values in the set.
  67. @param index the index of the value to retrieve, in the range 0 to (size() - 1).
  68. @returns the value at this index, or 0 if it's out-of-range
  69. */
  70. Type operator[] (Type index) const
  71. {
  72. for (int i = 0; i < values.size(); i += 2)
  73. {
  74. const Type start (values.getUnchecked (i));
  75. const Type len (values.getUnchecked (i + 1) - start);
  76. if (index < len)
  77. return start + index;
  78. index -= len;
  79. }
  80. return Type();
  81. }
  82. /** Checks whether a particular value is in the set. */
  83. bool contains (const Type valueToLookFor) const
  84. {
  85. for (int i = 0; i < values.size(); ++i)
  86. if (valueToLookFor < values.getUnchecked(i))
  87. return (i & 1) != 0;
  88. return false;
  89. }
  90. //==============================================================================
  91. /** Returns the number of contiguous blocks of values.
  92. @see getRange
  93. */
  94. int getNumRanges() const noexcept
  95. {
  96. return values.size() >> 1;
  97. }
  98. /** Returns one of the contiguous ranges of values stored.
  99. @param rangeIndex the index of the range to look up, between 0
  100. and (getNumRanges() - 1)
  101. @see getTotalRange
  102. */
  103. const Range<Type> getRange (const int rangeIndex) const
  104. {
  105. if (isPositiveAndBelow (rangeIndex, getNumRanges()))
  106. return Range<Type> (values.getUnchecked (rangeIndex << 1),
  107. values.getUnchecked ((rangeIndex << 1) + 1));
  108. return Range<Type>();
  109. }
  110. /** Returns the range between the lowest and highest values in the set.
  111. @see getRange
  112. */
  113. Range<Type> getTotalRange() const
  114. {
  115. if (values.size() > 0)
  116. {
  117. jassert ((values.size() & 1) == 0);
  118. return Range<Type> (values.getUnchecked (0),
  119. values.getUnchecked (values.size() - 1));
  120. }
  121. return Range<Type>();
  122. }
  123. //==============================================================================
  124. /** Adds a range of contiguous values to the set.
  125. e.g. addRange (Range \<int\> (10, 14)) will add (10, 11, 12, 13) to the set.
  126. */
  127. void addRange (const Range<Type> range)
  128. {
  129. jassert (range.getLength() >= 0);
  130. if (range.getLength() > 0)
  131. {
  132. removeRange (range);
  133. values.addUsingDefaultSort (range.getStart());
  134. values.addUsingDefaultSort (range.getEnd());
  135. simplify();
  136. }
  137. }
  138. /** Removes a range of values from the set.
  139. e.g. removeRange (Range\<int\> (10, 14)) will remove (10, 11, 12, 13) from the set.
  140. */
  141. void removeRange (const Range<Type> rangeToRemove)
  142. {
  143. jassert (rangeToRemove.getLength() >= 0);
  144. if (rangeToRemove.getLength() > 0
  145. && values.size() > 0
  146. && rangeToRemove.getStart() < values.getUnchecked (values.size() - 1)
  147. && values.getUnchecked(0) < rangeToRemove.getEnd())
  148. {
  149. const bool onAtStart = contains (rangeToRemove.getStart() - 1);
  150. const Type lastValue (jmin (rangeToRemove.getEnd(), values.getLast()));
  151. const bool onAtEnd = contains (lastValue);
  152. for (int i = values.size(); --i >= 0;)
  153. {
  154. if (values.getUnchecked(i) <= lastValue)
  155. {
  156. while (values.getUnchecked(i) >= rangeToRemove.getStart())
  157. {
  158. values.remove (i);
  159. if (--i < 0)
  160. break;
  161. }
  162. break;
  163. }
  164. }
  165. if (onAtStart) values.addUsingDefaultSort (rangeToRemove.getStart());
  166. if (onAtEnd) values.addUsingDefaultSort (lastValue);
  167. simplify();
  168. }
  169. }
  170. /** Does an XOR of the values in a given range. */
  171. void invertRange (const Range<Type> range)
  172. {
  173. SparseSet newItems;
  174. newItems.addRange (range);
  175. for (int i = getNumRanges(); --i >= 0;)
  176. newItems.removeRange (getRange (i));
  177. removeRange (range);
  178. for (int i = newItems.getNumRanges(); --i >= 0;)
  179. addRange (newItems.getRange(i));
  180. }
  181. /** Checks whether any part of a given range overlaps any part of this set. */
  182. bool overlapsRange (const Range<Type> range)
  183. {
  184. if (range.getLength() > 0)
  185. {
  186. for (int i = getNumRanges(); --i >= 0;)
  187. {
  188. if (values.getUnchecked ((i << 1) + 1) <= range.getStart())
  189. return false;
  190. if (values.getUnchecked (i << 1) < range.getEnd())
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. /** Checks whether the whole of a given range is contained within this one. */
  197. bool containsRange (const Range<Type> range)
  198. {
  199. if (range.getLength() > 0)
  200. {
  201. for (int i = getNumRanges(); --i >= 0;)
  202. {
  203. if (values.getUnchecked ((i << 1) + 1) <= range.getStart())
  204. return false;
  205. if (values.getUnchecked (i << 1) <= range.getStart()
  206. && range.getEnd() <= values.getUnchecked ((i << 1) + 1))
  207. return true;
  208. }
  209. }
  210. return false;
  211. }
  212. //==============================================================================
  213. bool operator== (const SparseSet<Type>& other) noexcept
  214. {
  215. return values == other.values;
  216. }
  217. bool operator!= (const SparseSet<Type>& other) noexcept
  218. {
  219. return values != other.values;
  220. }
  221. private:
  222. //==============================================================================
  223. // alternating start/end values of ranges of values that are present.
  224. Array<Type, DummyCriticalSection> values;
  225. void simplify()
  226. {
  227. jassert ((values.size() & 1) == 0);
  228. for (int i = values.size(); --i > 0;)
  229. if (values.getUnchecked(i) == values.getUnchecked (i - 1))
  230. values.removeRange (--i, 2);
  231. }
  232. };