Changeset 16442 for trunk/Mars
- Timestamp:
- 05/29/13 18:47:44 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/huffman.h
r15627 r16442 85 85 Code lut[1<<16]; 86 86 87 voidCreateEncoder(const TreeNode *n, size_t bits=0, uint8_t nbits=0)87 bool CreateEncoder(const TreeNode *n, size_t bits=0, uint8_t nbits=0) 88 88 { 89 89 if (n->isLeaf) 90 90 { 91 #ifdef __EXCEPTIONS 91 92 if (nbits>sizeof(size_t)*8) 92 93 throw std::runtime_error("Too many different symbols - this should not happen!"); 93 94 #else 95 if (nbits>sizeof(size_t)*8) 96 { 97 count = 0; 98 return false; 99 } 100 #endif 94 101 lut[n->symbol].bits = bits; 95 102 lut[n->symbol].numbits = nbits==0 ? 1 : nbits; 96 103 count++; 97 return; 98 } 99 100 CreateEncoder(n->zero, bits, nbits+1); 101 CreateEncoder(n->one, bits | (1<<nbits), nbits+1); 104 return true; 105 } 106 107 return 108 CreateEncoder(n->zero, bits, nbits+1) && 109 CreateEncoder(n->one, bits | (1<<nbits), nbits+1); 110 102 111 } 103 112 … … 296 305 const uint8_t curbyte = (*two >> curbit); 297 306 307 #ifdef __EXCEPTIONS 298 308 if (!p->lut) 299 309 throw std::runtime_error("Unknown bitcode in stream!"); 310 #else 311 if (!p->lut) 312 return NULL; 313 #endif 314 300 315 p = p->lut + curbyte; 301 316 if (!p->isLeaf) … … 321 336 } 322 337 323 Decoder(const uint8_t* bufin, unsigned int &pindex) : isLeaf(false), lut(NULL)338 Decoder(const uint8_t* bufin, int64_t &pindex) : isLeaf(false), lut(NULL) 324 339 { 325 340 // FIXME: Sanity check for size missing.... … … 348 363 349 364 const uint8_t numbytes = numbytes_from_numbits(numbits); 365 366 #ifdef __EXCEPTIONS 350 367 if (numbytes>sizeof(size_t)) 351 368 throw std::runtime_error("Number of bytes for a single symbol exceeds maximum."); 352 369 #else 370 if (numbytes>sizeof(size_t)) 371 { 372 pindex = -1; 373 return; 374 } 375 #endif 353 376 size_t bits=0; 354 377 memcpy(&bits, bufin+pindex, numbytes); … … 360 383 }; 361 384 362 void Encode(std::string &bufout, const uint16_t *bufin, size_t bufinlen) 363 { 385 bool Encode(std::string &bufout, const uint16_t *bufin, size_t bufinlen) 386 { 387 const Encoder encoder(bufin, bufinlen); 388 389 #ifndef __EXCEPTIONS 390 if (encoder.count==0) 391 return false; 392 #endif 393 364 394 bufout.append((char*)&bufinlen, sizeof(size_t)); 365 366 const Encoder encoder(bufin, bufinlen);367 395 encoder.WriteCodeTable(bufout); 368 396 encoder.Encode(bufout, bufin, bufinlen); 397 398 return true; 369 399 } 370 400 371 int Decode(const uint8_t *bufin,401 int64_t Decode(const uint8_t *bufin, 372 402 size_t bufinlen, 373 403 std::vector<uint16_t> &pbufout) 374 404 { 375 unsigned int i = 0;405 int64_t i = 0; 376 406 377 407 // Read the number of data bytes this encoding represents. … … 385 415 const Decoder decoder(bufin, i); 386 416 417 #ifndef __EXCEPTIONS 418 if (i==-1) 419 return -1; 420 #endif 421 387 422 const uint8_t *in_ptr = 388 423 decoder.Decode(bufin+i, bufin+bufinlen, 389 424 pbufout.data(), pbufout.data()+data_count); 390 425 426 #ifndef __EXCEPTIONS 427 if (!in_ptr) 428 return -1; 429 #endif 430 391 431 return in_ptr-bufin; 392 432 }
Note:
See TracChangeset
for help on using the changeset viewer.