Containers

CVectorSet

A sorted unique sequence with vector-style indexing and set operations.

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

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

insert(value) returns an iterator/bool pair; a duplicate returns {end(), false}. Preserve sorted order and uniqueness when using the mutable element references or storage accessors.

Jump to a declaration · 74

CVectorSet

template<typename T, class Allocator = std::allocator<T>> class CVectorSet

Types, constants & data

using Vector = CVector<T, Allocator>;
using value_type = T;
using iterator = typename Vector::iterator;
using const_iterator = typename Vector::const_iterator;
using reverse_iterator = typename Vector::reverse_iterator;
using const_reverse_iterator = typename Vector::const_reverse_iterator;
using reference = typename Vector::reference;
using const_reference = typename Vector::const_reference;
using allocator_type = typename Vector::allocator_type;
using indexed_type = reference;

Methods

CVectorSet

explicit CVectorSet(const Allocator& allocator = cContainerDefault<Allocator>());
CVectorSet(const CVectorSet& x);
CVectorSet(CVectorSet&& x);
template<CInputIterator InputIterator> CVectorSet(InputIterator first, InputIterator last, const Allocator& allocator = cContainerDefault<Allocator>());
CVectorSet(std::initializer_list<value_type> il);
CVectorSet(CBuffer& b);

Creates sorted unique storage. Initializer-list construction inserts and deduplicates values, but iterator-range construction copies the sequence directly: that range must already be sorted and unique. Buffer construction restores the stored sequence.

range

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

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

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_t size() const noexcept;

reserve

void reserve(size_t n);

Ensures vector capacity without changing the element count. Reallocation invalidates existing element references and iterators.

shrink_to_fit

void shrink_to_fit() noexcept(noexcept(v_.shrink_to_fit()));

Requests release of unused capacity. The underlying container may retain capacity, and storage relocation can invalidate existing references and iterators.

operator[]

reference operator[](size_t n);
const_reference operator[](size_t n) const;

Returns an element by zero-based position. The position must be valid; mutations through the returned reference must preserve sorted order and uniqueness.

at

const_reference at(size_t n) const;
reference at(size_t n);

Returns the element at a zero-based index. An invalid index raises COutOfRangeError.

uget

const T& uget(size_t n, const T& def) const;

Returns the indexed element, or def when the index is outside the container. The reference overload borrows either the element or the supplied fallback; it does not extend either lifetime.

front

reference front();
const_reference front() const;

Returns the first element by reference. The container must be nonempty.

back

reference back();
const_reference back() const;
reference back(size_t i);
const_reference back(size_t i) const;

Returns the last element, or the element i positions before it: back(0) is the last element. The container must contain the requested element.

data

value_type* data() noexcept requires(!CSame<T, bool>);
const value_type* data() const noexcept requires(!CSame<T, bool>);

view

std::span<T> view() noexcept requires(!CSame<T, bool>);
std::span<const T> view() const noexcept requires(!CSame<T, bool>);

Returns a borrowed span over the current elements. Keep the container alive and do not use the view after an operation that invalidates its storage.

vec

const Vector& vec() const;
Vector& vec();

Returns the underlying vector by reference. Any mutation must preserve the sorted, unique invariant expected by lookup and set operations.

find

iterator find(const T& x);
const_iterator find(const T& x) const;

Uses binary search to locate an equal value, returning end() when absent. The stored sequence must remain sorted.

has

bool has(const T& x) const;

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

insert

std::pair<iterator, bool> insert(const T& x);

Inserts while preserving sorted uniqueness and returns the inserted position with true. An existing value returns {end(), false}, rather than an iterator to that existing element.

operator<<

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

Inserts through push() and returns this set for chaining. Values remain sorted and unique.

push

void push(const T& x);

Inserts a value in sorted order if it is not already present.

append

void append(const CVectorSet& v);

Appends raw elements without sorting or removing duplicates. Use only when all appended values follow the existing values in sorted, unique order; use unite() for a general union.

orderedPush

void orderedPush(const T& x);

Appends directly without checking order or duplicates. The new value must follow all existing elements in sorted, unique order.

erase

iterator erase(iterator position);
void erase(const T& x);

Removes an element by iterator or by value. The iterator overload returns the following position; the value overload does nothing when absent. Later elements shift, invalidating references at and after the removal.

pop_back

void pop_back();

Removes the last element without returning it. The container must be nonempty; use popBack() to retain its value.

popBack

T popBack();

Removes and returns the last element. The container must be nonempty.

pop_front

void pop_front();

Removes the first element without returning it. The container must be nonempty; use popFront() to retain its value.

popFront

T popFront();

Removes and returns the first element. The container must be nonempty.

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).

resize

void resize(size_t size);
void resize(size_t size, const T& value);

Resizes the underlying vector. Growing it does not restore sorted order or uniqueness; callers must establish valid values before performing set operations.

intersects

bool intersects(const CVectorSet& s) const;

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

unite

CVectorSet& unite(const CVectorSet& v);

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

complement

CVectorSet& complement(const CVectorSet& v);

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

split

template<class R> CVectorSet split(R& rng, size_t m);

Randomly retains m selected elements in this set and returns the remaining elements. Sets with fewer than two elements are left unchanged and return an empty set; the random generator determines sampling behavior.

operator-

CVectorSet operator-(const CVectorSet& v) const;

Returns elements of this set that are absent from the argument, leaving both operands unchanged.

store

void store(CBuffer& b) const;

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

output

void output(std::ostream& ostr) const;

Writes comma-separated elements to the stream without the surrounding container brackets.

dump

cstr dump() const;

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

Free functions & types

Functions

operator<<

template<class T, class Allocator> std::ostream& operator<<(std::ostream& ostr, const CVectorSet<T, Allocator>& s);