Containers

CSet

An ordered collection of unique keys.

C++23 mc/CSet.h
#include <mc/CSet.h>

Standard-style names retain their familiar container meaning. The notes below explain lookup results, mutation, ownership, and Catalyst conveniences; only entirely obvious operations are left as declarations.

Exceptions escaping container operations are translated to CError. Direct iterator operations, element references, and calls through .std() follow the underlying type’s contracts.

Jump to a declaration · 80

CSet

template <class K, class Cmp = std::less<K>, class Alloc = std::allocator<K>> class CSet

Types, constants & data

using Set = std::set<K, Cmp, Alloc>;
using key_type = typename Set::key_type;
using value_type = typename Set::value_type;
using allocator_type = typename Set::allocator_type;
using reference = typename Set::reference;
using const_reference = typename Set::const_reference;
using pointer = typename Set::pointer;
using const_pointer = typename Set::const_pointer;
using size_type = typename Set::size_type;
using difference_type = typename Set::difference_type;
using iterator = typename Set::iterator;
using const_iterator = typename Set::const_iterator;
using node_type = typename Set::node_type;
using insert_return_type = typename Set::insert_return_type;
using key_compare = typename Set::key_compare;
using value_compare = typename Set::value_compare;
using reverse_iterator = typename Set::reverse_iterator;
using const_reverse_iterator = typename Set::const_reverse_iterator;

Methods

CSet

CSet() noexcept(std::is_nothrow_default_constructible_v<Set>);
explicit CSet(const key_compare& comp, const allocator_type& a = cContainerDefault<allocator_type>());
explicit CSet(const allocator_type& a);
template<CInputIterator I> CSet(I first, I last, const key_compare& comp = cContainerDefault<key_compare>(), const allocator_type& a = cContainerDefault<allocator_type>());
template<CInputIterator I> CSet(I first, I last, const allocator_type& a);
template<CContainerRange<value_type> R> CSet(std::from_range_t, R&& range, const key_compare& comp = cContainerDefault<key_compare>(), const allocator_type& a = cContainerDefault<allocator_type>());
template<CContainerRange<value_type> R> CSet(std::from_range_t, R&& range, const allocator_type& a);
CSet(std::initializer_list<value_type> values, const key_compare& comp = cContainerDefault<key_compare>(), const allocator_type& a = cContainerDefault<allocator_type>());
CSet(std::initializer_list<value_type> values, const allocator_type& a);
CSet(const CSet& other);
CSet(CSet&& other) noexcept(std::is_nothrow_move_constructible_v<Set>);
CSet(const CSet& other, const allocator_type& a);
CSet(CSet&& other, const allocator_type& a);
CSet(const Set& other);
CSet(Set&& other) noexcept(std::is_nothrow_move_constructible_v<Set>);
CSet(const Set& other, const allocator_type& a);
CSet(Set&& other, const allocator_type& a);
CSet(CBuffer& b);

Creates an empty container or copies entries from a range, initializer list, or compatible container. Equivalent keys are stored once. The CBuffer overload restores the corresponding typed container format.

range

static CSet range(int64_t a, int64_t b);
static CSet range(size_t size);

Builds the consecutive values in [a, b), or [0, size). An empty interval produces an empty set.

operator=

CSet& operator=(const CSet& s);
CSet& operator=(const Set& s);
CSet& operator=(CSet&& s) noexcept(std::is_nothrow_move_assignable_v<Set>);
CSet& operator=(Set&& s) noexcept(std::is_nothrow_move_assignable_v<Set>);
CSet& operator=(std::initializer_list<value_type> il);

begin

iterator begin() noexcept;
const_iterator begin() const noexcept;

cbegin

const_iterator cbegin() const noexcept;

end

iterator end() noexcept;
const_iterator end() const noexcept;

cend

const_iterator cend() const noexcept;

rbegin

reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;

crbegin

const_reverse_iterator crbegin() const noexcept;

rend

reverse_iterator rend() noexcept;
const_reverse_iterator rend() const noexcept;

crend

const_reverse_iterator crend() const noexcept;

span

cspan span() const noexcept;
cspan span(size_t start) const noexcept;
cspan span(size_t start, size_t endOffset) const noexcept;

Returns numeric indices from zero to size(), optionally excluding an initial or trailing portion. It does not return key/value pairs or an element view.

empty

bool empty() const noexcept;

size

size_type size() const noexcept;

max_size

size_type max_size() const noexcept;

operator[]

