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
18
21#include "shambase/print.hpp"
22#include "shambase/sets.hpp"
23#include "shambase/string.hpp"
24#include <unordered_map>
25#include <unordered_set>
26#include <algorithm>
27#include <functional>
28#include <map>
29#include <memory>
30#include <stdexcept>
31#include <string>
32#include <utility>
33#include <vector>
34
35namespace shambase {
36
46 template<class T>
48
49 std::unordered_map<u64, T> data;
50
51 using iterator = typename std::unordered_map<u64, T>::iterator;
52
56 mutable std::shared_ptr<const std::vector<u64>> sorted_ids_cache;
57
59 inline void invalidate_sorted_ids() { sorted_ids_cache.reset(); }
60
62 inline std::shared_ptr<const std::vector<u64>> get_sorted_ids() const {
63 if (!sorted_ids_cache) {
64 std::vector<u64> ids = get_ids();
65 std::sort(ids.begin(), ids.end());
66 sorted_ids_cache = std::make_shared<const std::vector<u64>>(std::move(ids));
67 }
68 return sorted_ids_cache;
69 }
70
71 public:
80 inline std::unordered_map<u64, T> &get_native() {
81 invalidate_sorted_ids();
82 return data;
83 }
84
95 inline iterator add_obj(u64 id, T &&obj) {
96
97 invalidate_sorted_ids();
98 std::pair<iterator, bool> ret = data.emplace(id, std::forward<T>(obj));
99
100 if (!ret.second) {
101 throw make_except_with_loc<std::runtime_error>("the key already exist");
102 }
103
104 return ret.first;
105 }
106
117 inline void reserve(u64 count) { data.reserve(count); }
118
124 inline void erase(u64 id) {
125 invalidate_sorted_ids();
126 data.erase(id);
127 }
128
138 inline void for_each(std::function<void(u64, T &)> &&f) {
139 for (auto &[id, obj] : data) {
140 f(id, obj);
141 }
142 }
143
145 inline void for_each(std::function<void(u64, const T &)> &&f) const {
146 for (auto &[id, obj] : data) {
147 f(id, obj);
148 }
149 }
150
165 inline void for_each_ordered(std::function<void(u64, T &)> &&f) {
166 std::shared_ptr<const std::vector<u64>> ids = get_sorted_ids();
167 for (u64 id : *ids) {
168 f(id, data.at(id));
169 }
170 }
171
173 inline void for_each_ordered(std::function<void(u64, const T &)> &&f) const {
174 std::shared_ptr<const std::vector<u64>> ids = get_sorted_ids();
175 for (u64 id : *ids) {
176 f(id, data.at(id));
177 }
178 }
179
185 inline std::vector<u64> get_ids() const {
186 std::vector<u64> ids;
187 ids.reserve(data.size());
188 for (const auto &[id, _] : data) {
189 ids.push_back(id);
190 }
191 return ids;
192 }
193
201 inline auto find(u64 id) { return data.find(id); }
202
208 inline auto not_found() { return data.end(); }
209
219 inline T &get(u64 id) {
220 try {
221 return data.at(id);
222 } catch (std::out_of_range &) {
223
224 std::vector<u64> id_list;
225
226 for_each([&](u64 id, T &) {
227 id_list.push_back(id);
228 });
229
231 "The querried id {} does not exist, current id list is {}", id, id_list));
232 }
233 }
234
236 inline const T &get(u64 id) const {
237 try {
238 return data.at(id);
239 } catch (std::out_of_range &) {
240
241 std::vector<u64> id_list;
242
243 for_each([&](u64 id, const T &) {
244 id_list.push_back(id);
245 });
246
248 "The querried id {} does not exist, current id list is {}", id, id_list));
249 }
250 }
251
259 inline bool has_key(u64 id) const { return (data.find(id) != data.end()); }
260
266 inline u64 get_element_count() const { return data.size(); }
267
273 inline bool is_empty() const { return data.empty(); }
274
293 template<typename... Tf>
294 inline void print_data(fmt::format_string<Tf...> fmt) const {
295 for_each([&](u64 id_patch, const T &ref) {
296 shambase::println(sham::format("{} -> {}", id_patch, sham::format(fmt, ref)));
297 });
298 }
299
323 template<class Tmap>
324 inline DistributedData<Tmap> map(std::function<Tmap(u64, T &)> map_func) {
326 ret.reserve(data.size());
327
328 for_each([&](u64 id, T &ref) {
329 ret.add_obj(id, map_func(id, ref));
330 });
331
332 return ret;
333 }
334
336 template<class Tmap>
337 inline DistributedData<Tmap> map(std::function<Tmap(u64, const T &)> map_func) const {
339 ret.reserve(data.size());
340
341 for_each([&](u64 id, const T &ref) {
342 ret.add_obj(id, map_func(id, ref));
343 });
344
345 return ret;
346 }
347
353 inline void reset() {
354 invalidate_sorted_ids();
355 // swap with a fresh map rather than clear() to also release the buckets, so that the
356 // iteration order after a reset does not depend on what the collection held before
357 std::unordered_map<u64, T>{}.swap(data);
358 }
359 };
360
381 template<class T1, class FuncMissing, class FuncMatch, class FuncExtra>
384 const std::vector<u64> &ref_ids,
385 FuncMissing &&func_missing,
386 FuncMatch &&func_match,
387 FuncExtra &&func_extra) {
388
389 std::vector<u64> missing;
390 std::vector<u64> matching;
391 std::vector<u64> extra;
392
393 // Only used for membership tests, its iteration order is never observed.
394 std::unordered_set<u64> ref_seen;
395 ref_seen.reserve(ref_ids.size());
396
397 // missing and matching follow the order of ref_ids, a repeated reference id is
398 // matched at most once and reported as missing afterwards
399 for (u64 id : ref_ids) {
400 bool first_occurrence = ref_seen.insert(id).second;
401 if (first_occurrence && dd.has_key(id)) {
402 matching.push_back(id);
403 } else {
404 missing.push_back(id);
405 }
406 }
407
408 // extra follows the iteration order of dd
409 dd.for_each([&](u64 id, const T1 &) {
410 if (ref_seen.find(id) == ref_seen.end()) {
411 extra.push_back(id);
412 }
413 });
414
415 for (auto id : missing) {
416 func_missing(id);
417 }
418
419 for (auto id : matching) {
420 func_match(id);
421 }
422
423 for (auto id : extra) {
424 func_extra(id);
425 }
426 }
427
445 template<class T1, class T2, class FuncMissing, class FuncMatch, class FuncExtra>
448 const shambase::DistributedData<T2> &reference,
449 FuncMissing &&func_missing,
450 FuncMatch &&func_match,
451 FuncExtra &&func_extra) {
452
453 std::vector<u64> missing;
454 std::vector<u64> matching;
455 std::vector<u64> extra;
456
457 // missing and matching follow the iteration order of reference
458 reference.for_each([&](u64 id, const T2 &) {
459 if (dd.has_key(id)) {
460 matching.push_back(id);
461 } else {
462 missing.push_back(id);
463 }
464 });
465
466 // extra follows the iteration order of dd
467 dd.for_each([&](u64 id, const T1 &) {
468 if (!reference.has_key(id)) {
469 extra.push_back(id);
470 }
471 });
472
473 for (auto id : missing) {
474 func_missing(id);
475 }
476
477 for (auto id : matching) {
478 func_match(id);
479 }
480
481 for (auto id : extra) {
482 func_extra(id);
483 }
484 }
485
486} // namespace shambase
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.
std::unordered_map< u64, T > & get_native()
Returns the underlying collection.
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.
void for_each_ordered(std::function< void(u64, const T &)> &&f) const
Same as for_each_ordered 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(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.
u64 get_element_count() const
Returns the number of elements in the collection.
void for_each(std::function< void(u64, T &)> &&f)
Applies a function to each object in the collection.
bool is_empty() const
Returns true if the collection is empty.
void reserve(u64 count)
Reserve storage for at least count objects.
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.
bool has_key(u64 id) const
Checks if an object exists in the collection.
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.
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 println(std::string_view sv)
Prints a string to the console followed by a newline.
Definition print.cpp:37
void on_distributeddata_diff(const shambase::DistributedData< T1 > &dd, const shambase::DistributedData< T2 > &reference, FuncMissing &&func_missing, FuncMatch &&func_match, FuncExtra &&func_extra)
Compare two distributed data and apply callbacks based on the difference.
void on_distributeddata_ids_diff(const shambase::DistributedData< T1 > &dd, const std::vector< u64 > &ref_ids, FuncMissing &&func_missing, FuncMatch &&func_match, FuncExtra &&func_extra)
Compare two distributed data and apply callbacks based on the difference.
ExcptTypes make_except_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Create an exception with a message and a location.