17 static std::vector<unsigned char>
pack(std::vector<std::size_t>
const& ids,
18 std::vector<double>
const& scales,
21 if (!slot || ids.size()!=scales.size() ||
22 ids.size()>std::numeric_limits<std::uint32_t>::max())
23 throw std::runtime_error(
"Invalid restriction group header input");
24 std::vector<unsigned char> out(4,0);
26 for (std::size_t first=0; first<ids.size();)
28 std::size_t end=first+1;
29 while (end<ids.size() && ids[end]==ids[first]) ++end;
30 float scale=
static_cast<float>(scales[first]), offset=0;
31 if (!(scale>0) || !std::isfinite(scale) ||
double(scale)!=scales[first])
32 throw std::runtime_error(
"Restriction group scale must be positive float32");
33 for (
auto k=first; k<end; ++k)
34 if (scales[k]!=scales[first])
35 throw std::runtime_error(
"One restriction group has inconsistent scales");
36 std::uint32_t
count=
static_cast<std::uint32_t
>(end-first);
37 auto pos=out.size(); out.resize(pos+12);
38 std::memcpy(out.data()+pos,&
count,4);
39 std::memcpy(out.data()+pos+4,&scale,4);
40 std::memcpy(out.data()+pos+8,&offset,4);
43 std::memcpy(out.data(),&runs,4);
44 out.resize((out.size()+slot-1)/slot*slot,0);
49 unpack(
unsigned char const* data, std::size_t bytes,
50 std::size_t expected, std::size_t slot)
52 if (!slot || bytes<4)
throw std::runtime_error(
"Truncated restriction group header");
53 std::uint32_t runs; std::memcpy(&runs,data,4);
54 if (runs>expected || runs>(bytes-4)/12)
55 throw std::runtime_error(
"Invalid restriction group run count");
56 std::size_t header=(4+12*std::size_t(runs)+slot-1)/slot*slot;
57 if (header>bytes)
throw std::runtime_error(
"Truncated restriction group padding");
58 std::vector<double> scales; scales.reserve(expected);
59 for (std::size_t r=0; r<runs; ++r)
61 std::uint32_t
count;
float scale,offset;
63 std::memcpy(&
count,data+pos,4);
64 std::memcpy(&scale,data+pos+4,4);
65 std::memcpy(&offset,data+pos+8,4);
66 if (!
count ||
count>expected-scales.size() || !(scale>0) ||
67 !std::isfinite(scale) || offset!=0)
68 throw std::runtime_error(
"Invalid restriction group descriptor");
69 scales.insert(scales.end(),
count,
double(scale));
71 if (scales.size()!=expected)
72 throw std::runtime_error(
"Restriction group counts do not match interface");
73 return {std::move(scales),header};