9 template<
typename VALUE_TYPE>
10 static bool TryDecodeUInt32(
CParaFile& source, VALUE_TYPE&
value,
int& nByteRead)
13 if (source.
read(&b, 1) == 0)
19 value = (VALUE_TYPE)b;
25 value = (VALUE_TYPE)(b & 0x7F);
30 if (source.
read(&b, 1) == 0)
35 keepGoing = (b & 0x80) != 0;
36 value |= ((VALUE_TYPE)(b & 0x7F)) << shift;
38 }
while (keepGoing && i < 4);
39 if (keepGoing && i == 4)
46 template<
typename VALUE_TYPE>
47 static int EncodeUInt32(VALUE_TYPE value, BYTE* buffer,
StringBuilder& stream)
49 int count = 0, index = 0;
52 buffer[index++] = (byte)((value & 0x7F) | 0x80);
56 buffer[index - 1] &= 0x7F;
57 stream.
append((
const char*)buffer, count);
70 template<
typename VALUE_TYPE>
73 int nCount = data.size();
74 if (nCount == 0)
return 0;
76 int last = -1, len = 0;
77 for (
int i = 0; i < nCount;)
79 int gap = data[i] - 2 - last, size = 0;
80 while (++i < nCount && data[i] == data[i - 1] + 1)
83 len += EncodeUInt32((VALUE_TYPE)gap, buffer, stream)
84 + EncodeUInt32((VALUE_TYPE)size, buffer, stream);
90 template<
typename VALUE_TYPE>
93 int nCount = data.size();
94 if (nCount < 2)
return false;
97 int nHalfCount = nCount >> 1;
98 for (
int i = 1; i < nCount && nSkipCount <= nHalfCount; ++i)
100 if (data[i] == (data[i - 1] + 1))
103 return nSkipCount >= nHalfCount;
106 template<
typename VALUE_TYPE>
107 static void DecodeSkipOne(
CParaFile& stream, std::vector<VALUE_TYPE>& list,
int nStreamSize = 0xfffffff)
109 VALUE_TYPE skip, take;
112 while (nByteRead<nStreamSize && TryDecodeUInt32(stream, skip, nByteRead)
113 && TryDecodeUInt32(stream, take, nByteRead))
115 last += (int)skip + 1;
116 for (VALUE_TYPE i = 0; i <= take; i++) {
117 list.push_back(last++);
122 template<
typename VALUE_TYPE>
123 static int EncodeIntDeltaArray(
StringBuilder& stream,
const std::vector<VALUE_TYPE>& data)
125 int nCount = data.size();
126 if (nCount == 0)
return 0;
129 len += EncodeUInt32((VALUE_TYPE)(data[0]), buffer, stream);
131 for (
int i = 1; i < nCount; ++i)
133 len += EncodeUInt32((VALUE_TYPE)(data[i] - data[i - 1]), buffer, stream);
138 template<
typename VALUE_TYPE>
139 static void DecodeIntDeltaArray(
CParaFile& stream, std::vector<VALUE_TYPE>& list,
int nStreamSize = 0xfffffff)
141 VALUE_TYPE lastValue, delta;
145 if (nByteRead<nStreamSize && TryDecodeUInt32(stream, lastValue, nByteRead))
147 list.push_back(lastValue);
148 while (nByteRead<nStreamSize && TryDecodeUInt32(stream, delta, nByteRead))
151 list.push_back(lastValue);
160 template<
typename VALUE_TYPE>
163 int nCount = data.size();
164 if (nCount == 0)
return 0;
169 len += EncodeUInt32(last, buffer, stream);
171 for (
int i = 0; i < nCount;)
175 VALUE_TYPE cur_last = last;
176 while (++i < nCount && (cur_last = data[i]) == last)
179 len += EncodeUInt32(size, buffer, stream);
180 if (last != cur_last)
183 len += EncodeUInt32(last, buffer, stream);
189 template<
typename VALUE_TYPE>
190 static void DecodeSameInteger(
CParaFile& stream, std::vector<VALUE_TYPE>& list,
int nStreamSize = 0xfffffff)
195 while (nByteRead<nStreamSize && TryDecodeUInt32(stream, last, nByteRead))
197 list.push_back(last);
199 if (nByteRead<nStreamSize && TryDecodeUInt32(stream, size, nByteRead))
201 for (uint32_t i = 0; i < size; i++) {
202 list.push_back(last);
208 template<
typename VALUE_TYPE>
209 static bool IsSameIntegerBetter(
const std::vector<VALUE_TYPE>& data)
211 int nCount = data.size();
214 VALUE_TYPE last = data[0];
217 for (
int i = 0; i < nCount && len<nCount;)
220 VALUE_TYPE cur_last = last;
221 while (++i < nCount && (cur_last = data[i]) == last)
229 template<
typename VALUE_TYPE>
230 static int EncodeIntArray(
StringBuilder& stream,
const std::vector<VALUE_TYPE>& data)
232 int nCount = data.size();
233 if (nCount == 0)
return 0;
236 for (
int i = 0; i < nCount; ++i)
238 len += EncodeUInt32(data[i], buffer, stream);
243 template<
typename VALUE_TYPE>
244 static void DecodeIntArray(
CParaFile& stream, std::vector<VALUE_TYPE>& list,
int nStreamSize = 0xfffffff)
249 while (nByteRead<nStreamSize && TryDecodeUInt32(stream, value, nByteRead))
251 list.push_back(value);
260 template<
typename VALUE_TYPE>
269 m_nLastValueCount = 0;
271 void Append(VALUE_TYPE nValue)
275 if (m_nLastValue == nValue)
282 m_nLastValue = nValue;
283 m_nLastValueCount = 1;
288 m_nLastValue = nValue;
289 m_nLastValueCount = 1;
295 void Append(VALUE_TYPE nValue,
int nCount)
300 if (m_nLastValue == nValue)
302 m_nLastValueCount += nCount;
307 m_nLastValue = nValue;
308 m_nLastValueCount = nCount;
313 m_nLastValue = nValue;
314 m_nLastValueCount = nCount;
322 if (m_nLastValueCount > 0)
325 CIntegerEncoder::EncodeUInt32(m_nLastValue, buffer, *m_pStream);
326 CIntegerEncoder::EncodeUInt32(m_nLastValueCount - 1, buffer, *m_pStream);
330 OUTPUT_LOG(
"Error: invalid call to finalize with nothing to finalize\n");
336 int m_nLastValueCount;
337 VALUE_TYPE m_nLastValue;
341 template<
typename VALUE_TYPE>
345 static void DecodeSameIntegerOfCount(
CParaFile& stream, std::vector<VALUE_TYPE>& list,
int nIntegerCount = 0xfffffff)
349 int nIntegerRead = 0;
351 while (nIntegerRead < nIntegerCount && CIntegerEncoder::TryDecodeUInt32(stream, last, nByteRead))
353 list.push_back(last);
355 if (CIntegerEncoder::TryDecodeUInt32(stream, size, nByteRead))
357 for (uint32_t i = 0; i < size; i++) {
358 list.push_back(last);
361 nIntegerRead += (size+1);
364 static void SkipDecodeSameIntegerOfCount(
CParaFile& stream,
int nIntegerCount = 0xfffffff)
368 int nIntegerRead = 0;
370 while (nIntegerRead < nIntegerCount && CIntegerEncoder::TryDecodeUInt32(stream, last, nByteRead))
373 CIntegerEncoder::TryDecodeUInt32(stream, size, nByteRead);
374 nIntegerRead += (size + 1);
static int EncodeSameInteger(StringBuilder &stream, const std::vector< VALUE_TYPE > &data)
Author: LiXizhi if the input array contains, many data of the same value.
Definition: BlockDataCodec.h:161
different physics engine has different winding order.
Definition: EventBinding.h:32
Author: LiXizhi if the input array contains, many data of the same value.
Definition: BlockDataCodec.h:261
PE_CORE_DECL size_t read(void *dest, size_t bytes)
read byte stream from file at its current location.
Definition: ParaFile.cpp:1073
void append(Char c)
Appends a character to the content of the builder.
Definition: StringBuilder.hpp:257
Definition: enum_maker.hpp:46
it presents a real or virtual file in ParaEngine.
Definition: ParaFile.h:31
Definition: BlockDataCodec.h:342
A NON-thread-safe, mutable sequence of characters(Binary is also possible).
Definition: StringBuilder.h:9
Definition: BlockDataCodec.h:6
static int EncodeSkipOne(StringBuilder &stream, const std::vector< VALUE_TYPE > &data)
Author: LiXizhi here we will use skip 1 algorithm to compress the data, since the result is sorted in...
Definition: BlockDataCodec.h:71
static bool IsSkipOneBetter(const std::vector< VALUE_TYPE > &data)
return true, if more than half of the input is continuous integer with step 1.
Definition: BlockDataCodec.h:91
void Finalize()
call this function when last element is added.
Definition: BlockDataCodec.h:320