const key_type& operator[](size_t i) const;

Returns the element at a zero-based position in iteration order; it is not a key lookup. The position must be valid. For tree and hash sets, finding it requires walking the iterators.

first

const key_type& first() const;
const_reference first();

Returns the first element in iteration order; the set must be nonempty. For a hash set this is not necessarily the smallest value.

find

iterator find(const key_type& k);
const_iterator find(const key_type& k) const;
template<class Q> requires(CTransparent<key_compare>) iterator find(const Q& key);
template<class Q> requires(CTransparent<key_compare>) const_iterator find(const Q& key) const;

Returns an iterator to an equivalent key or element, or end() when none is present. This lookup does not insert a missing entry.

has

bool has(const key_type& x) const;

Reports whether an equivalent key or element is present, without inserting anything.

contains

bool contains(const key_type& x) const;
template<class Q> requires(CTransparent<key_compare>) bool contains(const Q& key) const;

Reports whether an equivalent key is present. This is the standard-style spelling of a membership lookup.

count

size_type count(const key_type& k) const;
template<class Q> requires(CTransparent<key_compare>) size_type count(const Q& key) const;

Returns the number of elements equivalent to the key. A unique-key container returns either zero or one.

lower_bound

iterator lower_bound(const key_type& k);
const_iterator lower_bound(const key_type& k) const;
template<class Q> requires(CTransparent<key_compare>) iterator lower_bound(const Q& key);
template<class Q> requires(CTransparent<key_compare>) const_iterator lower_bound(const Q& key) const;

Returns the first position whose key is not less than the requested key according to the ordering policy, or end().

upper_bound

iterator upper_bound(const key_type& k);
const_iterator upper_bound(const key_type& k) const;
template<class Q> requires(CTransparent<key_compare>) iterator upper_bound(const Q& key);
template<class Q> requires(CTransparent<key_compare>) const_iterator upper_bound(const Q& key) const;

Returns the first position whose key is greater than the requested key according to the ordering policy, or end().

equal_range

std::pair<iterator, iterator> equal_range(const key_type& k);
std::pair<const_iterator, const_iterator> equal_range(const key_type& k) const;
template<class Q> requires(CTransparent<key_compare>) std::pair<iterator, iterator> equal_range(const Q& key);
template<class Q> requires(CTransparent<key_compare>) std::pair<const_iterator, const_iterator> equal_range(const Q& key) const;

Returns the half-open iterator range of equivalent keys. A missing key produces an empty range.

insert

std::pair<iterator, bool> insert(const value_type& v);
std::pair<iterator, bool> insert(value_type&& v);
iterator insert(const_iterator position, const value_type& v);
iterator insert(const_iterator position, value_type&& v);
template<CInputIterator InputIterator> void insert(InputIterator first, InputIterator last);
void insert(std::initializer_list<value_type> il);
insert_return_type insert(node_type&& node);
iterator insert(const_iterator hint, node_type&& node);

Inserts entries without replacing an equivalent existing key. For a single value, the pair-returning overload gives an iterator to the existing or inserted entry and a boolean indicating insertion.

insert_range

template<CContainerRange<value_type> R> void insert_range(R&& range);

Inserts elements from a C++ range using the container’s duplicate-key policy. It does not clear the existing contents.

emplace

template <class... Args> std::pair<iterator, bool> emplace(Args&&... args);

Constructs an entry from forwarded arguments and attempts insertion. For unique keys, the pair-returning overload reports whether insertion took place; construction may occur even when the key already exists.

emplace_hint

template <class... Args> iterator emplace_hint(const_iterator position, Args&&... args);

Attempts emplacement using the supplied position as a lookup hint and returns an iterator to the result. The hint does not change the key ordering or duplicate policy.

operator<<

template<class S> CSet& operator<<(S&& x);

Inserts one element and returns this set for chaining. Equivalent elements are retained only once.

erase

iterator erase(iterator position) requires(!std::same_as<iterator, const_iterator>);
iterator erase(const_iterator position);
size_type erase(const key_type& k);
iterator erase(const_iterator first, const_iterator last);
template<class Q> requires(CTransparent<key_compare> && !std::is_convertible_v<Q, iterator> && !std::is_convertible_v<Q, const_iterator>) size_type erase(Q&& key);

Erases an iterator, a half-open iterator range, or all entries equivalent to a key. Iterator forms return the following iterator; key forms return the number removed.

clear

void clear() noexcept;

clearExcept

