1 | /** |
---|
2 | * @file TightPointDataStorageD.c |
---|
3 | * @author Sheng Di and Dingwen Tao |
---|
4 | * @date Aug, 2016 |
---|
5 | * @brief The functions used to construct the tightPointDataStorage element for storing compressed bytes. |
---|
6 | * (C) 2016 by Mathematics and Computer Science (MCS), Argonne National Laboratory. |
---|
7 | * See COPYRIGHT in top-level directory. |
---|
8 | */ |
---|
9 | |
---|
10 | #include <stdlib.h> |
---|
11 | #include <stdio.h> |
---|
12 | #include <string.h> |
---|
13 | #include "TightDataPointStorageD.h" |
---|
14 | #include "sz.h" |
---|
15 | #include "Huffman.h" |
---|
16 | //#include "rw.h" |
---|
17 | |
---|
18 | void new_TightDataPointStorageD_Empty(TightDataPointStorageD **this) |
---|
19 | { |
---|
20 | *this = (TightDataPointStorageD*)malloc(sizeof(TightDataPointStorageD)); |
---|
21 | (*this)->dataSeriesLength = 0; |
---|
22 | (*this)->allSameData = 0; |
---|
23 | (*this)->exactDataNum = 0; |
---|
24 | (*this)->reservedValue = 0; |
---|
25 | (*this)->reqLength = 0; |
---|
26 | (*this)->radExpo = 0; |
---|
27 | |
---|
28 | (*this)->rtypeArray = NULL; |
---|
29 | (*this)->rtypeArray_size = 0; |
---|
30 | |
---|
31 | (*this)->typeArray = NULL; //its size is dataSeriesLength/4 (or xxx/4+1) |
---|
32 | (*this)->typeArray_size = 0; |
---|
33 | |
---|
34 | (*this)->leadNumArray = NULL; //its size is exactDataNum/4 (or exactDataNum/4+1) |
---|
35 | (*this)->leadNumArray_size = 0; |
---|
36 | |
---|
37 | (*this)->exactMidBytes = NULL; |
---|
38 | (*this)->exactMidBytes_size = 0; |
---|
39 | |
---|
40 | (*this)->residualMidBits = NULL; |
---|
41 | (*this)->residualMidBits_size = 0; |
---|
42 | |
---|
43 | (*this)->intervals = 0; |
---|
44 | (*this)->isLossless = 0; |
---|
45 | |
---|
46 | (*this)->segment_size = 0; |
---|
47 | (*this)->pwrErrBoundBytes = NULL; |
---|
48 | (*this)->pwrErrBoundBytes_size = 0; |
---|
49 | |
---|
50 | (*this)->raBytes = NULL; |
---|
51 | (*this)->raBytes_size = 0; |
---|
52 | |
---|
53 | } |
---|
54 | |
---|
55 | int new_TightDataPointStorageD_fromFlatBytes(TightDataPointStorageD **this, unsigned char* flatBytes, size_t flatBytesLength) |
---|
56 | { |
---|
57 | new_TightDataPointStorageD_Empty(this); |
---|
58 | size_t i, index = 0; |
---|
59 | size_t pwrErrBoundBytes_size = 0, segmentL = 0, radExpoL = 0, pwrErrBoundBytesL = 0; |
---|
60 | char version[3]; |
---|
61 | for (i = 0; i < 3; i++) |
---|
62 | version[i] = flatBytes[index++]; //3 |
---|
63 | unsigned char sameRByte = flatBytes[index++]; //1 |
---|
64 | if(checkVersion(version)!=1) |
---|
65 | { |
---|
66 | //wrong version |
---|
67 | printf("Wrong version: \nCompressed-data version (%d.%d.%d)\n",version[0], version[1], version[2]); |
---|
68 | printf("Current sz version: (%d.%d.%d)\n", versionNumber[0], versionNumber[1], versionNumber[2]); |
---|
69 | printf("Please double-check if the compressed data (or file) is correct.\n"); |
---|
70 | exit(0); |
---|
71 | } |
---|
72 | |
---|
73 | int same = sameRByte & 0x01; |
---|
74 | //confparams_dec->szMode = (sameRByte & 0x06)>>1; |
---|
75 | (*this)->isLossless = (sameRByte & 0x10)>>4; |
---|
76 | int isPW_REL = (sameRByte & 0x20)>>5; |
---|
77 | exe_params->SZ_SIZE_TYPE = ((sameRByte & 0x40)>>6)==1?8:4; |
---|
78 | |
---|
79 | int errorBoundMode = ABS; |
---|
80 | if(isPW_REL) |
---|
81 | { |
---|
82 | errorBoundMode = PW_REL; |
---|
83 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
84 | pwrErrBoundBytesL = 4; |
---|
85 | } |
---|
86 | |
---|
87 | sz_params* params = convertBytesToSZParams(&(flatBytes[index])); |
---|
88 | int mode = confparams_dec->szMode; |
---|
89 | int predictionMode = confparams_dec->predictionMode; |
---|
90 | int losslessCompressor = confparams_dec->losslessCompressor; |
---|
91 | if(confparams_dec!=NULL) |
---|
92 | free(confparams_dec); |
---|
93 | confparams_dec = params; |
---|
94 | confparams_dec->szMode = mode; |
---|
95 | confparams_dec->losslessCompressor = losslessCompressor; |
---|
96 | |
---|
97 | if(mode==SZ_TEMPORAL_COMPRESSION) |
---|
98 | { |
---|
99 | confparams_dec->szMode = SZ_TEMPORAL_COMPRESSION; |
---|
100 | confparams_dec->predictionMode = predictionMode; |
---|
101 | } |
---|
102 | index += MetaDataByteLength; |
---|
103 | |
---|
104 | int isRandomAccess = (sameRByte >> 7) & 0x01; |
---|
105 | |
---|
106 | unsigned char dsLengthBytes[8]; |
---|
107 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
108 | dsLengthBytes[i] = flatBytes[index++]; |
---|
109 | (*this)->dataSeriesLength = bytesToSize(dsLengthBytes); |
---|
110 | |
---|
111 | //printf("confparams_dec->szMode=%d\n",confparams_dec->szMode); |
---|
112 | |
---|
113 | if((*this)->isLossless==1) |
---|
114 | { |
---|
115 | //(*this)->exactMidBytes = flatBytes+8; |
---|
116 | return errorBoundMode; |
---|
117 | } |
---|
118 | else if(same==1) |
---|
119 | { |
---|
120 | (*this)->allSameData = 1; |
---|
121 | size_t exactMidBytesLength = sizeof(double);//flatBytesLength - 3 - 1 - MetaDataByteLength -exe_params->SZ_SIZE_TYPE; |
---|
122 | if(exactMidBytesLength>0) |
---|
123 | (*this)->exactMidBytes = (unsigned char*)malloc(sizeof(unsigned char)*exactMidBytesLength); |
---|
124 | else |
---|
125 | (*this)->exactMidBytes = NULL; |
---|
126 | for(i = 0;i<exactMidBytesLength;i++) |
---|
127 | (*this)->exactMidBytes[i] = flatBytes[index++]; |
---|
128 | return errorBoundMode; |
---|
129 | } |
---|
130 | else |
---|
131 | (*this)->allSameData = 0; |
---|
132 | |
---|
133 | if(isRandomAccess == 1) |
---|
134 | { |
---|
135 | (*this)->raBytes_size = flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE; |
---|
136 | (*this)->raBytes = &(flatBytes[index]); |
---|
137 | return errorBoundMode; |
---|
138 | } |
---|
139 | |
---|
140 | int rtype_ = sameRByte & 0x08; //1000 |
---|
141 | |
---|
142 | unsigned char byteBuf[8]; |
---|
143 | |
---|
144 | for (i = 0; i < 4; i++) |
---|
145 | byteBuf[i] = flatBytes[index++]; |
---|
146 | int max_quant_intervals = bytesToInt_bigEndian(byteBuf);// 4 |
---|
147 | |
---|
148 | confparams_dec->maxRangeRadius = max_quant_intervals/2; |
---|
149 | |
---|
150 | if(errorBoundMode>=PW_REL) |
---|
151 | { |
---|
152 | (*this)->radExpo = flatBytes[index++];//1 |
---|
153 | radExpoL = 1; |
---|
154 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
155 | byteBuf[i] = flatBytes[index++]; |
---|
156 | params->segment_size = (*this)->segment_size = bytesToSize(byteBuf);// exe_params->SZ_SIZE_TYPE |
---|
157 | |
---|
158 | for (i = 0; i < 4; i++) |
---|
159 | byteBuf[i] = flatBytes[index++]; |
---|
160 | pwrErrBoundBytes_size = (*this)->pwrErrBoundBytes_size = bytesToInt_bigEndian(byteBuf);// 4 |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | pwrErrBoundBytes_size = 0; |
---|
165 | (*this)->pwrErrBoundBytes = NULL; |
---|
166 | } |
---|
167 | |
---|
168 | for (i = 0; i < 4; i++) |
---|
169 | byteBuf[i] = flatBytes[index++]; |
---|
170 | (*this)->intervals = bytesToInt_bigEndian(byteBuf);// 4 |
---|
171 | |
---|
172 | for (i = 0; i < 8; i++) |
---|
173 | byteBuf[i] = flatBytes[index++]; |
---|
174 | (*this)->medianValue = bytesToDouble(byteBuf);//8 |
---|
175 | |
---|
176 | (*this)->reqLength = flatBytes[index++]; //1 |
---|
177 | |
---|
178 | for (i = 0; i < 8; i++) |
---|
179 | byteBuf[i] = flatBytes[index++]; |
---|
180 | (*this)->realPrecision = bytesToDouble(byteBuf);//8 |
---|
181 | |
---|
182 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
183 | byteBuf[i] = flatBytes[index++]; |
---|
184 | (*this)->typeArray_size = bytesToSize(byteBuf);// exe_params->SZ_SIZE_TYPE |
---|
185 | |
---|
186 | if(rtype_!=0) |
---|
187 | { |
---|
188 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++) |
---|
189 | byteBuf[i] = flatBytes[index++]; |
---|
190 | (*this)->rtypeArray_size = bytesToSize(byteBuf);//ST |
---|
191 | } |
---|
192 | else |
---|
193 | (*this)->rtypeArray_size = 0; |
---|
194 | |
---|
195 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
196 | byteBuf[i] = flatBytes[index++]; |
---|
197 | (*this)->exactDataNum = bytesToSize(byteBuf);// ST |
---|
198 | |
---|
199 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
200 | byteBuf[i] = flatBytes[index++]; |
---|
201 | (*this)->exactMidBytes_size = bytesToSize(byteBuf);// ST |
---|
202 | |
---|
203 | if (rtype_ != 0) { |
---|
204 | if((*this)->rtypeArray_size>0) |
---|
205 | (*this)->rtypeArray = (unsigned char*)malloc(sizeof(unsigned char)*(*this)->rtypeArray_size); |
---|
206 | else |
---|
207 | (*this)->rtypeArray = NULL; |
---|
208 | |
---|
209 | for (i = 0; i < 8; i++) |
---|
210 | byteBuf[i] = flatBytes[index++]; |
---|
211 | (*this)->reservedValue = bytesToDouble(byteBuf);//8 |
---|
212 | } |
---|
213 | |
---|
214 | size_t logicLeadNumBitsNum = (*this)->exactDataNum * 2; |
---|
215 | if (logicLeadNumBitsNum % 8 == 0) |
---|
216 | { |
---|
217 | (*this)->leadNumArray_size = logicLeadNumBitsNum >> 3; |
---|
218 | } |
---|
219 | else |
---|
220 | { |
---|
221 | (*this)->leadNumArray_size = (logicLeadNumBitsNum >> 3) + 1; |
---|
222 | } |
---|
223 | |
---|
224 | int minLogValueSize = 0; |
---|
225 | if(errorBoundMode>=PW_REL) |
---|
226 | minLogValueSize = 8; |
---|
227 | |
---|
228 | if ((*this)->rtypeArray != NULL) |
---|
229 | { |
---|
230 | (*this)->residualMidBits_size = flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE - 4 - radExpoL - segmentL - pwrErrBoundBytesL - 4 - 8 - 1 - 8 |
---|
231 | - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - 8 - (*this)->rtypeArray_size |
---|
232 | - minLogValueSize - (*this)->typeArray_size - (*this)->leadNumArray_size |
---|
233 | - (*this)->exactMidBytes_size - pwrErrBoundBytes_size; |
---|
234 | for (i = 0; i < (*this)->rtypeArray_size; i++) |
---|
235 | (*this)->rtypeArray[i] = flatBytes[index++]; |
---|
236 | } |
---|
237 | else |
---|
238 | { |
---|
239 | (*this)->residualMidBits_size = flatBytesLength - 3 - 1 - MetaDataByteLength - exe_params->SZ_SIZE_TYPE - 4 - radExpoL - segmentL - pwrErrBoundBytesL - 4 - 8 - 1 - 8 |
---|
240 | - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - exe_params->SZ_SIZE_TYPE - minLogValueSize - (*this)->typeArray_size |
---|
241 | - (*this)->leadNumArray_size - (*this)->exactMidBytes_size - pwrErrBoundBytes_size; |
---|
242 | } |
---|
243 | |
---|
244 | if(errorBoundMode >= PW_REL){ |
---|
245 | (*this)->minLogValue = bytesToDouble(&flatBytes[index]); |
---|
246 | index+=8; |
---|
247 | } |
---|
248 | |
---|
249 | (*this)->typeArray = &flatBytes[index]; |
---|
250 | //retrieve the number of states (i.e., stateNum) |
---|
251 | (*this)->allNodes = bytesToInt_bigEndian((*this)->typeArray); //the first 4 bytes store the stateNum |
---|
252 | (*this)->stateNum = ((*this)->allNodes+1)/2; |
---|
253 | |
---|
254 | index+=(*this)->typeArray_size; |
---|
255 | |
---|
256 | (*this)->pwrErrBoundBytes = &flatBytes[index]; |
---|
257 | |
---|
258 | index+=pwrErrBoundBytes_size; |
---|
259 | |
---|
260 | (*this)->leadNumArray = &flatBytes[index]; |
---|
261 | |
---|
262 | index+=(*this)->leadNumArray_size; |
---|
263 | |
---|
264 | (*this)->exactMidBytes = &flatBytes[index]; |
---|
265 | |
---|
266 | index+=(*this)->exactMidBytes_size; |
---|
267 | |
---|
268 | (*this)->residualMidBits = &flatBytes[index]; |
---|
269 | |
---|
270 | //index+=(*this)->residualMidBits_size; |
---|
271 | |
---|
272 | return errorBoundMode; |
---|
273 | } |
---|
274 | |
---|
275 | /** |
---|
276 | * |
---|
277 | * type's length == dataSeriesLength |
---|
278 | * exactMidBytes's length == exactMidBytes_size |
---|
279 | * leadNumIntArray's length == exactDataNum |
---|
280 | * escBytes's length == escBytes_size |
---|
281 | * resiBitLength's length == resiBitLengthSize |
---|
282 | * */ |
---|
283 | void new_TightDataPointStorageD(TightDataPointStorageD **this, |
---|
284 | size_t dataSeriesLength, size_t exactDataNum, |
---|
285 | int* type, unsigned char* exactMidBytes, size_t exactMidBytes_size, |
---|
286 | unsigned char* leadNumIntArray, //leadNumIntArray contains readable numbers.... |
---|
287 | unsigned char* resiMidBits, size_t resiMidBits_size, |
---|
288 | unsigned char resiBitLength, |
---|
289 | double realPrecision, double medianValue, char reqLength, unsigned int intervals, |
---|
290 | unsigned char* pwrErrBoundBytes, size_t pwrErrBoundBytes_size, unsigned char radExpo) { |
---|
291 | //int i = 0; |
---|
292 | *this = (TightDataPointStorageD *)malloc(sizeof(TightDataPointStorageD)); |
---|
293 | (*this)->allSameData = 0; |
---|
294 | (*this)->realPrecision = realPrecision; |
---|
295 | (*this)->medianValue = medianValue; |
---|
296 | (*this)->reqLength = reqLength; |
---|
297 | |
---|
298 | (*this)->dataSeriesLength = dataSeriesLength; |
---|
299 | (*this)->exactDataNum = exactDataNum; |
---|
300 | |
---|
301 | (*this)->rtypeArray = NULL; |
---|
302 | (*this)->rtypeArray_size = 0; |
---|
303 | |
---|
304 | int stateNum = 2*intervals; |
---|
305 | HuffmanTree* huffmanTree = createHuffmanTree(stateNum); |
---|
306 | encode_withTree(huffmanTree, type, dataSeriesLength, &(*this)->typeArray, &(*this)->typeArray_size); |
---|
307 | SZ_ReleaseHuffman(huffmanTree); |
---|
308 | |
---|
309 | (*this)->exactMidBytes = exactMidBytes; |
---|
310 | (*this)->exactMidBytes_size = exactMidBytes_size; |
---|
311 | |
---|
312 | (*this)->leadNumArray_size = convertIntArray2ByteArray_fast_2b(leadNumIntArray, exactDataNum, &((*this)->leadNumArray)); |
---|
313 | |
---|
314 | (*this)->residualMidBits_size = convertIntArray2ByteArray_fast_dynamic(resiMidBits, resiBitLength, exactDataNum, &((*this)->residualMidBits)); |
---|
315 | |
---|
316 | (*this)->intervals = intervals; |
---|
317 | |
---|
318 | (*this)->isLossless = 0; |
---|
319 | |
---|
320 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
321 | (*this)->pwrErrBoundBytes = pwrErrBoundBytes; |
---|
322 | else |
---|
323 | (*this)->pwrErrBoundBytes = NULL; |
---|
324 | |
---|
325 | (*this)->radExpo = radExpo; |
---|
326 | |
---|
327 | (*this)->pwrErrBoundBytes_size = pwrErrBoundBytes_size; |
---|
328 | } |
---|
329 | |
---|
330 | void new_TightDataPointStorageD2(TightDataPointStorageD **this, |
---|
331 | size_t dataSeriesLength, size_t exactDataNum, |
---|
332 | int* type, unsigned char* exactMidBytes, size_t exactMidBytes_size, |
---|
333 | unsigned char* leadNumIntArray, //leadNumIntArray contains readable numbers.... |
---|
334 | unsigned char* resiMidBits, size_t resiMidBits_size, |
---|
335 | unsigned char* resiBitLength, size_t resiBitLengthSize, |
---|
336 | double realPrecision, double medianValue, char reqLength, unsigned int intervals, |
---|
337 | unsigned char* pwrErrBoundBytes, size_t pwrErrBoundBytes_size, unsigned char radExpo) { |
---|
338 | //int i = 0; |
---|
339 | *this = (TightDataPointStorageD *)malloc(sizeof(TightDataPointStorageD)); |
---|
340 | (*this)->allSameData = 0; |
---|
341 | (*this)->realPrecision = realPrecision; |
---|
342 | (*this)->medianValue = medianValue; |
---|
343 | (*this)->reqLength = reqLength; |
---|
344 | |
---|
345 | (*this)->dataSeriesLength = dataSeriesLength; |
---|
346 | (*this)->exactDataNum = exactDataNum; |
---|
347 | |
---|
348 | (*this)->rtypeArray = NULL; |
---|
349 | (*this)->rtypeArray_size = 0; |
---|
350 | |
---|
351 | int stateNum = 2*intervals; |
---|
352 | HuffmanTree* huffmanTree = createHuffmanTree(stateNum); |
---|
353 | encode_withTree(huffmanTree, type, dataSeriesLength, &(*this)->typeArray, &(*this)->typeArray_size); |
---|
354 | SZ_ReleaseHuffman(huffmanTree); |
---|
355 | |
---|
356 | (*this)->exactMidBytes = exactMidBytes; |
---|
357 | (*this)->exactMidBytes_size = exactMidBytes_size; |
---|
358 | |
---|
359 | (*this)->leadNumArray_size = convertIntArray2ByteArray_fast_2b(leadNumIntArray, exactDataNum, &((*this)->leadNumArray)); |
---|
360 | |
---|
361 | //(*this)->residualMidBits = resiMidBits; |
---|
362 | //(*this)->residualMidBits_size = resiMidBits_size; |
---|
363 | |
---|
364 | (*this)->residualMidBits_size = convertIntArray2ByteArray_fast_dynamic2(resiMidBits, resiBitLength, resiBitLengthSize, &((*this)->residualMidBits)); |
---|
365 | |
---|
366 | (*this)->intervals = intervals; |
---|
367 | |
---|
368 | (*this)->isLossless = 0; |
---|
369 | |
---|
370 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
371 | (*this)->pwrErrBoundBytes = pwrErrBoundBytes; |
---|
372 | else |
---|
373 | (*this)->pwrErrBoundBytes = NULL; |
---|
374 | |
---|
375 | (*this)->radExpo = radExpo; |
---|
376 | |
---|
377 | (*this)->pwrErrBoundBytes_size = pwrErrBoundBytes_size; |
---|
378 | } |
---|
379 | |
---|
380 | void convertTDPStoBytes_double(TightDataPointStorageD* tdps, unsigned char* bytes, unsigned char* dsLengthBytes, unsigned char sameByte) |
---|
381 | { |
---|
382 | size_t i, k = 0; |
---|
383 | unsigned char intervalsBytes[4]; |
---|
384 | unsigned char typeArrayLengthBytes[8]; |
---|
385 | unsigned char exactLengthBytes[8]; |
---|
386 | unsigned char exactMidBytesLength[8]; |
---|
387 | unsigned char realPrecisionBytes[8]; |
---|
388 | |
---|
389 | unsigned char medianValueBytes[8]; |
---|
390 | |
---|
391 | unsigned char segment_sizeBytes[8]; |
---|
392 | unsigned char pwrErrBoundBytes_sizeBytes[4]; |
---|
393 | unsigned char max_quant_intervals_Bytes[4]; |
---|
394 | |
---|
395 | for(i = 0;i<3;i++)//3 bytes |
---|
396 | bytes[k++] = versionNumber[i]; |
---|
397 | bytes[k++] = sameByte; //1 byte |
---|
398 | |
---|
399 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
400 | k = k + MetaDataByteLength; |
---|
401 | |
---|
402 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST: 4 or 8 bytes |
---|
403 | bytes[k++] = dsLengthBytes[i]; |
---|
404 | intToBytes_bigEndian(max_quant_intervals_Bytes, confparams_cpr->max_quant_intervals); |
---|
405 | for(i = 0;i<4;i++)//4 |
---|
406 | bytes[k++] = max_quant_intervals_Bytes[i]; |
---|
407 | |
---|
408 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
409 | { |
---|
410 | bytes[k++] = tdps->radExpo; //1 byte |
---|
411 | |
---|
412 | sizeToBytes(segment_sizeBytes, confparams_cpr->segment_size); |
---|
413 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
414 | bytes[k++] = segment_sizeBytes[i]; |
---|
415 | |
---|
416 | intToBytes_bigEndian(pwrErrBoundBytes_sizeBytes, tdps->pwrErrBoundBytes_size); |
---|
417 | for(i = 0;i<4;i++)//4 |
---|
418 | bytes[k++] = pwrErrBoundBytes_sizeBytes[i]; |
---|
419 | } |
---|
420 | |
---|
421 | intToBytes_bigEndian(intervalsBytes, tdps->intervals); |
---|
422 | for(i = 0;i<4;i++)//4 |
---|
423 | bytes[k++] = intervalsBytes[i]; |
---|
424 | |
---|
425 | doubleToBytes(medianValueBytes, tdps->medianValue); |
---|
426 | for (i = 0; i < 8; i++)// 8 |
---|
427 | bytes[k++] = medianValueBytes[i]; |
---|
428 | |
---|
429 | bytes[k++] = tdps->reqLength; //1 byte |
---|
430 | |
---|
431 | /*if(errorBoundMode>=PW_REL) |
---|
432 | doubleToBytes(realPrecisionBytes, pw_relBoundRatio); |
---|
433 | else*/ |
---|
434 | doubleToBytes(realPrecisionBytes, tdps->realPrecision); |
---|
435 | for (i = 0; i < 8; i++)// 8 |
---|
436 | bytes[k++] = realPrecisionBytes[i]; |
---|
437 | |
---|
438 | sizeToBytes(typeArrayLengthBytes, tdps->typeArray_size); |
---|
439 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
440 | bytes[k++] = typeArrayLengthBytes[i]; |
---|
441 | |
---|
442 | sizeToBytes(exactLengthBytes, tdps->exactDataNum); |
---|
443 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
444 | bytes[k++] = exactLengthBytes[i]; |
---|
445 | |
---|
446 | sizeToBytes(exactMidBytesLength, tdps->exactMidBytes_size); |
---|
447 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
448 | bytes[k++] = exactMidBytesLength[i]; |
---|
449 | |
---|
450 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
451 | { |
---|
452 | doubleToBytes(exactMidBytesLength, tdps->minLogValue); |
---|
453 | for(i = 0;i < 8; i++) |
---|
454 | bytes[k++] = exactMidBytesLength[i]; |
---|
455 | } |
---|
456 | |
---|
457 | memcpy(&(bytes[k]), tdps->typeArray, tdps->typeArray_size); |
---|
458 | k += tdps->typeArray_size; |
---|
459 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
460 | { |
---|
461 | memcpy(&(bytes[k]), tdps->pwrErrBoundBytes, tdps->pwrErrBoundBytes_size); |
---|
462 | k += tdps->pwrErrBoundBytes_size; |
---|
463 | } |
---|
464 | |
---|
465 | memcpy(&(bytes[k]), tdps->leadNumArray, tdps->leadNumArray_size); |
---|
466 | k += tdps->leadNumArray_size; |
---|
467 | memcpy(&(bytes[k]), tdps->exactMidBytes, tdps->exactMidBytes_size); |
---|
468 | k += tdps->exactMidBytes_size; |
---|
469 | |
---|
470 | if(tdps->residualMidBits!=NULL) |
---|
471 | { |
---|
472 | memcpy(&(bytes[k]), tdps->residualMidBits, tdps->residualMidBits_size); |
---|
473 | k += tdps->residualMidBits_size; |
---|
474 | } |
---|
475 | } |
---|
476 | |
---|
477 | void convertTDPStoBytes_double_reserve(TightDataPointStorageD* tdps, unsigned char* bytes, unsigned char* dsLengthBytes, unsigned char sameByte) |
---|
478 | { |
---|
479 | size_t i, k = 0; |
---|
480 | unsigned char intervalsBytes[4]; |
---|
481 | unsigned char typeArrayLengthBytes[8]; |
---|
482 | unsigned char rTypeLengthBytes[8]; |
---|
483 | unsigned char exactLengthBytes[8]; |
---|
484 | unsigned char exactMidBytesLength[8]; |
---|
485 | unsigned char reservedValueBytes[8]; |
---|
486 | unsigned char realPrecisionBytes[8]; |
---|
487 | |
---|
488 | unsigned char medianValueBytes[8]; |
---|
489 | |
---|
490 | unsigned char segment_sizeBytes[8]; |
---|
491 | unsigned char pwrErrBoundBytes_sizeBytes[4]; |
---|
492 | unsigned char max_quant_intervals_Bytes[4]; |
---|
493 | |
---|
494 | for(i = 0;i<3;i++)//3 |
---|
495 | bytes[k++] = versionNumber[i]; |
---|
496 | bytes[k++] = sameByte; //1 |
---|
497 | |
---|
498 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
499 | k = k + MetaDataByteLength; |
---|
500 | |
---|
501 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
502 | bytes[k++] = dsLengthBytes[i]; |
---|
503 | |
---|
504 | intToBytes_bigEndian(max_quant_intervals_Bytes, confparams_cpr->max_quant_intervals); |
---|
505 | for(i = 0;i<4;i++)//4 |
---|
506 | bytes[k++] = max_quant_intervals_Bytes[i]; |
---|
507 | |
---|
508 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
509 | { |
---|
510 | bytes[k++] = tdps->radExpo; //1 byte |
---|
511 | |
---|
512 | sizeToBytes(segment_sizeBytes, confparams_cpr->segment_size); |
---|
513 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//4 |
---|
514 | bytes[k++] = segment_sizeBytes[i]; |
---|
515 | |
---|
516 | intToBytes_bigEndian(pwrErrBoundBytes_sizeBytes, tdps->pwrErrBoundBytes_size); |
---|
517 | for(i = 0;i<4;i++)//4 |
---|
518 | bytes[k++] = pwrErrBoundBytes_sizeBytes[i]; |
---|
519 | } |
---|
520 | intToBytes_bigEndian(intervalsBytes, tdps->intervals); |
---|
521 | for(i = 0;i<4;i++)//4 |
---|
522 | bytes[k++] = intervalsBytes[i]; |
---|
523 | |
---|
524 | doubleToBytes(medianValueBytes, tdps->medianValue); |
---|
525 | for (i = 0; i < 8; i++)// 8 |
---|
526 | bytes[k++] = medianValueBytes[i]; |
---|
527 | |
---|
528 | bytes[k++] = tdps->reqLength; //1 byte |
---|
529 | |
---|
530 | doubleToBytes(realPrecisionBytes, tdps->realPrecision); |
---|
531 | for (i = 0; i < 8; i++)// 8 |
---|
532 | bytes[k++] = realPrecisionBytes[i]; |
---|
533 | |
---|
534 | sizeToBytes(typeArrayLengthBytes, tdps->typeArray_size); |
---|
535 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
536 | bytes[k++] = typeArrayLengthBytes[i]; |
---|
537 | |
---|
538 | sizeToBytes(rTypeLengthBytes, tdps->rtypeArray_size); |
---|
539 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
540 | bytes[k++] = rTypeLengthBytes[i]; |
---|
541 | |
---|
542 | sizeToBytes(exactLengthBytes, tdps->exactDataNum); |
---|
543 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
544 | bytes[k++] = exactLengthBytes[i]; |
---|
545 | |
---|
546 | sizeToBytes(exactMidBytesLength, tdps->exactMidBytes_size); |
---|
547 | for(i = 0;i<exe_params->SZ_SIZE_TYPE;i++)//ST |
---|
548 | bytes[k++] = exactMidBytesLength[i]; |
---|
549 | |
---|
550 | doubleToBytes(reservedValueBytes, tdps->reservedValue); |
---|
551 | for (i = 0; i < 8; i++)// 8 |
---|
552 | bytes[k++] = reservedValueBytes[i]; |
---|
553 | |
---|
554 | memcpy(&(bytes[k]), tdps->rtypeArray, tdps->rtypeArray_size); |
---|
555 | k += tdps->rtypeArray_size; |
---|
556 | |
---|
557 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
558 | { |
---|
559 | doubleToBytes(exactMidBytesLength, tdps->minLogValue); |
---|
560 | for(i = 0;i < 8; i++) |
---|
561 | bytes[k++] = exactMidBytesLength[i]; |
---|
562 | } |
---|
563 | |
---|
564 | memcpy(&(bytes[k]), tdps->typeArray, tdps->typeArray_size); |
---|
565 | k += tdps->typeArray_size; |
---|
566 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
567 | { |
---|
568 | memcpy(&(bytes[k]), tdps->pwrErrBoundBytes, tdps->pwrErrBoundBytes_size); |
---|
569 | k += tdps->pwrErrBoundBytes_size; |
---|
570 | } |
---|
571 | memcpy(&(bytes[k]), tdps->leadNumArray, tdps->leadNumArray_size); |
---|
572 | k += tdps->leadNumArray_size; |
---|
573 | memcpy(&(bytes[k]), tdps->exactMidBytes, tdps->exactMidBytes_size); |
---|
574 | k += tdps->exactMidBytes_size; |
---|
575 | if(tdps->residualMidBits!=NULL) |
---|
576 | { |
---|
577 | memcpy(&(bytes[k]), tdps->residualMidBits, tdps->residualMidBits_size); |
---|
578 | k += tdps->residualMidBits_size; |
---|
579 | } |
---|
580 | } |
---|
581 | |
---|
582 | //Convert TightDataPointStorageD to bytes... |
---|
583 | void convertTDPStoFlatBytes_double(TightDataPointStorageD *tdps, unsigned char** bytes, size_t *size) |
---|
584 | { |
---|
585 | size_t i, k = 0; |
---|
586 | unsigned char dsLengthBytes[8]; |
---|
587 | |
---|
588 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
589 | intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4 |
---|
590 | else |
---|
591 | longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8 |
---|
592 | |
---|
593 | unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0; |
---|
594 | sameByte = sameByte | (confparams_cpr->szMode << 1); |
---|
595 | if(tdps->isLossless) |
---|
596 | sameByte = (unsigned char) (sameByte | 0x10); |
---|
597 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
598 | sameByte = (unsigned char) (sameByte | 0x20); // 00100000, the 5th bit |
---|
599 | if(exe_params->SZ_SIZE_TYPE==8) |
---|
600 | sameByte = (unsigned char) (sameByte | 0x40); // 01000000, the 6th bit |
---|
601 | |
---|
602 | if(tdps->allSameData==1) |
---|
603 | { |
---|
604 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactMidBytes_size; |
---|
605 | *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength); |
---|
606 | |
---|
607 | for (i = 0; i < 3; i++)//3 |
---|
608 | (*bytes)[k++] = versionNumber[i]; |
---|
609 | (*bytes)[k++] = sameByte; |
---|
610 | |
---|
611 | convertSZParamsToBytes(confparams_cpr, &((*bytes)[k])); |
---|
612 | k = k + MetaDataByteLength; |
---|
613 | |
---|
614 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
615 | (*bytes)[k++] = dsLengthBytes[i]; |
---|
616 | |
---|
617 | for (i = 0; i < tdps->exactMidBytes_size; i++) |
---|
618 | (*bytes)[k++] = tdps->exactMidBytes[i]; |
---|
619 | |
---|
620 | *size = totalByteLength; |
---|
621 | } |
---|
622 | else if (tdps->rtypeArray == NULL) |
---|
623 | { |
---|
624 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
625 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
626 | |
---|
627 | int minLogValueSize = 0; |
---|
628 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
629 | { |
---|
630 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
631 | radExpoL = 1; |
---|
632 | pwrBoundArrayL = 4; |
---|
633 | minLogValueSize = 8; |
---|
634 | } |
---|
635 | |
---|
636 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 8 + 1 + 8 |
---|
637 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE |
---|
638 | + minLogValueSize /*max absolute log value*/ |
---|
639 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
640 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
641 | |
---|
642 | *bytes = (unsigned char *)malloc(sizeof(unsigned char)*totalByteLength); |
---|
643 | |
---|
644 | convertTDPStoBytes_double(tdps, *bytes, dsLengthBytes, sameByte); |
---|
645 | |
---|
646 | *size = totalByteLength; |
---|
647 | } |
---|
648 | else //the case with reserved value |
---|
649 | { |
---|
650 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
651 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
652 | int minLogValueSize = 0; |
---|
653 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
654 | { |
---|
655 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
656 | radExpoL = 1; |
---|
657 | pwrBoundArrayL = 4; |
---|
658 | minLogValueSize = 8; |
---|
659 | } |
---|
660 | |
---|
661 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 8 + 1 + 8 |
---|
662 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + 8 + tdps->rtypeArray_size |
---|
663 | + minLogValueSize + tdps->typeArray_size + tdps->leadNumArray_size |
---|
664 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
665 | |
---|
666 | sameByte = (unsigned char) (sameByte | 0x08); // 00001000, the 4th bit |
---|
667 | // denotes whether it is |
---|
668 | // with "reserved value" |
---|
669 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
670 | sameByte = (unsigned char) (sameByte | 0x10); // 00001000, the 5th bit |
---|
671 | |
---|
672 | *bytes = (unsigned char*)malloc(sizeof(unsigned char)*totalByteLength); |
---|
673 | |
---|
674 | convertTDPStoBytes_double_reserve(tdps, *bytes, dsLengthBytes, sameByte); |
---|
675 | |
---|
676 | *size = totalByteLength; |
---|
677 | } |
---|
678 | } |
---|
679 | |
---|
680 | void convertTDPStoFlatBytes_double_args(TightDataPointStorageD *tdps, unsigned char* bytes, size_t *size) |
---|
681 | { |
---|
682 | size_t i, k = 0; |
---|
683 | unsigned char dsLengthBytes[8]; |
---|
684 | |
---|
685 | if(exe_params->SZ_SIZE_TYPE==4) |
---|
686 | intToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//4 |
---|
687 | else |
---|
688 | longToBytes_bigEndian(dsLengthBytes, tdps->dataSeriesLength);//8 |
---|
689 | |
---|
690 | unsigned char sameByte = tdps->allSameData==1?(unsigned char)1:(unsigned char)0; |
---|
691 | sameByte = sameByte | (confparams_cpr->szMode << 1); |
---|
692 | if(tdps->isLossless) |
---|
693 | sameByte = (unsigned char) (sameByte | 0x10); |
---|
694 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
695 | sameByte = (unsigned char) (sameByte | 0x20); // 00100000, the 5th bit |
---|
696 | if(exe_params->SZ_SIZE_TYPE==8) |
---|
697 | sameByte = (unsigned char) (sameByte | 0x40); //01000000, the 6th bit |
---|
698 | |
---|
699 | if(tdps->allSameData==1) |
---|
700 | { |
---|
701 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + tdps->exactMidBytes_size; |
---|
702 | |
---|
703 | for (i = 0; i < 3; i++)//3 |
---|
704 | bytes[k++] = versionNumber[i]; |
---|
705 | bytes[k++] = sameByte; |
---|
706 | |
---|
707 | convertSZParamsToBytes(confparams_cpr, &(bytes[k])); |
---|
708 | k = k + MetaDataByteLength; |
---|
709 | |
---|
710 | for (i = 0; i < exe_params->SZ_SIZE_TYPE; i++) |
---|
711 | bytes[k++] = dsLengthBytes[i]; |
---|
712 | for (i = 0; i < tdps->exactMidBytes_size; i++) |
---|
713 | bytes[k++] = tdps->exactMidBytes[i]; |
---|
714 | |
---|
715 | *size = totalByteLength; |
---|
716 | } |
---|
717 | else if (tdps->rtypeArray == NULL) |
---|
718 | { |
---|
719 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
720 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
721 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
722 | { |
---|
723 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
724 | radExpoL = 1; |
---|
725 | pwrBoundArrayL = 4; |
---|
726 | } |
---|
727 | |
---|
728 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE+ 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 8 + 1 + 8 |
---|
729 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE |
---|
730 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
731 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
732 | |
---|
733 | convertTDPStoBytes_double(tdps, bytes, dsLengthBytes, sameByte); |
---|
734 | |
---|
735 | *size = totalByteLength; |
---|
736 | } |
---|
737 | else //the case with reserved value |
---|
738 | { |
---|
739 | size_t residualMidBitsLength = tdps->residualMidBits == NULL ? 0 : tdps->residualMidBits_size; |
---|
740 | size_t segmentL = 0, radExpoL = 0, pwrBoundArrayL = 0; |
---|
741 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
742 | { |
---|
743 | segmentL = exe_params->SZ_SIZE_TYPE; |
---|
744 | radExpoL = 1; |
---|
745 | pwrBoundArrayL = 4; |
---|
746 | } |
---|
747 | |
---|
748 | size_t totalByteLength = 3 + 1 + MetaDataByteLength + exe_params->SZ_SIZE_TYPE + 4 + radExpoL + segmentL + pwrBoundArrayL + 4 + 8 + 1 + 8 |
---|
749 | + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + exe_params->SZ_SIZE_TYPE + 8 + tdps->rtypeArray_size |
---|
750 | + tdps->typeArray_size + tdps->leadNumArray_size |
---|
751 | + tdps->exactMidBytes_size + residualMidBitsLength + tdps->pwrErrBoundBytes_size; |
---|
752 | |
---|
753 | sameByte = (unsigned char) (sameByte | 0x08); // 00001000, the 4th bit |
---|
754 | // denotes whether it is |
---|
755 | // with "reserved value" |
---|
756 | if(confparams_cpr->errorBoundMode>=PW_REL) |
---|
757 | sameByte = (unsigned char) (sameByte | 0x10); // 00010000, the 5th bit |
---|
758 | |
---|
759 | convertTDPStoBytes_double_reserve(tdps, bytes, dsLengthBytes, sameByte); |
---|
760 | |
---|
761 | *size = totalByteLength; |
---|
762 | } |
---|
763 | } |
---|
764 | |
---|
765 | |
---|
766 | void free_TightDataPointStorageD(TightDataPointStorageD *tdps) |
---|
767 | { |
---|
768 | if(tdps->rtypeArray!=NULL) |
---|
769 | free(tdps->rtypeArray); |
---|
770 | if(tdps->typeArray!=NULL) |
---|
771 | free(tdps->typeArray); |
---|
772 | if(tdps->leadNumArray!=NULL) |
---|
773 | free(tdps->leadNumArray); |
---|
774 | if(tdps->exactMidBytes!=NULL) |
---|
775 | free(tdps->exactMidBytes); |
---|
776 | if(tdps->residualMidBits!=NULL) |
---|
777 | free(tdps->residualMidBits); |
---|
778 | if(tdps->pwrErrBoundBytes!=NULL) |
---|
779 | free(tdps->pwrErrBoundBytes); |
---|
780 | free(tdps); |
---|
781 | } |
---|
782 | |
---|
783 | /** |
---|
784 | * to free the memory used in the decompression |
---|
785 | * */ |
---|
786 | void free_TightDataPointStorageD2(TightDataPointStorageD *tdps) |
---|
787 | { |
---|
788 | free(tdps); |
---|
789 | } |
---|