From 4d82c7e2704c035e33b9b606c409e5fac0f4f708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 9 Oct 2022 23:02:54 +0200 Subject: Stored set --- include/erebos/merge.h | 20 ++++++++++ include/erebos/set.h | 96 ++++++++++++++++++++++++++++++++++++++++++++++++ include/erebos/storage.h | 10 +++++ 3 files changed, 126 insertions(+) create mode 100644 include/erebos/merge.h create mode 100644 include/erebos/set.h (limited to 'include') diff --git a/include/erebos/merge.h b/include/erebos/merge.h new file mode 100644 index 0000000..bef8212 --- /dev/null +++ b/include/erebos/merge.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace erebos +{ + +template struct Mergeable +{ +}; + +template<> struct Mergeable>> +{ + using Component = Object; + + static vector> components(const vector> & x) { return x; } + static vector> merge(const vector> & x) { return x; } +}; + +} diff --git a/include/erebos/set.h b/include/erebos/set.h new file mode 100644 index 0000000..f625cb0 --- /dev/null +++ b/include/erebos/set.h @@ -0,0 +1,96 @@ +#pragma once + +#include +#include + +namespace erebos +{ + +class SetViewBase; +template class SetView; + +class SetBase +{ +protected: + struct Priv; + + SetBase(); + SetBase(const vector &); + SetBase(shared_ptr); + + shared_ptr add(Storage &, const vector &) const; + + vector> toList() const; + +public: + vector digests() const; + +protected: + shared_ptr p; +}; + +template +class Set : public SetBase +{ + Set(shared_ptr p): SetBase(p) {}; +public: + Set() = default; + Set(const vector & refs): SetBase(move(refs)) {} + Set(const Set &) = default; + Set(Set &&) = default; + Set & operator=(const Set &) = default; + Set & operator=(Set &&) = default; + + static Set load(const vector & refs) { return Set(move(refs)); } + + Set add(Storage &, const T &) const; + + template + SetView view(F && cmp) const; +}; + +template +class SetView +{ +public: + template + SetView(F && cmp, const vector> & refs); + + typename vector::const_iterator begin() const { return items.begin(); } + typename vector::const_iterator end() const { return items.end(); } + +private: + vector items; +}; + +template +Set Set::add(Storage & st, const T & x) const +{ + return Set(SetBase::add(st, storedRefs(Mergeable::components(x)))); +} + +template +template +SetView Set::view(F && cmp) const +{ + return SetView(std::move(cmp), toList()); +} + +template +template +SetView::SetView(F && cmp, const vector> & refs) +{ + items.reserve(refs.size()); + for (const auto & crefs : refs) { + vector::Component>> comps; + comps.reserve(crefs.size()); + for (const auto & r : crefs) + comps.push_back(Stored::Component>::load(r)); + + filterAncestors(comps); + items.push_back(Mergeable::merge(comps)); + } + std::sort(items.begin(), items.end(), cmp); +} + +} diff --git a/include/erebos/storage.h b/include/erebos/storage.h index 15ee0bb..735b399 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -668,6 +668,16 @@ WatchedHead::~WatchedHead() T::headTypeId, Head::id(), watcherId); } +template +vector storedRefs(const vector> & v) +{ + vector res; + res.reserve(v.size()); + for (const auto & x : v) + res.push_back(x.ref()); + return res; +} + } namespace std -- cgit v1.2.3