0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 23:44:01 +01:00

ircd::json: Add merge constructor.

This commit is contained in:
Jason Volk 2017-03-21 01:54:10 -07:00
parent 4d2e41970b
commit 7ae5785431
2 changed files with 27 additions and 0 deletions

View file

@ -61,8 +61,11 @@ struct obj
explicit operator std::string() const; explicit operator std::string() const;
IRCD_OVERLOAD(merge)
obj(std::initializer_list<member>); obj(std::initializer_list<member>);
obj(const doc &d, const bool &recurse = false); obj(const doc &d, const bool &recurse = false);
obj(merge_t, const std::initializer_list<doc> &);
obj() = default; obj() = default;
obj(obj &&) = default; obj(obj &&) = default;
obj(const obj &) = delete; obj(const obj &) = delete;

View file

@ -559,6 +559,30 @@ ircd::json::operator<<(std::ostream &s, const obj &obj)
return s; return s;
} }
ircd::json::obj::obj(merge_t,
const std::initializer_list<doc> &merge)
:idx
{
std::accumulate(std::begin(merge), std::end(merge), size_t(0), []
(auto sum, const auto &doc)
{
return sum += doc.count();
})
}
{
auto it(std::begin(idx));
for(auto dit(std::rbegin(merge)); dit != std::rend(merge); ++dit)
for(const doc::member &m : *dit)
{
*it = obj::member{m};
++it;
}
std::stable_sort(std::begin(idx), std::end(idx));
const auto e(std::unique(std::begin(idx), std::end(idx)));
idx.erase(e, std::end(idx));
}
ircd::json::obj::obj(const doc &doc, ircd::json::obj::obj(const doc &doc,
const bool &recurse) const bool &recurse)
:idx{doc.count()} :idx{doc.count()}