Containers

CMap

An ordered key–value container with unique keys and convenient lookup and merging.

C++23 mc/CMap.h
#include <mc/CMap.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 · 86

CMap

template<class K, class V, class Cmp = std::less<K>, class Alloc = std::allocator<std::pair<const K, V>>> class CMap

Types, constants & data

using Map = std::map<K, V, Cmp, Alloc>;
using key_type = typename Map::key_type;
using value_type = typename Map::value_type;
using allocator_type = typename Map::allocator_type;
using reference = typename Map::reference;
using const_reference = typename Map::const_reference;
using pointer = typename Map::pointer;
using const_pointer = typename Map::const_pointer;
using size_type = typename Map::size_type;
using difference_type = typename Map::difference_type;
using iterator = typename Map::iterator;
using const_iterator = typename Map::const_iterator;
using node_type = typename Map::node_type;
using insert_return_type = typename Map::insert_return_type;
using mapped_type = typename Map::mapped_type;
using key_compare = typename Map::key_compare;
using value_compare = typename Map::value_compare;
using reverse_iterator = typename Map::reverse_iterator;
using const_reverse_iterator = typename Map::const_reverse_iterator;
using indexed_type = V&;

Methods

CMap

CMap() noexcept(std::is_nothrow_default_constructible_v<Map>);
explicit CMap(const key_compare& comp, const allocator_type& a = cContainerDefault<allocator_type>());
explicit CMap(const allocator_type& a);
template<CInputIterator I> CMap(I first, I last, const key_compare& comp = cContainerDefault<key_compare>(), const allocator_type& a = cContainerDefault<allocator_type>());
template<CInputIterator I> CMap(I first, I last, const allocator_type& a);
template<CContainerRange<value_type> R> CMap(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> CMap(std::from_range_t, R&& range, const allocator_type& a);
CMap(std::initializer_list<value_type> values, const key_compare& comp = cContainerDefault<key_compare>(), const allocator_type& a = cContainerDefault<allocator_type>());
CMap(std::initializer_list<value_type> values, const allocator_type& a);
CMap(const CMap& other);
CMap(CMap&& other) noexcept(std::is_nothrow_move_constructible_v<Map>);
CMap(const CMap& other, const allocator_type& a);
CMap(CMap&& other, const allocator_type& a);
CMap(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.

operator=

CMap& operator=(const CMap& x);
CMap& operator=(CMap&& x) noexcept(std::is_nothrow_move_assignable_v<Map>);
CMap& 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[]

V& operator[](const key_type& x);
V& operator[](key_type&& k);

Returns the mapped value by reference, inserting a value-initialized value when the key is missing. Use at() or get() for lookup without insertion.

at

V& at(const key_type& k);
const V& at(const key_type& k) const;

Returns the mapped value by reference without insertion. A missing key raises COutOfRangeError.

get

V get(const key_type& x, const V& def) const;
V& get(const key_type& x, V& def);

Looks up a key without inserting it. The const overload returns a value copy or fallback; the mutable overload returns a reference to the stored value or the supplied fallback.

keys

CVector<K> keys() const;

Returns a new vector containing keys in this map’s iteration order. Hash-map order is unspecified.

find

iterator find(const key_type& x);
const_iterator find(const key_type& x) 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& x) 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& x);
const_iterator lower_bound(const key_type& x) 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& x);
const_iterator upper_bound(const key_type& x) 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& x);
std::pair<const_iterator, const_iterator> equal_range(const key_type& x) 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.

keyForValue

K keyForValue(const V& value);

Scans values and returns the key of the first equal value in iteration order. Raises CError if no value matches.

insert

std::pair<iterator, bool> insert(const value_type& x);
iterator insert(const_iterator position, const value_type& x);
template<class P> requires std::is_constructible_v<value_type, P&&> std::pair<iterator, bool> insert(P&& p);
template<CInputIterator InputIterator> void insert(InputIterator first, InputIterator last);
std::pair<iterator, bool> insert(value_type&& value);
iterator insert(const_iterator hint, value_type&& value);
template<class P> requires std::is_constructible_v<value_type, P&&> iterator insert(const_iterator hint, P&& value);
void insert(std::initializer_list<value_type> values);
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.

