7#include <unordered_map>
17template <
typename Key,
typename Hash = std::hash<Key>,
typename Eq = std::equal_to<Key>>
22 [[nodiscard]] uint64_t
find(
const K &
id)
const noexcept {
23 const auto it = ids_.find(
id);
24 return it == ids_.end() || !it->second.held ? 0 : it->second.order;
29 [[nodiscard]]
bool claim(
const Key &
id, uint64_t order_num = 0) {
30 const auto [it, inserted] = ids_.try_emplace(
id, Entry{order_num});
31 if (!inserted && (order_num == 0 || it->second.order != order_num)) {
41 [[nodiscard]]
bool claim(
const K &
id, uint64_t order_num = 0) {
42 return claim(Key(
id), order_num);
48 if (
const auto it = ids_.find(
id); it != ids_.end() && it->second.claims > 0) {
56 void assign(
const K &
id, uint64_t order_num) {
57 assert(order_num != 0);
58 auto &entry = ids_[Key(
id)];
61 assert(!entry.held || entry.order == order_num);
62 entry.order = order_num;
68 void forget(
const K &
id, uint64_t order_num)
noexcept {
69 if (
const auto it = ids_.find(
id); it != ids_.end() && it->second.order == order_num) {
70 it->second.held =
false;
77 void rename(
const K &from,
const K &to, uint64_t order_num) {
83 [[nodiscard]]
size_t size() const noexcept {
return ids_.size(); }
84 [[nodiscard]]
bool empty() const noexcept {
return ids_.empty(); }
92 using Map = std::unordered_map<Key, Entry, Hash, Eq>;
94 void erase_if_unused(
typename Map::iterator it)
noexcept {
95 if (it->second.claims == 0 && !it->second.held) {
One session's ClOrdIDs, each mapped to the working order that holds it.
size_t size() const noexcept
How many ids are taken - by a working order, by a request in flight, or both.
bool claim(const Key &id, uint64_t order_num=0)
Claims id for a new order (order_num 0) or for a replace of order_num.
void rename(const K &from, const K &to, uint64_t order_num)
A replace renamed order_num from from to to.
bool empty() const noexcept
bool claim(const K &id, uint64_t order_num=0)
The same, by the key's lookup form.
void forget(const K &id, uint64_t order_num) noexcept
order_num no longer holds id: it is done, or was renamed.
uint64_t find(const K &id) const noexcept
The working order named id, or 0 when no order holds it (a request may).
void release(const K &id) noexcept
The request that claimed id was answered.
void assign(const K &id, uint64_t order_num)
id now names order_num.