Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
DistributedData.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
21#include "shambase/print.hpp"
22#include "shambase/sets.hpp"
23#include "shambase/string.hpp"
24#include <functional>
25#include <map>
26#include <stdexcept>
27#include <string>
28#include <utility>
29#include <vector>
30
31namespace shambase {
32
42 template<class T>
44
45 std::map<u64, T> data;
46
47 using iterator = typename std::map<u64, T>::iterator;
48
49 public:
55 inline std::map<u64, T> &get_native() { return data; }
56
67 inline iterator add_obj(u64 id, T &&obj) {
68
69 std::pair<iterator, bool> ret = data.emplace(id, std::forward<T>(obj));
70
71 if (!ret.second) {
72 throw make_except_with_loc<std::runtime_error>("the key already exist");
73 }
74
75 return ret.first;
76 }
77
83 inline void erase(u64 id) { data.erase(id); }
84
93 inline void for_each(std::function<void(u64, T &)> &&f) {
94 for (auto &[id, obj] : data) {
95 f(id, obj);
96 }
97 }
98
100 inline void for_each(std::function<void(u64, const T &)> &&f) const {
101 for (auto &[id, obj] : data) {
102 f(id, obj);
103 }
104 }
105
111 inline std::vector<u64> get_ids() const {
112 std::vector<u64> ids;
113 ids.reserve(data.size());
114 for (const auto &[id, _] : data) {
115 ids.push_back(id);
116 }
117 return ids;
118 }
119
127 inline auto find(u64 id) { return data.find(id); }
128
134 inline auto not_found() { return data.end(); }
135
145 inline T &get(u64 id) {
146 try {
147 return data.at(id);
148 } catch (std::out_of_range &) {
149
150 std::vector<u64> id_list;
151
152 for_each([&](u64 id, T &) {
153 id_list.push_back(id);
154 });
155
156 throw make_except_with_loc<std::runtime_error>(shambase::format(
157 "The querried id {} does not exist, current id list is {}", id, id_list));
158 }
159 }
160
162 inline const T &get(u64 id) const {
163 try {
164 return data.at(id);
165 } catch (std::out_of_range &) {
166
167 std::vector<u64> id_list;
168
169 for_each([&](u64 id, const T &) {
170 id_list.push_back(id);
171 });
172
173 throw make_except_with_loc<std::runtime_error>(shambase::format(
174 "The querried id {} does not exist, current id list is {}", id, id_list));
175 }
176 }
177
185 inline bool has_key(u64 id) { return (data.find(id) != data.end()); }
186
192 inline u64 get_element_count() { return data.size(); }
193
199 inline bool is_empty() { return data.empty(); }
200
219 template<typename... Tf>
220 inline void print_data(fmt::format_string<Tf...> fmt) const {
221 for_each([&](u64 id_patch, const T &ref) {
223 shambase::format("{} -> {}", id_patch, shambase::format(fmt, ref)));
224 });
225 }
226
250 template<class Tmap>
251 inline DistributedData<Tmap> map(std::function<Tmap(u64, T &)> map_func) {
253
254 for_each([&](u64 id, T &ref) {
255 ret.add_obj(id, map_func(id, ref));
256 });
257
258 return ret;
259 }
260
262 template<class Tmap>
263 inline DistributedData<Tmap> map(std::function<Tmap(u64, const T &)> map_func) const {
265
266 for_each([&](u64 id, const T &ref) {
267 ret.add_obj(id, map_func(id, ref));
268 });
269
270 return ret;
271 }
272
278 inline void reset() { data.clear(); }
279 };
280
301 template<class T1, class FuncMatch, class FuncMissing, class FuncExtra>
304 const std::vector<u64> &ref_ids,
308
309 std::vector<u64> dd_ids;
310
311 dd.for_each([&](u32 id, const T1 &data) {
312 dd_ids.push_back(id);
313 });
314
315 std::vector<u64> missing;
316 std::vector<u64> matching;
317 std::vector<u64> extra;
318
320
321 for (auto id : missing) {
322 func_missing(id);
323 }
324
325 for (auto id : matching) {
326 func_match(id);
327 }
328
329 for (auto id : extra) {
330 func_extra(id);
331 }
332 }
333
351 template<class T1, class T2, class FuncMatch, class FuncMissing, class FuncExtra>
354 const shambase::DistributedData<T2> &reference,
358
359 std::vector<u64> ref_ids;
360
361 reference.for_each([&](u32 id, const T2 &data) {
362 ref_ids.push_back(id);
363 });
364
366 }
367
368} // namespace shambase
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.
Definition print.cpp:37
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.
Definition sets.hpp:41
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.