...
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
#include <concepts>
|
#include <concepts>
|
||||||
#include <coroutine>
|
#include <coroutine>
|
||||||
#include <expected>
|
#include <expected>
|
||||||
#include <future>
|
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
|
|
||||||
namespace snplib
|
namespace snplib
|
||||||
@@ -100,6 +99,12 @@ static constexpr bool snplib_check_indices_v = snplib_check_indices_t<T>::value;
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
concept snplib_tuple_like_c = details::snplib_check_indices_v<std::remove_cvref_t<T>>;
|
concept snplib_tuple_like_c = details::snplib_check_indices_v<std::remove_cvref_t<T>>;
|
||||||
|
|
||||||
|
// strictly std::tuple
|
||||||
|
template <typename T>
|
||||||
|
concept snplib_tuple_c = requires {
|
||||||
|
[]<typename... Ts>(std::type_identity<std::tuple<Ts...>>) {}(std::type_identity<std::remove_cvref_t<T>>{});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* fixed-size and non-resizable ranges */
|
/* fixed-size and non-resizable ranges */
|
||||||
|
|
||||||
|
|||||||
@@ -3,11 +3,46 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
#include "snipplib/concepts/snplib_concepts.h"
|
||||||
|
|
||||||
|
|
||||||
namespace snplib
|
namespace snplib
|
||||||
{
|
{
|
||||||
|
|
||||||
|
namespace details
|
||||||
|
{
|
||||||
|
|
||||||
|
template <typename OT, typename TT, size_t... Is>
|
||||||
|
concept snplib_all_func_oper_c = requires(OT obj) { (obj(std::declval<std::tuple_element_t<Is, TT>&>()), ...); };
|
||||||
|
|
||||||
|
template <typename OT, typename TT>
|
||||||
|
struct snplib_check_all_func_oper_t {
|
||||||
|
static constexpr bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename OT, typename TT>
|
||||||
|
requires requires { typename std::tuple_size<TT>::type; }
|
||||||
|
struct snplib_check_all_func_oper_t<OT, TT> {
|
||||||
|
static constexpr bool value = []<std::size_t... Is>(std::index_sequence<Is...>) {
|
||||||
|
return snplib_all_func_oper_c<OT, TT, Is...>;
|
||||||
|
}(std::make_index_sequence<std::tuple_size<TT>::value>{});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
template <typename OT, typename TT>
|
||||||
|
static constexpr bool snplib_check_all_func_oper_v = snplib_check_all_func_oper_t<OT, TT>::value;
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
concept snplib_hvec_visitor_c = requires {
|
||||||
|
requires snplib_tuple_c<typename T::types>;
|
||||||
|
|
||||||
|
requires details::snplib_check_all_func_oper_v<T, typename T::types>;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
class HeterogenVec
|
class HeterogenVec
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
@@ -16,7 +51,58 @@ protected:
|
|||||||
|
|
||||||
inline static std::vector<std::function<void(const HeterogenVec*)>> _clearFunc{};
|
inline static std::vector<std::function<void(const HeterogenVec*)>> _clearFunc{};
|
||||||
|
|
||||||
|
|
||||||
|
// move: void(from_obj_ptr, to_obj_ptr)
|
||||||
|
inline static std::vector<std::function<void(HeterogenVec*, HeterogenVec*)>> _moveFunc{};
|
||||||
|
// copy: void(const from_obj_ptr, to_obj_ptr)
|
||||||
|
inline static std::vector<std::function<void(const HeterogenVec*, HeterogenVec*)>> _copyFunc{};
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
HeterogenVec() = default;
|
||||||
|
|
||||||
|
~HeterogenVec() = default;
|
||||||
|
|
||||||
|
HeterogenVec(const HeterogenVec& other)
|
||||||
|
{
|
||||||
|
if (&other != this) {
|
||||||
|
for (auto& func : _copyFunc) {
|
||||||
|
func(&other, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HeterogenVec(HeterogenVec&& other)
|
||||||
|
{
|
||||||
|
if (&other != this) {
|
||||||
|
for (auto& func : _moveFunc) {
|
||||||
|
func(&other, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
HeterogenVec& operator=(const HeterogenVec& other)
|
||||||
|
{
|
||||||
|
if (&other != this) {
|
||||||
|
for (auto& func : _copyFunc) {
|
||||||
|
func(&other, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
HeterogenVec& operator=(HeterogenVec&& other)
|
||||||
|
{
|
||||||
|
if (&other != this) {
|
||||||
|
for (auto& func : _moveFunc) {
|
||||||
|
func(&other, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
for (auto& func : _clearFunc) {
|
for (auto& func : _clearFunc) {
|
||||||
@@ -26,16 +112,47 @@ public:
|
|||||||
|
|
||||||
|
|
||||||
template <typename VT>
|
template <typename VT>
|
||||||
void insert(VT&& value)
|
void push(VT&& value)
|
||||||
{
|
{
|
||||||
using v_t = std::decay_t<VT>;
|
using v_t = std::decay_t<VT>;
|
||||||
|
|
||||||
if (_values<v_t>.empty()) {
|
if (_values<v_t>.empty()) {
|
||||||
_clearFunc.emplace_back([](const HeterogenVec* obj) { _values<v_t>[obj].clear(); });
|
_clearFunc.emplace_back([](const HeterogenVec* obj) {
|
||||||
|
// _values<v_t>[obj].clear();
|
||||||
|
_values<v_t>.erase(obj);
|
||||||
|
});
|
||||||
|
|
||||||
|
_moveFunc.emplace_back([](HeterogenVec* from, HeterogenVec* to) {
|
||||||
|
if (_values<v_t>.contains(from)) {
|
||||||
|
_values<v_t>[to] = std::move(_values<v_t>[from]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
_copyFunc.emplace_back([](HeterogenVec* from, HeterogenVec* to) {
|
||||||
|
if (_values<v_t>.contains(from)) {
|
||||||
|
_values<v_t>[to] = _values<v_t>[from];
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
_values<v_t>[this].push_back(std::forward<VT>(value));
|
_values<v_t>[this].push_back(std::forward<VT>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template <snplib_hvec_visitor_c VT>
|
||||||
|
void visit(VT&& visitor)
|
||||||
|
{
|
||||||
|
auto invoke_per_type = [&visitor, this]<typename T>() {
|
||||||
|
for (auto& elem : _values<T>[this]) {
|
||||||
|
visitor(elem);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
[&invoke_per_type]<size_t... Is>(std::index_sequence<Is...>) {
|
||||||
|
(invoke_per_type.template operator()<std::tuple_element_t<Is, typename VT::types>>(), ...);
|
||||||
|
}(std::make_index_sequence<std::tuple_size_v<typename VT::types>>{});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end of namespace snplib
|
} // end of namespace snplib
|
||||||
Reference in New Issue
Block a user