Template Class Set#
Defined in File set.hpp
Class Documentation#
-
template<typename ElementType_>
class Set# An unordered set of distinct elements of the same type. Elements are stored in ascending order and duplicates are removed.
For example, consider creating a set from the following array:
myArr = { 4, 4, 3, 7, 5, 2, 5, 6, 7, 1, 8, 9 } mySet = Set(myArr) // mySet -> Set(1, 2, 3, 4, 5, 6, 7, 8, 9)
- Template Parameters
ElementType_ – The type of the elements in the set
Public Types
-
using ElementType = ElementType_#
The type of the elements in the set.
-
using VectorType = std::vector<ElementType>#
The type of the underlying vector.
-
using VectorIterator = typename VectorType::iterator#
The type of the iterator for the underlying vector.
-
using VectorConstIterator = typename VectorType::const_iterator#
The type of the const iterator for the underlying vector.
Public Functions
-
Set() = default#
Default constructor.
-
template<typename ShapeType, typename StorageType>
inline Set(const array::ArrayContainer<ShapeType, StorageType> &arr)# Construct a set from an array.
In the case of multidimensional arrays, the elements are flattened. This means a 2D array will still result in a 1D set. Create a
Set<Set<...>>
if needed.
-
inline Set(const std::vector<ElementType> &data)#
Construct a set from a vector.
- Parameters
data – The vector to construct the set from
-
inline Set(const std::initializer_list<ElementType> &data)#
Construct a set from an initializer list.
- Parameters
data – The initializer list to construct the set from
-
inline int64_t size() const#
- Returns
Return the cardinality of the set \( |S| \)
-
inline const ElementType &operator[](int64_t index) const#
Access the n-th element of the set in ascending order (i.e. \( S_n \))
index
must be in the range \( [0, |S|) \)- Parameters
index – The index of the element to access
- Returns
Return the n-th element of the set
-
inline bool contains(const ElementType &val) const#
Check if the set contains a value ( \( \text{val} \in S \))
- Parameters
val – The value to check for
- Returns
True if the value is present, false otherwise
-
inline Set &insert(const ElementType &val)#
Insert a value into the set ( \( S \cup \{\text{val}\} \))
- Parameters
val – The value to insert
- Returns
Return a reference to the set
-
inline Set &insert(const std::vector<ElementType> &data)#
Insert an
std::vector
of values into the set ( \( S \leftarrow S \cup \text{data} \))Each element of the vector is inserted into the set.
- Parameters
data –
- Returns
Reference to the set
-
inline Set &insert(const std::initializer_list<ElementType> &data)#
Insert an initializer list of values into the set ( \( S \leftarrow S \cup \text{data} \))
- Parameters
data –
- Returns
Reference to the set
-
inline Set &operator+=(const ElementType &val)#
Insert an element into the set ( \( S \leftarrow S \cup \{\text{val}\} \))
See also
- Parameters
val – The value to insert
- Returns
Return a reference to the set
-
inline Set &operator+=(const std::vector<ElementType> &data)#
Insert an
std::vector
of values into the set ( \( S \leftarrow S \cup \text{data} \))See also
insert(const std::vector<ElementType> &data)
- Parameters
data –
- Returns
Reference to the set
-
inline Set &operator+=(const std::initializer_list<ElementType> &data)#
Insert an initializer list of values into the set ( \( S \leftarrow S \cup \text{data} \))
See also
insert(const std::initializer_list<ElementType> &data)
- Parameters
data –
- Returns
Reference to the set
-
inline Set operator+(const ElementType &val)#
Insert an element into the set and return the result ( \( R = S \cup \{\text{val}\} \))
- Parameters
val – The value to insert
- Returns
A new set \( R = S \cup \{\text{val}\} \)
-
inline Set operator+(const std::vector<ElementType> &data)#
Insert an
std::vector
of values into the set and return the result ( \( R = S \cup \text{data} \))- Parameters
data – The vector of values to insert
- Returns
A new set \( R = S \cup \text{data} \)
-
inline Set operator+(const std::initializer_list<ElementType> &data)#
Insert an initializer list of values into the set and return the result ( \( R = S \cup \text{data} \))
- Parameters
data – The initializer list of values to insert
- Returns
A new set \( R = S \cup \text{data} \)
-
inline Set &discard(const ElementType &val)#
Discard
val
from the set if it exists ( \( S \setminus \{\text{val}\} \))If
val
is not contained within the set, nothing happens.- Parameters
val – The value to discard
- Returns
A reference to the set
-
inline Set &discard(const std::vector<ElementType> &data)#
Discard an
std::vector
of values from the set ( \( S \setminus \text{data} \))If an element in
data
is not contained within the set, nothing happens.- Parameters
data – The vector of values to Discard
- Returns
A reference to the set
-
inline Set &discard(const std::initializer_list<ElementType> &data)#
Discard an initializer list of values from the set ( \( S \setminus \text{data} \))
If an element in
data
is not contained within the set, nothing happens.- Parameters
data – The initializer list of values to Discard
- Returns
A reference to the set
-
inline Set &remove(const ElementType &val)#
Remove
val
from the set ( \( S \setminus \{\text{val}\} \))If
val
is not contained within the set, an exception is thrown.- Parameters
val – The value to remove
- Returns
A reference to the set
-
inline Set &remove(const std::vector<ElementType> &data)#
Remove an
std::vector
of values from the set ( \( S \setminus \text{data} \))If an element in
data
is not contained within the set, an exception is thrown.- Parameters
data – The vector of values to remove
- Returns
A reference to the set
-
inline Set &remove(const std::initializer_list<ElementType> &data)#
Remove an initializer list of values from the set ( \( S \setminus \text{data} \))
If an element in
data
is not contained within the set, an exception is thrown.- Parameters
data – The initializer list of values to remove
- Returns
A reference to the set
-
inline Set &operator-=(const ElementType &val)#
Discard
val
from the set if it exists.See also
- Parameters
val – The value to discard
- Returns
A reference to the set
-
inline Set &operator-=(const std::vector<ElementType> &data)#
Discard an
std::vector
of values from the set.- Parameters
data – The vector of values to discard
- Returns
A reference to the set
-
inline Set &operator-=(const std::initializer_list<ElementType> &data)#
Discard an initializer list of values from the set.
- Parameters
data – The initializer list of values to discard
- Returns
A reference to the set
-
inline Set operator-(const ElementType &val)#
Discard
val
from the set if it exists and return the result.See also
- Parameters
val – The value to discard
- Returns
A new set \( R = S \setminus \{\text{val}\} \)
-
inline Set operator-(const std::vector<ElementType> &data)#
Discard an
std::vector
of values from the set and return the result.- Parameters
data – The vector of values to discard
- Returns
A new set \( R = S \setminus \text{data} \)
-
inline Set operator-(const std::initializer_list<ElementType> &data)#
Discard an initializer list of values from the set and return the result.
- Parameters
data – The initializer list of values to discard
- Returns
A new set \( R = S \setminus \text{data} \)
-
inline Set operator|(const Set &other) const#
Return the union of two sets ( \( R = S_1 \cup S_2 \))
\( \{ x : x \in S_1 \lor x \in S_2 \} \)
- Parameters
other – \( S_2 \)
- Returns
A new set \( R = S_1 \cup S_2 \)
-
inline Set operator&(const Set &other) const#
Return the intersection of two sets ( \( R = S_1 \cap S_2 \))
\( \{ x : x \in S_1 \land x \in S_2 \} \)
- Parameters
other – \( S_2 \)
- Returns
A new set \( R = S_1 \cap S_2 \)
-
inline Set operator^(const Set &other) const#
Return the symmetric difference of two sets ( \( R = S_1 \oplus S_2 \))
\( \{ x : x \in S_1 \oplus x \in S_2 \} \)
- Parameters
other – \( S_2 \)
- Returns
A new set \( R = S_1 \oplus S_2 \)
-
inline Set operator-(const Set &other) const#
Return the set difference of two sets ( \( R = S_1 \setminus S_2 \))
\( \{ x : x \in S_1 \land x \notin S_2 \} \)
- Parameters
other – \( S_2 \)
- Returns
A new set \( R = S_1 \setminus S_2 \)
-
inline auto begin() const#
- Returns
Iterator to the beginning of the set
-
inline auto end() const#
- Returns
Iterator to the end of the set
-
template<typename T, typename Char, typename Ctx>
inline void str(const fmt::formatter<T, Char> &format, Ctx &ctx) const# Used for formatting the set with {fmt}.
- Template Parameters
T – Formatter type
Char – Character type
Ctx – Context type
- Parameters
format –
formatter
instancectx –
format_context
instance
Protected Functions
-
inline void reserve(size_t elements)#
Reserve space in the underlying vector for
elements
elements.- Parameters
elements – The number of elements to reserve space for
-
inline void sort()#
Sort the underlying vector.
-
inline void prune()#
Remove duplicates from the underlying vector.
-
inline void pushBack(const ElementType &val)#
Add a value to the end of the set if it is known to be the largest element.
- Parameters
val –
-
inline void insert(VectorConstIterator insertLocation, VectorConstIterator begin, VectorConstIterator end)#
Insert a vector of values into a location in the set if they are known to be valid in that position.
- Parameters
insertLocation –
begin –
end –