juce_SparseSet.h 9.5 KB

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