commons 0.1.5
Header-only C++23 library of common/shared types for the C++ libraries
Loading...
Searching...
No Matches
comms::PrioritizedSet< T > Class Template Reference

A set that behaves like std::set<T> from the outside — unique by T value, iterators yield const T&, every method is expressed in terms of T — but whose iteration order is (priority asc, insertion-order asc) rather than by T value. More...

#include <prioritized.hpp>

Classes

class  const_iterator
 A thin bidirectional adapter over std::set<Item>::const_iterator that projects each Item to its const T& value — the private Item is never observable, so iteration looks exactly like a std::set<T>'s. More...
 

Public Member Functions

std::pair< iterator, bool > insert (const T &v)
 Insert v at its discovered priority (get_priority(v), i.e.
 
std::pair< iterator, bool > insert (int priority, T v)
 Priority-aware addition.
 
template<typename... Args>
std::pair< iterator, bool > emplace (Args &&... args)
 Construct a T from args and insert it at its discovered priority.
 
size_type erase (const T &v)
 Erase the element equal to v. Returns 0 or 1.
 
iterator find (const T &v) const
 Linear lookup by value; end() when absent.
 
size_type count (const T &v) const
 0 or 1 — elements are unique by value.
 
int priority_of (const T &v) const
 The snapshotted priority of v, or DEFAULT_PRIORITY if absent.
 
bool set_priority (const T &v, int priority)
 Re-snapshot v's priority and reorder it, keeping its insertion-order tie-break.
 
void clear () noexcept
 Remove all elements.
 

Friends

bool operator== (const PrioritizedSet &a, const PrioritizedSet &b)
 Element-wise comparison over the ordered T sequence.
 

Detailed Description

template<typename T>
class comms::PrioritizedSet< T >

A set that behaves like std::set<T> from the outside — unique by T value, iterators yield const T&, every method is expressed in terms of T — but whose iteration order is (priority asc, insertion-order asc) rather than by T value.

Identity is the T value only; priority is metadata. Following std::set, insert never mutates an existing element: re-inserting an equal T is a no-op that leaves the stored priority unchanged — use set_priority to change it.

Priority is snapshotted at insert into a private Item, so later mutation of an element's own priority cannot corrupt the tree's ordering invariant.

Note
Deliberately omitted: lower_bound/upper_bound/equal_range/ key_comp/value_comp and the node-handle extract/merge. They assume the container is ordered by T, but ordering here is by priority, so they have no coherent meaning.
Complexity: insert, find, erase(const T&) and set_priority are O(n) — uniqueness and value lookup cannot use the priority-keyed tree. Iteration and erase(iterator) match std::set. This is intended for config-sized collections (adapters, transports).

Member Function Documentation

◆ clear()

template<typename T >
void comms::PrioritizedSet< T >::clear ( )
inlinenoexcept

Remove all elements.

Does not reset the insertion-order counter, which is monotonic for the lifetime of the set.

◆ insert() [1/2]

template<typename T >
std::pair< iterator, bool > comms::PrioritizedSet< T >::insert ( const T &  v)
inline

Insert v at its discovered priority (get_priority(v), i.e.

DEFAULT_PRIORITY for a plain T). No-op if an equal T is present.

◆ insert() [2/2]

template<typename T >
std::pair< iterator, bool > comms::PrioritizedSet< T >::insert ( int  priority,
v 
)
inline

Priority-aware addition.

If an equal T is already present this is a no-op returning {existing, false} and the passed priority is ignored (use set_priority to change a present element's priority).

◆ set_priority()

template<typename T >
bool comms::PrioritizedSet< T >::set_priority ( const T &  v,
int  priority 
)
inline

Re-snapshot v's priority and reorder it, keeping its insertion-order tie-break.

Returns false if v is absent.


The documentation for this class was generated from the following file: