From 159bebbd26b8a0c51311753d7171649ca685147e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 16 Dec 2020 17:06:10 -0800 Subject: [PATCH] ircd::m::event: Add more ergonomic bulk id/idx extraction interface. --- include/ircd/m/event/auth.h | 42 ++++++++++++++++++++++++++++++++++++ include/ircd/m/event/event.h | 2 +- include/ircd/m/event/prev.h | 42 ++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 1 deletion(-) diff --git a/include/ircd/m/event/auth.h b/include/ircd/m/event/auth.h index 8f210f83f..058d149f6 100644 --- a/include/ircd/m/event/auth.h +++ b/include/ircd/m/event/auth.h @@ -37,6 +37,8 @@ struct ircd::m::event::auth 5 }; + template vector_view ids(event::id (&)[N]) const; + template vector_view idxs(event::idx (&)[N]) const; std::tuple auth_events(const size_t &idx) const; event::id auth_event(const size_t &idx) const; bool auth_event_exists(const size_t &idx) const; @@ -48,3 +50,43 @@ struct ircd::m::event::auth using super_type::tuple; using super_type::operator=; }; + +template +inline ircd::vector_view +ircd::m::event::auth::idxs(event::idx (&out)[N]) +const +{ + event::id buf[N]; + const auto &ids + { + auth::ids(buf) + }; + + const auto &found + { + m::index(out, ids) + }; + + return vector_view + ( + out, out + ids.size() + ); +} + +template +inline ircd::vector_view +ircd::m::event::auth::ids(event::id (&out)[N]) +const +{ + size_t i(0); + m::for_each(*this, [&i, &out](const event::id &event_id) + { + out[i++] = event_id; + return i < N; + }); + + return vector_view + ( + out, i + ); +} diff --git a/include/ircd/m/event/event.h b/include/ircd/m/event/event.h index f466c2c8c..a0b826e19 100644 --- a/include/ircd/m/event/event.h +++ b/include/ircd/m/event/event.h @@ -156,11 +156,11 @@ struct ircd::m::event event() = default; }; +#include "index.h" #include "auth.h" #include "prev.h" #include "refs.h" #include "horizon.h" -#include "index.h" #include "event_id.h" #include "fetch.h" #include "cached.h" diff --git a/include/ircd/m/event/prev.h b/include/ircd/m/event/prev.h index 9acde3595..7772b9298 100644 --- a/include/ircd/m/event/prev.h +++ b/include/ircd/m/event/prev.h @@ -44,6 +44,8 @@ struct ircd::m::event::prev 20 }; + template vector_view ids(event::id (&)[N]) const; + template vector_view idxs(event::idx (&)[N]) const; std::tuple prev_events(const size_t &idx) const; event::id prev_event(const size_t &idx) const; bool prev_event_exists(const size_t &idx) const; @@ -55,3 +57,43 @@ struct ircd::m::event::prev using super_type::tuple; using super_type::operator=; }; + +template +inline ircd::vector_view +ircd::m::event::prev::idxs(event::idx (&out)[N]) +const +{ + event::id buf[N]; + const auto &ids + { + prev::ids(buf) + }; + + const auto &found + { + m::index(out, ids) + }; + + return vector_view + ( + out, out + ids.size() + ); +} + +template +inline ircd::vector_view +ircd::m::event::prev::ids(event::id (&out)[N]) +const +{ + size_t i(0); + m::for_each(*this, [&i, &out](const event::id &event_id) + { + out[i++] = event_id; + return i < N; + }); + + return vector_view + ( + out, i + ); +}