$darkmode
#include <uid_tracker.hpp>
Public Member Functions | |
| UniqueIDTracker (int min, int max, int ttl=1) | |
| int | assign (int id) |
| void | next_cycle () |
| void | add_delete_observer (void f(int in_id, int out_id)) |
UniqueIDTracker maintains a mapping from objects to unique IDs, reusing objects as far as possible.
It fulfills the following requirements:
Currently, the implementation does not remove a mapped ID unless it has not been seen for ttl time. Therefore, if ttl is 1, then it will be reserved for one cycle it is not seen. A ttl of smaller than one makes no sense, and is considered a programmer error.
|
inline |
Create a UniqueIDTracker with the minimum and maximum output ID and the maximum TTL (time-to-live) cycles for each ID after not being seen.
|
inline |
Add an observer that is called with the in and output ID when that ID is removed from the map (i.e., when it expires).
| int cloe::utility::UniqueIDTracker::assign | ( | int | id | ) |
Return a unique persistent ID across multiple cycles (steps). The input ID is a unique number representable by int (can be random for all the package cares) and is mapped to an int that is within the range specified on creation of the class (see min and max parameters).
It is not an error to assign the same input ID twice, the same output ID will be returned.
| id | a unique int for an object |
| std::range_error | when no more free output IDs available |
| void cloe::utility::UniqueIDTracker::next_cycle | ( | ) |
Advance to the next cycle of the assignments. This removes all mapped IDs if they have exceeded their TTL.