1 #ifndef ZSERIO_DELTA_CONTEXT_H_INC 2 #define ZSERIO_DELTA_CONTEXT_H_INC 21 inline uint8_t absDeltaBitLength(uint64_t absDelta)
35 uint8_t calcBitLength(T lhs, T rhs)
37 const uint64_t absDelta = lhs > rhs
38 ?
static_cast<uint64_t
>(lhs) - static_cast<uint64_t>(rhs)
39 : static_cast<uint64_t>(rhs) - static_cast<uint64_t>(lhs);
41 return absDeltaBitLength(absDelta);
47 int64_t calcUncheckedDelta(T lhs, uint64_t rhs)
49 return static_cast<int64_t
>(
static_cast<uint64_t
>(lhs) - rhs);
86 template <
typename ARRAY_TRAITS,
typename OWNER_TYPE>
87 void init(
const OWNER_TYPE& owner,
typename ARRAY_TRAITS::ElementType element)
90 m_unpackedBitSize += bitSizeOfUnpacked<ARRAY_TRAITS>(owner, element);
92 if (!isFlagSet(INIT_STARTED_FLAG))
94 setFlag(INIT_STARTED_FLAG);
95 m_previousElement =
static_cast<uint64_t
>(element);
96 m_firstElementBitSize =
static_cast<uint8_t
>(m_unpackedBitSize);
100 if (m_maxBitNumber <= MAX_BIT_NUMBER_LIMIT)
102 setFlag(IS_PACKED_FLAG);
103 const auto previousElement =
static_cast<typename ARRAY_TRAITS::ElementType
>(
105 const uint8_t maxBitNumber = detail::calcBitLength(element, previousElement);
106 if (maxBitNumber > m_maxBitNumber)
108 m_maxBitNumber = maxBitNumber;
109 if (m_maxBitNumber > MAX_BIT_NUMBER_LIMIT)
110 resetFlag(IS_PACKED_FLAG);
112 m_previousElement =
static_cast<uint64_t
>(element);
125 template <
typename ARRAY_TRAITS,
typename OWNER_TYPE>
126 size_t bitSizeOf(
const OWNER_TYPE& owner,
typename ARRAY_TRAITS::ElementType element)
128 if (!isFlagSet(PROCESSING_STARTED_FLAG))
130 setFlag(PROCESSING_STARTED_FLAG);
133 return bitSizeOfDescriptor() + bitSizeOfUnpacked<ARRAY_TRAITS>(owner, element);
135 else if (!isFlagSet(IS_PACKED_FLAG))
137 return bitSizeOfUnpacked<ARRAY_TRAITS>(owner, element);
141 return m_maxBitNumber + (m_maxBitNumber > 0 ? 1 : 0);
153 template <
typename ARRAY_TRAITS,
typename OWNER_TYPE>
156 if (!isFlagSet(PROCESSING_STARTED_FLAG))
158 setFlag(PROCESSING_STARTED_FLAG);
161 return readUnpacked<ARRAY_TRAITS>(owner, in);
163 else if (!isFlagSet(IS_PACKED_FLAG))
165 return readUnpacked<ARRAY_TRAITS>(owner, in);
169 if (m_maxBitNumber > 0)
172 const typename ARRAY_TRAITS::ElementType element =
173 static_cast<typename ARRAY_TRAITS::ElementType
>(
174 m_previousElement +
static_cast<uint64_t
>(delta));
175 m_previousElement =
static_cast<uint64_t
>(element);
178 return static_cast<typename ARRAY_TRAITS::ElementType
>(m_previousElement);
189 template <
typename ARRAY_TRAITS,
typename OWNER_TYPE>
192 if (!isFlagSet(PROCESSING_STARTED_FLAG))
194 setFlag(PROCESSING_STARTED_FLAG);
196 writeDescriptor(out);
198 writeUnpacked<ARRAY_TRAITS>(owner, out, element);
200 else if (!isFlagSet(IS_PACKED_FLAG))
202 writeUnpacked<ARRAY_TRAITS>(owner, out, element);
206 if (m_maxBitNumber > 0)
209 const int64_t delta = detail::calcUncheckedDelta(element, m_previousElement);
211 m_previousElement =
static_cast<uint64_t
>(element);
223 template <
typename ARRAY_TRAITS>
224 void init(
typename ARRAY_TRAITS::ElementType element)
226 init<ARRAY_TRAITS>(DummyOwner(), element);
236 template <
typename ARRAY_TRAITS>
237 size_t bitSizeOf(
typename ARRAY_TRAITS::ElementType element)
239 return bitSizeOf<ARRAY_TRAITS>(DummyOwner(), element);
249 template <
typename ARRAY_TRAITS>
252 return read<ARRAY_TRAITS>(DummyOwner(), in);
261 template <
typename ARRAY_TRAITS>
264 write<ARRAY_TRAITS>(DummyOwner(), out, element);
273 if (isFlagSet(IS_PACKED_FLAG))
275 const size_t deltaBitSize = m_maxBitNumber + (m_maxBitNumber > 0 ? 1 : 0);
276 const size_t packedBitSizeWithDescriptor = 1 + MAX_BIT_NUMBER_BITS +
277 m_firstElementBitSize + (m_numElements - 1) * deltaBitSize;
278 const size_t unpackedBitSizeWithDescriptor = 1 + m_unpackedBitSize;
279 if (packedBitSizeWithDescriptor >= unpackedBitSizeWithDescriptor)
280 resetFlag(IS_PACKED_FLAG);
284 size_t bitSizeOfDescriptor()
const 286 if (isFlagSet(IS_PACKED_FLAG))
287 return 1 + MAX_BIT_NUMBER_BITS;
292 template <
typename ARRAY_TRAITS,
293 typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
294 static size_t bitSizeOfUnpacked(
const typename ARRAY_TRAITS::OwnerType& owner,
295 typename ARRAY_TRAITS::ElementType element)
300 template <
typename ARRAY_TRAITS,
301 typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
302 static size_t bitSizeOfUnpacked(
const DummyOwner&,
303 typename ARRAY_TRAITS::ElementType element)
312 setFlag(IS_PACKED_FLAG);
313 m_maxBitNumber =
static_cast<uint8_t
>(in.
readBits(MAX_BIT_NUMBER_BITS));
317 resetFlag(IS_PACKED_FLAG);
321 template <
typename ARRAY_TRAITS,
322 typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
323 typename ARRAY_TRAITS::ElementType readUnpacked(
const typename ARRAY_TRAITS::OwnerType& owner,
327 m_previousElement =
static_cast<uint64_t
>(element);
331 template <
typename ARRAY_TRAITS,
332 typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
333 typename ARRAY_TRAITS::ElementType readUnpacked(
const DummyOwner&,
BitStreamReader& in)
336 m_previousElement =
static_cast<uint64_t
>(element);
342 const bool isPacked = isFlagSet(IS_PACKED_FLAG);
345 out.
writeBits(m_maxBitNumber, MAX_BIT_NUMBER_BITS);
348 template <
typename ARRAY_TRAITS,
349 typename std::enable_if<has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
350 void writeUnpacked(
const typename ARRAY_TRAITS::OwnerType& owner,
353 m_previousElement =
static_cast<uint64_t
>(element);
357 template <
typename ARRAY_TRAITS,
358 typename std::enable_if<!has_owner_type<ARRAY_TRAITS>::value,
int>::type = 0>
359 void writeUnpacked(
const DummyOwner&,
BitStreamWriter& out,
typename ARRAY_TRAITS::ElementType element)
361 m_previousElement =
static_cast<uint64_t
>(element);
365 void setFlag(uint8_t flagMask)
370 void resetFlag(uint8_t flagMask)
372 m_flags &=
static_cast<uint8_t
>(~flagMask);
375 bool isFlagSet(uint8_t flagMask)
const 377 return ((m_flags & flagMask) != 0);
380 static const uint8_t MAX_BIT_NUMBER_BITS = 6;
381 static const uint8_t MAX_BIT_NUMBER_LIMIT = 62;
383 static const uint8_t INIT_STARTED_FLAG = 0x01;
384 static const uint8_t IS_PACKED_FLAG = 0x02;
385 static const uint8_t PROCESSING_STARTED_FLAG = 0x04;
387 uint64_t m_previousElement = 0;
388 uint8_t m_maxBitNumber = 0;
389 uint8_t m_flags = 0x00;
391 uint8_t m_firstElementBitSize = 0;
392 uint32_t m_numElements = 0;
393 size_t m_unpackedBitSize = 0;
398 #endif // ZSERIO_DELTA_CONTEXT_H_INC
ARRAY_TRAITS::ElementType read(BitStreamReader &in)
uint32_t readBits(uint8_t numBits=32)
void init(const OWNER_TYPE &owner, typename ARRAY_TRAITS::ElementType element)
size_t bitSizeOf(typename ARRAY_TRAITS::ElementType element)
T read(BitStreamReader &in)
void write(BitStreamWriter &out, T value)
int64_t readSignedBits64(uint8_t numBits=64)
void writeBits(uint32_t data, uint8_t numBits=32)
ARRAY_TRAITS::ElementType read(const OWNER_TYPE &owner, BitStreamReader &in)
void write(BitStreamWriter &out, typename ARRAY_TRAITS::ElementType element)
void write(const OWNER_TYPE &owner, BitStreamWriter &out, typename ARRAY_TRAITS::ElementType element)
void init(typename ARRAY_TRAITS::ElementType element)
void writeBool(bool data)
size_t bitSizeOf(const OWNER_TYPE &owner, typename ARRAY_TRAITS::ElementType element)
void writeSignedBits64(int64_t data, uint8_t numBits=64)
size_t bitSizeOf(T value)