![]() |
Shamrock 2025.10.0
Astrophysical Code
|
Represents a collection of objects distributed across patches identified by a u64 id. More...
#include <shambase/include/shambase/DistributedData.hpp>
Public Member Functions | |
| std::unordered_map< u64, T > & | get_native () |
| Returns the underlying collection. | |
| iterator | add_obj (u64 id, T &&obj) |
| Adds a new object to the collection. | |
| void | reserve (u64 count) |
| Reserve storage for at least count objects. | |
| void | erase (u64 id) |
| Removes an object from the collection. | |
| void | for_each (std::function< void(u64, T &)> &&f) |
| Applies a function to each object in the collection. | |
| void | for_each (std::function< void(u64, const T &)> &&f) const |
| Same as for_each but for const objects. | |
| void | for_each_ordered (std::function< void(u64, T &)> &&f) |
| Applies a function to each object in the collection, in increasing id order. | |
| void | for_each_ordered (std::function< void(u64, const T &)> &&f) const |
| Same as for_each_ordered but for const objects. | |
| std::vector< u64 > | get_ids () const |
| Returns a vector of all the ids of the objects in the collection. | |
| auto | find (u64 id) |
| Finds an object in the collection. | |
| auto | not_found () |
| Returns an iterator pointing to the end of the collection. | |
| T & | get (u64 id) |
| Returns a reference to an object in the collection. | |
| const T & | get (u64 id) const |
| Const variant of get. | |
| bool | has_key (u64 id) const |
| Checks if an object exists in the collection. | |
| u64 | get_element_count () const |
| Returns the number of elements in the collection. | |
| bool | is_empty () const |
| Returns true if the collection is empty. | |
| template<typename... Tf> | |
| void | print_data (fmt::format_string< Tf... > fmt) const |
| Prints all the objects in the collection to the logger. | |
| template<class Tmap> | |
| DistributedData< Tmap > | map (std::function< Tmap(u64, T &)> map_func) |
| Apply a function to all objects in the collection and return a new collection containing the results. | |
| template<class Tmap> | |
| DistributedData< Tmap > | map (std::function< Tmap(u64, const T &)> map_func) const |
| Same as map but for const objects. | |
| void | reset () |
| Reset the collection to its initial state. | |
Represents a collection of objects distributed across patches identified by a u64 id.
This class provides methods for managing the distributed data collection, including adding, removing, finding, and iterating over elements. It also supports mapping the collection to a new type using a user-defined mapping function.
| T | The type of the object in the collection. |
Definition at line 47 of file DistributedData.hpp.
|
inline |
Adds a new object to the collection.
| id | The id of the patch the object belongs to. |
| obj | The object to add. |
| If | the key already exist. |
Definition at line 95 of file DistributedData.hpp.
|
inline |
Removes an object from the collection.
| id | The id of the patch the object belongs to. |
Definition at line 124 of file DistributedData.hpp.
|
inline |
Finds an object in the collection.
| id | The id of the patch the object belongs to. |
Definition at line 201 of file DistributedData.hpp.
|
inline |
Same as for_each but for const objects.
Definition at line 145 of file DistributedData.hpp.
|
inline |
Applies a function to each object in the collection.
| f | A function taking two arguments: the id of the patch the object belongs to, and a non-const reference to the object itself. |
Definition at line 138 of file DistributedData.hpp.
|
inline |
Same as for_each_ordered but for const objects.
Definition at line 173 of file DistributedData.hpp.
|
inline |
Applies a function to each object in the collection, in increasing id order.
Unlike for_each, the iteration order does not depend on the insertion history or on the standard library implementation. Use it when the order matters (e.g. concatenation of per-patch data or floating-point reductions that must be reproducible).
The sorted ids are cached and only recomputed after the key set may have changed (add_obj, erase, reset, get_native). The cache is not thread safe: do not call this concurrently on the same object, even through a const reference.
| f | A function taking two arguments: the id of the patch the object belongs to, and a non-const reference to the object itself. |
Definition at line 165 of file DistributedData.hpp.
|
inline |
Returns a reference to an object in the collection.
| id | The id of the patch the object belongs to. |
| If | the object is not found. |
Definition at line 219 of file DistributedData.hpp.
|
inline |
Const variant of get.
Definition at line 236 of file DistributedData.hpp.
|
inline |
Returns the number of elements in the collection.
Definition at line 266 of file DistributedData.hpp.
|
inline |
Returns a vector of all the ids of the objects in the collection.
Definition at line 185 of file DistributedData.hpp.
|
inline |
Returns the underlying collection.
Definition at line 80 of file DistributedData.hpp.
|
inline |
Checks if an object exists in the collection.
| id | The id of the patch the object belongs to. |
Definition at line 259 of file DistributedData.hpp.
|
inline |
Returns true if the collection is empty.
Definition at line 273 of file DistributedData.hpp.
|
inline |
Same as map but for const objects.
Definition at line 337 of file DistributedData.hpp.
|
inline |
Apply a function to all objects in the collection and return a new collection containing the results.
The map() function applies the given function to each object in the collection and stores the result in a new collection. The function is passed the id of the object and a reference to the object as arguments.
Example:
| Tmap | Type of the objects in the returned collection. |
| map_func | The function to apply to each object. |
Definition at line 324 of file DistributedData.hpp.
|
inline |
Returns an iterator pointing to the end of the collection.
Definition at line 208 of file DistributedData.hpp.
|
inline |
Prints all the objects in the collection to the logger.
The format string is passed to fmt::format to format each object, so the syntax is the same as fmt::format. The object is passed as the second argument to fmt::format.
Example:
| Tf | Types of the format string placeholders. |
| fmt | The format string. |
Definition at line 294 of file DistributedData.hpp.
|
inline |
Reserve storage for at least count objects.
Sets the number of buckets of the underlying hash map so that count objects can be inserted without further rehashing. The content of the collection is unchanged, but the rehash may change the order in which for_each visits the objects (still deterministic). for_each_ordered is not affected.
| count | The number of objects the collection is expected to hold. |
Definition at line 117 of file DistributedData.hpp.
|
inline |
Reset the collection to its initial state.
All objects in the collection are removed.
Definition at line 353 of file DistributedData.hpp.