Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
Public Member Functions | List of all members
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::map< u64, T > & get_native ()
 Returns the underlying collection.
 
iterator add_obj (u64 id, T &&obj)
 Adds a new object to the collection.
 
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.
 
std::vector< u64get_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)
 Checks if an object exists in the collection.
 
u64 get_element_count ()
 Returns the number of elements in the collection.
 
bool is_empty ()
 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< Tmapmap (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< Tmapmap (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 43 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 67 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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 83 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 127 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 100 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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 function is applied to each object in the order of insertion.

Definition at line 93 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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 145 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ get() [2/2]

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

Const variant of get.

Definition at line 162 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ get_element_count()

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

Returns the number of elements in the collection.

Returns
The number of elements in the collection.

Definition at line 192 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 111 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ get_native()

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

Returns the underlying collection.

Returns
The underlying collection.

Definition at line 55 of file DistributedData.hpp.

◆ has_key()

template<class T >
bool shambase::DistributedData< T >::has_key ( u64  id)
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 185 of file DistributedData.hpp.

◆ is_empty()

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

Returns true if the collection is empty.

Returns
True if the collection is empty, false otherwise.

Definition at line 199 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 263 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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.
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.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
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 251 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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 134 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"
void print_data(fmt::format_string< Tf... > fmt) const
Prints all the objects in the collection to the logger.
Template Parameters
TfTypes of the format string placeholders.
Parameters
fmtThe format string.

Definition at line 220 of file DistributedData.hpp.

+ Here is the call graph for this function:

◆ 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 278 of file DistributedData.hpp.


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