From 88c9b1acd2b372d1405a40fb3e3802d411709fd4 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 2 Dec 2021 17:56:44 +0300 Subject: Add support for three-dimensional containers emulation --- odb/nested-container.hxx | 169 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 132 insertions(+), 37 deletions(-) diff --git a/odb/nested-container.hxx b/odb/nested-container.hxx index c7627ed..82abf0b 100644 --- a/odb/nested-container.hxx +++ b/odb/nested-container.hxx @@ -19,22 +19,24 @@ namespace odb { // Nested container emulation support for ODB. // - // Note that the outer key in the inner container should strictly - // speaking be a foreign key pointing to the key of the outer - // container. The only way to achieve this currently is to manually - // add the constraint via ALTER TABLE ADD CONSTRAINT. Note, however, - // that as long as we only modify these tables via the ODB container - // interface, not having the foreign key (and not having ON DELETE - // CASCADE) should be harmless (since we have a foreign key pointing - // to the object id). + // In a nutshell, the idea is to represent a nested container, for example, + // vector>, as map where nested_key is a composite + // key consisting of the outer and inner container indexes. // - - // Map key that is used to emulate nested container mapping in ODB. - // Template parameter T is a tag that allows us to distinguish keys - // for unrelated containers in order to assign column names, etc. - // Use inner container type for T. + // Note that the outer key in the inner container should strictly speaking + // be a foreign key pointing to the key of the outer container. The only way + // to achieve this currently is to manually add the constraint via ALTER + // TABLE ADD CONSTRAINT. Note, however, that as long as we only modify these + // tables via the ODB container interface, not having the foreign key (and + // not having ON DELETE CASCADE) should be harmless (since we have a foreign + // key pointing to the object id). + + // Map key that is used to emulate 1-level nested container mapping (for + // example, vector>). Template parameter IC is a tag that allows + // us to distinguish keys for unrelated containers in order to assign column + // names, etc. Use the inner container type (for example, vector) for IC. // - template struct nested_key @@ -54,10 +56,40 @@ namespace odb return outer < v.outer || (outer == v.outer && inner < v.inner); } }; + + // Map key that is used to emulate 2-level nested container mapping (for + // example, vector>>>). Use the middle container type for + // MC (for example, vector>). + // + template + struct nested2_key + { + using outer_type = O; + using middle_type = M; + using inner_type = I; + + outer_type outer; + middle_type middle; + inner_type inner; + + nested2_key () = default; + nested2_key (outer_type o, middle_type m, inner_type i) + : outer (o), middle (m), inner (i) {} + + bool + operator< (const nested2_key& v) const + { + return outer != v.outer ? outer < v.outer : + middle != v.middle ? middle < v.middle : + inner < v.inner ; + } + }; } #include -#include #include // size_t #include // move(), declval() #include @@ -65,46 +97,109 @@ namespace odb namespace odb { - // vector> - // - template - struct nested_value_type: - std::remove_reference ()[0])> {}; + template + struct nested1_type: + std::remove_reference ()[0])> {}; + + template + struct nested2_type: + std::remove_reference ()[0][0])> {}; + + template + struct nested3_type: + std::remove_reference ()[0][0][0])> {}; - template - std::map, typename nested_value_type::type> - nested_get (const std::vector& v) + // 1-level nesting. + // + template // For example, OC = vector>. + std::map::type>, + typename nested2_type::type> + nested_get (const OC& oc) { using namespace std; - using I = typename nested_value_type::type; + using IC = typename nested1_type::type; + using V = typename nested2_type::type; using K = nested_key; - map r; - for (size_t n (0); n != v.size (); ++n) + map r; + for (size_t o (0); o != oc.size (); ++o) { - const IC& o (v[n]); - for (size_t m (0); m != o.size (); ++m) - r.emplace (K (n, m), o[m]); + const IC& ic (oc[o]); + for (size_t i (0); i != ic.size (); ++i) + r.emplace (K (o, i), ic[i]); } return r; } - template + template void - nested_set (std::vector& v, std::map&& r) + nested_set (OC& oc, std::map&& r) { using namespace std; for (auto& p: r) { - size_t n (p.first.outer); - size_t m (p.first.inner); - I& i (p.second); + size_t o (p.first.outer); + size_t i (p.first.inner); + V& v (p.second); + + assert (o < oc.size ()); + assert (i == oc[o].size ()); + oc[o].push_back (move (v)); + } + } + + // 2-level nesting. + // + template // For example, OC = vector>>. + std::map::type>, + typename nested3_type::type> + nested2_get (const OC& oc) + { + using namespace std; + + using MC = typename nested1_type::type; + using V = typename nested3_type::type; + using K = nested2_key; + + map r; + for (size_t o (0); o != oc.size (); ++o) + { + const auto& mc (oc[o]); + for (size_t m (0); m != mc.size (); ++m) + { + const auto& ic (mc[m]); + for (size_t i (0); i != ic.size (); ++i) + r.emplace (K (o, m, i), ic[i]); + } + } + return r; + } + + template + void + nested2_set (OC& oc, std::map&& r) + { + using namespace std; + + for (auto& p: r) + { + size_t o (p.first.outer); + size_t m (p.first.middle); + size_t i (p.first.inner); + V& v (p.second); + + assert (o < oc.size ()); + + auto& mc (oc[o]); + + if (m >= mc.size ()) + mc.resize (m + 1); + + assert (i == mc[m].size ()); - assert (n < v.size ()); - assert (m == v[n].size ()); - v[n].push_back (move (i)); + mc[m].push_back (move (v)); } } } -- cgit v1.1