我应该如何将一个值插入到另一个值的"middle"中?

How should I insert a value into the "middle" of another?

本文关键字:插入 另一个 middle 一个 我应该      更新时间:2023-10-16

我有两个值 v1 和 v2,类型分别为 T1 和 T2,大小为 (T1)>sizeof(T2)。这两种类型都是普通的旧数据。现在,我想替换第 k、k+1、...k+sizeof(T2)-v1 的第 1 个字节,其中字节为 v2。

C++ 在语言中本身不提供此功能,据我所知,在标准库中也没有提供此功能(至少不是直接提供)。一般实施这一点的最佳方法是什么?即实施:

template<typename T1, typename T2>
void replace_bytes(T1& v1, T2 v2, std::size_t k)

或至少

template<typename T1, typename T2, std::size_t k>
void replace_bytes(T1& v1, T2 v2)

我的想法是:

  1. 重新解释转换为字节数组
  2. 重新解释转换为 std::字节数组
  3. 使用跨度
  4. 地址
  5. 为 v1 的指针算术
  6. 对于不太大的类型 - 重新解释为无符号整数并使用位运算:AND 与掩码、shift 或组合现有位和替换位。

笔记:

  • 当然,如果k太高,这里会有UB(或者我们可以检查它不是太高)。
  • 为了简单起见,您可能会假设内存布局是小端序的。
  • 如果对齐是一个问题,请明确您的选择。
  • 效率/速度当然是一个关键问题。
  • 如果你的建议需要更新的C++语言标准,那很好,但一定要提到它。
  • 编译过程中对代码进行良好的优化非常重要。
// needed include files
#include <iostream>  // for cout
#include <stdexcept> // for runtime_error
#include <cstring>   // for memcpy
// generic template function that takes 3 arguments
// 1 destination object
// 2 source object
// 3 from which byte to start in the destination
template<class T1, class T2>
void replace_bytes ( T1& t1, const T2& t2, std::size_t k )
{
// at compile time, store the size of T1 type in t1_size
constexpr std::size_t t1_size = sizeof(T1);
// at compile time, store the size of T2 type in t2_size
constexpr std::size_t t2_size = sizeof(T2);
// if we copy t2 bytes to t1, do we run out of memory ?
if ( k + t2_size > t1_size )
{
throw std::runtime_error("Can't copy out of bounds.");
}
// do the copying, casting is required for proper pointer arithmitic
std::memcpy( (void*) (((char*)&t1)+k), (const void*) &t2, t2_size );
}
int main()
{
int x = 0;
char c = 10;
replace_bytes(x, c, 0);
std::cout << x << std::endl;
}

干净的代码版本(无注释):

#include <iostream>
#include <stdexcept>
#include <cstring>
template <class T1, class T2>
void replace_bytes ( T1& t1, const T2& t2, std::size_t k )
{
constexpr std::size_t t1_size = sizeof(T1);
constexpr std::size_t t2_size = sizeof(T2);
if ( k + t2_size > t1_size )
{
throw std::runtime_error("Can't copy out of bounds.");
}
std::memcpy( (void*) (((char*)&t1)+k), (const void*) &t2, t2_size );
}
int main()
{
int x = 0;
char c = 10;
replace_bytes(x, c, 0);
std::cout << x << std::endl;
}

以下内容适用于最大 8 字节的 T1 大小,并且似乎在 GCC、clang 和 MSVC 上得到了足够好的优化 - 至少在内联时:

namespace detail {
template <unsigned NBytes> struct uint;
template<> struct uint<1> { using type = uint8_t;  };
template<> struct uint<2> { using type = uint16_t; };
template<> struct uint<4> { using type = uint32_t; };
template<> struct uint<8> { using type = uint64_t; };
} // namespace detail
template <unsigned NBytes>
using uint_t = typename detail::uint<NBytes>::type;
template <typename T1, typename T2>
inline void replace_bytes(T1& v1 ,T2 v2, std::size_t k)
{
static_assert(sizeof(T1) > sizeof(T2), "invalid sizes");
static_assert(std::is_trivial<T1>::value, "T1 must be a trivial type");
static_assert(std::is_trivial<T2>::value, "T2 must be a trivial type");
auto shift_amount = k * CHAR_BIT;
using uint_1_t = uint<sizeof(v1)>;
using uint_2_t = uint<sizeof(v2)>;
auto& v1_as_uint = *reinterpret_cast<uint_1_t*>(&v1);
const auto& v2_as_uint = *reinterpret_cast<uint_2_t*>(&v2);
auto v1_mask = ~( (uint_1_t{1} << (sizeof(T2) * CHAR_BIT) - 1) << shift_amount);
auto shifted_v2 = uint_1_t{v2_as_uint} <<  shift_amount;
v1_as_uint = (v1_as_uint & v1_mask ) | shifted_v2;
}

但我觉得最好避免参数输出 - 事实上,这样做允许函数实现严格在寄存器中:

template <typename T1, typename T2>
T1 replace_bytes(T1 v1 ,T2 v2, std::size_t k)
{
static_assert(sizeof(T1) > sizeof(T2), "invalid sizes");
static_assert(std::is_trivial<T1>::value, "T1 must be a trivial type");
static_assert(std::is_trivial<T2>::value, "T2 must be a trivial type");
auto shift_amount = k * CHAR_BIT;
using uint_1_t = uint_t<sizeof(v1)>;
using uint_2_t = uint_t<sizeof(v2)>;
auto& v1_as_uint = *reinterpret_cast<uint_1_t*>(&v1);
const auto& v2_as_uint = *reinterpret_cast<uint_2_t*>(&v2);
auto v1_mask = ~( (uint_1_t{1} << (sizeof(T2) * CHAR_BIT) - 1) << shift_amount);
auto shifted_v2 = uint_1_t{v2_as_uint} <<  shift_amount;
return (v1_as_uint & v1_mask ) | shifted_v2;
}