45 std::map<u64, T> data;
47 using iterator =
typename std::map<u64, T>::iterator;
69 std::pair<iterator, bool>
ret = data.emplace(
id, std::forward<T>(
obj));
94 for (
auto &[
id,
obj] : data) {
100 inline void for_each(std::function<
void(
u64,
const T &)> &&f)
const {
101 for (
auto &[
id,
obj] : data) {
112 std::vector<u64>
ids;
113 ids.reserve(data.size());
114 for (
const auto &[
id, _] : data) {
127 inline auto find(
u64 id) {
return data.find(
id); }
148 }
catch (std::out_of_range &) {
157 "The querried id {} does not exist, current id list is {}",
id,
id_list));
165 }
catch (std::out_of_range &) {
174 "The querried id {} does not exist, current id list is {}",
id,
id_list));
185 inline bool has_key(
u64 id) {
return (data.find(
id) != data.end()); }
219 template<
typename...
Tf>
220 inline void print_data(fmt::format_string<Tf...> fmt)
const {
223 shambase::format(
"{} -> {}", id_patch, shambase::format(fmt,
ref)));
278 inline void reset() { data.clear(); }
301 template<
class T1,
class FuncMatch,
class FuncMissing,
class FuncExtra>
304 const std::vector<u64> &
ref_ids,
311 dd.for_each([&](
u32 id,
const T1 &data) {
317 std::vector<u64>
extra;
329 for (
auto id :
extra) {
351 template<
class T1,
class T2,
class FuncMatch,
class FuncMissing,
class FuncExtra>
361 reference.for_each([&](
u32 id,
const T2 &data) {
std::uint32_t u32
32 bit unsigned integer
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.
const T & get(u64 id) const
Const variant of get.
bool is_empty()
Returns true if the collection is empty.
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.
u64 get_element_count()
Returns the number of elements in the collection.
void for_each(std::function< void(u64, const T &)> &&f) const
Same as for_each but for const objects.
void print_data(fmt::format_string< Tf... > fmt) const
Prints all the objects in the collection to the logger.
auto not_found()
Returns an iterator pointing to the end of the collection.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
void erase(u64 id)
Removes an object from 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.
T & get(u64 id)
Returns a reference to an object in the collection.
DistributedData< Tmap > map(std::function< Tmap(u64, const T &)> map_func) const
Same as map but for const objects.
bool has_key(u64 id)
Checks if an object exists in the collection.
std::map< u64, T > & get_native()
Returns the underlying collection.
void reset()
Reset the collection to its initial state.
This header file contains utility functions related to exception handling in the code.
namespace for basic c++ utilities
void on_distributeddata_diff(const shambase::DistributedData< T1 > &dd, const shambase::DistributedData< T2 > &reference, FuncMatch &&func_missing, FuncMissing &&func_match, FuncExtra &&func_extra)
Compare two distributed data and apply callbacks based on the difference.
void println(std::string_view sv)
Prints a string to the console followed by a newline.
void set_diff(Container1 &c1, Container2 &ref, std::vector< T > &missing, std::vector< T > &matching, std::vector< T > &extra)
Compute the difference between two containers as three separate vectors.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void on_distributeddata_ids_diff(const shambase::DistributedData< T1 > &dd, const std::vector< u64 > &ref_ids, FuncMatch &&func_missing, FuncMissing &&func_match, FuncExtra &&func_extra)
Compare two distributed data and apply callbacks based on the difference.