Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iterator for sparse set #1497

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions modules/juce_core/containers/juce_SparseSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,69 @@ class SparseSet
}
};

//==============================================================================
/** Iterator for a SparseSet.
You shouldn't ever need to use this class directly - it's used internally by begin()
and end() to allow range-based-for loops on a SparseSet.
*/
template <class Type>
struct SparseSetIterator
{
SparseSetIterator (const SparseSet<Type>& s, bool isEnd)
: set (s)
{
if (isEnd)
rangeIndex = set.getRanges().size();
}

SparseSetIterator& operator++()
{
valueIndex++;
if (valueIndex == set.getRanges()[rangeIndex].getLength())
{
rangeIndex++;
valueIndex = 0;
}
return *this;
}

bool operator== (const SparseSetIterator<Type>& other) const
{
return rangeIndex == other.rangeIndex && valueIndex == other.valueIndex;
}

bool operator!= (const SparseSetIterator<Type>& other) const
{
return ! (*this == other);
}

Type operator*() const
{
return set.getRanges()[rangeIndex].getStart() + valueIndex;
}

using difference_type = std::ptrdiff_t;
using value_type = Type;
using reference = Type&;
using pointer = Type*;
using iterator_category = std::forward_iterator_tag;

private:
const SparseSet<Type>& set;
int rangeIndex = 0;
int valueIndex = 0;
};

template <class Type>
SparseSetIterator<Type> begin (const SparseSet<Type>& ss)
{
return SparseSetIterator<Type> (ss, false);
}

template <class Type>
SparseSetIterator<Type> end (const SparseSet<Type>& ss)
{
return SparseSetIterator<Type> (ss, true);
}

} // namespace juce
Loading