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.

269 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  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. @tags{Core}
  28. */
  29. template <class Type>
  30. class SparseSet
  31. {
  32. public:
  33. //==============================================================================
  34. SparseSet() = default;
  35. SparseSet (const SparseSet&) = default;
  36. SparseSet& operator= (const SparseSet&) = default;
  37. SparseSet (SparseSet&& other) noexcept : ranges (std::move (other.ranges)) {}
  38. SparseSet& operator= (SparseSet&& other) noexcept { ranges = std::move (other.ranges); return *this; }
  39. //==============================================================================
  40. /** Clears the set. */
  41. void clear() { ranges.clear(); }
  42. /** Checks whether the set is empty.
  43. This is much quicker than using (size() == 0).
  44. */
  45. bool isEmpty() const noexcept { return ranges.isEmpty(); }
  46. /** Returns the number of values in the set.
  47. Because of the way the data is stored, this method can take longer if there
  48. are a lot of items in the set. Use isEmpty() for a quick test of whether there
  49. are any items.
  50. */
  51. Type size() const noexcept
  52. {
  53. Type total = {};
  54. for (auto& r : ranges)
  55. total += r.getLength();
  56. return total;
  57. }
  58. /** Returns one of the values in the set.
  59. @param index the index of the value to retrieve, in the range 0 to (size() - 1).
  60. @returns the value at this index, or 0 if it's out-of-range
  61. */
  62. Type operator[] (Type index) const noexcept
  63. {
  64. Type total = {};
  65. for (auto& r : ranges)
  66. {
  67. auto end = total + r.getLength();
  68. if (index < end)
  69. return r.getStart() + (index - total);
  70. total = end;
  71. }
  72. return {};
  73. }
  74. /** Checks whether a particular value is in the set. */
  75. bool contains (Type valueToLookFor) const noexcept
  76. {
  77. for (auto& r : ranges)
  78. {
  79. if (r.getStart() > valueToLookFor)
  80. break;
  81. if (r.getEnd() > valueToLookFor)
  82. return true;
  83. }
  84. return false;
  85. }
  86. //==============================================================================
  87. /** Returns the number of contiguous blocks of values.
  88. @see getRange
  89. */
  90. int getNumRanges() const noexcept { return ranges.size(); }
  91. /** Returns one of the contiguous ranges of values stored.
  92. @param rangeIndex the index of the range to look up, between 0
  93. and (getNumRanges() - 1)
  94. @see getTotalRange
  95. */
  96. Range<Type> getRange (int rangeIndex) const noexcept { return ranges[rangeIndex]; }
  97. /** Returns the range between the lowest and highest values in the set.
  98. @see getRange
  99. */
  100. Range<Type> getTotalRange() const noexcept
  101. {
  102. if (ranges.isEmpty())
  103. return {};
  104. return { ranges.getFirst().getStart(),
  105. ranges.getLast().getEnd() };
  106. }
  107. //==============================================================================
  108. /** Adds a range of contiguous values to the set.
  109. e.g. addRange (Range \<int\> (10, 14)) will add (10, 11, 12, 13) to the set.
  110. */
  111. void addRange (Range<Type> range)
  112. {
  113. if (! range.isEmpty())
  114. {
  115. removeRange (range);
  116. ranges.add (range);
  117. std::sort (ranges.begin(), ranges.end(),
  118. [] (Range<Type> a, Range<Type> b) { return a.getStart() < b.getStart(); });
  119. simplify();
  120. }
  121. }
  122. /** Removes a range of values from the set.
  123. e.g. removeRange (Range\<int\> (10, 14)) will remove (10, 11, 12, 13) from the set.
  124. */
  125. void removeRange (Range<Type> rangeToRemove)
  126. {
  127. if (getTotalRange().intersects (rangeToRemove) && ! rangeToRemove.isEmpty())
  128. {
  129. for (int i = ranges.size(); --i >= 0;)
  130. {
  131. auto& r = ranges.getReference(i);
  132. if (r.getEnd() <= rangeToRemove.getStart())
  133. break;
  134. if (r.getStart() >= rangeToRemove.getEnd())
  135. continue;
  136. if (rangeToRemove.contains (r))
  137. {
  138. ranges.remove (i);
  139. }
  140. else if (r.contains (rangeToRemove))
  141. {
  142. auto r1 = r.withEnd (rangeToRemove.getStart());
  143. auto r2 = r.withStart (rangeToRemove.getEnd());
  144. // this should be covered in if (rangeToRemove.contains (r))
  145. jassert (! r1.isEmpty() || ! r2.isEmpty());
  146. r = r1;
  147. if (r.isEmpty())
  148. r = r2;
  149. if (! r1.isEmpty() && ! r2.isEmpty())
  150. ranges.insert (i + 1, r2);
  151. }
  152. else if (rangeToRemove.getEnd() > r.getEnd())
  153. {
  154. r.setEnd (rangeToRemove.getStart());
  155. }
  156. else
  157. {
  158. r.setStart (rangeToRemove.getEnd());
  159. }
  160. }
  161. }
  162. }
  163. /** Does an XOR of the values in a given range. */
  164. void invertRange (Range<Type> range)
  165. {
  166. SparseSet newItems;
  167. newItems.addRange (range);
  168. for (auto& r : ranges)
  169. newItems.removeRange (r);
  170. removeRange (range);
  171. for (auto& r : newItems.ranges)
  172. addRange (r);
  173. }
  174. /** Checks whether any part of a given range overlaps any part of this set. */
  175. bool overlapsRange (Range<Type> range) const noexcept
  176. {
  177. if (! range.isEmpty())
  178. for (auto& r : ranges)
  179. if (r.intersects (range))
  180. return true;
  181. return false;
  182. }
  183. /** Checks whether the whole of a given range is contained within this one. */
  184. bool containsRange (Range<Type> range) const noexcept
  185. {
  186. if (! range.isEmpty())
  187. for (auto& r : ranges)
  188. if (r.contains (range))
  189. return true;
  190. return false;
  191. }
  192. /** Returns the set as a list of ranges, which you may want to iterate over. */
  193. const Array<Range<Type>>& getRanges() const noexcept { return ranges; }
  194. //==============================================================================
  195. bool operator== (const SparseSet& other) const noexcept { return ranges == other.ranges; }
  196. bool operator!= (const SparseSet& other) const noexcept { return ranges != other.ranges; }
  197. private:
  198. //==============================================================================
  199. Array<Range<Type>> ranges;
  200. void simplify()
  201. {
  202. for (int i = ranges.size(); --i > 0;)
  203. {
  204. auto& r1 = ranges.getReference (i - 1);
  205. auto& r2 = ranges.getReference (i);
  206. if (r1.getEnd() == r2.getStart())
  207. {
  208. r1.setEnd (r2.getEnd());
  209. ranges.remove (i);
  210. }
  211. }
  212. }
  213. };
  214. } // namespace juce