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