/* Decompresses sparse counters */
static HLLCounter 
hll_decompress_dense_V1(HLLCounter hloglog)
{
    char * dest;
    int m,i;
    HLLCounter htemp;
    /* reset b to positive value for calcs and to indicate data is
     * decompressed */
    hloglog->b = -1 * (hloglog->b);

    /* allocate and zero an array large enough to hold all the decompressed
     * bins */
    m = (int) pow(2,hloglog->b);
    dest = malloc(m);
    memset(dest,0,m);

    /* decompress the data */
    pglz_decompress((PGLZ_Header *)hloglog->data,dest);

    /* copy the struct internals but not the data into a counter with enough
     * space for the uncompressed data  */
    htemp = palloc(sizeof(HLLData) + (int)ceil((m * hloglog->binbits / 8.0)));
    memcpy(htemp,hloglog,sizeof(HLLData));
    hloglog = htemp;

    /* set the registers to the appropriate value based on the decompressed
     * data */
    for (i=0; i<m; i++){
        HLL_DENSE_SET_REGISTER(hloglog->data,i,dest[i],hloglog->binbits);
    }

    /* set the varsize to the appropriate length  */
    SET_VARSIZE(hloglog,sizeof(HLLData) + (int)ceil((m * hloglog->binbits / 8.0)) );


    /* free allocated memory */
    if (dest){
        free(dest);
    }

    return hloglog;
}
static int hll_merge(lua_State *lua)
{
  hyperloglog *dest = check_hll(lua, 2);
  hyperloglog *src = luaL_checkudata(lua, 2, mozsvc_hyperloglog);
  if (dest == src) {
    return 1;
  }

  int i;
  uint8_t sval, dval;
  for (i = 0; i < HLL_REGISTERS; i++) {
    HLL_DENSE_GET_REGISTER(dval, dest->registers, i);
    HLL_DENSE_GET_REGISTER(sval, src->registers, i);
    if (sval > dval) {
      HLL_DENSE_SET_REGISTER(dest->registers, i, sval);
    }
  }
  HLL_INVALIDATE_CACHE(dest);
  lua_pushvalue(lua, 1);
  return 1;
}