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-11-14 09:58:19 +01:00
|
|
|
namespace ircd::json
|
|
|
|
{
|
|
|
|
template<class tuple> struct keys;
|
|
|
|
}
|
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.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys
|
2018-05-20 10:37:23 +02:00
|
|
|
:std::array<string_view, tuple::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.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::selection
|
|
|
|
:std::bitset<tuple::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)
|
|
|
|
:std::bitset<tuple::size()>{val}
|
|
|
|
{}
|
|
|
|
|
|
|
|
static_assert(tuple::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.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::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.
|
|
|
|
template<class tuple>
|
|
|
|
struct ircd::json::keys<tuple>::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
|
|
|
|
//
|
|
|
|
|
2019-08-17 09:28:24 +02:00
|
|
|
template<class tuple>
|
|
|
|
void
|
|
|
|
ircd::json::keys<tuple>::selection::set(const string_view &key,
|
|
|
|
const bool &val)
|
|
|
|
{
|
|
|
|
this->set(json::indexof<tuple>(key), val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
void
|
|
|
|
ircd::json::keys<tuple>::selection::set(const size_t &pos,
|
|
|
|
const bool &val)
|
|
|
|
{
|
|
|
|
this->std::bitset<tuple::size()>::set(pos, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
bool
|
|
|
|
ircd::json::keys<tuple>::selection::has(const string_view &key)
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return this->test(json::indexof<tuple>(key));
|
|
|
|
}
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
template<class tuple>
|
2019-01-17 22:15:02 +01:00
|
|
|
template<class it>
|
|
|
|
constexpr it
|
|
|
|
ircd::json::keys<tuple>::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
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
template<class closure>
|
2018-05-20 10:37:23 +02:00
|
|
|
constexpr void
|
2019-01-17 22:15:02 +01:00
|
|
|
ircd::json::keys<tuple>::selection::for_each(closure&& function)
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
this->until([&function](auto&& key)
|
|
|
|
{
|
|
|
|
function(key);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
template<class closure>
|
2018-05-20 10:37:23 +02:00
|
|
|
constexpr bool
|
2019-01-17 22:15:02 +01:00
|
|
|
ircd::json::keys<tuple>::selection::until(closure&& function)
|
2018-05-20 02:32:31 +02:00
|
|
|
const
|
|
|
|
{
|
|
|
|
for(size_t i(0); i < tuple::size(); ++i)
|
|
|
|
if(this->test(i))
|
|
|
|
if(!function(key<tuple>(i)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// keys
|
|
|
|
//
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
ircd::json::keys<tuple>::keys(const selection &selection)
|
|
|
|
{
|
|
|
|
selection.transform(this->begin(), this->end());
|
|
|
|
}
|
|
|
|
|
2018-05-20 03:52:50 +02:00
|
|
|
template<class tuple>
|
|
|
|
ircd::json::keys<tuple>::operator
|
|
|
|
vector_view<const string_view>()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return { this->data(), this->count() };
|
|
|
|
}
|
|
|
|
|
2019-01-16 23:48:35 +01:00
|
|
|
template<class tuple>
|
|
|
|
bool
|
|
|
|
ircd::json::keys<tuple>::has(const string_view &key)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-20 02:32:31 +02:00
|
|
|
template<class tuple>
|
|
|
|
size_t
|
|
|
|
ircd::json::keys<tuple>::count()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
size_t i(0);
|
|
|
|
for(; i < this->size(); ++i)
|
|
|
|
if(!(*this)[i])
|
|
|
|
break;
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|