24#include <unordered_map>
25#include <unordered_set>
49 std::unordered_map<u64, T> data;
51 using iterator =
typename std::unordered_map<u64, T>::iterator;
56 mutable std::shared_ptr<const std::vector<u64>> sorted_ids_cache;
59 inline void invalidate_sorted_ids() { sorted_ids_cache.
reset(); }
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));
68 return sorted_ids_cache;
81 invalidate_sorted_ids();
97 invalidate_sorted_ids();
98 std::pair<iterator, bool> ret = data.emplace(
id, std::forward<T>(obj));
125 invalidate_sorted_ids();
139 for (
auto &[
id, obj] : data) {
145 inline void for_each(std::function<
void(
u64,
const T &)> &&f)
const {
146 for (
auto &[
id, obj] : data) {
166 std::shared_ptr<const std::vector<u64>> ids = get_sorted_ids();
167 for (
u64 id : *ids) {
174 std::shared_ptr<const std::vector<u64>> ids = get_sorted_ids();
175 for (
u64 id : *ids) {
186 std::vector<u64> ids;
187 ids.reserve(data.size());
188 for (
const auto &[
id, _] : data) {
201 inline auto find(
u64 id) {
return data.find(
id); }
222 }
catch (std::out_of_range &) {
224 std::vector<u64> id_list;
227 id_list.push_back(
id);
231 "The querried id {} does not exist, current id list is {}",
id, id_list));
239 }
catch (std::out_of_range &) {
241 std::vector<u64> id_list;
244 id_list.push_back(
id);
248 "The querried id {} does not exist, current id list is {}",
id, id_list));
259 inline bool has_key(
u64 id)
const {
return (data.find(
id) != data.end()); }
273 inline bool is_empty()
const {
return data.empty(); }
293 template<
typename... Tf>
294 inline void print_data(fmt::format_string<Tf...> fmt)
const {
329 ret.
add_obj(
id, map_func(
id, ref));
342 ret.
add_obj(
id, map_func(
id, ref));
354 invalidate_sorted_ids();
357 std::unordered_map<u64, T>{}.swap(data);
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) {
389 std::vector<u64> missing;
390 std::vector<u64> matching;
391 std::vector<u64> extra;
394 std::unordered_set<u64> ref_seen;
395 ref_seen.reserve(ref_ids.size());
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);
404 missing.push_back(
id);
410 if (ref_seen.find(
id) == ref_seen.end()) {
415 for (
auto id : missing) {
419 for (
auto id : matching) {
423 for (
auto id : extra) {
445 template<
class T1,
class T2,
class FuncMissing,
class FuncMatch,
class FuncExtra>
449 FuncMissing &&func_missing,
450 FuncMatch &&func_match,
451 FuncExtra &&func_extra) {
453 std::vector<u64> missing;
454 std::vector<u64> matching;
455 std::vector<u64> extra;
460 matching.push_back(
id);
462 missing.push_back(
id);
473 for (
auto id : missing) {
477 for (
auto id : matching) {
481 for (
auto id : extra) {
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.
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.