region_packer.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #pragma once
  2. #include "packers.h"
  3. #include <util/generic/array_ref.h>
  4. // Stores an array of PODs in the trie (copying them with memcpy).
  5. // Byte order and alignment are your problem.
  6. template <class TRecord>
  7. class TRegionPacker {
  8. public:
  9. typedef TArrayRef<TRecord> TRecords;
  10. void UnpackLeaf(const char* p, TRecords& result) const {
  11. size_t len;
  12. NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len);
  13. size_t start = NPackers::TIntegralPacker<size_t>().SkipLeaf(p);
  14. result = TRecords((TRecord*)(p + start), len);
  15. }
  16. void PackLeaf(char* buf, const TRecords& data, size_t computedSize) const {
  17. size_t len = data.size();
  18. size_t lenChar = len * sizeof(TRecord);
  19. size_t start = computedSize - lenChar;
  20. NPackers::TIntegralPacker<size_t>().PackLeaf(buf, len, NPackers::TIntegralPacker<size_t>().MeasureLeaf(len));
  21. memcpy(buf + start, data.data(), lenChar);
  22. }
  23. size_t MeasureLeaf(const TRecords& data) const {
  24. size_t len = data.size();
  25. return NPackers::TIntegralPacker<size_t>().MeasureLeaf(len) + len * sizeof(TRecord);
  26. }
  27. size_t SkipLeaf(const char* p) const {
  28. size_t result = NPackers::TIntegralPacker<size_t>().SkipLeaf(p);
  29. size_t len;
  30. NPackers::TIntegralPacker<size_t>().UnpackLeaf(p, len);
  31. result += len * sizeof(TRecord);
  32. return result;
  33. }
  34. };