template<class S> void clearExcept(const S& s);

Removes entries whose keys are not present in s, as tested by s.has(key).

swap

void swap(CSet& s) noexcept(noexcept(s_.swap(s.s_)));

extract

node_type extract(const_iterator position);
node_type extract(const key_type& key);
template<class Q> requires(CTransparent<key_compare> && !std::is_convertible_v<Q, iterator> && !std::is_convertible_v<Q, const_iterator>) node_type extract(Q&& key);

Detaches one element into an owning node handle without copying its value. Key lookup returns an empty handle if absent; the node may be inserted into a compatible container.

replace

bool replace(const key_type& xf, const key_type& xr);
bool replace(const key_type& xf, key_type&& xr);

Removes xf and inserts xr if xf exists, returning whether removal took place. If xr was already present, the set becomes one element smaller.

merge

template<class C2> void merge(CSet<K, C2, Alloc>& source);
template<class C2> void merge(CSet<K, C2, Alloc>&& source);
template<class C2> void merge(std::set<K, C2, Alloc>& source);
template<class C2> void merge(std::set<K, C2, Alloc>&& source);
template<class C2> void merge(std::multiset<K, C2, Alloc>& source);
template<class C2> void merge(std::multiset<K, C2, Alloc>&& source);

Transfers nodes with keys not already in this set from the source. Duplicates remain in the source; node transfer requires compatible allocators.

intersect

void intersect(const CSet& s);

Replaces this set with the elements shared by both sets. The supplied set is unchanged.

intersects

bool intersects(const CSet& s) const;

Reports whether the sets share any element. The implementation forms an intersection and may allocate temporary storage.

unite

void unite(const CSet& s);

Adds the other set’s elements to this set, retaining each distinct value once. The supplied set is unchanged.

complement

void complement(const CSet& s);

Removes from this set every element present in the supplied set. This is the directional difference: this set minus the argument.

std

Set& std() noexcept;
const Set& std() const noexcept;

Returns a reference to the underlying container for interoperation. Changes affect this object directly; operations through that reference bypass Catalyst exception translation.

operator Set&

operator Set&() noexcept;

Borrows the underlying container for interoperability. This does not copy storage; references and iterators follow that container’s lifetime and invalidation rules.

operator const Set&

operator const Set&() const noexcept;

Borrows the underlying container for interoperability. This does not copy storage; references and iterators follow that container’s lifetime and invalidation rules.

store

void store(CBuffer& b) const;

Appends the container to a CBuffer; restore it with the buffer-taking constructor.

dump

cstr dump() const;

Returns the stream-formatted contents as a cstr. Use store() for binary serialization.

Free functions & types

Functions

operator==

template<class K, class C, class A> bool operator==(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

operator<

template<class K, class C, class A> bool operator<(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

operator!=

template<class K, class C, class A> bool operator!=(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

operator>

template<class K, class C, class A> bool operator>(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

operator>=

template<class K, class C, class A> bool operator>=(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

operator<=

template<class K, class C, class A> bool operator<=(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

swap

template <class K, class C, class A> void swap(CSet<K, C, A>& x, CSet<K, C, A>& y) noexcept(noexcept(x.swap(y)));

operator<<

template<class K, class C, class A> std::ostream& operator<<(std::ostream& ostr, const CSet<K, C, A>& s);

operator<=>

template<class K, class C, class A> auto operator<=>(const CSet<K, C, A>& x, const CSet<K, C, A>& y);

erase_if

template<class K, class C, class A, class Predicate> typename CSet<K, C, A>::size_type erase_if(CSet<K, C, A>& container, Predicate predicate);

Removes every element for which the predicate returns true and returns the number removed.

<deduction guide for CSet>

template<class... Args, class S = decltype(std::set(std::declval<Args>()...))> CSet(Args&&...) -> CSet< typename S::key_type, typename S::key_compare, typename S::allocator_type>;
template<class K, class C = std::less<std::remove_const_t<K>>, CAllocator A = std::allocator<K>> requires(!CAllocator<C>) CSet(std::initializer_list<K>, C = C(), A = A()) -> CSet<std::remove_const_t<K>, C, A>;
template<class K, CAllocator A> CSet(std::initializer_list<K>, A) -> CSet<std::remove_const_t<K>, std::less<std::remove_const_t<K>>, A>;

pmr

Types, constants & data

template<class K, class C = std::less<K>> using CSet = mc::CSet<K, C, std::pmr::polymorphic_allocator<K>>;