Reputation: 1759
I am looking for a way to efficiently and without UB convert a std::vector<uint8_t>
to a std::vector<uint64_t>
s.t each element in the std::vector<uint64_t>
holds information from 8 elements from the std::vector<uint8_t>
. The remainder elements should be filled with zeros but that can be done later.
The best approach I've come up with so far is:
std::vector<uint64_t> foo(std::vector<uint8_t> const & v8) {
std::vector<uint64_t> v64;
v64.reserve((v8.size() + 7) / 8);
size_t i = 0;
uint64_t tmp;
for(; i + 8 < v8.size(); i += 8) {
memcpy(&tmp, v8.data() + i, 8);
v64.push_back(tmp);
}
tmp = 0; // fill remainder with 0s.
memcpy(&tmp, v8.data() + i, v8.size() - i);
v64.push_back(tmp);
return v64;
}
But I'm hoping there is some cleaner / better approach.
Edit1: The solution about misses byte-order concerns. Pointed out by @VainMain.
Could be fixed with a byte-swap after the memcpy
.
Upvotes: 5
Views: 1094
Reputation: 3303
If you are up to using the range-v3 library, you can use ranges::view::chunk
(or the probably soon available C++23 standard library implementation equivalent).
This will especially relieve you from calculating the required size of the vector to store the packed values in:
#include <array>
#include <cstddef>
#include <span>
#include <vector>
#include <range/v3/all.hpp>
std::vector<std::uint64_t> pack(const std::span<const std::uint8_t> values)
{
const auto chunked_view = ranges::view::chunk(values, 8);
std::vector<std::uint64_t> packed(ranges::size(chunked_view));
ranges::transform(chunked_view, packed.begin(), [](const auto& word) {
std::array<std::uint8_t, 8> buf{0}; // init with all 0's
ranges::copy(word, buf.begin());
std::uint64_t packed_word;
std::memcpy(&packed_word, buf.data(), 8);
return packed_word;
});
return packed;
}
Example (see on godbolt.org)
int main()
{
std::array<uint8_t, 9> values;
std::iota(values.begin(), values.end(), std::uint8_t{0});
for (auto t : pack(values))
std::cout << std::hex << t << std::endl;
// prints
// 706050403020100
// 8
return 0;
}
Upvotes: 2