Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
shambase::DistributedData< T > Class Template Reference

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.

Detailed Description

template<class T>
class shambase::DistributedData< T >

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.

Template Parameters
TThe type of the object in the collection.

Definition at line 47 of file DistributedData.hpp.

Member Function Documentation

◆ add_obj()

template<class T>
iterator shambase::DistributedData< T >::add_obj ( u64 id,
T && obj )
inline

Adds a new object to the collection.

Parameters
idThe id of the patch the object belongs to.
objThe object to add.
Returns
An iterator pointing to the inserted object.
Exceptions
Ifthe key already exist.

Definition at line 95 of file DistributedData.hpp.

◆ erase()

template<class T>
void shambase::DistributedData< T >::erase ( u64 id)
inline

Removes an object from the collection.

Parameters
idThe id of the patch the object belongs to.

Definition at line 124 of file DistributedData.hpp.

◆ find()

template<class T>
auto shambase::DistributedData< T >::find ( u64 id)
inline

Finds an object in the collection.

Parameters
idThe id of the patch the object belongs to.
Returns
An iterator pointing to the object if found, or data.end() otherwise.

Definition at line 201 of file DistributedData.hpp.

◆ for_each() [1/2]

template<class T>
void shambase::DistributedData< T >::for_each ( std::function< void(u64, const T &)> && f) const
inline

Same as for_each but for const objects.

Definition at line 145 of file DistributedData.hpp.

◆ for_each() [2/2]

template<class T>
void shambase::DistributedData< T >::for_each ( std::function< void(u64, T &)> && f)
inline

Applies a function to each object in the collection.

Parameters
fA function taking two arguments: the id of the patch the object belongs to, and a non-const reference to the object itself.
Remarks
The iteration order is deterministic but unspecified: do not rely on the objects being visited in ascending id order, nor in insertion order.

Definition at line 138 of file DistributedData.hpp.

◆ for_each_ordered() [1/2]

template<class T>
void shambase::DistributedData< T >::for_each_ordered ( std::function< void(u64, const T &)> && f) const
inline

Same as for_each_ordered but for const objects.

Definition at line 173 of file DistributedData.hpp.

◆ for_each_ordered() [2/2]

template<class T>
void shambase::DistributedData< T >::for_each_ordered ( std::function< void(u64, T &)> && f)
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.

Parameters
fA 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.

◆ get() [1/2]

template<class T>
T & shambase::DistributedData< T >::get ( u64 id)
inline

Returns a reference to an object in the collection.

Parameters
idThe id of the patch the object belongs to.
Returns
A reference to the object.
Exceptions
Ifthe object is not found.

Definition at line 219 of file DistributedData.hpp.

◆ get() [2/2]

template<class T>
const T & shambase::DistributedData< T >::get ( u64 id) const
inline

Const variant of get.

Definition at line 236 of file DistributedData.hpp.

◆ get_element_count()

template<class T>
u64 shambase::DistributedData< T >::get_element_count ( ) const
inline

Returns the number of elements in the collection.

Returns
The number of elements in the collection.

Definition at line 266 of file DistributedData.hpp.

◆ get_ids()

template<class T>
std::vector< u64 > shambase::DistributedData< T >::get_ids ( ) const
inline

Returns a vector of all the ids of the objects in the collection.

Returns
A vector of all the ids of the objects in the collection.

Definition at line 185 of file DistributedData.hpp.

◆ get_native()

template<class T>
std::unordered_map< u64, T > & shambase::DistributedData< T >::get_native ( )
inline

Returns the underlying collection.

Returns
The underlying collection.
Remarks
The sorted ids cache is invalidated by this call, but not by later modifications made through the returned reference while it is held.

Definition at line 80 of file DistributedData.hpp.

◆ has_key()

template<class T>
bool shambase::DistributedData< T >::has_key ( u64 id) const
inline

Checks if an object exists in the collection.

Parameters
idThe id of the patch the object belongs to.
Returns
True if the object is found, false otherwise.

Definition at line 259 of file DistributedData.hpp.

◆ is_empty()

template<class T>
bool shambase::DistributedData< T >::is_empty ( ) const
inline

Returns true if the collection is empty.

Returns
True if the collection is empty, false otherwise.

Definition at line 273 of file DistributedData.hpp.

◆ map() [1/2]

template<class T>
template<class Tmap>
DistributedData< Tmap > shambase::DistributedData< T >::map ( std::function< Tmap(u64, const T &)> map_func) const
inline

Same as map but for const objects.

Definition at line 337 of file DistributedData.hpp.

◆ map() [2/2]

template<class T>
template<class Tmap>
DistributedData< Tmap > shambase::DistributedData< T >::map ( std::function< Tmap(u64, T &)> map_func)
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:

data.add_obj(0, 42);
data.add_obj(1, 24);
// a DistributedData<float> with doubled values from the input
auto mapped = data.map<float>([](u64 id, int& val) { return val * 2.; });
std::uint64_t u64
64 bit unsigned integer
Represents a collection of objects distributed across patches identified by a u64 id.
iterator add_obj(u64 id, T &&obj)
Adds a new object to the collection.
Template Parameters
TmapType of the objects in the returned collection.
Parameters
map_funcThe function to apply to each object.
Returns
A new collection containing the results of the map function.

Definition at line 324 of file DistributedData.hpp.

◆ not_found()

template<class T>
auto shambase::DistributedData< T >::not_found ( )
inline

Returns an iterator pointing to the end of the collection.

Returns
An iterator pointing to the end of the collection.

Definition at line 208 of file DistributedData.hpp.

◆ print_data()

template<class T>
template<typename... Tf>
void shambase::DistributedData< T >::print_data ( fmt::format_string< Tf... > fmt) const
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:

data.add_obj(0, 42);
data.add_obj(1, 24);
data.print_data("{:d}"); // will print "0 -> 42" and "1 -> 24"
Template Parameters
TfTypes of the format string placeholders.
Parameters
fmtThe format string.

Definition at line 294 of file DistributedData.hpp.

◆ reserve()

template<class T>
void shambase::DistributedData< T >::reserve ( u64 count)
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.

Parameters
countThe number of objects the collection is expected to hold.

Definition at line 117 of file DistributedData.hpp.

◆ reset()

template<class T>
void shambase::DistributedData< T >::reset ( )
inline

Reset the collection to its initial state.

All objects in the collection are removed.

Definition at line 353 of file DistributedData.hpp.


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