try_emplace

template<class... Args> std::pair<iterator, bool> try_emplace(const key_type& key, Args&&... args);
template<class... Args> iterator try_emplace(const_iterator hint, const key_type& key, Args&&... args);
template<class... Args> std::pair<iterator, bool> try_emplace(key_type&& key, Args&&... args);
template<class... Args> iterator try_emplace(const_iterator hint, key_type&& key, Args&&... args);

Constructs the mapped value only if the key is absent. An existing entry is preserved and forwarded value arguments are not moved from in that case.

insert_or_assign

template<class M> std::pair<iterator, bool> insert_or_assign(const key_type& key, M&& value);
template<class M> iterator insert_or_assign(const_iterator hint, const key_type& key, M&& value);
template<class M> std::pair<iterator, bool> insert_or_assign(key_type&& key, M&& value);
template<class M> iterator insert_or_assign(const_iterator hint, key_type&& key, M&& value);

Inserts a missing key or replaces the value of an existing key. The pair-returning overload reports insertion with true and replacement with false.

add

CMap& add(const K& k, const V& t);

Inserts or replaces a mapped value and returns this map for chaining. Unlike insert(), it overwrites an existing value.

init

template<class K2, class V2> V& init(K2&& k, V2&& dv);

Returns the existing mapped value by reference, or inserts dv for a missing key and returns the new value. Existing entries are left unchanged.

erase

iterator erase(iterator position);
size_type erase(const key_type& x);
iterator erase(const_iterator first, const_iterator last);
iterator erase(const_iterator position);
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.

take

V take(const key_type& k);
V take(const key_type& k, V def);

Moves out the mapped value and erases its key. A missing key raises CError, or returns the fallback when that overload is used.

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(CMap& m) noexcept(noexcept(m_.swap(m.m_)));

extract

node_type extract(const_iterator itr);
node_type extract(const key_type& x);
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.

merge

template<class C2> void merge(CMap<K, V, C2, Alloc>& source);
template<class C2> void merge(CMap<K, V, C2, Alloc>&& source);
template<class C2> void merge(CMultimap<K, V, C2, Alloc>& source);
template<class C2> void merge(CMultimap<K, V, C2, Alloc>&& source);
template<class C2> void merge(std::map<K, V, C2, Alloc>& source);
template<class C2> void merge(std::map<K, V, C2, Alloc>&& source);
template<class C2> void merge(std::multimap<K, V, C2, Alloc>& source);
template<class C2> void merge(std::multimap<K, V, C2, Alloc>&& source);

Transfers entries whose keys are absent here from the source. Conflicting entries stay in the source; existing mapped values are preserved.

innerMerge

void innerMerge(const CMap& m);

Adds entries whose keys are absent, preserving values already present.

outerMerge

void outerMerge(const CMap& m);

Copies incoming entries into this map, replacing values for keys that already exist. The source is unchanged; mapped values are assigned as whole values.

std

const Map& std() const noexcept;
Map& std() noexcept;

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

operator const Map&

operator const Map&() const noexcept;

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

operator Map&

operator Map&() 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 V, class C, class A> bool operator==(const CMap<K, V, C, A>& x, const CMap<K, V, C, A>& y);

operator!=

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

operator<

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

operator>

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

operator<=

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

operator>=

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

cOutputMap

template<class K, class V, class C, class A> inline void cOutputMap(std::ostream& ostr, const CMap<K, V, C, A>& m);

operator<<

template<class K, class V, class C, class A> std::ostream& operator<<(std::ostream& ostr, const CMap<K, V, C, A>& m);

operator<=>

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

swap

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

erase_if

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

Removes every element for which the predicate returns true and returns the number removed. Map predicates receive key/value entries.

<deduction guide for CMap>

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

pmr

Types, constants & data

template<class K, class V, class C = std::less<K>> using CMap = mc::CMap<K, V, C, std::pmr::polymorphic_allocator<std::pair<const K, V>>>;