Result RSS14Reader::decodeRow(int rowNumber, const BitArray& row_, std::unique_ptr<DecodingState>& state) const { RSS14DecodingState* prevState = nullptr; if (state == nullptr) { state.reset(prevState = new RSS14DecodingState); } else { #if !defined(ZX_HAVE_CONFIG) #error "You need to include ZXConfig.h" #elif !defined(ZX_NO_RTTI) prevState = dynamic_cast<RSS14DecodingState*>(state.get()); #else prevState = static_cast<RSS14DecodingState*>(state.get()); #endif } if (prevState == nullptr) { throw std::runtime_error("Invalid state"); } BitArray row = row_.copy(); AddOrTally(prevState->possibleLeftPairs, DecodePair(row, false, rowNumber)); row.reverse(); AddOrTally(prevState->possibleRightPairs, DecodePair(row, true, rowNumber)); // row.reverse(); for (const auto& left : prevState->possibleLeftPairs) { if (left.count() > 1) { for (const auto& right : prevState->possibleRightPairs) { if (right.count() > 1) { if (CheckChecksum(left, right)) { return ConstructResult(left, right); } } } } } return Result(DecodeStatus::NotFound); }
// test bit array int testBitArray() { BitArray bit_array; bool r; unsigned int count; unsigned int index, value; unsigned int mem_used; info("- Testing BitArray -----"); // first set a value in the empty array r = bit_array.set(1); if (!testPrint(r, " * Putting a non-existent value (1) into the array", true)) return -1; // put the same value again (it should exist) r = bit_array.set(1); if (!testPrint(r, " * Putting the value that already exist in the array", false)) return -1; // test the value r = bit_array.is_set(1); if (!testPrint(r, " * Testing if the value exist in the array", true)) return -1; // set next value r = bit_array.set(3); if (!testPrint(r, " * Putting another value (3) into the array", true)) return -1; // test the count of items in the array count = bit_array.count(); if (!testPrint(count == 2, " * Number of values in the array", true)) return -1; // n-th index value = bit_array.by_count(2); if (!testPrint(value == 3, " * The value at position 2 should be 3", true)) return -1; // put one more value into the array r = bit_array.set(2); if (!testPrint(r, " * Putting one more value (2) into the array", true)) return -1; // ITERATION // test the first index index = bit_array.first(); if (!testPrint(index == 1, " * The first index in the array should be one (1)", true)) return -1; index = bit_array.next(index); if (!testPrint(index == 2, " * The next index in the array should be two (2)", true)) return -1; index = bit_array.last(); if (!testPrint(index == 3, " * The last index in the array should be two (3)", true)) return -1; index = bit_array.prev(index); if (!testPrint(index == 2, " * The previous index in the array should be two (2)", true)) return -1; // COPY BitArray dup; r = dup.copy(&bit_array); if (!testPrint(r, " * Making copy of the array", true)) return -1; // test copied values value = bit_array.by_count(1); if (!testPrint(value == 1, " - The value at position 1 should be 1", true)) return -1; value = bit_array.by_count(2); if (!testPrint(value == 2, " - The value at position 2 should be 3", true)) return -1; value = bit_array.by_count(3); if (!testPrint(value == 3, " - The value at position 3 should be 3", true)) return -1; // MEMORY // mem used mem_used = bit_array.mem_used(); if (!testPrint(mem_used > 2, " * Used memory should be nonzero", true)) return -1; // free memory bit_array.free(); testPrint(true, " * Freeing memory", true); mem_used = bit_array.mem_used(); if (!testPrint(mem_used == 0, " * Used memory should be zero", true)) return -1; return 0; }