public void Generate(UInt32[] valsTable, UInt32 n) { UInt32 i; // lengths: represents lengths of encoded values var lengths = new UInt32[n]; this.n = n; totalLength = 0; for (i = 0; i < this.n; i++) { if (valsTable[i] == 0) { lengths[i] = 0; } else { lengths[i] = ILog2(valsTable[i] + 1); totalLength += lengths[i]; } } storeTable = new UInt32[(totalLength + 31) >> 5]; totalLength = 0; for (i = 0; i < this.n; i++) { if (valsTable[i] == 0) { continue; } var storedValue = valsTable[i] - ((1U << (Int32)lengths[i]) - 1U); BitBool.SetBitsAtPos(storeTable, totalLength, storedValue, lengths[i]); totalLength += lengths[i]; } remR = ILog2(totalLength / this.n); if (remR == 0) { remR = 1; } lengthRems = new UInt32[((this.n * remR) + 0x1f) >> 5]; var remsMask = (1U << (Int32)remR) - 1U; totalLength = 0; for (i = 0; i < this.n; i++) { totalLength += lengths[i]; BitBool.SetBitsValue(lengthRems, i, totalLength & remsMask, remR, remsMask); lengths[i] = totalLength >> (Int32)remR; } sel = new Select(); sel.Generate(lengths, this.n, (totalLength >> (Int32)remR)); }
public UInt32 Query(UInt32 idx) { UInt32 selRes; UInt32 encIdx; var remsMask = (UInt32)((1 << (Int32)remR) - 1); if (idx == 0) { encIdx = 0; selRes = sel.Query(idx); } else { selRes = sel.Query(idx - 1); encIdx = (selRes - (idx - 1)) << (Int32)remR; encIdx += BitBool.GetBitsValue(lengthRems, idx - 1, remR, remsMask); selRes = sel.NextQuery(selRes); } var encLength = (selRes - idx) << (Int32)remR; encLength += BitBool.GetBitsValue(lengthRems, idx, remR, remsMask); encLength -= encIdx; if (encLength == 0) { return(0); } return(BitBool.GetBitsAtPos(storeTable, encIdx, encLength) + ((UInt32)((1 << (Int32)encLength) - 1))); }