1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
// SPDX-FileCopyrightText: 2025 Kohei Yoshida
//
// SPDX-License-Identifier: MIT
#include "test_global.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <execution>
#include <mdds/multi_type_vector/types.hpp>
#include <mdds/multi_type_vector/soa/main.hpp>
#include <mdds/multi_type_vector/aos/main.hpp>
namespace {
struct custom_num
{
double value;
custom_num() : value(0.0) {}
custom_num(double v) : value(v) {}
operator double() const { return value; }
bool operator==(const custom_num& other) const
{
return value == other.value;
}
bool operator!=(const custom_num& other) const
{
return !operator==(other);
}
};
struct custom_str1
{
std::string value;
operator const char*() const
{
return value.c_str();
}
bool operator==(const custom_str1& other) const
{
return value == other.value;
}
bool operator!=(const custom_str1& other) const
{
return !operator==(other);
}
};
struct custom_str2
{
std::string value;
operator const char*() const
{
return value.c_str();
}
bool operator==(const custom_str2& other) const
{
return value == other.value;
}
bool operator!=(const custom_str2& other) const
{
return !operator==(other);
}
};
constexpr mdds::mtv::element_t block1_id = mdds::mtv::element_type_user_start;
constexpr mdds::mtv::element_t block2_id = mdds::mtv::element_type_user_start + 1;
constexpr mdds::mtv::element_t block3_id = mdds::mtv::element_type_user_start + 2;
using block1_type = mdds::mtv::default_element_block<block1_id, custom_num, std::vector>;
using block2_type = mdds::mtv::noncopyable_managed_element_block<block2_id, custom_str1, std::vector>;
using block3_type = mdds::mtv::noncopyable_managed_element_block<block3_id, custom_str2, std::vector>;
MDDS_MTV_DEFINE_ELEMENT_CALLBACKS(custom_num, block1_id, custom_num{}, block1_type)
MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(custom_str1, block2_id, nullptr, block2_type)
MDDS_MTV_DEFINE_ELEMENT_CALLBACKS_PTR(custom_str2, block3_id, nullptr, block3_type)
struct mtv_traits : mdds::mtv::default_traits
{
using block_funcs = mdds::mtv::element_block_funcs<block1_type, block2_type, block3_type>;
};
using soa_mtv_type = mdds::mtv::soa::multi_type_vector<mtv_traits>;
using aos_mtv_type = mdds::mtv::aos::multi_type_vector<mtv_traits>;
}
namespace mdds { namespace mtv {
template<>
struct clone_block<block2_type>
{
block2_type* operator()(const block2_type& src) const
{
std::unique_ptr<block2_type> cloned_blk = std::make_unique<block2_type>();
auto cloned(src.store());
std::transform(cloned.begin(), cloned.end(), cloned.begin(),
[] (const custom_str1* p){ return new custom_str1(*p); });
cloned_blk->store().swap(cloned);
return cloned_blk.release();
}
};
template<>
struct clone_value<custom_str2*>
{
custom_str2* operator()(const custom_str2* src) const
{
return new custom_str2(*src);
}
};
}}
void test_block1()
{
MDDS_TEST_FUNC_SCOPE;
using blk_type = block1_type;
blk_type src;
blk_type::append_value(src, 1.1);
auto* cloned = blk_type::clone_block(src);
auto n = blk_type::size(*cloned);
std::cout << "size=" << n << std::endl;
for (std::size_t i = 0; i < n; ++i)
std::cout << "v(" << i << ")=" << blk_type::at(*cloned, i) << std::endl;
blk_type::delete_block(cloned);
}
void test_block2()
{
MDDS_TEST_FUNC_SCOPE;
using blk_type = block2_type;
blk_type src;
blk_type::append_value(src, new custom_str1{"foo1"});
blk_type::append_value(src, new custom_str1{"foo2"});
blk_type::append_value(src, new custom_str1{"foo3"});
blk_type::append_value(src, new custom_str1{"foo4"});
blk_type::append_value(src, new custom_str1{"foo5"});
auto* cloned = blk_type::clone_block(src);
auto n = blk_type::size(*cloned);
std::cout << "size=" << n << std::endl;
for (std::size_t i = 0; i < n; ++i)
std::cout << "v(" << i << ")=" << *blk_type::at(*cloned, i) << std::endl;
blk_type::delete_block(cloned);
}
void test_block3()
{
MDDS_TEST_FUNC_SCOPE;
using blk_type = block3_type;
blk_type src;
blk_type::append_value(src, new custom_str2{"custom foo1"});
blk_type::append_value(src, new custom_str2{"custom foo2"});
blk_type::append_value(src, new custom_str2{"custom foo3"});
blk_type::append_value(src, new custom_str2{"custom foo4"});
auto* cloned = blk_type::clone_block(src);
auto n = blk_type::size(*cloned);
std::cout << "size=" << n << std::endl;
for (std::size_t i = 0; i < n; ++i)
std::cout << "v(" << i << ")=" << *blk_type::at(*cloned, i) << std::endl;
blk_type::delete_block(cloned);
}
void test_mtv_soa()
{
MDDS_TEST_FUNC_SCOPE;
soa_mtv_type store;
store.push_back<custom_num>(1.2);
store.push_back<custom_num>(1.3);
store.push_back_empty();
store.push_back(new custom_str1{"custom str1"});
store.push_back(new custom_str2{"custom str2"});
auto cloned = store.clone();
std::cout << "size=" << cloned.size() << std::endl;
std::cout << "block-size=" << cloned.block_size() << std::endl;
std::cout << "v(0)=" << cloned.get<custom_num>(0) << std::endl;
std::cout << "v(1)=" << cloned.get<custom_num>(1) << std::endl;
std::cout << "v(3)=" << *cloned.get<custom_str1*>(3) << std::endl;
std::cout << "v(4)=" << *cloned.get<custom_str2*>(4) << std::endl;
std::cout << "store == cloned ? " << std::boolalpha << (store == cloned) << std::endl;
cloned.shrink_to_fit();
}
void test_mtv_aos()
{
MDDS_TEST_FUNC_SCOPE;
aos_mtv_type store;
store.push_back<custom_num>(1.2);
store.push_back<custom_num>(1.3);
store.push_back_empty();
store.push_back(new custom_str1{"custom str1"});
store.push_back(new custom_str2{"custom str2"});
auto cloned = store.clone();
std::cout << "size=" << cloned.size() << std::endl;
std::cout << "block-size=" << cloned.block_size() << std::endl;
std::cout << "v(0)=" << cloned.get<custom_num>(0) << std::endl;
std::cout << "v(1)=" << cloned.get<custom_num>(1) << std::endl;
std::cout << "v(3)=" << *cloned.get<custom_str1*>(3) << std::endl;
std::cout << "v(4)=" << *cloned.get<custom_str2*>(4) << std::endl;
std::cout << "store == cloned ? " << std::boolalpha << (store == cloned) << std::endl;
}
int main()
{
test_block1();
test_block2();
test_block3();
test_mtv_soa();
test_mtv_aos();
return EXIT_SUCCESS;
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|