SortedSet< ElementType, TypeOfCriticalSectionToUse > Class Template Reference
juce_core » containers

Holds a set of unique primitive objects, such as ints or doubles. More...

## Public Types

using | ScopedLockType = typename TypeOfCriticalSectionToUse::ScopedLockType
| Returns the type of scoped lock to use for locking this array. More...

## Public Member Functions

| SortedSet ()=default
| Creates an empty set. More...
| SortedSet (const SortedSet &)=default
| Creates a copy of another set. More...
| SortedSet (SortedSet &&) noexcept=default
| Creates a copy of another set. More...
SortedSet & | operator= (const SortedSet &)=default
| Makes a copy of another set. More...
SortedSet & | operator= (SortedSet &&) noexcept=default
| Makes a copy of another set. More...
| ~SortedSet ()=default
| Destructor. More...
bool | operator== (const SortedSet< ElementType > &other) const noexcept
| Compares this set to another one. More...
bool | operator!= (const SortedSet< ElementType > &other) const noexcept
| Compares this set to another one. More...
void | clear () noexcept
| Removes all elements from the set. More...
void | clearQuick () noexcept
| Removes all elements from the set without freeing the array's allocated storage. More...
int | size () const noexcept
| Returns the current number of elements in the set. More...
bool | isEmpty () const noexcept
| Returns true if the set is empty, false otherwise. More...
ElementType | operator[] (const int index) const noexcept
| Returns one of the elements in the set. More...
ElementType | getUnchecked (const int index) const noexcept
| Returns one of the elements in the set, without checking the index passed in. More...
ElementType & | getReference (const int index) noexcept
| Returns a direct reference to one of the elements in the set, without checking the index passed in. More...
const ElementType & | getReference (const int index) const noexcept
| Returns a direct reference to one of the elements in the set, without checking the index passed in. More...
ElementType | getFirst () const noexcept
| Returns the first element in the set, or 0 if the set is empty. More...
ElementType | getLast () const noexcept
| Returns the last element in the set, or 0 if the set is empty. More...
const ElementType * | begin () const noexcept
| Returns a pointer to the first element in the set. More...
const ElementType * | end () const noexcept
| Returns a pointer to the element which follows the last element in the set. More...
int | indexOf (const ElementType &elementToLookFor) const noexcept
| Finds the index of the first element which matches the value passed in. More...
bool | contains (const ElementType &elementToLookFor) const noexcept
| Returns true if the set contains at least one occurrence of an object. More...
bool | add (const ElementType &newElement) noexcept
| Adds a new element to the set, (as long as it's not already in there). More...
void | addArray (const ElementType *elementsToAdd, int numElementsToAdd) noexcept
| Adds elements from an array to this set. More...
template<class OtherSetType >
void | addSet (const OtherSetType &setToAddFrom, int startIndex=0, int numElementsToAdd=-1) noexcept
| Adds elements from another set to this one. More...
ElementType | remove (const int indexToRemove) noexcept
| Removes an element from the set. More...
void | removeValue (const ElementType valueToRemove) noexcept
| Removes an item from the set. More...
template<class OtherSetType >
void | removeValuesIn (const OtherSetType &otherSet) noexcept
| Removes any elements which are also in another set. More...
template<class OtherSetType >
void | removeValuesNotIn (const OtherSetType &otherSet) noexcept
| Removes any elements which are not found in another set. More...
template<class OtherSetType >
void | swapWith (OtherSetType &otherSet) noexcept
| This swaps the contents of this array with those of another array. More...
void | minimiseStorageOverheads () noexcept
| Reduces the amount of storage being used by the set. More...
void | ensureStorageAllocated (const int minNumElements)
| Increases the set's internal storage to hold a minimum number of elements. More...
const TypeOfCriticalSectionToUse & | getLock () const noexcept
| Returns the CriticalSection that locks this array. More...

## Detailed Description

### template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>
class SortedSet< ElementType, TypeOfCriticalSectionToUse >

Holds a set of unique primitive objects, such as ints or doubles.

A set can only hold one item with a given value, so if for example it's a set of integers, attempting to add the same integer twice will do nothing the second time.

Internally, the list of items is kept sorted (which means that whatever kind of primitive type is used must support the ==, <, >, <= and >= operators to determine the order), and searching the set for known values is very fast because it uses a binary-chop method.

Note that if you're using a class or struct as the element type, it must be capable of being copied or moved with a straightforward memcpy, rather than needing construction and destruction code.

To make all the set's methods thread-safe, pass in "CriticalSection" as the templated TypeOfCriticalSectionToUse parameter, instead of the default DummyCriticalSection.

See also
  Array, OwnedArray, ReferenceCountedArray, StringArray, CriticalSection

## Member Typedef Documentation

## ScopedLockType

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

using SortedSet< ElementType, TypeOfCriticalSectionToUse >::ScopedLockType = typename TypeOfCriticalSectionToUse::ScopedLockType

Returns the type of scoped lock to use for locking this array.

## Constructor & Destructor Documentation

## SortedSet() [1/3]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet< ElementType, TypeOfCriticalSectionToUse >::SortedSet | ( |  | ) |

default

Creates an empty set.

## SortedSet() [2/3]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet< ElementType, TypeOfCriticalSectionToUse >::SortedSet | ( | const SortedSet< ElementType, TypeOfCriticalSectionToUse > & |  | ) |

default

Creates a copy of another set.

## SortedSet() [3/3]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet< ElementType, TypeOfCriticalSectionToUse >::SortedSet | ( | SortedSet< ElementType, TypeOfCriticalSectionToUse > && |  | ) |

default noexcept

Creates a copy of another set.

## ~SortedSet()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet< ElementType, TypeOfCriticalSectionToUse >::~SortedSet | ( |  | ) |

default

Destructor.

## Member Function Documentation

## operator=() [1/2]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet& SortedSet< ElementType, TypeOfCriticalSectionToUse >::operator= | ( | const SortedSet< ElementType, TypeOfCriticalSectionToUse > & |  | ) |

default

Makes a copy of another set.

## operator=() [2/2]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

SortedSet& SortedSet< ElementType, TypeOfCriticalSectionToUse >::operator= | ( | SortedSet< ElementType, TypeOfCriticalSectionToUse > && |  | ) |

default noexcept

Makes a copy of another set.

## operator==()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

bool SortedSet< ElementType, TypeOfCriticalSectionToUse >::operator== | ( | const SortedSet< ElementType > & | other | ) | const

noexcept

Compares this set to another one.

Two sets are considered equal if they both contain the same set of elements.

Parameters

  other | the other set to compare with

Referenced by SortedSet< ColourSetting >::operator!=().

## operator!=()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

bool SortedSet< ElementType, TypeOfCriticalSectionToUse >::operator!= | ( | const SortedSet< ElementType > & | other | ) | const

noexcept

Compares this set to another one.

Two sets are considered equal if they both contain the same set of elements.

Parameters

  other | the other set to compare with

## clear()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::clear | ( |  | ) |

noexcept

Removes all elements from the set.

This will remove all the elements, and free any storage that the set is using. To clear it without freeing the storage, use the clearQuick() method instead.

See also
  clearQuick

Referenced by SortedSet< ColourSetting >::removeValuesIn(), and SortedSet< ColourSetting >::removeValuesNotIn().

## clearQuick()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::clearQuick | ( |  | ) |

noexcept

Removes all elements from the set without freeing the array's allocated storage.

See also
  clear

## size()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

int SortedSet< ElementType, TypeOfCriticalSectionToUse >::size | ( |  | ) | const

noexcept

Returns the current number of elements in the set.

Referenced by SortedSet< ColourSetting >::isEmpty().

## isEmpty()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

bool SortedSet< ElementType, TypeOfCriticalSectionToUse >::isEmpty | ( |  | ) | const

noexcept

Returns true if the set is empty, false otherwise.

## operator[]()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType SortedSet< ElementType, TypeOfCriticalSectionToUse >::operator[] | ( | const int | index | ) | const

noexcept

Returns one of the elements in the set.

If the index passed in is beyond the range of valid elements, this will return zero.

If you're certain that the index will always be a valid element, you can call getUnchecked() instead, which is faster.

Parameters

  index | the index of the element being requested (0 is the first element in the set)

See also
  getUnchecked, getFirst, getLast

## getUnchecked()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType SortedSet< ElementType, TypeOfCriticalSectionToUse >::getUnchecked | ( | const int | index | ) | const

noexcept

Returns one of the elements in the set, without checking the index passed in.

Unlike the operator[] method, this will try to return an element without checking that the index is within the bounds of the set, so should only be used when you're confident that it will always be a valid index.

Parameters

  index | the index of the element being requested (0 is the first element in the set)

See also
  operator[], getFirst, getLast

## getReference() [1/2]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType& SortedSet< ElementType, TypeOfCriticalSectionToUse >::getReference | ( | const int | index | ) |

noexcept

Returns a direct reference to one of the elements in the set, without checking the index passed in.

This is like getUnchecked, but returns a direct reference to the element, so that you can alter it directly. Obviously this can be dangerous, so only use it when absolutely necessary.

Parameters

  index | the index of the element being requested (0 is the first element in the array)

## getReference() [2/2]

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

const ElementType& SortedSet< ElementType, TypeOfCriticalSectionToUse >::getReference | ( | const int | index | ) | const

noexcept

Returns a direct reference to one of the elements in the set, without checking the index passed in.

Parameters

  index | the index of the element being requested (0 is the first element in the array)

## getFirst()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType SortedSet< ElementType, TypeOfCriticalSectionToUse >::getFirst | ( |  | ) | const

noexcept

Returns the first element in the set, or 0 if the set is empty.

See also
  operator[], getUnchecked, getLast

## getLast()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType SortedSet< ElementType, TypeOfCriticalSectionToUse >::getLast | ( |  | ) | const

noexcept

Returns the last element in the set, or 0 if the set is empty.

See also
  operator[], getUnchecked, getFirst

## begin()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

const ElementType* SortedSet< ElementType, TypeOfCriticalSectionToUse >::begin | ( |  | ) | const

noexcept

Returns a pointer to the first element in the set.

This method is provided for compatibility with standard C++ iteration mechanisms.

## end()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

const ElementType* SortedSet< ElementType, TypeOfCriticalSectionToUse >::end | ( |  | ) | const

noexcept

Returns a pointer to the element which follows the last element in the set.

This method is provided for compatibility with standard C++ iteration mechanisms.

## indexOf()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

int SortedSet< ElementType, TypeOfCriticalSectionToUse >::indexOf | ( | const ElementType & | elementToLookFor | ) | const

noexcept

Finds the index of the first element which matches the value passed in.

This will search the set for the given object, and return the index of its first occurrence. If the object isn't found, the method will return -1.

Parameters

  elementToLookFor | the value or object to look for

Returns
  the index of the object, or -1 if it's not found

Referenced by SortedSet< ColourSetting >::contains(), and SortedSet< ColourSetting >::removeValue().

## contains()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

bool SortedSet< ElementType, TypeOfCriticalSectionToUse >::contains | ( | const ElementType & | elementToLookFor | ) | const

noexcept

Returns true if the set contains at least one occurrence of an object.

Parameters

  elementToLookFor | the value or object to look for

Returns
  true if the item is found

## add()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

bool SortedSet< ElementType, TypeOfCriticalSectionToUse >::add | ( | const ElementType & | newElement | ) |

noexcept

Adds a new element to the set, (as long as it's not already in there).

Note that if a matching element already exists, the new value will be assigned to the existing one using operator=, so that if there are any differences between the objects which were not recognised by the object's operator==, then the set will always contain a copy of the most recently added one.

Parameters

  newElement | the new object to add to the set

Returns
  true if the value was added, or false if it already existed

See also
  set, insert, addIfNotAlreadyThere, addSorted, addSet, addArray

Referenced by SortedSet< ColourSetting >::addArray().

## addArray()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::addArray | ( | const ElementType * | elementsToAdd,
|  | int | numElementsToAdd
| ) |  |

noexcept

Adds elements from an array to this set.

Parameters

  elementsToAdd | the array of elements to add
  numElementsToAdd | how many elements are in this other array

See also
  add

Referenced by SortedSet< ColourSetting >::addSet().

## addSet()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherSetType >

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::addSet | ( | const OtherSetType & | setToAddFrom,
|  | int | startIndex = `0`,
|  | int | numElementsToAdd = `-1`
| ) |  |

noexcept

Adds elements from another set to this one.

Parameters

  setToAddFrom | the set from which to copy the elements
  startIndex | the first element of the other set to start copying from
  numElementsToAdd | how many elements to add from the other set. If this value is negative or greater than the number of available elements, all available elements will be copied.

See also
  add

## remove()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

ElementType SortedSet< ElementType, TypeOfCriticalSectionToUse >::remove | ( | const int | indexToRemove | ) |

noexcept

Removes an element from the set.

This will remove the element at a given index. If the index passed in is out-of-range, nothing will happen.

Parameters

  indexToRemove | the index of the element to remove

Returns
  the element that has been removed

See also
  removeValue, removeRange

Referenced by SortedSet< ColourSetting >::removeValuesIn(), and SortedSet< ColourSetting >::removeValuesNotIn().

## removeValue()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::removeValue | ( | const ElementType | valueToRemove | ) |

noexcept

Removes an item from the set.

This will remove the given element from the set, if it's there.

Parameters

  valueToRemove | the object to try to remove

See also
  remove, removeRange

## removeValuesIn()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherSetType >

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::removeValuesIn | ( | const OtherSetType & | otherSet | ) |

noexcept

Removes any elements which are also in another set.

Parameters

  otherSet | the other set in which to look for elements to remove

See also
  removeValuesNotIn, remove, removeValue, removeRange

## removeValuesNotIn()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherSetType >

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::removeValuesNotIn | ( | const OtherSetType & | otherSet | ) |

noexcept

Removes any elements which are not found in another set.

Only elements which occur in this other set will be retained.

Parameters

  otherSet | the set in which to look for elements NOT to remove

See also
  removeValuesIn, remove, removeValue, removeRange

## swapWith()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>
template<class OtherSetType >

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::swapWith | ( | OtherSetType & | otherSet | ) |

noexcept

This swaps the contents of this array with those of another array.

If you need to exchange two arrays, this is vastly quicker than using copy-by-value because it just swaps their internal pointers.

## minimiseStorageOverheads()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::minimiseStorageOverheads | ( |  | ) |

noexcept

Reduces the amount of storage being used by the set.

Sets typically allocate slightly more storage than they need, and after removing elements, they may have quite a lot of unused space allocated. This method will reduce the amount of allocated storage to a minimum.

## ensureStorageAllocated()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

void SortedSet< ElementType, TypeOfCriticalSectionToUse >::ensureStorageAllocated | ( | const int | minNumElements | ) |

Increases the set's internal storage to hold a minimum number of elements.

Calling this before adding a large known number of elements means that the set won't have to keep dynamically resizing itself as the elements are added, and it'll therefore be more efficient.

## getLock()

template<class ElementType, class TypeOfCriticalSectionToUse = DummyCriticalSection>

const TypeOfCriticalSectionToUse& SortedSet< ElementType, TypeOfCriticalSectionToUse >::getLock | ( |  | ) | const

noexcept

Returns the CriticalSection that locks this array.

To lock, you can call getLock().enter() and getLock().exit(), or preferably use an object of ScopedLockType as an RAII lock for it.

Referenced by SortedSet< ColourSetting >::add(), SortedSet< ColourSetting >::addArray(), SortedSet< ColourSetting >::addSet(), SortedSet< ColourSetting >::removeValue(), SortedSet< ColourSetting >::removeValuesIn(), and SortedSet< ColourSetting >::removeValuesNotIn().

The documentation for this class was generated from the following file:

- juce_SortedSet.h