diff --git a/modules/juce_core/containers/juce_SparseSet.h b/modules/juce_core/containers/juce_SparseSet.h index 37efeb4f78..3903db0ed2 100644 --- a/modules/juce_core/containers/juce_SparseSet.h +++ b/modules/juce_core/containers/juce_SparseSet.h @@ -39,16 +39,13 @@ class SparseSet { public: //============================================================================== - /** Creates a new empty set. */ - SparseSet() - { - } + SparseSet() noexcept = default; - /** Creates a copy of another SparseSet. */ - SparseSet (const SparseSet& other) - : values (other.values) - { - } + SparseSet (const SparseSet&) = default; + SparseSet& operator= (const SparseSet&) = default; + + SparseSet (SparseSet&& other) noexcept : values (static_cast&&> (other.values)) {} + SparseSet& operator= (SparseSet&& other) noexcept { values = static_cast&&> (other.values); return *this; } //============================================================================== /** Clears the set. */ @@ -58,12 +55,11 @@ public: } /** Checks whether the set is empty. - This is much quicker than using (size() == 0). */ bool isEmpty() const noexcept { - return values.size() == 0; + return values.isEmpty(); } /** Returns the number of values in the set. @@ -72,9 +68,9 @@ public: are a lot of items in the set. Use isEmpty() for a quick test of whether there are any items. */ - Type size() const + Type size() const noexcept { - Type total (0); + Type total = {}; for (int i = 0; i < values.size(); i += 2) total += values.getUnchecked (i + 1) - values.getUnchecked (i); @@ -87,12 +83,12 @@ public: @param index the index of the value to retrieve, in the range 0 to (size() - 1). @returns the value at this index, or 0 if it's out-of-range */ - Type operator[] (Type index) const + Type operator[] (Type index) const noexcept { for (int i = 0; i < values.size(); i += 2) { - const Type start (values.getUnchecked (i)); - const Type len (values.getUnchecked (i + 1) - start); + auto start = values.getUnchecked (i); + auto len = values.getUnchecked (i + 1) - start; if (index < len) return start + index; @@ -104,7 +100,7 @@ public: } /** Checks whether a particular value is in the set. */ - bool contains (const Type valueToLookFor) const + bool contains (Type valueToLookFor) const noexcept { for (int i = 0; i < values.size(); ++i) if (valueToLookFor < values.getUnchecked(i)) @@ -127,38 +123,36 @@ public: and (getNumRanges() - 1) @see getTotalRange */ - const Range getRange (const int rangeIndex) const + const Range getRange (int rangeIndex) const noexcept { if (isPositiveAndBelow (rangeIndex, getNumRanges())) - return Range (values.getUnchecked (rangeIndex << 1), - values.getUnchecked ((rangeIndex << 1) + 1)); + return { values.getUnchecked (rangeIndex << 1), + values.getUnchecked ((rangeIndex << 1) + 1) }; - return Range(); + return {}; } /** Returns the range between the lowest and highest values in the set. @see getRange */ - Range getTotalRange() const + Range getTotalRange() const noexcept { - if (values.size() > 0) + if (auto num = values.size()) { - jassert ((values.size() & 1) == 0); - return Range (values.getUnchecked (0), - values.getUnchecked (values.size() - 1)); + jassert ((num & 1) == 0); + return { values.getUnchecked (0), values.getUnchecked (num - 1) }; } - return Range(); + return {}; } //============================================================================== /** Adds a range of contiguous values to the set. e.g. addRange (Range \ (10, 14)) will add (10, 11, 12, 13) to the set. */ - void addRange (const Range range) + void addRange (Range range) { - jassert (range.getLength() >= 0); - if (range.getLength() > 0) + if (! range.isEmpty()) { removeRange (range); @@ -172,18 +166,16 @@ public: /** Removes a range of values from the set. e.g. removeRange (Range\ (10, 14)) will remove (10, 11, 12, 13) from the set. */ - void removeRange (const Range rangeToRemove) + void removeRange (Range rangeToRemove) { - jassert (rangeToRemove.getLength() >= 0); - if (rangeToRemove.getLength() > 0 && values.size() > 0 && rangeToRemove.getStart() < values.getUnchecked (values.size() - 1) && values.getUnchecked(0) < rangeToRemove.getEnd()) { - const bool onAtStart = contains (rangeToRemove.getStart() - 1); - const Type lastValue (jmin (rangeToRemove.getEnd(), values.getLast())); - const bool onAtEnd = contains (lastValue); + bool onAtStart = contains (rangeToRemove.getStart() - 1); + auto lastValue = jmin (rangeToRemove.getEnd(), values.getLast()); + bool onAtEnd = contains (lastValue); for (int i = values.size(); --i >= 0;) { @@ -209,7 +201,7 @@ public: } /** Does an XOR of the values in a given range. */ - void invertRange (const Range range) + void invertRange (Range range) { SparseSet newItems; newItems.addRange (range); @@ -224,7 +216,7 @@ public: } /** Checks whether any part of a given range overlaps any part of this set. */ - bool overlapsRange (const Range range) + bool overlapsRange (Range range) const noexcept { if (range.getLength() > 0) { @@ -242,7 +234,7 @@ public: } /** Checks whether the whole of a given range is contained within this one. */ - bool containsRange (const Range range) + bool containsRange (Range range) const noexcept { if (range.getLength() > 0) { @@ -261,15 +253,8 @@ public: } //============================================================================== - bool operator== (const SparseSet& other) noexcept - { - return values == other.values; - } - - bool operator!= (const SparseSet& other) noexcept - { - return values != other.values; - } + bool operator== (const SparseSet& other) const noexcept { return values == other.values; } + bool operator!= (const SparseSet& other) const noexcept { return values != other.values; } private: //==============================================================================