General Utilities
Supporting Utilities
Introduction
Enum Flags
EnumFlags stores flag bits from a scoped enum in a small value object.
Use it when a public API needs a set of independent flags but should not accept arbitrary integers by accident.
Define flag enums as enum class values with an unsigned underlying type.
When you want to use the complement operator, add an All entry that contains every valid bit.
The complement is then bounded to All instead of the whole underlying integer type.
Basic Usage
Prefer defining a small alias and an explicit free operator| next to the enum:
enum class Mode : uint8_t {
Read = 1U << 0U,
Write = 1U << 1U,
Execute = 1U << 2U,
All = (1U << 0U) | (1U << 1U) | (1U << 2U),
};
using Modes = el::EnumFlags<Mode>;
[[nodiscard]] constexpr auto operator|(Mode left, Mode right) noexcept -> Modes {
return Modes{left} | right;
}
auto modes = Mode::Read | Mode::Write;
modes.set(Mode::Execute);
Raw Values
Use toRawValue() and
fromRawValue() at boundaries where raw bits are required.
Raw construction preserves all bits, including bits outside All.
This makes raw import explicit and avoids silently changing data from an external source.
Named Flag Wrappers
When a flag set needs domain-specific methods, define a named class using the optional CRTP parameter. Inherit the Core constructors so the wrapper retains the regular single-flag and initializer-list syntax:
class Modes : public el::EnumFlags<Mode, Modes> {
using Base = el::EnumFlags<Mode, Modes>;
public:
using Base::Base;
auto toString() const -> el::String;
};
The derived class must be nothrow default-constructible.
Bitwise operators, compound assignments and fromRawValue() return the derived type, so domain-specific methods
remain available on expression results.
Hash Helper
The hash helpers implement the small combining pattern used by Erbsland Core value types.
combineHash() merges two already-computed hash values.
advanceHash() hashes one argument with std::hash and combines it with the
current hash value.
createHash() starts with 0 and sequentially advances the hash for all
provided arguments.
Use it when a type’s hash depends on more than one stored value.
Only combineHash() is a compile-time helper.
advanceHash() and createHash() call
std::hash, whose call operator is not required to be constexpr in C++20.
CoGenerator
CoGenerator is a small C++20 coroutine generator for lazily yielding a
sequence of values.
It is intended for internal algorithms and compact public helpers where a full container would add unnecessary storage
or control-flow noise.
Generators are move-only and single-pass.
Consume one generator either with next() or with range iteration, but do
not mix both styles for the same instance.
Exceptions that escape from the coroutine body are rethrown when the consumer advances the generator.
Use next() when yielded values must be moved out of the coroutine frame,
for example for move-only values.
Range iteration exposes yielded values as const references while the iterator is positioned at the value.
CoTask
CoTask represents one eagerly started coroutine result. It is move-only and has
one consumer.
Poll isComplete() when integrating with non-coroutine code, inspect a completed value through result(), move it
out through takeResult(), or await and consume the task as an rvalue.
CoTask::run() moves bounded work onto the process-wide coroutine worker service.
This service is separate from stream native-I/O workers, and continuations resume without caller-thread affinity.
Exceptions are stored and rethrown by result retrieval or co_await.
Destroying an incomplete task requests cancellation; an already running callable finishes normally, but a cancelled
Erbsland Core coroutine does not continue past its next awaited completion.
CoAsyncGenerator
CoAsyncGenerator is the asynchronous counterpart to CoGenerator.
Its producer starts lazily and may combine co_yield with co_await.
Each co_await generator.next() produces an optional value, with an empty optional marking completion.
The generator is move-only and single-pass, and only one next() may be outstanding.
Exceptions from its producer are rethrown by next().
Use this type for parser, tokenizer, or stream sequences whose producer must suspend between values; keep
CoGenerator for simple synchronous pull sequences.
Results With Data
ResultWithData combines the success/failure state of
Result with a typed payload. Its optional second template argument selects the
Result -derived status base, preserving the specialized states and predicates while exposing data through data()
and takeData().
Convenience Collections
Interface
-
template<typename tValue>
class CoAsyncGenerator An asynchronous, single-pass coroutine generator.
Unlike
CoGenerator, advancing this generator is itself asynchronous. The producer may use bothco_yieldandco_await; consumers request values withco_await generator.next(). The generator is move-only and permits one outstandingnext()operation.- Template Parameters:
tValue – The yielded value type.
Public Types
-
using handle_type = std::coroutine_handle<promise_type>
The producer coroutine handle type.
Public Functions
-
CoAsyncGenerator() = default
Create an empty generator.
-
inline explicit CoAsyncGenerator(handle_type handle) noexcept
Create a generator from its coroutine handle.
- Parameters:
handle – The producer coroutine handle.
-
inline ~CoAsyncGenerator()
Destroy the producer coroutine.
-
inline CoAsyncGenerator(CoAsyncGenerator &&other) noexcept
Move ownership from another generator.
- Parameters:
other – The generator whose coroutine is transferred.
-
inline CoAsyncGenerator &operator=(CoAsyncGenerator &&other) noexcept
Replace this generator’s coroutine with another generator’s coroutine.
-
inline NextOperation next() noexcept
Request the next generated value.
- Returns:
An awaitable operation that produces the next value, or an empty optional at completion. Awaiting the returned operation rethrows exceptions that escaped from the producer coroutine.
-
template<typename tValue>
class CoGenerator A C++20 coroutine generator for yielding a single-pass sequence of values.
CoGeneratorowns the coroutine state and destroys it when the generator object is destroyed. Values can be consumed either withnext()or through range iteration. Do not mix both consumption styles for the same generator. The generator is move-only, and iterators remain valid only while the owning generator object is alive.- Template Parameters:
tValue – The value type yielded by the coroutine.
Public Types
-
using handle_type = std::coroutine_handle<promise_type>
The standard coroutine handle type.
-
using iterator = Iterator
The standard iterator type.
Public Functions
-
CoGenerator() = default
Create an empty generator.
-
inline explicit CoGenerator(handle_type handle) noexcept
Create a generator from a coroutine handle.
- Parameters:
handle – The coroutine handle this generator takes ownership of.
-
inline ~CoGenerator()
Destroy the owned coroutine state, if present.
-
inline CoGenerator(CoGenerator &&other) noexcept
Move a generator, transferring ownership of the coroutine state.
- Parameters:
other – The generator to move from.
-
inline CoGenerator &operator=(CoGenerator &&other) noexcept
Move-assign a generator, replacing any owned coroutine state.
- Parameters:
other – The generator to move from.
- Returns:
A reference to this generator.
-
inline std::optional<Value> next()
Consume the next yielded value.
- Throws:
Any – exception that escaped from the coroutine body.
- Returns:
The next value, or an empty optional if the coroutine has finished.
-
template<typename tValue>
class CoTask An eagerly started, single-consumer coroutine task.
CoTaskstores its result independently from the coroutine frame. Destroying an incomplete task requests cancellation; Erbsland Core coroutine awaiters observe that request at their next completion and unwind the coroutine. Continuations run on the thread that completes the awaited operation and have no caller-thread affinity.- Template Parameters:
tValue – The task result type.
Public Types
Public Functions
-
CoTask() = default
Create an empty task.
-
inline CoTask(CoTask &&other) noexcept
Move ownership from another task.
- Parameters:
other – The task whose state is transferred.
-
inline ~CoTask()
Request cancellation when this task is still incomplete.
-
inline bool isComplete() const noexcept
Test if this task completed.
- Returns:
trueif the task is empty or its coroutine completed.
-
inline const tValue &result() const
Access the completed result.
- Throws:
err::LogicError – If the task is empty, incomplete, or its result was consumed.
Any – exception produced by the coroutine.
- Returns:
A reference to the stored result without consuming it.
-
inline tValue takeResult()
Take the completed result.
- Throws:
err::LogicError – If the task is empty, incomplete, or its result was consumed.
Any – exception produced by the coroutine.
- Returns:
The moved task result.
-
inline void cancel() noexcept
Request cancellation of this task.
Running bounded work is allowed to finish; the coroutine unwinds instead of resuming user code afterward.
-
inline impl::CoTaskAwaiter<tValue> operator co_await() &&
Await and consume this task result.
- Throws:
err::LogicError – If this task is empty.
- Returns:
An awaiter for the result.
Public Static Functions
-
template<typename tFunction>
static inline CoTask run(tFunction function) Run a callable on the coroutine worker service.
- Template Parameters:
tFunction – The callable type.
- Parameters:
function – The callable to execute.
- Returns:
An eagerly started task for the callable result. Exceptions from the callable are stored in the task and rethrown when its result is observed.
-
template<impl::EnumFlagsEnum tEnum, typename tDerived = void>
class EnumFlags A safe value wrapper for scoped enum flags.
See: General Utilities
- Template Parameters:
tEnum – The scoped enum type with unsigned underlying type.
tDerived – Optional CRTP-derived result type. It must be nothrow default-constructible.
Public Types
Public Functions
-
constexpr EnumFlags() noexcept = default
Create an empty flag set.
-
inline constexpr EnumFlags(tEnum flag) noexcept
Create a flag set with one enum flag.
- Parameters:
flag – The flag to set.
-
inline constexpr EnumFlags(std::initializer_list<tEnum> flags) noexcept
Create a flag set from a list of enum flags.
- Parameters:
flags – The flags to set.
-
constexpr bool operator==(const EnumFlags &other) const noexcept = default
Test if two flag sets store the same raw bits.
-
inline constexpr Result operator&(EnumFlags other) const noexcept
Calculate the intersection of this flag set and another flag set.
- Parameters:
other – The other flag set.
- Returns:
The flags common to both sets.
-
inline constexpr Result operator&(tEnum flag) const noexcept
Calculate the intersection of this flag set and an enum flag.
- Parameters:
flag – The flag to intersect with.
- Returns:
The flags common to both sets.
-
inline constexpr Result &operator&=(EnumFlags other) noexcept
Keep only the bits also set in another flag set.
- Parameters:
other – The flag set to intersect with.
- Returns:
A reference to this flag set.
-
inline constexpr Result &operator&=(tEnum flag) noexcept
Keep only the bits also set in an enum flag.
- Parameters:
flag – The flag to intersect with.
- Returns:
A reference to this flag set.
-
inline constexpr Result operator|(EnumFlags other) const noexcept
Calculate the union of this flag set and another flag set.
- Parameters:
other – The other flag set.
- Returns:
The combined flags.
-
inline constexpr Result operator|(tEnum flag) const noexcept
Calculate the union of this flag set and an enum flag.
- Parameters:
flag – The flag to add.
- Returns:
The combined flags.
-
inline constexpr Result &operator|=(EnumFlags other) noexcept
Add all bits from another flag set.
- Parameters:
other – The flag set to add.
- Returns:
A reference to this flag set.
-
inline constexpr Result &operator|=(tEnum flag) noexcept
Add all bits from an enum flag.
- Parameters:
flag – The flag to add.
- Returns:
A reference to this flag set.
-
inline constexpr Result operator^(EnumFlags other) const noexcept
Calculate the exclusive union of this flag set and another flag set.
- Parameters:
other – The other flag set.
- Returns:
The flags set in exactly one of the two sets.
-
inline constexpr Result operator^(tEnum flag) const noexcept
Calculate the exclusive union of this flag set and an enum flag.
- Parameters:
flag – The flag to toggle.
- Returns:
The toggled flags.
-
inline constexpr Result &operator^=(EnumFlags other) noexcept
Toggle all bits from another flag set.
- Parameters:
other – The flag set to toggle.
- Returns:
A reference to this flag set.
-
inline constexpr Result &operator^=(tEnum flag) noexcept
Toggle all bits from an enum flag.
- Parameters:
flag – The flag to toggle.
- Returns:
A reference to this flag set.
-
inline constexpr auto operator~() const noexcept -> Result
Calculate the bounded complement of this flag set using
tEnum::Allas the valid bit domain.- Returns:
The inverted flags within the valid domain.
-
inline constexpr bool isEmpty() const noexcept
Test if this flag set is empty.
- Returns:
trueif no flags are set.
-
inline constexpr bool hasAny() const noexcept
Test if this flag set contains at least one raw bit.
- Returns:
trueif any flag is set.
-
inline constexpr bool isSet(tEnum flag) const noexcept
Test if all bits of an enum flag are set.
- Parameters:
flag – The flag to check.
- Returns:
trueif the flag is set.
-
inline constexpr bool isCleared(tEnum flag) const noexcept
Test if all bits of an enum flag are cleared.
- Parameters:
flag – The flag to check.
- Returns:
trueif the flag is cleared.
-
inline constexpr bool contains(EnumFlags flags) const noexcept
Test if all bits from another flag set are set.
- Parameters:
flags – The flags to check.
- Returns:
trueif all specified flags are set.
-
inline constexpr bool intersects(EnumFlags flags) const noexcept
Test if at least one bit from another flag set is set.
- Parameters:
flags – The flags to check.
- Returns:
trueif any of the specified flags are set.
-
inline constexpr void set(tEnum flag) noexcept
Set all bits from an enum flag.
- Parameters:
flag – The flag to set.
-
inline constexpr void set(EnumFlags flags) noexcept
Set all bits from another flag set.
- Parameters:
flags – The flags to set.
-
inline constexpr void clear(tEnum flag) noexcept
Clear all bits from an enum flag.
- Parameters:
flag – The flag to clear.
-
inline constexpr void clear(EnumFlags flags) noexcept
Clear all bits from another flag set.
- Parameters:
flags – The flags to clear.
-
inline constexpr void clear() noexcept
Clear all bits.
Public Static Functions
-
constexpr std::size_t erbsland::util::combineHash(const std::size_t hash1, const std::size_t hash2) noexcept
Combines two hash values into a single hash value.
This function uses a common hash combination algorithm (often attributed to Boost) to combine two
std::size_thash values into one.- Parameters:
hash1 – The first hash value.
hash2 – The second hash value.
- Returns:
The combined hash value.
-
template<typename T>
void erbsland::util::advanceHash(std::size_t &hash, const T &arg) noexcept Advances an existing hash value by hashing a new argument and combining it.
This function hashes the provided argument using
std::hashand then combines it with the current hash value usingcombineHash.- Template Parameters:
T – The type of the argument to hash.
- Parameters:
hash – The current hash value, which will be updated.
arg – The argument to be hashed and combined.
-
template<typename T1, typename ...Rest>
std::size_t erbsland::util::createHash(const T1 &arg1, const Rest&... rest) noexcept Creates a new hash value from one or more arguments.
Combines hashes of all arguments into a single value.
See: General Utilities
- Template Parameters:
T1 – The type of the first argument.
Rest – The types of the remaining arguments.
- Parameters:
arg1 – The first argument.
rest – The remaining arguments.
- Returns:
The combined hash value of all arguments.
-
template<typename tKey, typename tValue, typename tHash = std::hash<tKey>, typename tEqual = std::equal_to<tKey>, typename tSelf = void>
class HashMap A copy-on-write unordered key/value container with Erbsland-style access and algorithms.
See: General Utilities
- Template Parameters:
tKey – The key type.
tValue – The value type.
tHash – The key hash type.
tEqual – The key equality type.
tSelf – Internal CRTP type used by derived public hash map types.
Subclassed by erbsland::text::impl::StringHashMap< std::size_t >, erbsland::text::impl::StringHashMap< TlsConfigurationConstPtr >, erbsland::text::impl::StringHashMap< erbsland::event::EventId >, erbsland::text::impl::StringHashMap< erbsland::event::EventBackendId >, erbsland::text::impl::StringHashMap< erbsland::text::U8String >, erbsland::text::impl::StringHashMap< std::shared_ptr< const erbsland::text::U8Format > >, erbsland::text::impl::StringHashMap< erbsland::re::impl::LabelTargetWithSource >, erbsland::text::impl::StringHashMap< CaptureGroupIndex >, erbsland::text::impl::StringHashMap< erbsland::text::impl::StringHashMap >, erbsland::text::impl::StringHashMap< erbsland::system::UserName >, erbsland::text::impl::StringHashMap< erbsland::system::GroupName >, erbsland::text::impl::StringHashMap< erbsland::system::UserId >, erbsland::text::impl::StringHashMap< erbsland::system::GroupId >
Public Types
-
using Storage = mem::CowManualStorage<Raw>
The COW storage type.
Public Functions
-
HashMap()
Create an empty hash map.
-
explicit HashMap(std::initializer_list<Entry> values)
Create a hash map from an initializer list.
- Parameters:
values – The key-value pairs to insert.
-
explicit HashMap(const Raw &raw)
Create a hash map from a standard unordered map.
- Parameters:
raw – The map to copy.
-
explicit HashMap(Raw &&raw)
Create a hash map from a standard unordered map, taking ownership.
- Parameters:
raw – The map to move.
-
const Raw &toRawValue() const noexcept
Return a reference to the underlying standard unordered map.
- Returns:
The wrapped map.
-
std::map<Key, Value> toStdMap() const
Convert to a standard ordered map.
- Returns:
An ordered map with all entries.
-
Raw toStdUnorderedMap() const
Convert to a standard unordered map.
- Returns:
A copy of the wrapped map.
-
std::vector<Key> toStdKeyVector() const
Convert keys to a standard vector.
- Returns:
A vector with all keys.
-
std::vector<Entry> toStdVector() const
Convert entries to a standard vector.
- Returns:
A vector with all entries.
-
HashSet<Key, Hash, Equal> toKeyHashSet() const
Convert keys to a hash set.
- Returns:
A hash set with all keys.
-
HashSet<Value> toValueHashSet() const
Convert values to a hash set.
- Returns:
A hash set with all values.
-
template<typename Function>
Count countIf(Function function) const Count all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
The number of matching entries.
-
template<typename Function>
Count countIfKey(Function function) const Count all keys matching a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
The number of matching keys.
-
template<typename Function>
Count countIfValue(Function function) const Count all values matching a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
The number of matching values.
-
std::optional<Value> get(const Key &key) const
Get a value by key.
- Parameters:
key – The key to look up.
- Returns:
The value, or
std::nulloptif not found.
-
Value get(const Key &key, const Value &defaultValue) const
Get a value by key, with a default.
- Parameters:
key – The key to look up.
defaultValue – The value to return if not found.
- Returns:
The value, or the default.
-
Self &reserve(Count count)
Reserve capacity.
- Parameters:
count – The minimum capacity to reserve.
- Returns:
A reference to this map.
-
Self &swap(Self &other) noexcept
Swap with another map.
- Parameters:
other – The map to swap with.
- Returns:
A reference to this map.
-
Self &remove(const Key &key)
Remove an entry by key.
- Parameters:
key – The key to remove.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIf(Function function) Remove all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIfKey(Function function) Remove all entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIfValue(Function function) Remove all entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A reference to this map.
-
Self removed(const Key &key) const
Return a hash map without one key.
- Parameters:
key – The key to remove.
- Returns:
A new hash map without the key.
-
template<typename Function>
Self removedIf(Function function) const Return a hash map without entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A new hash map without matching entries.
-
template<typename Function>
Self removedIfKey(Function function) const Return a hash map without entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A new hash map without matching keys.
-
template<typename Function>
Self removedIfValue(Function function) const Return a hash map without entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A new hash map without matching values.
-
Value take(const Key &key)
Take a value by key.
- Parameters:
key – The key to take.
- Returns:
The value associated with the key.
-
template<typename Function>
Self takeIf(Function function) Take all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A hash map containing the taken entries.
-
template<typename Function>
Self takeIfKey(Function function) Take all entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A hash map containing the taken entries.
-
template<typename Function>
Self takeIfValue(Function function) Take all entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A hash map containing the taken entries.
-
template<typename Function>
LoopResult forEach(Function function) const Call a function for every entry.
- Parameters:
function – The function called with each entry.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachKey(Function function) const Call a function for every key.
- Parameters:
function – The function called with each key.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachValue(Function function) const Call a function for every value.
- Parameters:
function – The function called with each value.
- Returns:
The result of the iteration.
-
template<typename Function>
Self &mapValue(Function function) Replace every value with the result of a function.
- Parameters:
function – The function used to map values.
- Returns:
A reference to this map.
-
template<typename Function>
Self mappedValues(Function function) const Return a copy with every value mapped through a function.
- Parameters:
function – The function used to map values.
- Returns:
A mapped copy of the map.
-
template<typename tValueFwd>
Self &set(const Key &key, tValueFwd &&value) Set a key-value pair.
- Parameters:
key – The key.
value – The value.
- Returns:
A reference to this map.
-
template<typename tValueFwd>
Self &set(Key &&key, tValueFwd &&value) Set a key-value pair, taking ownership of the key.
- Parameters:
key – The key.
value – The value.
- Returns:
A reference to this map.
-
template<typename tValueFwd>
bool tryReplace(const Key &key, tValueFwd &&value) Try to replace an existing value.
- Parameters:
key – The key to replace.
value – The new value.
- Returns:
trueif the key existed and was replaced.
-
template<typename tValueFwd>
bool tryReplace(Key &&key, tValueFwd &&value) Try to replace an existing value, taking ownership of the key when replaced.
- Parameters:
key – The key to replace.
value – The new value.
- Returns:
trueif the key existed and was replaced.
-
template<typename tValueFwd>
bool tryInsert(const Key &key, tValueFwd &&value) Try to insert a new entry.
- Parameters:
key – The key.
value – The value.
- Returns:
trueif the key was not already present.
-
template<typename tValueFwd>
bool tryInsert(Key &&key, tValueFwd &&value) Try to insert a new entry, taking ownership of the key when inserted.
- Parameters:
key – The key.
value – The value.
- Returns:
trueif the key was not already present.
-
bool compare(const Self &other) const
Test if two maps contain the same entries.
- Parameters:
other – The map to compare with.
- Returns:
trueif both maps have the same key-value pairs.
-
bool compareKeys(const Self &other) const
Test if two maps contain the same keys.
- Parameters:
other – The map to compare with.
- Returns:
trueif both maps have the same keys.
-
bool contains(const Key &key) const
Test if the map contains a key.
- Parameters:
key – The key to search for.
- Returns:
trueif the key is present.
-
template<typename Function>
bool allOf(Function function) const Test if all entries match a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif all entries match.
-
template<typename Function>
bool anyOf(Function function) const Test if any entry matches a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif at least one entry matches.
-
template<typename Function>
bool noneOf(Function function) const Test if no entry matches a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif no entry matches.
-
const_iterator begin() const noexcept
Return an iterator to the first entry.
- Returns:
The begin iterator.
-
const_iterator end() const noexcept
Return an iterator past the last entry.
- Returns:
The end iterator.
-
template<typename tKey, typename tHash, typename tEqual, typename tSelf>
class HashSet A copy-on-write unordered key container with Erbsland-style access and set algorithms.
See: General Utilities
- Template Parameters:
tKey – The key type.
tHash – The key hash type.
tEqual – The key equality type.
tSelf – Internal CRTP type used by derived public hash set types.
Public Types
-
using Storage = mem::CowManualStorage<Raw>
The COW storage type.
Public Functions
-
HashSet()
Create an empty hash set.
-
explicit HashSet(std::initializer_list<Key> values)
Create a hash set from an initializer list.
- Parameters:
values – The keys to insert.
-
explicit HashSet(const Raw &raw)
Create a hash set from a standard unordered set.
- Parameters:
raw – The set to copy.
-
explicit HashSet(Raw &&raw)
Create a hash set from a standard unordered set, taking ownership.
- Parameters:
raw – The set to move.
-
const Raw &toRawValue() const noexcept
Return a reference to the underlying standard unordered set.
- Returns:
The wrapped set.
-
std::vector<Key> toStdVector() const
Convert to a standard vector.
- Returns:
A vector with all keys.
-
Raw toStdUnorderedSet() const
Convert to a standard unordered set.
- Returns:
A copy of the wrapped set.
-
template<typename Function>
Count countIf(Function function) const Count all keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
The number of matching keys.
-
Self &reserve(Count count)
Reserve capacity.
- Parameters:
count – The minimum capacity to reserve.
- Returns:
A reference to this set.
-
Self &swap(Self &other) noexcept
Swap with another set.
- Parameters:
other – The set to swap with.
- Returns:
A reference to this set.
-
Self &remove(const Key &key)
Remove a key.
- Parameters:
key – The key to remove.
- Returns:
A reference to this set.
-
bool tryRemove(const Key &key)
Try to remove a key.
- Parameters:
key – The key to remove.
- Returns:
trueif the key was present and removed.
-
template<typename Function>
Self &removeIf(Function function) Remove all keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
A reference to this set.
-
Self removed(const Key &key) const
Return a set with a key removed.
- Parameters:
key – The key to remove.
- Returns:
A new set without the key.
-
template<typename Function>
Self removedIf(Function function) const Return a set without keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
A new set without matching keys.
-
template<typename Function>
LoopResult forEach(Function function) const Call a function for every key.
- Parameters:
function – The function called for every key.
- Returns:
The result of the iteration.
-
Self &unite(const Self &other)
Unite with another set (union).
- Parameters:
other – The set to unite with.
- Returns:
A reference to this set.
-
Self &intersect(const Self &other)
Intersect with another set.
- Parameters:
other – The set to intersect with.
- Returns:
A reference to this set.
-
Self &subtract(const Self &other)
Subtract another set (difference).
- Parameters:
other – The set to subtract.
- Returns:
A reference to this set.
-
Self &symmetricDifference(const Self &other)
Compute the symmetric difference with another set.
- Parameters:
other – The set for the symmetric difference.
- Returns:
A reference to this set.
-
Self unitedWith(const Self &other) const
Return the union of this set and another.
- Parameters:
other – The set to unite with.
- Returns:
A new set containing all elements.
-
Self intersectedWith(const Self &other) const
Return the intersection of this set and another.
- Parameters:
other – The set to intersect with.
- Returns:
A new set containing common elements.
-
Self subtractedBy(const Self &other) const
Return the difference of this set from another.
- Parameters:
other – The set to subtract.
- Returns:
A new set with elements of
otherremoved.
-
Self symmetricDifferenceWith(const Self &other) const
Return the symmetric difference with another set.
- Parameters:
other – The set for the symmetric difference.
- Returns:
A new set with elements unique to either set.
-
Self &insert(const Key &key)
Insert a key.
- Parameters:
key – The key to insert.
- Returns:
A reference to this set.
-
Self &insert(Key &&key)
Insert a key, taking ownership of the value.
- Parameters:
key – The key to insert.
- Returns:
A reference to this set.
-
bool tryInsert(const Key &key)
Try to insert a key.
- Parameters:
key – The key to insert.
- Returns:
trueif the key was not already present.
-
bool tryInsert(Key &&key)
Try to insert a key, taking ownership of the value.
- Parameters:
key – The key to insert.
- Returns:
trueif the key was not already present.
-
bool compare(const Self &other) const
Test if two sets contain the same elements.
- Parameters:
other – The set to compare with.
- Returns:
trueif both sets contain the same keys.
-
bool contains(const Key &key) const
Test if the set contains a key.
- Parameters:
key – The key to search for.
- Returns:
trueif the key is present.
-
template<typename Function>
bool allOf(Function function) const Test if all keys match a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif all keys match.
-
template<typename Function>
bool anyOf(Function function) const Test if any key matches a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif at least one key matches.
-
template<typename Function>
bool noneOf(Function function) const Test if no key matches a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif no key matches.
-
bool isSubsetOf(const Self &other) const
Test if this set is a subset of another.
- Parameters:
other – The superset to compare with.
- Returns:
trueif all elements are inother.
-
bool isSupersetOf(const Self &other) const
Test if this set is a superset of another.
- Parameters:
other – The subset to compare with.
- Returns:
trueif all elements ofotherare in this set.
-
bool isDisjointWith(const Self &other) const
Test if this set has no elements in common with another.
- Parameters:
other – The set to compare with.
- Returns:
trueif the sets share no elements.
-
bool intersects(const Self &other) const
Test if this set shares any elements with another.
- Parameters:
other – The set to compare with.
- Returns:
trueif there is at least one common element.
-
const_iterator begin() const noexcept
Return an iterator to the first key.
- Returns:
The begin iterator.
-
const_iterator end() const noexcept
Return an iterator past the last key.
- Returns:
The end iterator.
Public Static Functions
-
template<typename tElement, typename tSelf>
class List A copy-on-write list container with Erbsland-style element access and algorithms.
See: General Utilities
- Template Parameters:
tElement – The element type stored in this list.
tSelf – Internal CRTP type used by derived public list types.
Subclassed by erbsland::text::impl::StringList< U32String >
Public Types
-
using Storage = mem::CowManualStorage<Raw>
The COW storage type.
Public Functions
-
List()
Create an empty list.
-
explicit List(std::initializer_list<Element> values)
Create a list from an initializer list.
- Parameters:
values – The elements to copy.
-
explicit List(Element value)
Create a list with a single element.
- Parameters:
value – The element to store.
-
List(Count count, const Element &value)
Create a list with repeated elements.
- Parameters:
count – The number of elements.
value – The element value to repeat.
-
explicit List(const Raw &raw)
Create a list from a standard vector.
- Parameters:
raw – The vector to copy.
-
explicit List(Raw &&raw)
Create a list from a standard vector, taking ownership.
- Parameters:
raw – The vector to move.
-
Self operator+(const Self &other) const
Concatenate two lists.
- Parameters:
other – The list to append.
- Returns:
A new list containing all elements.
-
Self operator+(const Element &value) const
Append an element to a list.
- Parameters:
value – The element to append.
- Returns:
A new list with the element added.
-
Self operator+(Element &&value) const
Append an element to a list, taking ownership of the value.
- Parameters:
value – The element to append.
- Returns:
A new list with the element added.
-
Self &operator+=(const Self &other)
Append another list to this list.
- Parameters:
other – The list to append.
- Returns:
A reference to this list.
-
Self &operator+=(const Element &value)
Append an element to this list.
- Parameters:
value – The element to append.
- Returns:
A reference to this list.
-
Self &operator+=(Element &&value)
Append an element to this list, taking ownership of the value.
- Parameters:
value – The element to append.
- Returns:
A reference to this list.
-
std::strong_ordering operator<=>(const Self &other) const
Compare two lists lexicographically.
- Parameters:
other – The list to compare with.
- Returns:
A three-way comparison result.
-
bool operator==(const Self &other) const
Test two lists for equality.
- Parameters:
other – The list to compare with.
- Returns:
trueif both lists have the same elements in the same order.
-
const Raw &toRawValue() const noexcept
Return a reference to the underlying standard vector.
- Returns:
The wrapped vector.
-
Raw toStdVector() const
Convert to a standard vector.
- Returns:
A copy of the list as a
std::vector.
-
std::set<Element> toStdSet() const
Convert to a standard set.
- Returns:
A copy of the list as a
std::set.
-
template<typename Function>
Count countIf(Function function) const Count all elements matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
The number of matching elements.
-
Element get(Index index) const
Get an element by index.
- Parameters:
index – The element index.
- Returns:
A copy of the element.
-
Element get(Index index, const Element &defaultValue) const
Get an element by index, with a default value.
- Parameters:
index – The element index.
defaultValue – The value to return if the index is out of range.
- Returns:
The element, or the default value.
-
const Element &getRef(Index index) const
Get a const reference to an element or a shared default element.
A stored-element reference remains valid only until this list is modified or destroyed. The immutable default element returned for an invalid index has static lifetime.
- Parameters:
index – The element index.
- Returns:
A const reference to the element, or to a default-constructed element if
indexis invalid.
-
const Element &getRefOrThrow(Index index) const
Get a const reference to an element by index.
The returned reference is borrowed from this list and remains valid only until this list is modified or destroyed.
- Parameters:
index – The element index.
- Throws:
err::OutOfRangeError – If
indexis invalid or outside this list.- Returns:
A const reference to the element.
-
Self &set(Index index, const Element &value)
Set an element at a given index.
- Parameters:
index – The element index.
value – The new element value.
- Returns:
A reference to this list.
-
Self &set(Index index, Element &&value)
Set an element at a given index, taking ownership of the value.
- Parameters:
index – The element index.
value – The new element value.
- Returns:
A reference to this list.
-
Self &resize(Count count)
Resize the list.
- Parameters:
count – The new element count.
- Returns:
A reference to this list.
-
Self &resize(Count count, const Element &value)
Resize the list, filling new elements with a value.
- Parameters:
count – The new element count.
value – The value for new elements.
- Returns:
A reference to this list.
-
Self &reserve(Count count)
Reserve capacity.
- Parameters:
count – The minimum capacity to reserve.
- Returns:
A reference to this list.
-
Self &swap(Self &other) noexcept
Swap with another list.
- Parameters:
other – The list to swap with.
- Returns:
A reference to this list.
-
std::pair<Element, Self> sliceFirst() const
Split the list into first element and the rest.
- Returns:
A pair of the first element and the remaining list.
-
std::pair<Element, Self> sliceLast() const
Split the list into the last element and the rest.
- Returns:
A pair of the last element and the remaining list.
-
Self slice(Range range) const
Extract a sub-list from a range.
- Parameters:
range – The element range to extract.
- Returns:
A new list containing the sliced elements.
-
Self prefix(Count count) const
Get the first
countelements.- Parameters:
count – The number of elements to take.
- Returns:
A new list with the prefix.
-
Self suffix(Count count) const
Get the last
countelements.- Parameters:
count – The number of elements to take.
- Returns:
A new list with the suffix.
-
Self &remove(Index index)
Remove an element at a given index.
- Parameters:
index – The index to remove.
- Returns:
A reference to this list.
-
Self &remove(Range range)
Remove elements in a range.
- Parameters:
range – The element range to remove.
- Returns:
A reference to this list.
-
template<typename Function>
Self &removeIf(Function function) Remove all elements matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
A reference to this list.
-
Self removed(Index index) const
Return a copy with one element removed.
- Parameters:
index – The index to remove.
- Returns:
A new list without the element.
-
Self removed(Range range) const
Return a copy with a range removed.
- Parameters:
range – The element range to remove.
- Returns:
A new list without the range.
-
template<typename Function>
Self removedIf(Function function) const Return a copy without elements matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
A new list without matching elements.
-
Element take(Index index)
Take an element at a given index.
- Parameters:
index – The index to take from.
- Returns:
The element at the index.
-
Self take(Range range)
Take all elements in a range.
- Parameters:
range – The range to remove and return.
- Returns:
A list containing the taken elements.
-
template<typename Function>
Self takeIf(Function function) Take all elements matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
A list containing the taken elements.
-
template<typename Function>
LoopResult forEach(Function function) const Call a function for every element.
- Parameters:
function – The function called as
function(value)orfunction(value, index). If both forms are supported, the indexed form is used.- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachReverse(Function function) const Call a function for every element in reverse order.
- Parameters:
function – The function called as
function(value)orfunction(value, index). If both forms are supported, the indexed form is used.- Returns:
The result of the iteration.
-
template<typename Function>
Self &map(Function function) Replace every element with the result of a function.
- Parameters:
function – The function used to map elements.
- Returns:
A reference to this list.
-
template<typename Function>
Self mapped(Function function) const Return a copy with every element mapped through a function.
- Parameters:
function – The function used to map elements.
- Returns:
A mapped copy of the list.
-
Self collapsed() const
Return a copy with consecutive duplicate elements collapsed.
- Returns:
A new list with duplicate runs collapsed.
-
template<typename Function>
Self &sort(Function function) Sort the list in place using a comparison function.
- Parameters:
function – The comparison function.
- Returns:
A reference to this list.
-
template<typename Function>
Self sorted(Function function) const Return a sorted copy using a comparison function.
- Parameters:
function – The comparison function.
- Returns:
A new sorted list.
-
Index findFirst(const Element &value) const
Find the first occurrence of a value.
- Parameters:
value – The value to search for.
- Returns:
The index of the first occurrence.
-
Index findFirst(const Element &value, Index start) const
Find the first occurrence of a value starting from an index.
- Parameters:
value – The value to search for.
start – The index to start searching from.
- Returns:
The index of the first occurrence.
-
template<typename Function>
Index findFirstIf(Function function) const Find the first element matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
The index of the first matching element.
-
template<typename Function>
Index findFirstIf(Function function, Index start) const Find the first element matching a predicate starting from an index.
- Parameters:
function – The predicate called for every element.
start – The index to start searching from.
- Returns:
The index of the first matching element.
-
Index findLast(const Element &value) const
Find the last occurrence of a value.
- Parameters:
value – The value to search for.
- Returns:
The index of the last occurrence.
-
Index findLast(const Element &value, Index start) const
Find the last occurrence of a value starting backwards from an index.
- Parameters:
value – The value to search for.
start – The index to start searching from.
- Returns:
The index of the last occurrence.
-
template<typename Function>
Index findLastIf(Function function) const Find the last element matching a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
The index of the last matching element.
-
template<typename Function>
Index findLastIf(Function function, Index start) const Find the last element matching a predicate starting backwards from an index.
- Parameters:
function – The predicate called for every element.
start – The index to start searching from.
- Returns:
The index of the last matching element.
-
Self &insert(Index index, const Element &value)
Insert an element at a given index.
- Parameters:
index – The index to insert at.
value – The element to insert.
- Returns:
A reference to this list.
-
Self &insert(Index index, Element &&value)
Insert an element at a given index, taking ownership of the value.
- Parameters:
index – The index to insert at.
value – The element to insert.
- Returns:
A reference to this list.
-
Self &insert(Index index, const Self &other)
Insert another list at a given index.
- Parameters:
index – The index to insert at.
other – The list to insert.
- Returns:
A reference to this list.
-
Self &append(const Element &value)
Append an element.
- Parameters:
value – The element to append.
- Returns:
A reference to this list.
-
Self &append(Element &&value)
Append an element, taking ownership of the value.
- Parameters:
value – The element to append.
- Returns:
A reference to this list.
-
Self &append(const Self &other)
Append another list.
- Parameters:
other – The list to append.
- Returns:
A reference to this list.
-
Self &prepend(const Element &value)
Prepend an element.
- Parameters:
value – The element to prepend.
- Returns:
A reference to this list.
-
Self &prepend(Element &&value)
Prepend an element, taking ownership of the value.
- Parameters:
value – The element to prepend.
- Returns:
A reference to this list.
-
Self &prepend(const Self &other)
Prepend another list.
- Parameters:
other – The list to prepend.
- Returns:
A reference to this list.
-
bool isEmpty() const
Test if this list is empty.
- Returns:
trueif the list has no elements.
-
std::strong_ordering compare(const Self &other) const
Compare two lists lexicographically.
- Parameters:
other – The list to compare with.
- Returns:
A three-way comparison result.
-
bool contains(const Element &value) const
Test if the list contains a value.
- Parameters:
value – The value to search for.
- Returns:
trueif the value is present.
-
template<typename Function>
bool allOf(Function function) const Test if all elements match a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
trueif all elements match.
-
template<typename Function>
bool anyOf(Function function) const Test if any element matches a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
trueif at least one element matches.
-
template<typename Function>
bool noneOf(Function function) const Test if no element matches a predicate.
- Parameters:
function – The predicate called for every element.
- Returns:
trueif no element matches.
-
const_iterator begin() const noexcept
Return an iterator to the first element.
- Returns:
The begin iterator.
-
const_iterator end() const noexcept
Return an iterator past the last element.
- Returns:
The end iterator.
Friends
-
inline friend Self operator+(const Element &value, const Self &list)
Prepend an element to a list.
- Parameters:
value – The element to prepend.
list – The list to prepend to.
- Returns:
A new list with the element added at the front.
-
enum class erbsland::util::LoopResult : uint8_t
A generic result why a loop has ended.
Values:
-
enumerator Success
The main end condition was reached.
for strings: the end of the string was reached.
for parsers: the end condition was reached.
-
enumerator Stopped
The read function requested a stop before the end condition was reached.
-
enumerator LimitReached
A maximum number of iterations/chars was reached, but the loop could potentially continue.
-
enumerator EndOfData
The end of the data was reached.
-
enumerator Error
The loop function reported an error.
-
enumerator Success
-
enum class erbsland::util::LoopStatus : uint8_t
The reported status of a loop function.
Values:
-
enumerator Continue
Continue looping.
-
enumerator Stop
Stop the loop early in a regular way.
-
enumerator Error
Stop the loop, reporting an error.
-
enumerator Continue
-
template<typename tKey, typename tValue, typename tCompare = std::less<tKey>, typename tSelf = void>
class Map A copy-on-write ordered key/value container with Erbsland-style access and algorithms.
See: General Utilities
- Template Parameters:
tKey – The key type.
tValue – The value type.
tCompare – The key comparison type.
tSelf – Internal CRTP type used by derived public map types.
Subclassed by erbsland::text::impl::StringMap< text::String >, erbsland::text::impl::StringMap< std::optional< erbsland::text::U8String > >, erbsland::text::impl::StringMap< Value >, erbsland::text::impl::StringMap< FilterFn >, erbsland::text::impl::StringMap< ConstCompiledBlockPtr >, erbsland::text::impl::StringMap< ConstBlockChainPtr >, erbsland::text::impl::StringMap< bool >, erbsland::text::impl::StringMap< ConstCompiledLayoutPtr >
Public Types
-
using Storage = mem::CowManualStorage<Raw>
The COW storage type.
Public Functions
-
Map()
Create an empty map.
-
explicit Map(std::initializer_list<Entry> values)
Create a map from an initializer list.
- Parameters:
values – The key-value pairs to insert.
-
explicit Map(Raw &&raw)
Create a map from a standard map, taking ownership.
- Parameters:
raw – The map to move.
-
const Raw &toRawValue() const noexcept
Return a reference to the underlying standard map.
- Returns:
The wrapped map.
-
std::unordered_map<Key, Value> toStdUnorderedMap() const
Convert to a standard unordered map.
- Returns:
An unordered map with all entries.
-
std::vector<Key> toStdKeyVector() const
Convert keys to a standard vector.
- Returns:
A vector with all keys.
-
std::vector<Entry> toStdVector() const
Convert entries to a standard vector.
- Returns:
A vector with all entries.
-
HashSet<Value> toValueHashSet() const
Convert values to a hash set.
- Returns:
A hash set with all values.
-
template<typename Function>
Count countIf(Function function) const Count all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
The number of matching entries.
-
template<typename Function>
Count countIfKey(Function function) const Count all keys matching a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
The number of matching keys.
-
template<typename Function>
Count countIfValue(Function function) const Count all values matching a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
The number of matching values.
-
std::optional<Value> get(const Key &key) const
Get a value by key.
- Parameters:
key – The key to look up.
- Returns:
The value, or
std::nulloptif not found.
-
Value get(const Key &key, const Value &defaultValue) const
Get a value by key, with a default.
- Parameters:
key – The key to look up.
defaultValue – The value to return if not found.
- Returns:
The value, or the default.
-
Self &reserve(Count count)
Reserve capacity.
- Parameters:
count – The minimum capacity to reserve.
- Returns:
A reference to this map.
-
Self &swap(Self &other) noexcept
Swap with another map.
- Parameters:
other – The map to swap with.
- Returns:
A reference to this map.
-
Self &remove(const Key &key)
Remove an entry by key.
- Parameters:
key – The key to remove.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIf(Function function) Remove all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIfKey(Function function) Remove all entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A reference to this map.
-
template<typename Function>
Self &removeIfValue(Function function) Remove all entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A reference to this map.
-
Self removed(const Key &key) const
Return a map without one key.
- Parameters:
key – The key to remove.
- Returns:
A new map without the key.
-
template<typename Function>
Self removedIf(Function function) const Return a map without entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A new map without matching entries.
-
template<typename Function>
Self removedIfKey(Function function) const Return a map without entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A new map without matching keys.
-
template<typename Function>
Self removedIfValue(Function function) const Return a map without entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A new map without matching values.
-
Value take(const Key &key)
Take a value by key.
- Parameters:
key – The key to take.
- Returns:
The value associated with the key.
-
template<typename Function>
Self takeIf(Function function) Take all entries matching a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
A map containing the taken entries.
-
template<typename Function>
Self takeIfKey(Function function) Take all entries whose key matches a predicate.
- Parameters:
function – The predicate called with each key.
- Returns:
A map containing the taken entries.
-
template<typename Function>
Self takeIfValue(Function function) Take all entries whose value matches a predicate.
- Parameters:
function – The predicate called with each value.
- Returns:
A map containing the taken entries.
-
template<typename Function>
LoopResult forEach(Function function) const Call a function for every entry.
- Parameters:
function – The function called with each entry.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachKey(Function function) const Call a function for every key.
- Parameters:
function – The function called with each key.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachValue(Function function) const Call a function for every value.
- Parameters:
function – The function called with each value.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachReverse(Function function) const Call a function for every entry in reverse key order.
- Parameters:
function – The function called with each entry.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachKeyReverse(Function function) const Call a function for every key in reverse order.
- Parameters:
function – The function called with each key.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachValueReverse(Function function) const Call a function for every value in reverse key order.
- Parameters:
function – The function called with each value.
- Returns:
The result of the iteration.
-
template<typename Function>
Self &mapValue(Function function) Replace every value with the result of a function.
- Parameters:
function – The function used to map values.
- Returns:
A reference to this map.
-
template<typename Function>
Self mappedValues(Function function) const Return a copy with every value mapped through a function.
- Parameters:
function – The function used to map values.
- Returns:
A mapped copy of the map.
-
template<typename tValueFwd>
Self &set(const Key &key, tValueFwd &&value) Set a key-value pair.
- Parameters:
key – The key.
value – The value.
- Returns:
A reference to this map.
-
template<typename tValueFwd>
Self &set(Key &&key, tValueFwd &&value) Set a key-value pair, taking ownership of the key.
- Parameters:
key – The key.
value – The value.
- Returns:
A reference to this map.
-
template<typename tValueFwd>
bool tryReplace(const Key &key, tValueFwd &&value) Try to replace an existing value.
- Parameters:
key – The key to replace.
value – The new value.
- Returns:
trueif the key existed and was replaced.
-
template<typename tValueFwd>
bool tryReplace(Key &&key, tValueFwd &&value) Try to replace an existing value, taking ownership of the key when replaced.
- Parameters:
key – The key to replace.
value – The new value.
- Returns:
trueif the key existed and was replaced.
-
template<typename tValueFwd>
bool tryInsert(const Key &key, tValueFwd &&value) Try to insert a new entry.
- Parameters:
key – The key.
value – The value.
- Returns:
trueif the key was not already present.
-
template<typename tValueFwd>
bool tryInsert(Key &&key, tValueFwd &&value) Try to insert a new entry, taking ownership of the key when inserted.
- Parameters:
key – The key.
value – The value.
- Returns:
trueif the key was not already present.
-
bool compare(const Self &other) const
Test if two maps contain the same entries.
- Parameters:
other – The map to compare with.
- Returns:
trueif both maps have the same key-value pairs.
-
bool compareKeys(const Self &other) const
Test if two maps contain the same keys.
- Parameters:
other – The map to compare with.
- Returns:
trueif both maps have the same keys.
-
bool contains(const Key &key) const
Test if the map contains a key.
- Parameters:
key – The key to search for.
- Returns:
trueif the key is present.
-
template<typename Function>
bool allOf(Function function) const Test if all entries match a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif all entries match.
-
template<typename Function>
bool anyOf(Function function) const Test if any entry matches a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif at least one entry matches.
-
template<typename Function>
bool noneOf(Function function) const Test if no entry matches a predicate.
- Parameters:
function – The predicate called with each entry.
- Returns:
trueif no entry matches.
-
const_iterator begin() const noexcept
Return an iterator to the first entry.
- Returns:
The begin iterator.
-
const_iterator end() const noexcept
Return an iterator past the last entry.
- Returns:
The end iterator.
-
class Result
A type safe, extendable result type.
Using
Resultinstead ofboolmakes user code more readable and easier to understand.Resolves to one byte, same size as
bool.May be extended to transport more states and more information.
Supports multiple success and failure states. Internally uses a single byte to store the result value. Success values are in the range [0, 127], failure values are in the range [128, 255], counting down from 255. The first success value is therefore 0, and the last success value is 127. Read the documentation for more information how to write custom result types.
Subclassed by erbsland::cterm::ReadLineStatus, erbsland::network::NetworkSendStatus, erbsland::path::PathWalkResult, erbsland::stream::StreamCloseStatus, erbsland::stream::StreamPositionStatus, erbsland::stream::StreamReadStatus, erbsland::stream::StreamWaitStatus, erbsland::stream::StreamWriteStatus, erbsland::text::TextWalkResult, erbsland::util::ResultWithData< tData, tStatus >
Public Functions
-
inline constexpr Result(const Value value)
Create a new result.
- Parameters:
value – The value of the result.
-
inline constexpr bool isSuccessful() const
Test if the call was successful.
-
inline constexpr bool isFailure() const
Test if the call has failed.
Public Static Attributes
-
template<typename tData, typename tStatus = Result>
class ResultWithData : public erbsland::util::Result A result that transports additional typed data.
The result derives from its status type and keeps its success/failure semantics.
- Template Parameters:
tData – The transported data type.
tStatus – The result status type.
Public Types
Public Functions
-
inline ResultWithData(const Status result, const Data &data)
Create a result with copied data.
- Parameters:
result – The result status.
data – The data to transport.
-
template<typename tKey, typename tCompare, typename tSelf>
class Set A copy-on-write ordered key container with Erbsland-style access and set algorithms.
See: General Utilities
- Template Parameters:
tKey – The key type.
tCompare – The key comparison type.
tSelf – Internal CRTP type used by derived public set types.
Public Types
-
using Storage = mem::CowManualStorage<Raw>
The COW storage type.
Public Functions
-
Set()
Create an empty set.
-
explicit Set(std::initializer_list<Key> values)
Create a set from an initializer list.
- Parameters:
values – The keys to insert.
-
explicit Set(Raw &&raw)
Create a set from a standard set, taking ownership.
- Parameters:
raw – The set to move.
-
const Raw &toRawValue() const noexcept
Return a reference to the underlying standard set.
- Returns:
The wrapped set.
-
std::vector<Key> toStdVector() const
Convert to a standard vector.
- Returns:
A vector with all keys.
-
std::unordered_set<Key> toStdUnorderedSet() const
Convert to a standard unordered set.
- Returns:
An unordered set with all keys.
-
template<typename Function>
Count countIf(Function function) const Count all keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
The number of matching keys.
-
Self &reserve(Count count)
Reserve capacity.
- Parameters:
count – The minimum capacity to reserve.
- Returns:
A reference to this set.
-
Self &swap(Self &other) noexcept
Swap with another set.
- Parameters:
other – The set to swap with.
- Returns:
A reference to this set.
-
Self &remove(const Key &key)
Remove a key.
- Parameters:
key – The key to remove.
- Returns:
A reference to this set.
-
bool tryRemove(const Key &key)
Try to remove a key.
- Parameters:
key – The key to remove.
- Returns:
trueif the key was present and removed.
-
template<typename Function>
Self &removeIf(Function function) Remove all keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
A reference to this set.
-
Self removed(const Key &key) const
Return a set with a key removed.
- Parameters:
key – The key to remove.
- Returns:
A new set without the key.
-
template<typename Function>
Self removedIf(Function function) const Return a set without keys matching a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
A new set without matching keys.
-
template<typename Function>
LoopResult forEach(Function function) const Call a function for every key.
- Parameters:
function – The function called for every key.
- Returns:
The result of the iteration.
-
template<typename Function>
LoopResult forEachReverse(Function function) const Call a function for every key in reverse order.
- Parameters:
function – The function called for every key.
- Returns:
The result of the iteration.
-
Self &unite(const Self &other)
Unite with another set (union).
- Parameters:
other – The set to unite with.
- Returns:
A reference to this set.
-
Self &intersect(const Self &other)
Intersect with another set.
- Parameters:
other – The set to intersect with.
- Returns:
A reference to this set.
-
Self &subtract(const Self &other)
Subtract another set (difference).
- Parameters:
other – The set to subtract.
- Returns:
A reference to this set.
-
Self &symmetricDifference(const Self &other)
Compute the symmetric difference with another set.
- Parameters:
other – The set for the symmetric difference.
- Returns:
A reference to this set.
-
Self unitedWith(const Self &other) const
Return the union of this set and another.
- Parameters:
other – The set to unite with.
- Returns:
A new set containing all elements.
-
Self intersectedWith(const Self &other) const
Return the intersection of this set and another.
- Parameters:
other – The set to intersect with.
- Returns:
A new set containing common elements.
-
Self subtractedBy(const Self &other) const
Return the difference of this set from another.
- Parameters:
other – The set to subtract.
- Returns:
A new set with elements of
otherremoved.
-
Self symmetricDifferenceWith(const Self &other) const
Return the symmetric difference with another set.
- Parameters:
other – The set for the symmetric difference.
- Returns:
A new set with elements unique to either set.
-
Self &insert(const Key &key)
Insert a key.
- Parameters:
key – The key to insert.
- Returns:
A reference to this set.
-
Self &insert(Key &&key)
Insert a key, taking ownership of the value.
- Parameters:
key – The key to insert.
- Returns:
A reference to this set.
-
bool tryInsert(const Key &key)
Try to insert a key.
- Parameters:
key – The key to insert.
- Returns:
trueif the key was not already present.
-
bool tryInsert(Key &&key)
Try to insert a key, taking ownership of the value.
- Parameters:
key – The key to insert.
- Returns:
trueif the key was not already present.
-
bool compare(const Self &other) const
Test if two sets contain the same elements.
- Parameters:
other – The set to compare with.
- Returns:
trueif both sets contain the same keys.
-
bool contains(const Key &key) const
Test if the set contains a key.
- Parameters:
key – The key to search for.
- Returns:
trueif the key is present.
-
template<typename Function>
bool allOf(Function function) const Test if all keys match a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif all keys match.
-
template<typename Function>
bool anyOf(Function function) const Test if any key matches a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif at least one key matches.
-
template<typename Function>
bool noneOf(Function function) const Test if no key matches a predicate.
- Parameters:
function – The predicate called for every key.
- Returns:
trueif no key matches.
-
bool isSubsetOf(const Self &other) const
Test if this set is a subset of another.
- Parameters:
other – The superset to compare with.
- Returns:
trueif all elements are inother.
-
bool isSupersetOf(const Self &other) const
Test if this set is a superset of another.
- Parameters:
other – The subset to compare with.
- Returns:
trueif all elements ofotherare in this set.
-
bool isDisjointWith(const Self &other) const
Test if this set has no elements in common with another.
- Parameters:
other – The set to compare with.
- Returns:
trueif the sets share no elements.
-
bool intersects(const Self &other) const
Test if this set shares any elements with another.
- Parameters:
other – The set to compare with.
- Returns:
trueif there is at least one common element.
-
const_iterator begin() const noexcept
Return an iterator to the first key.
- Returns:
The begin iterator.
-
const_iterator end() const noexcept
Return an iterator past the last key.
- Returns:
The end iterator.
Public Static Functions