![]() |
Google OR-Tools v9.14
a fast and portable software suite for combinatorial optimization
|
#include <thread_safe_id_map.h>
Public Member Functions | |
ThreadSafeIdMap ()=default | |
absl::Span< const std::pair< int64_t, std::unique_ptr< V > > > | UpdateAndGetAll () |
std::vector< std::pair< int64_t, V * > > | GetAll () const |
V * | UpdateAndGet (int64_t id) |
V * | Get (int64_t id) const |
int64_t | Insert (std::unique_ptr< V > value) |
Inserts value into the map and returns the assigned key. | |
bool | Erase (int64_t key) |
Erases key from the map, returning true if the key was found in the map. | |
int64_t | Size () const |
The number of elements in the map. |
A map from int64_t ids to V, where the ids are created by this map and handed out sequentially.
The underlying storage for this map is vector<pair<int64_t, unique_ptr<V>>>. Insertions and deletions from this vector are done lazily whenever any of the functions UpdateXXX() are invoked.
At a high level, the purpose of this class is to allow for the thread-safe removal of elements from the map, while have as little overhead as possible when iterating over the elements of the map (UpdateAndGetAll()). In particular, in the common case where there is nothing to update, UpdateAndGetAll() only incurs the cost of a single atomic read with std::memory_order_relaxed, which is much faster than acquiring a lock on a mutex.
This map has pointer stability for values, users can only insert by providing a unique_ptr<V>.
The functions of this class are mutually thread-safe. However, the functions:
Definition at line 63 of file thread_safe_id_map.h.
|
default |
bool operations_research::math_opt::ThreadSafeIdMap< V >::Erase | ( | int64_t | key | ) |
Erases key from the map, returning true if the key was found in the map.
Definition at line 233 of file thread_safe_id_map.h.
V * operations_research::math_opt::ThreadSafeIdMap< V >::Get | ( | int64_t | id | ) | const |
Returns the value for this key, or nullptr if this key is not in the map.
The returned pointer is invalidated by any call to Erase(id) or ~ThreadSafeIdMap().
This function is similar to UpdateAndGet(), but it is const. It can be slightly slower, but it is also safer to use from a concurrent context, as it will not invalidate any pointers returned by other functions on this class.
Definition at line 173 of file thread_safe_id_map.h.
std::vector< std::pair< int64_t, V * > > operations_research::math_opt::ThreadSafeIdMap< V >::GetAll | ( | ) | const |
Returns all key-value paris in the map.
In contrast to UpdateAndGetAll(), this function will always acquire a lock and copy the data before returning. Thus, this function is slower, but the values are harder to invalidate. This function is also const, while UpdateAndGetAll() is not. Last, because this function does not update, it will not invalidate any pointers returned by other functions on this class.
For each (id, V*) pair, the V* is invalidated by either:
Definition at line 192 of file thread_safe_id_map.h.
int64_t operations_research::math_opt::ThreadSafeIdMap< V >::Insert | ( | std::unique_ptr< V > | value | ) |
Inserts value into the map and returns the assigned key.
Definition at line 223 of file thread_safe_id_map.h.
int64_t operations_research::math_opt::ThreadSafeIdMap< V >::Size | ( | ) | const |
The number of elements in the map.
Definition at line 166 of file thread_safe_id_map.h.
V * operations_research::math_opt::ThreadSafeIdMap< V >::UpdateAndGet | ( | int64_t | id | ) |
Returns the value for this key, or nullptr if this key is not in the map.
The returned pointer is invalidated by either of:
Definition at line 209 of file thread_safe_id_map.h.
absl::Span< const std::pair< int64_t, std::unique_ptr< V > > > operations_research::math_opt::ThreadSafeIdMap< V >::UpdateAndGetAll | ( | ) |
Returns all key-value pairs in the map.
The returned span is invalidated by an Insert() or Erase() followed by a call to UpdateAndGetAll() or UpdateAndGet().
Template function implementations.
Definition at line 157 of file thread_safe_id_map.h.