added bytes consumed result to decode, and exit early after bytes consumed

This commit is contained in:
ZappoMan 2015-07-08 18:19:12 -07:00
parent c0cdf3256c
commit 7790e35d24
3 changed files with 17 additions and 9 deletions

View file

@ -1014,7 +1014,7 @@ void Application::paintGL() {
void Application::runTests() { void Application::runTests() {
runTimingTests(); runTimingTests();
//runUnitTests(); runUnitTests();
} }
void Application::audioMuteToggled() { void Application::audioMuteToggled() {

View file

@ -232,7 +232,7 @@ void runTimingTests() {
(double)(elapsedUsecs / numTests), (double)result); (double)(elapsedUsecs / numTests), (double)result);
quint64 BYTE_CODE_MAX_VALUE = 99999999; quint64 BYTE_CODE_MAX_TEST_VALUE = 99999999;
quint64 BYTE_CODE_TESTS_SKIP = 999; quint64 BYTE_CODE_TESTS_SKIP = 999;
QByteArray extraJunk; QByteArray extraJunk;
@ -246,7 +246,7 @@ void runTimingTests() {
startTime.start(); startTime.start();
quint64 tests = 0; quint64 tests = 0;
quint64 failed = 0; quint64 failed = 0;
for (quint64 value = 0; value < BYTE_CODE_MAX_VALUE; value += BYTE_CODE_TESTS_SKIP) { for (quint64 value = 0; value < BYTE_CODE_MAX_TEST_VALUE; value += BYTE_CODE_TESTS_SKIP) {
quint64 valueA = value; // usecTimestampNow(); quint64 valueA = value; // usecTimestampNow();
ByteCountCoded<quint64> codedValueA = valueA; ByteCountCoded<quint64> codedValueA = valueA;
QByteArray codedValueABuffer = codedValueA; QByteArray codedValueABuffer = codedValueA;
@ -328,9 +328,11 @@ void runUnitTests() {
qDebug() << "codedValueBuffer:"; qDebug() << "codedValueBuffer:";
outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size()); outputBufferBits((const unsigned char*)codedValueBuffer.constData(), codedValueBuffer.size());
ByteCountCoded<quint64> valueDecoder = codedValueBuffer; ByteCountCoded<quint64> valueDecoder;
size_t bytesConsumed = valueDecoder.decode(codedValueBuffer);
quint64 valueDecoded = valueDecoder; quint64 valueDecoded = valueDecoder;
qDebug() << "valueDecoded:" << valueDecoded; qDebug() << "valueDecoded:" << valueDecoded;
qDebug() << "bytesConsumed:" << bytesConsumed;
if (value == valueDecoded) { if (value == valueDecoded) {

View file

@ -41,8 +41,8 @@ public:
ByteCountCoded(const QByteArray& fromEncoded) : data(0) { decode(fromEncoded); } ByteCountCoded(const QByteArray& fromEncoded) : data(0) { decode(fromEncoded); }
QByteArray encode() const; QByteArray encode() const;
void decode(const QByteArray& fromEncoded); size_t decode(const QByteArray& fromEncoded);
void decode(const char* encodedBuffer, int encodedSize); size_t decode(const char* encodedBuffer, int encodedSize);
bool operator==(const ByteCountCoded& other) const { return data == other.data; } bool operator==(const ByteCountCoded& other) const { return data == other.data; }
bool operator!=(const ByteCountCoded& other) const { return data != other.data; } bool operator!=(const ByteCountCoded& other) const { return data != other.data; }
@ -115,12 +115,13 @@ template<typename T> inline QByteArray ByteCountCoded<T>::encode() const {
return output; return output;
} }
template<typename T> inline void ByteCountCoded<T>::decode(const QByteArray& fromEncodedBytes) { template<typename T> inline size_t ByteCountCoded<T>::decode(const QByteArray& fromEncodedBytes) {
decode(fromEncodedBytes.constData(), fromEncodedBytes.size()); return decode(fromEncodedBytes.constData(), fromEncodedBytes.size());
} }
template<typename T> inline void ByteCountCoded<T>::decode(const char* encodedBuffer, int encodedSize) { template<typename T> inline size_t ByteCountCoded<T>::decode(const char* encodedBuffer, int encodedSize) {
data = 0; // reset data data = 0; // reset data
size_t bytesConsumed = 0;
int bitCount = BITS_IN_BYTE * encodedSize; int bitCount = BITS_IN_BYTE * encodedSize;
int encodedByteCount = 1; // there is at least 1 byte (after the leadBits) int encodedByteCount = 1; // there is at least 1 byte (after the leadBits)
@ -133,6 +134,7 @@ template<typename T> inline void ByteCountCoded<T>::decode(const char* encodedBu
for(int byte = 0; byte < encodedSize; byte++) { for(int byte = 0; byte < encodedSize; byte++) {
char originalByte = encodedBuffer[byte]; char originalByte = encodedBuffer[byte];
bytesConsumed++;
unsigned char maskBit = 128; // LEFT MOST BIT set unsigned char maskBit = 128; // LEFT MOST BIT set
for(int bit = 0; bit < BITS_IN_BYTE; bit++) { for(int bit = 0; bit < BITS_IN_BYTE; bit++) {
bool bitIsSet = originalByte & maskBit; bool bitIsSet = originalByte & maskBit;
@ -165,7 +167,11 @@ template<typename T> inline void ByteCountCoded<T>::decode(const char* encodedBu
bitAt++; bitAt++;
maskBit = maskBit >> 1; maskBit = maskBit >> 1;
} }
if (!inLeadBits && bitAt > lastValueBit) {
break;
} }
}
return bytesConsumed;
} }
#endif // hifi_ByteCountCoding_h #endif // hifi_ByteCountCoding_h