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.

296 lines
9.4KB

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