(gamma) Minor clean-up

This commit is contained in:
Viktor Lofgren 2024-06-24 13:56:43 +02:00
parent b798f28443
commit 40bca93884
2 changed files with 13 additions and 4 deletions

View File

@ -17,8 +17,8 @@ import java.util.StringJoiner;
public class GammaCodedSequence implements BinarySerializable, Iterable<Integer> { public class GammaCodedSequence implements BinarySerializable, Iterable<Integer> {
private final ByteBuffer raw; private final ByteBuffer raw;
int startPos = 0; private final int startPos;
int startLimit = 0; private final int startLimit;
/** Create a new GammaCodedSequence from a sequence of integers. /** Create a new GammaCodedSequence from a sequence of integers.
* *
@ -116,6 +116,9 @@ public class GammaCodedSequence implements BinarySerializable, Iterable<Integer>
return sj.toString(); return sj.toString();
} }
/** Return the backing ByteBuffer of the sequence, configured with a position and limit
* that is equal to the relevant data range
*/
public ByteBuffer buffer() { public ByteBuffer buffer() {
raw.position(startPos); raw.position(startPos);
raw.limit(startLimit); raw.limit(startLimit);
@ -123,11 +126,17 @@ public class GammaCodedSequence implements BinarySerializable, Iterable<Integer>
return raw; return raw;
} }
/** Return the number of bytes used by the sequence in the buffer */
public int bufferSize() { public int bufferSize() {
return raw.capacity(); return startLimit - startPos;
} }
/** Return the number of items in the sequence */
public int valueCount() { public int valueCount() {
// if the first byte is zero, the sequence is empty and we can skip decoding
if (0 == raw.get(startPos))
return 0;
return EliasGammaCodec.readCount(buffer()); return EliasGammaCodec.readCount(buffer());
} }
} }

View File

@ -88,7 +88,7 @@ public class BitWriter {
var outBuffer = ByteBuffer.allocate(totalMeaningfulBytes); var outBuffer = ByteBuffer.allocate(totalMeaningfulBytes);
outBuffer.put(underlying.array(), 0, totalMeaningfulBytes); outBuffer.put(0, underlying, 0, totalMeaningfulBytes);
outBuffer.position(0); outBuffer.position(0);
outBuffer.limit(totalMeaningfulBytes); outBuffer.limit(totalMeaningfulBytes);