From 3357cbc91e7ff4d0d455c88785fc455067b34820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 14 Mar 2020 21:14:52 +0100 Subject: Storage: filter ancestors and overwritable Ref and Stored Simple but slow implementation of the filtering for now. --- include/erebos/storage.h | 76 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 19 deletions(-) (limited to 'include/erebos') diff --git a/include/erebos/storage.h b/include/erebos/storage.h index b6595b6..4d7c691 100644 --- a/include/erebos/storage.h +++ b/include/erebos/storage.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -120,7 +121,9 @@ class PartialRef { public: PartialRef(const PartialRef &) = default; - PartialRef & operator=(const PartialRef &) = delete; + PartialRef(PartialRef &&) = default; + PartialRef & operator=(const PartialRef &) = default; + PartialRef & operator=(PartialRef &&) = default; static PartialRef create(PartialStorage, const Digest &); @@ -135,7 +138,7 @@ public: protected: friend class Storage; struct Priv; - const std::shared_ptr p; + std::shared_ptr p; PartialRef(const std::shared_ptr p): p(p) {} }; @@ -143,7 +146,9 @@ class Ref : public PartialRef { public: Ref(const Ref &) = default; - Ref & operator=(const Ref &) = delete; + Ref(Ref &&) = default; + Ref & operator=(const Ref &) = default; + Ref & operator=(Ref &&) = default; static std::optional create(Storage, const Digest &); @@ -208,7 +213,7 @@ public: name(name), value(value) {} template Item(const std::string & name, const Stored & value): - Item(name, value.ref) {} + Item(name, value.ref()) {} Item(const Item &) = default; Item & operator=(const Item &) = delete; @@ -327,33 +332,42 @@ std::optional> RecordT::Item::as() const template class Stored { - Stored(Ref ref, std::shared_ptr val): ref(ref), val(val) {} + Stored(Ref ref, std::shared_ptr val): mref(ref), mval(val) {} friend class Storage; public: + Stored(const Stored &) = default; + Stored(Stored &&) = default; + Stored & operator=(const Stored &) = default; + Stored & operator=(Stored &&) = default; + static std::optional> load(const Ref &); Ref store(const Storage &) const; bool operator==(const Stored & other) const - { return ref.digest() == other.ref.digest(); } + { return mref.digest() == other.mref.digest(); } bool operator!=(const Stored & other) const - { return ref.digest() != other.ref.digest(); } + { return mref.digest() != other.mref.digest(); } bool operator<(const Stored & other) const - { return ref.digest() < other.ref.digest(); } + { return mref.digest() < other.mref.digest(); } bool operator<=(const Stored & other) const - { return ref.digest() <= other.ref.digest(); } + { return mref.digest() <= other.mref.digest(); } bool operator>(const Stored & other) const - { return ref.digest() > other.ref.digest(); } + { return mref.digest() > other.mref.digest(); } bool operator>=(const Stored & other) const - { return ref.digest() >= other.ref.digest(); } + { return mref.digest() >= other.mref.digest(); } - const T & operator*() const { return *val; } - const T * operator->() const { return val.get(); } + const T & operator*() const { return *mval; } + const T * operator->() const { return mval.get(); } std::vector> previous() const; bool precedes(const Stored &) const; - const Ref ref; - const std::shared_ptr val; + const Ref & ref() const { return mref; } + const std::shared_ptr & value() const { return mval; } + +private: + Ref mref; + std::shared_ptr mval; }; template @@ -373,15 +387,15 @@ std::optional> Stored::load(const Ref & ref) template Ref Stored::store(const Storage & st) const { - if (st == ref.storage()) - return ref; - return st.storeObject(*ref); + if (st == mref.storage()) + return mref; + return st.storeObject(*mref); } template std::vector> Stored::previous() const { - auto rec = ref->asRecord(); + auto rec = mref->asRecord(); if (!rec) return {}; @@ -415,6 +429,30 @@ bool Stored::precedes(const Stored & other) const return false; } +template +void filterAncestors(std::vector> & xs) +{ + if (xs.size() < 2) + return; + + std::sort(xs.begin(), xs.end()); + xs.erase(std::unique(xs.begin(), xs.end()), xs.end()); + + std::vector> old; + old.swap(xs); + + for (auto i = old.begin(); i != old.end(); i++) { + bool add = true; + for (auto j = i + 1; j != old.end(); j++) + if (i->precedes(*j)) { + add = false; + break; + } + if (add) + xs.push_back(std::move(*i)); + } +} + } namespace std -- cgit v1.2.3