Changeset 16442 for trunk/Mars/mcore


Ignore:
Timestamp:
05/29/13 18:47:44 (11 years ago)
Author:
tbretz
Message:
If exceptions are not enabled, don't throw...
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mcore/huffman.h

    r15627 r16442  
    8585        Code lut[1<<16];
    8686
    87         void CreateEncoder(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)
    8888        {
    8989            if (n->isLeaf)
    9090            {
     91#ifdef __EXCEPTIONS
    9192                if (nbits>sizeof(size_t)*8)
    9293                    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
    94101                lut[n->symbol].bits    = bits;
    95102                lut[n->symbol].numbits = nbits==0 ? 1 : nbits;
    96103                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
    102111        }
    103112
     
    296305                const uint8_t curbyte = (*two >> curbit);
    297306
     307#ifdef __EXCEPTIONS
    298308                if (!p->lut)
    299309                    throw std::runtime_error("Unknown bitcode in stream!");
     310#else
     311                if (!p->lut)
     312                    return NULL;
     313#endif
     314
    300315                p = p->lut + curbyte;
    301316                if (!p->isLeaf)
     
    321336        }
    322337
    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)
    324339        {
    325340            // FIXME: Sanity check for size missing....
     
    348363
    349364                const uint8_t numbytes = numbytes_from_numbits(numbits);
     365
     366#ifdef __EXCEPTIONS
    350367                if (numbytes>sizeof(size_t))
    351368                    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
    353376                size_t bits=0;
    354377                memcpy(&bits, bufin+pindex, numbytes);
     
    360383    };
    361384
    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
    364394        bufout.append((char*)&bufinlen, sizeof(size_t));
    365 
    366         const Encoder encoder(bufin, bufinlen);
    367395        encoder.WriteCodeTable(bufout);
    368396        encoder.Encode(bufout, bufin, bufinlen);
     397
     398        return true;
    369399    }
    370400
    371     int Decode(const uint8_t *bufin,
     401    int64_t Decode(const uint8_t *bufin,
    372402               size_t         bufinlen,
    373403               std::vector<uint16_t> &pbufout)
    374404    {
    375         unsigned int i = 0;
     405        int64_t i = 0;
    376406
    377407        // Read the number of data bytes this encoding represents.
     
    385415        const Decoder decoder(bufin, i);
    386416
     417#ifndef __EXCEPTIONS
     418        if (i==-1)
     419            return -1;
     420#endif
     421
    387422        const uint8_t *in_ptr =
    388423            decoder.Decode(bufin+i, bufin+bufinlen,
    389424                           pbufout.data(), pbufout.data()+data_count);
    390425
     426#ifndef __EXCEPTIONS
     427        if (!in_ptr)
     428            return -1;
     429#endif
     430
    391431        return in_ptr-bufin;
    392432    }
Note: See TracChangeset for help on using the changeset viewer.