diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2023-01-08 13:20:26 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2023-01-08 17:20:29 +0100 |
commit | 5b1887367680c494e27794252fee8d1aacf088ef (patch) | |
tree | f4c97d2e98cb81a2ce58cd681ebb2898c1db66de | |
parent | 7ec9843046705368f7a0888f8f95f8a0400862ba (diff) |
Property lookup helpers
-rw-r--r-- | include/erebos/merge.h | 53 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/merge.cpp | 26 |
3 files changed, 80 insertions, 0 deletions
diff --git a/include/erebos/merge.h b/include/erebos/merge.h index bef8212..9705e94 100644 --- a/include/erebos/merge.h +++ b/include/erebos/merge.h @@ -2,9 +2,16 @@ #include <erebos/storage.h> +#include <optional> +#include <vector> + namespace erebos { +using std::nullopt; +using std::optional; +using std::vector; + template<class T> struct Mergeable { }; @@ -17,4 +24,50 @@ template<> struct Mergeable<vector<Stored<Object>>> static vector<Stored<Object>> merge(const vector<Stored<Object>> & x) { return x; } }; +vector<Stored<Object>> findPropertyObjects(const vector<Stored<Object>> & leaves, const string & prop); + +template<typename T> +optional<Stored<typename Mergeable<T>::Component>> findPropertyComponent(const vector<Stored<typename Mergeable<T>::Component>> & components, const string & prop) +{ + vector<Stored<Object>> leaves; + leaves.reserve(components.size()); + + for (const auto & c : components) + leaves.push_back(Stored<Object>::load(c.ref())); + + auto candidates = findPropertyObjects(leaves, prop); + if (!candidates.empty()) + return Stored<typename Mergeable<T>::Component>::load(candidates[0].ref()); + return nullopt; +} + +template<typename T> +optional<Stored<typename Mergeable<T>::Component>> findPropertyComponent(const T & x, const string & prop) +{ + return findPropertyComponent(x.components(), prop); +} + +template<typename T> +vector<Stored<typename Mergeable<T>::Component>> findPropertyComponents(const vector<Stored<typename Mergeable<T>::Component>> & components, const string & prop) +{ + vector<Stored<Object>> leaves; + leaves.reserve(components.size()); + + for (const auto & c : components) + leaves.push_back(Stored<Object>::load(c.ref())); + + auto candidates = findPropertyObjects(leaves, prop); + vector<Stored<typename Mergeable<T>::Component>> result; + result.reserve(candidates.size()); + for (const auto & obj : candidates) + result.push_back(Stored<typename Mergeable<T>::Component>::load(obj.ref())); + return result; +} + +template<typename T> +vector<Stored<typename Mergeable<T>::Component>> findPropertyComponents(const T & x, const string & prop) +{ + return findPropertyComponents(x.components(), prop); +} + } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 72094b8..cb0db6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -8,6 +8,7 @@ add_library(erebos contact.cpp frp.cpp identity.cpp + merge.cpp message.cpp network.cpp pairing.cpp diff --git a/src/merge.cpp b/src/merge.cpp new file mode 100644 index 0000000..040ebb4 --- /dev/null +++ b/src/merge.cpp @@ -0,0 +1,26 @@ +#include <erebos/merge.h> + +namespace erebos { + +static void findPropertyObjects(vector<Stored<Object>> & candidates, const Stored<Object> & obj, const string & prop) +{ + if (auto rec = obj->asRecord()) { + if (rec->item(prop)) { + candidates.push_back(obj); + } else { + for (const auto & r : obj.ref().previous()) + findPropertyObjects(candidates, Stored<Object>::load(r), prop); + } + } +} + +vector<Stored<Object>> findPropertyObjects(const vector<Stored<Object>> & leaves, const string & prop) +{ + vector<Stored<Object>> candidates; + for (const auto & obj : leaves) + findPropertyObjects(candidates, obj, prop); + filterAncestors(candidates); + return candidates; +} + +} |