Advertisement
dlemire

integercodec.h

Jun 4th, 2012
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1.  
  2. class IntegerCODEC {
  3. public:
  4.  
  5.     /**
  6.      * You specify input and input length, as well as
  7.      * output and output length. nvalue gets modified to
  8.      * reflect how much was used. If the new value of
  9.      * nvalue is more than the original value, we can
  10.      * consider this a buffer overrun.
  11.      *
  12.      * You are responsible for allocating the memory (length
  13.      * for *in and nvalue for *out).
  14.      */
  15.     virtual void encodeArray(const uint32_t *in, const size_t length,
  16.             uint32_t *out, size_t &nvalue) = 0;
  17.  
  18.     /**
  19.      * Usage is similar to decodeArray except that it returns a pointer
  20.      * incremented from in. In theory it should be in+length. If the
  21.      * returned pointer is less than in+length, then this generally means
  22.      * that the decompression is not finished (some scheme compress
  23.      * the bulk of the data one way, and they then they compress remaining
  24.      * integers using another scheme).
  25.      *
  26.      * As with encodeArray, you need to have length element allocated
  27.      * for *in and at least nvalue elements allocated for out. The value
  28.      * of the variable nvalue gets updated with the number actually use
  29.      * (if nvalue exceeds the original value, there might be a buffer
  30.      * overrun).
  31.      */
  32.     virtual const uint32_t * decodeArray(const uint32_t *in,
  33.             const size_t length, uint32_t *out, size_t &nvalue)= 0;
  34.     virtual ~IntegerCODEC() {
  35.     }
  36.  
  37.     /**
  38.      * Will compress the content of a vector into
  39.      * another vector.
  40.      */
  41.     virtual vector<uint32_t> compress(const vector<uint32_t> & data) {
  42.         vector<uint32_t> compresseddata(data.size() * 2 + 1024);// allocate plenty of memory
  43.         size_t memavailable = compresseddata.size();
  44.         encodeArray(&data[0], data.size(), &compresseddata[0], memavailable);
  45.         compresseddata.resize(memavailable);
  46.         return compresseddata;
  47.     }
  48.  
  49.     /**
  50.      * Will uncompress the content of a vector into
  51.      * another vector. Some CODECs know exactly how much data to uncompress,
  52.      * others need to uncompress it all to know how data there is to uncompress...
  53.      * So it useful to have a hint (expected_uncompressed_size) that tells how
  54.      * much data there will be to uncompress. Otherwise, the code will
  55.      * try to guess, but the result is uncertain and inefficient. You really
  56.      * ought to keep track of how many symbols you had compressed.
  57.      */
  58.     virtual vector<uint32_t> uncompress(
  59.             const vector<uint32_t> & compresseddata,
  60.             size_t expected_uncompressed_size = 0) {
  61.         vector<uint32_t> data(expected_uncompressed_size);// allocate plenty of memory
  62.         size_t memavailable = data.size();
  63.         try {
  64.             decodeArray(&compresseddata[0], compresseddata.size(), &data[0],
  65.                     memavailable);
  66.         } catch (NotEnoughStorage & nes) {
  67.             data.resize(nes.required + 1024);
  68.             decodeArray(&compresseddata[0], compresseddata.size(), &data[0],
  69.                     memavailable);
  70.  
  71.         }
  72.         data.resize(memavailable);
  73.         return data;
  74.     }
  75.  
  76.     virtual string name() const = 0;
  77. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement