2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
2018-01-12 11:52:45 +01:00
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
2018-02-04 03:22:01 +01:00
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
2018-01-12 11:52:45 +01:00
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
2018-02-04 03:22:01 +01:00
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-12-12 21:05:15 +01:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-12 11:52:45 +01:00
|
|
|
#define HAVE_IRCD_UTIL_TUPLE_H
|
2017-12-12 21:05:15 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Utilities for std::tuple
|
|
|
|
//
|
|
|
|
|
2018-01-12 11:52:45 +01:00
|
|
|
namespace ircd {
|
2019-06-23 10:01:26 +02:00
|
|
|
inline namespace util {
|
2017-12-12 21:05:15 +01:00
|
|
|
|
2017-12-13 22:32:35 +01:00
|
|
|
template<class tuple>
|
|
|
|
constexpr bool
|
|
|
|
is_tuple()
|
|
|
|
{
|
|
|
|
return is_specialization_of<tuple, std::tuple>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class tuple>
|
|
|
|
constexpr typename std::enable_if<is_tuple<tuple>(), size_t>::type
|
|
|
|
size()
|
|
|
|
{
|
|
|
|
return std::tuple_size<tuple>::value;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class... args>
|
|
|
|
constexpr size_t
|
|
|
|
size(const std::tuple<args...> &t)
|
|
|
|
{
|
|
|
|
return size<std::tuple<args...>>();
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:05:15 +01:00
|
|
|
//
|
|
|
|
// Iteration of a tuple
|
|
|
|
//
|
|
|
|
// for_each(tuple, [](auto&& elem) { ... });
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr inline bool
|
2017-12-12 21:05:15 +01:00
|
|
|
for_each(const std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
if constexpr(i < size<std::tuple<args...>>())
|
|
|
|
{
|
|
|
|
using closure_result = std::invoke_result_t
|
|
|
|
<
|
|
|
|
decltype(f), decltype(std::get<i>(t))
|
|
|
|
>;
|
|
|
|
|
|
|
|
constexpr bool terminable
|
|
|
|
{
|
|
|
|
std::is_same<closure_result, bool>()
|
|
|
|
};
|
|
|
|
|
|
|
|
if constexpr(terminable)
|
|
|
|
{
|
|
|
|
if(!f(std::get<i>(t)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else f(std::get<i>(t));
|
|
|
|
|
|
|
|
return for_each<i + 1, func, args...>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
else return true;
|
2017-12-12 21:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i = 0,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr inline bool
|
2017-12-12 21:05:15 +01:00
|
|
|
for_each(std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
if constexpr(i < size<std::tuple<args...>>())
|
|
|
|
{
|
|
|
|
using closure_result = std::invoke_result_t
|
|
|
|
<
|
|
|
|
decltype(f), decltype(std::get<i>(t))
|
|
|
|
>;
|
|
|
|
|
|
|
|
constexpr bool terminable
|
|
|
|
{
|
|
|
|
std::is_same<closure_result, bool>()
|
|
|
|
};
|
|
|
|
|
|
|
|
if constexpr(terminable)
|
|
|
|
{
|
|
|
|
if(!f(std::get<i>(t)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else f(std::get<i>(t));
|
|
|
|
|
|
|
|
return for_each<i + 1, func, args...>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
else return true;
|
2017-12-12 21:05:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Circuits for reverse iteration of a tuple
|
|
|
|
//
|
|
|
|
// rfor_each(tuple, [](auto&& elem) { ... });
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
template<class func,
|
|
|
|
class... args,
|
|
|
|
ssize_t i = size<std::tuple<args...>>() - 1>
|
|
|
|
constexpr inline bool
|
2017-12-12 21:05:15 +01:00
|
|
|
rfor_each(const std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
if constexpr(i >= 0)
|
2017-12-12 21:05:15 +01:00
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
using closure_result = std::invoke_result_t
|
|
|
|
<
|
|
|
|
decltype(f), decltype(std::get<i>(t))
|
|
|
|
>;
|
|
|
|
|
|
|
|
constexpr bool terminable
|
|
|
|
{
|
|
|
|
std::is_same<closure_result, bool>()
|
|
|
|
};
|
|
|
|
|
|
|
|
if constexpr(terminable)
|
|
|
|
{
|
|
|
|
if(!f(std::get<i>(t)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else f(std::get<i>(t));
|
|
|
|
|
|
|
|
return rfor_each<func, args..., i - 1>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
else return true;
|
2017-12-12 21:05:15 +01:00
|
|
|
}
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
template<class func,
|
|
|
|
class... args,
|
|
|
|
ssize_t i = size<std::tuple<args...>>() - 1>
|
|
|
|
constexpr inline bool
|
2017-12-12 21:05:15 +01:00
|
|
|
rfor_each(std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
if constexpr(i >= 0)
|
2017-12-12 21:05:15 +01:00
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
using closure_result = std::invoke_result_t
|
|
|
|
<
|
|
|
|
decltype(f), decltype(std::get<i>(t))
|
|
|
|
>;
|
|
|
|
|
|
|
|
constexpr bool terminable
|
|
|
|
{
|
|
|
|
std::is_same<closure_result, bool>()
|
|
|
|
};
|
|
|
|
|
|
|
|
if constexpr(terminable)
|
|
|
|
{
|
|
|
|
if(!f(std::get<i>(t)))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else f(std::get<i>(t));
|
|
|
|
|
|
|
|
return rfor_each<func, args..., i - 1>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
else return true;
|
2017-12-12 21:05:15 +01:00
|
|
|
}
|
|
|
|
|
2017-12-13 22:32:35 +01:00
|
|
|
//
|
2022-06-30 21:21:14 +02:00
|
|
|
// test() is a logical inversion of for_each() for intuitive find()-like
|
2017-12-13 22:32:35 +01:00
|
|
|
// boolean semantics.
|
|
|
|
//
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
template<class func,
|
2017-12-13 22:32:35 +01:00
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr inline auto
|
|
|
|
test(const std::tuple<args...> &t,
|
2017-12-13 22:32:35 +01:00
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
return !for_each(t, [&f](const auto &arg)
|
2017-12-13 22:32:35 +01:00
|
|
|
{
|
|
|
|
return !f(arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
template<class func,
|
2017-12-13 22:32:35 +01:00
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr inline auto
|
|
|
|
rtest(const std::tuple<args...> &t,
|
2017-12-13 22:32:35 +01:00
|
|
|
func&& f)
|
|
|
|
{
|
2022-06-30 21:21:14 +02:00
|
|
|
return !rfor_each(t, [&f](const auto &arg)
|
2017-12-13 22:32:35 +01:00
|
|
|
{
|
|
|
|
return !f(arg);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-12 21:05:15 +01:00
|
|
|
//
|
|
|
|
// Kronecker delta
|
|
|
|
//
|
|
|
|
|
|
|
|
template<size_t j,
|
|
|
|
size_t i,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr typename std::enable_if<i == j, void>::type
|
2017-12-12 21:05:15 +01:00
|
|
|
kronecker_delta(const std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
|
|
|
using value_type = typename std::tuple_element<i, std::tuple<args...>>::type;
|
|
|
|
f(static_cast<const value_type &>(std::get<i>(t)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t i,
|
|
|
|
size_t j,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr typename std::enable_if<i == j, void>::type
|
2017-12-12 21:05:15 +01:00
|
|
|
kronecker_delta(std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
|
|
|
using value_type = typename std::tuple_element<i, std::tuple<args...>>::type;
|
|
|
|
f(static_cast<value_type &>(std::get<i>(t)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t j,
|
|
|
|
size_t i = 0,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr typename std::enable_if<(i < j), void>::type
|
2017-12-12 21:05:15 +01:00
|
|
|
kronecker_delta(const std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
|
|
|
kronecker_delta<j, i + 1>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t j,
|
|
|
|
size_t i = 0,
|
|
|
|
class func,
|
|
|
|
class... args>
|
2022-06-30 21:21:14 +02:00
|
|
|
constexpr typename std::enable_if<(i < j), void>::type
|
2017-12-12 21:05:15 +01:00
|
|
|
kronecker_delta(std::tuple<args...> &t,
|
|
|
|
func&& f)
|
|
|
|
{
|
|
|
|
kronecker_delta<j, i + 1>(t, std::forward<func>(f));
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
/// Get the index of a tuple element by address at runtime
|
2017-12-12 21:05:15 +01:00
|
|
|
template<class tuple>
|
2022-06-30 21:21:14 +02:00
|
|
|
inline size_t
|
2017-12-12 21:05:15 +01:00
|
|
|
indexof(tuple &t, const void *const &ptr)
|
|
|
|
{
|
|
|
|
size_t ret(0);
|
|
|
|
const auto closure([&ret, &ptr]
|
|
|
|
(auto &elem)
|
|
|
|
{
|
|
|
|
if(reinterpret_cast<const void *>(std::addressof(elem)) == ptr)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
++ret;
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(unlikely(until(t, closure)))
|
|
|
|
throw std::out_of_range("no member of this tuple with that address");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-30 21:21:14 +02:00
|
|
|
//// Tuple layouts are not standard layouts; we can only do this at runtime
|
2017-12-12 21:05:15 +01:00
|
|
|
template<size_t index,
|
|
|
|
class tuple>
|
2022-06-30 21:21:14 +02:00
|
|
|
inline off_t
|
2017-12-12 21:05:15 +01:00
|
|
|
tuple_offset(const tuple &t)
|
|
|
|
{
|
2019-06-23 08:28:48 +02:00
|
|
|
return off_t
|
2017-12-12 21:05:15 +01:00
|
|
|
{
|
|
|
|
reinterpret_cast<const uint8_t *>(std::addressof(std::get<index>(t))) -
|
|
|
|
reinterpret_cast<const uint8_t *>(std::addressof(t))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:52:45 +01:00
|
|
|
} // namespace util
|
|
|
|
} // namespace ircd
|