2018-05-07 06:31:32 +02:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_JSON_TUPLE_KEYS_H
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Array of string literals (in string_views) representing just the keys of a
|
|
|
|
/// tuple. By default construction all keys are included in the array. A
|
|
|
|
/// selection construction will only include keys that are selected. Note that
|
|
|
|
/// the selection construction packs all the chosen keys at the front of the
|
|
|
|
/// array so you cannot rely on this for a key's index into the tuple.
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2018-05-20 02:32:31 +02:00
|
|
|
struct ircd::json::keys
|
2020-10-29 08:08:06 +01:00
|
|
|
:std::array<string_view, T::size()>
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
|
|
|
struct selection;
|
|
|
|
struct include;
|
|
|
|
struct exclude;
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
size_t count() const;
|
2019-01-16 23:48:35 +01:00
|
|
|
bool has(const string_view &) const;
|
2018-05-20 02:32:31 +02:00
|
|
|
|
2018-05-20 03:52:50 +02:00
|
|
|
operator vector_view<const string_view>() const;
|
|
|
|
|
2019-01-17 22:15:02 +01:00
|
|
|
keys(const selection & = {});
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Selection of keys in a tuple represented by a bitset. Users generally
|
|
|
|
/// do not construct this class directly. Instead, construct one of the
|
|
|
|
/// `include` or `exclude` classes which will set these bits appropriately.
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
|
|
|
struct ircd::json::keys<T>::selection
|
|
|
|
:std::bitset<T::size()>
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
2019-08-17 09:28:24 +02:00
|
|
|
template<class closure> constexpr bool until(closure&&) const;
|
|
|
|
template<class closure> constexpr void for_each(closure&&) const;
|
|
|
|
template<class it> constexpr it transform(it, const it end) const;
|
|
|
|
bool has(const string_view &) const;
|
|
|
|
void set(const string_view &, const bool & = true);
|
|
|
|
void set(const size_t &, const bool & = true);
|
2018-05-20 10:37:23 +02:00
|
|
|
|
2019-01-17 22:15:02 +01:00
|
|
|
// Note the default all-bits set.
|
2018-05-20 10:37:23 +02:00
|
|
|
constexpr selection(const uint64_t &val = -1)
|
2020-10-29 08:08:06 +01:00
|
|
|
:std::bitset<T::size()>{val}
|
2018-05-20 10:37:23 +02:00
|
|
|
{}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
static_assert(T::size() <= sizeof(uint64_t) * 8);
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Construct this class with a list of keys you want to select for a given
|
|
|
|
/// tuple. This constructs a bitset representing the keys of the tuple and
|
|
|
|
/// lights the bits for your selections.
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
|
|
|
struct ircd::json::keys<T>::include
|
2018-05-07 06:31:32 +02:00
|
|
|
:selection
|
|
|
|
{
|
2018-05-20 04:17:07 +02:00
|
|
|
include(const vector_view<const string_view> &list)
|
2018-05-20 10:37:23 +02:00
|
|
|
:selection{0}
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
2018-05-20 10:37:23 +02:00
|
|
|
assert(this->none());
|
2018-05-07 06:31:32 +02:00
|
|
|
for(const auto &key : list)
|
2019-08-17 09:28:24 +02:00
|
|
|
this->set(key, true);
|
2018-05-07 06:31:32 +02:00
|
|
|
}
|
2018-05-20 06:19:47 +02:00
|
|
|
|
|
|
|
include(const std::initializer_list<const string_view> &list)
|
|
|
|
:include(vector_view<const string_view>(list))
|
|
|
|
{}
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
/// Construct this class with a list of keys you want to deselect for a given
|
|
|
|
/// tuple. This constructs a bitset representing the keys of the tuple and
|
|
|
|
/// lights the bits which are not in the list.
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
|
|
|
struct ircd::json::keys<T>::exclude
|
2018-05-07 06:31:32 +02:00
|
|
|
:selection
|
|
|
|
{
|
2018-05-20 04:17:07 +02:00
|
|
|
exclude(const vector_view<const string_view> &list)
|
2018-05-20 10:37:23 +02:00
|
|
|
:selection{}
|
2018-05-07 06:31:32 +02:00
|
|
|
{
|
2018-05-20 10:37:23 +02:00
|
|
|
assert(this->all());
|
2018-05-07 06:31:32 +02:00
|
|
|
for(const auto &key : list)
|
2019-08-17 09:28:24 +02:00
|
|
|
this->set(key, false);
|
2018-05-07 06:31:32 +02:00
|
|
|
}
|
2018-05-20 06:19:47 +02:00
|
|
|
|
|
|
|
exclude(const std::initializer_list<const string_view> &list)
|
|
|
|
:exclude(vector_view<const string_view>(list))
|
|
|
|
{}
|
2018-05-07 06:31:32 +02:00
|
|
|
};
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
//
|
|
|
|
// selection
|
|
|
|
//
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2019-08-17 09:28:24 +02:00
|
|
|
void
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::set(const string_view &key,
|
|
|
|
const bool &val)
|
2019-08-17 09:28:24 +02:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
this->set(json::indexof<T>(key), val);
|
2019-08-17 09:28:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2019-08-17 09:28:24 +02:00
|
|
|
void
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::set(const size_t &pos,
|
|
|
|
const bool &val)
|
2019-08-17 09:28:24 +02:00
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
this->std::bitset<T::size()>::set(pos, val);
|
2019-08-17 09:28:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2019-08-17 09:28:24 +02:00
|
|
|
bool
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::has(const string_view &key)
|
2019-08-17 09:28:24 +02:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
return this->test(json::indexof<T>(key));
|
2019-08-17 09:28:24 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2019-01-17 22:15:02 +01:00
|
|
|
template<class it>
|
|
|
|
constexpr it
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::transform(it i,
|
|
|
|
const it end)
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
2019-01-17 22:15:02 +01:00
|
|
|
this->until([&i, &end](auto&& key)
|
2018-05-20 02:32:31 +02:00
|
|
|
{
|
2019-01-17 22:15:02 +01:00
|
|
|
if(i == end)
|
2018-05-20 02:32:31 +02:00
|
|
|
return false;
|
|
|
|
|
2019-01-17 22:15:02 +01:00
|
|
|
*i = key;
|
|
|
|
++i;
|
2018-05-20 02:32:31 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2019-01-17 22:15:02 +01:00
|
|
|
return i;
|
2018-05-20 02:32:31 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2018-05-20 02:32:31 +02:00
|
|
|
template<class closure>
|
2018-05-20 10:37:23 +02:00
|
|
|
constexpr void
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::for_each(closure&& function)
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->until([&function](auto&& key)
|
|
|
|
{
|
|
|
|
function(key);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2018-05-20 02:32:31 +02:00
|
|
|
template<class closure>
|
2018-05-20 10:37:23 +02:00
|
|
|
constexpr bool
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::selection::until(closure&& function)
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
2020-10-29 08:08:06 +01:00
|
|
|
for(size_t i(0); i < T::size(); ++i)
|
2018-05-20 02:32:31 +02:00
|
|
|
if(this->test(i))
|
2020-10-29 08:08:06 +01:00
|
|
|
if(!function(key<T>(i)))
|
2018-05-20 02:32:31 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// keys
|
|
|
|
//
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
|
|
|
ircd::json::keys<T>::keys(const selection &selection)
|
2018-05-20 02:32:31 +02:00
|
|
|
{
|
|
|
|
selection.transform(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
|
|
|
ircd::json::keys<T>::operator
|
2018-05-20 03:52:50 +02:00
|
|
|
vector_view<const string_view>()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return { this->data(), this->count() };
|
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2019-01-16 23:48:35 +01:00
|
|
|
bool
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::has(const string_view &key)
|
2019-01-16 23:48:35 +01:00
|
|
|
const
|
|
|
|
{
|
|
|
|
const auto &start
|
|
|
|
{
|
2019-01-17 22:15:02 +01:00
|
|
|
this->begin()
|
2019-01-16 23:48:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const auto &stop
|
|
|
|
{
|
|
|
|
start + this->count()
|
|
|
|
};
|
|
|
|
|
2019-01-17 22:15:02 +01:00
|
|
|
assert(!empty(key));
|
|
|
|
return stop != std::find(start, stop, key);
|
2019-01-16 23:48:35 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 08:08:06 +01:00
|
|
|
template<class T>
|
2018-05-20 02:32:31 +02:00
|
|
|
size_t
|
2020-10-29 08:08:06 +01:00
|
|
|
ircd::json::keys<T>::count()
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t i(0);
|
2020-04-03 07:18:55 +02:00
|
|
|
#pragma clang loop unroll (disable)
|
2018-05-20 02:32:31 +02:00
|
|
|
for(; i < this->size(); ++i)
|
|
|
|
if(!(*this)[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|