Skip to content

Commit

Permalink
fix various small compression related bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
douira committed Dec 11, 2023
1 parent f446ea1 commit 8268ef6
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,28 @@ static int[] compressIndexes(IntArrayList indexes) {
compressed[1] = minDelta;

// write the deltas
final int positionLimit = Integer.SIZE - width;
int outputIndex = HEADER_LENGTH;
int gatherInt = 0;
int bitPosition = 0;
for (int i = 1; i < workingList.size(); i++) {
int shiftedDelta = workingList.getInt(i) - minDelta;
gatherInt |= shiftedDelta << bitPosition;
bitPosition += width;
if (bitPosition >= Integer.SIZE) {
if (bitPosition > positionLimit) {
compressed[outputIndex++] = gatherInt;
gatherInt = 0;
bitPosition = 0;
}
}

// System.out.println("Compressed " + indexes.size() + " indexes to " + size + "
// ints, compression ratio "
// + (indexes.size() / size));
// flush the last int if it hasn't been written yet
if (bitPosition > 0) {
compressed[outputIndex++] = gatherInt;
}

System.out.println("Compressed " + indexes.size() + " indexes to " + size + " ints, compression ratio "
+ (indexes.size() / size));
return compressed;
}

Expand Down Expand Up @@ -218,6 +223,7 @@ static int decompressWithOffset(int[] indexes, int fixedIndexOffset, IntConsumer
int mask = (1 << width) - 1;

// write value (optionally map), read deltas, apply base delta and loop
final int positionLimit = Integer.SIZE - width;
int readIndex = HEADER_LENGTH;
int splitInt = indexes[readIndex++];
int splitIntBitPosition = 0;
Expand All @@ -231,7 +237,7 @@ static int decompressWithOffset(int[] indexes, int fixedIndexOffset, IntConsumer

int delta = (splitInt >> splitIntBitPosition) & mask;
splitIntBitPosition += width;
if (splitIntBitPosition >= Integer.SIZE && valueCount > 1) {
if (splitIntBitPosition > positionLimit && valueCount > 1) {
splitInt = indexes[readIndex++];
splitIntBitPosition = 0;
}
Expand Down

0 comments on commit 8268ef6

Please sign in to comment.