This commit is contained in:
Sean Owen 2021-09-01 12:48:20 -05:00
parent f6087eea12
commit 490769e75f

View file

@ -42,12 +42,14 @@ final class State {
private final int binaryShiftByteCount; private final int binaryShiftByteCount;
// The total number of bits generated (including Binary Shift). // The total number of bits generated (including Binary Shift).
private final int bitCount; private final int bitCount;
private final int binaryShiftCost;
private State(Token token, int mode, int binaryBytes, int bitCount) { private State(Token token, int mode, int binaryBytes, int bitCount) {
this.token = token; this.token = token;
this.mode = mode; this.mode = mode;
this.binaryShiftByteCount = binaryBytes; this.binaryShiftByteCount = binaryBytes;
this.bitCount = bitCount; this.bitCount = bitCount;
this.binaryShiftCost = calculateBinaryShiftCost(binaryBytes);
} }
int getMode() { int getMode() {
@ -155,7 +157,7 @@ final class State {
int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mode][other.mode] >> 16); int newModeBitCount = this.bitCount + (HighLevelEncoder.LATCH_TABLE[this.mode][other.mode] >> 16);
if (this.binaryShiftByteCount < other.binaryShiftByteCount) { if (this.binaryShiftByteCount < other.binaryShiftByteCount) {
// add additional B/S encoding cost of other, if any // add additional B/S encoding cost of other, if any
newModeBitCount += calculateBinaryShiftCost(other) - calculateBinaryShiftCost(this); newModeBitCount += other.binaryShiftCost - this.binaryShiftCost;
} else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.binaryShiftByteCount > 0) { } else if (this.binaryShiftByteCount > other.binaryShiftByteCount && other.binaryShiftByteCount > 0) {
// maximum possible additional cost (we end up exceeding the 31 byte boundary and other state can stay beneath it) // maximum possible additional cost (we end up exceeding the 31 byte boundary and other state can stay beneath it)
newModeBitCount += 10; newModeBitCount += 10;
@ -184,14 +186,14 @@ final class State {
return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode], bitCount, binaryShiftByteCount); return String.format("%s bits=%d bytes=%d", HighLevelEncoder.MODE_NAMES[mode], bitCount, binaryShiftByteCount);
} }
private static int calculateBinaryShiftCost(State state) { private static int calculateBinaryShiftCost(int binaryShiftByteCount) {
if (state.binaryShiftByteCount > 62) { if (binaryShiftByteCount > 62) {
return 21; // B/S with extended length return 21; // B/S with extended length
} }
if (state.binaryShiftByteCount > 31) { if (binaryShiftByteCount > 31) {
return 20; // two B/S return 20; // two B/S
} }
if (state.binaryShiftByteCount > 0) { if (binaryShiftByteCount > 0) {
return 10; // one B/S return 10; // one B/S
} }
return 0; return 0;