0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-16 06:51:08 +01:00

ircd::json::tuple: Complete constexpr codepaths for keys selections.

This commit is contained in:
Jason Volk 2022-07-12 15:46:14 -07:00
parent fe8c11dc94
commit 6cfd037ebd
3 changed files with 45 additions and 33 deletions

View file

@ -41,7 +41,7 @@ noexcept
template<class tuple, template<class tuple,
size_t i = 0, size_t i = 0,
size_t N> size_t N>
inline enable_if_tuple<tuple, size_t> inline constexpr enable_if_tuple<tuple, size_t>
indexof(const char (&name)[N]) indexof(const char (&name)[N])
noexcept noexcept
{ {
@ -54,7 +54,7 @@ noexcept
template<class tuple, template<class tuple,
size_t i = 0> size_t i = 0>
inline enable_if_tuple<tuple, size_t> inline constexpr enable_if_tuple<tuple, size_t>
indexof(const string_view &name) indexof(const string_view &name)
noexcept noexcept
{ {

View file

@ -25,7 +25,7 @@ noexcept
template<size_t i, template<size_t i,
class tuple> class tuple>
inline enable_if_tuple<tuple, const char *> inline constexpr enable_if_tuple<tuple, const char *>
key(const tuple &t) key(const tuple &t)
noexcept noexcept
{ {
@ -34,7 +34,7 @@ noexcept
template<class tuple, template<class tuple,
size_t i> size_t i>
inline typename std::enable_if<i == tuple::size(), const char *>::type inline constexpr typename std::enable_if<i == tuple::size(), const char *>::type
key(const size_t &j) key(const size_t &j)
noexcept noexcept
{ {
@ -43,7 +43,7 @@ noexcept
template<class tuple, template<class tuple,
size_t i = 0> size_t i = 0>
inline typename std::enable_if<i < tuple::size(), const char *>::type inline constexpr typename std::enable_if<i < tuple::size(), const char *>::type
key(const size_t &j) key(const size_t &j)
noexcept noexcept
{ {

View file

@ -37,17 +37,17 @@ struct ircd::json::keys
/// `include` or `exclude` classes which will set these bits appropriately. /// `include` or `exclude` classes which will set these bits appropriately.
template<class T> template<class T>
struct ircd::json::keys<T>::selection struct ircd::json::keys<T>::selection
:std::bitset<T::size()> :util::bitset<T::size()>
{ {
template<class closure> constexpr bool for_each(closure&&) const; template<class closure> constexpr bool for_each(closure&&) const;
template<class it> constexpr it transform(it, const it end) const; template<class it> constexpr it transform(it, const it end) const;
bool has(const string_view &) const; constexpr bool has(const string_view &) const;
void set(const string_view &, const bool & = true); constexpr void set(const string_view &, const bool = true);
void set(const size_t &, const bool & = true); constexpr void set(const size_t &, const bool = true);
// Note the default all-bits set. // Note the default all-bits set.
constexpr selection(const uint64_t &val = -1) constexpr selection(const uint128_t val = -1)
:std::bitset<T::size()>{val} :util::bitset<T::size()>{val}
{} {}
static_assert(T::size() <= sizeof(uint64_t) * 8); static_assert(T::size() <= sizeof(uint64_t) * 8);
@ -60,16 +60,22 @@ template<class T>
struct ircd::json::keys<T>::include struct ircd::json::keys<T>::include
:selection :selection
{ {
include(const vector_view<const string_view> &list) constexpr include(const vector_view<const string_view> list)
:selection{0} :selection{0}
{ {
assert(this->none()); for(const auto key : list)
for(const auto &key : list) selection::set(key, true);
this->set(key, true);
} }
include(const std::initializer_list<const string_view> &list) constexpr include(const std::initializer_list<const string_view> list)
:include(vector_view<const string_view>(list)) :selection{0}
{
for(const auto key : list)
selection::set(key, true);
}
constexpr include()
:selection{0}
{} {}
}; };
@ -80,16 +86,22 @@ template<class T>
struct ircd::json::keys<T>::exclude struct ircd::json::keys<T>::exclude
:selection :selection
{ {
exclude(const vector_view<const string_view> &list) constexpr exclude(const vector_view<const string_view> list)
:selection{} :selection{}
{ {
assert(this->all()); for(const auto key : list)
for(const auto &key : list) selection::set(key, false);
this->set(key, false);
} }
exclude(const std::initializer_list<const string_view> &list) constexpr exclude(const std::initializer_list<const string_view> list)
:exclude(vector_view<const string_view>(list)) :selection{}
{
for(const auto key : list)
selection::set(key, false);
}
constexpr exclude()
:selection{}
{} {}
}; };
@ -98,27 +110,27 @@ struct ircd::json::keys<T>::exclude
// //
template<class T> template<class T>
inline void inline constexpr void
ircd::json::keys<T>::selection::set(const string_view &key, ircd::json::keys<T>::selection::set(const string_view &key,
const bool &val) const bool val)
{ {
this->set(json::indexof<T>(key), val); selection::set(json::indexof<T>(key), val);
} }
template<class T> template<class T>
inline void inline constexpr void
ircd::json::keys<T>::selection::set(const size_t &pos, ircd::json::keys<T>::selection::set(const size_t &pos,
const bool &val) const bool val)
{ {
this->std::bitset<T::size()>::set(pos, val); util::bitset<T::size()>::set(pos, val);
} }
template<class T> template<class T>
inline bool inline constexpr bool
ircd::json::keys<T>::selection::has(const string_view &key) ircd::json::keys<T>::selection::has(const string_view &key)
const const
{ {
return this->test(json::indexof<T>(key)); return util::bitset<T::size()>::test(json::indexof<T>(key));
} }
template<class T> template<class T>
@ -128,7 +140,7 @@ ircd::json::keys<T>::selection::transform(it i,
const it end) const it end)
const const
{ {
this->for_each([&i, &end](auto&& key) for_each([&i, &end](auto&& key)
{ {
if(i == end) if(i == end)
return false; return false;
@ -148,7 +160,7 @@ ircd::json::keys<T>::selection::for_each(closure&& function)
const const
{ {
for(size_t i(0); i < T::size(); ++i) for(size_t i(0); i < T::size(); ++i)
if(this->test(i)) if(util::bitset<T::size()>::test(i))
if(!function(key<T>(i))) if(!function(key<T>(i)))
return false; return false;