Пример #1
0
static RSS::FinderPattern
ParseFoundFinderPattern(const BitArray& row, int rowNumber, bool right, BitArray::Range range, FinderCounters& finderCounters)
{
	if (!range || range.begin == row.begin())
		return {};

	// Actually we found elements 2-5 -> Locate element 1
	auto i = std::find(BitArray::ReverseIterator(range.begin), row.rend(), *range.begin);
	int firstCounter = range.begin - i.base();
	range.begin = i.base();

	// Make 'counters' hold 1-4
	std::copy_backward(finderCounters.begin(), finderCounters.end() - 1, finderCounters.end());
	finderCounters[0] = firstCounter;
	int value = RSS::ReaderHelper::ParseFinderValue(finderCounters, FINDER_PATTERNS);
	if (value < 0)
		return {};

	int start = range.begin - row.begin();
	int end = range.end - row.begin();
	if (right) {
		// row is actually reversed
		start = row.size() - 1 - start;
		end = row.size() - 1 - end;
	}

	return {value,
			range.begin - row.begin(),
			range.end - row.begin(),
			{ResultPoint(start, rowNumber), ResultPoint(end, rowNumber)}};
}
static std::string
DecodeAI01393x(const BitArray& bits)
{
	static const int HEADER_SIZE = 5 + 1 + 2;
	static const int LAST_DIGIT_SIZE = 2;
	static const int FIRST_THREE_DIGITS_SIZE = 10;

	if (bits.size() < HEADER_SIZE + AI01_GTIN_SIZE) {
		return std::string();
	}

	std::string buffer;
	AI01EncodeCompressedGtin(buffer, bits, HEADER_SIZE);

	int lastAIdigit = GenericAppIdDecoder::ExtractNumeric(bits, HEADER_SIZE + AI01_GTIN_SIZE, LAST_DIGIT_SIZE);

	buffer.append("(393");
	buffer.append(std::to_string(lastAIdigit));
	buffer.push_back(')');

	int firstThreeDigits = GenericAppIdDecoder::ExtractNumeric(bits, HEADER_SIZE + AI01_GTIN_SIZE + LAST_DIGIT_SIZE, FIRST_THREE_DIGITS_SIZE);
	if (firstThreeDigits / 100 == 0) {
		buffer.push_back('0');
	}
	if (firstThreeDigits / 10 == 0) {
		buffer.push_back('0');
	}
	buffer.append(std::to_string(firstThreeDigits));

	if (StatusIsOK(GenericAppIdDecoder::DecodeGeneralPurposeField(bits, HEADER_SIZE + AI01_GTIN_SIZE + LAST_DIGIT_SIZE + FIRST_THREE_DIGITS_SIZE, buffer))) {
		return buffer;
	}
	return std::string();
}
static std::string
DecodeAI013x0x1x(const BitArray& bits, const char* firstAIdigits, const char* dateCode)
{
	static const int HEADER_SIZE = 7 + 1;
	static const int WEIGHT_SIZE = 20;
	static const int DATE_SIZE = 16;

	if (bits.size() != HEADER_SIZE + AI01_GTIN_SIZE + WEIGHT_SIZE + DATE_SIZE) {
		return std::string();
	}

	std::string buffer;
	AI01EncodeCompressedGtin(buffer, bits, HEADER_SIZE);
	AI01EncodeCompressedWeight(buffer, bits, HEADER_SIZE + AI01_GTIN_SIZE, WEIGHT_SIZE,
		// addWeightCode
		[firstAIdigits](std::string& buf, int weight) {
			buf.push_back('(');
			buf.append(firstAIdigits);
			buf.append(std::to_string(weight / 100000));
			buf.push_back(')');
		},
		// checkWeight
		[](int weight) {
			return weight % 100000;
		});

	// encode compressed date
	int numericDate = GenericAppIdDecoder::ExtractNumeric(bits, HEADER_SIZE + AI01_GTIN_SIZE + WEIGHT_SIZE, DATE_SIZE);
	if (numericDate != 38400) {
		buffer.push_back('(');
		buffer.append(dateCode);
		buffer.push_back(')');

		int day = numericDate % 32;
		numericDate /= 32;
		int month = numericDate % 12 + 1;
		numericDate /= 12;
		int year = numericDate;

		if (year / 10 == 0) {
			buffer.push_back('0');
		}
		buffer.append(std::to_string(year));
		if (month / 10 == 0) {
			buffer.push_back('0');
		}
		buffer.append(std::to_string(month));
		if (day / 10 == 0) {
			buffer.push_back('0');
		}
		buffer.append(std::to_string(day));
	}

	return buffer;
}
static std::string
DecodeAI01320x(const BitArray& bits)
{
	static const int HEADER_SIZE = 4 + 1;
	static const int WEIGHT_SIZE = 15;

	if (bits.size() != HEADER_SIZE + AI01_GTIN_SIZE + WEIGHT_SIZE) {
		return std::string();
	}

	std::string buffer;
	AI01EncodeCompressedGtin(buffer, bits, HEADER_SIZE);
	AI01EncodeCompressedWeight(buffer, bits, HEADER_SIZE + AI01_GTIN_SIZE, WEIGHT_SIZE,
		// addWeightCode
		[](std::string& buf, int weight) { buf.append(weight < 10000 ? "(3202)" : "(3203)"); },
		// checkWeight
		[](int weight) { return weight < 10000 ? weight : weight - 10000; });

	return buffer;
}
Пример #5
0
BitArray GammaEncoder::encode(unsigned int num) const{
    BitArray numBinaryRemaining = getBinaryWithoutHighestOne(num);
    return unary.encode(numBinaryRemaining.size() + 1) + numBinaryRemaining;
}
Пример #6
0
// Test program
int main() {
    // Test exceptions
    BitArray<> b;
    throw_(b[0],logic_error);
    throw_(b.toggle(0),logic_error);
    const BitArray<> b1{b}; // Test copy constructor
    throw_(b1[0],logic_error);

    // Test empty Bitarray properties
    test_(b.size() == 0);
    test_(b.count() == 0);
    test_(b.capacity() == 0);
    test_(!b.any());

    // Validate construction and to_string()
    BitArray<> b2{5};
    test_(b2.size() == 5);
    for (size_t i = 0; i < 5; ++i) {
        test_(!b2[i]);
    }
    test_(b2.to_string() == "00000");

    // Test copy, assign, equality, and from_string
    BitArray<> b3{b2};
    test_(b2 == b3);
    test_(b != b2);
    test_(!b3[2]);
    b3[2] = 1;
    test_(b3[2]);
    test_(b2 != b3);
    test_(b2.to_string() == "00000");
    b = b2;
    test_(b.to_string() == "00000");

    // Test move operations
    BitArray<> b4{move(b3)};
    test_(b4[2]);
    BitArray<> b4b;
    b4b = move(b4);
    test_(b4b[2]);

    // Test bit ops
    BitArray<> x{"011010110"}; // Also tests string constructor
    test_(x.count() == 5);
    test_(x.any());
    test_((x << 6).to_string() == "110000000");
    test_((x >> 6).to_string() == "000000011");
    test_((x <<= 3).to_string() == "010110000");
    test_((x >>= 3).to_string() == "000010110");
    BitArray<> y{~x};
    nothrow_(x.toggle());
    test_(x == y);
    test_(x.to_string() == "111101001");

    b = BitArray<> {};
    test_(!b.any());
    b += 1;
    b += 0;
    b += 1;
    test_(b.to_string() == "101");
    test_(b.any());

    b2 = BitArray<> {"10101011"};
    test_(b2.count() == 5);
    b2.toggle();
    test_(b2.to_string() == "01010100");
    b2.erase(3);
    test_(b2.to_string() == "0100100");
    b2.erase(b2.size() - 1);
    test_(b2.to_string() == "010010");
    b2.erase(1,4);
    test_(b2.to_string() == "00");
    b2.insert(1,1);
    test_(b2.to_string() == "010");
    b2.insert(1,0);
    test_(b2.to_string() == "0010");
    b2 += b;
    test_(b2.to_string() == "0010101");
    b2.insert(3, b);
    test_(b2.to_string() == "0011010101");

    ostringstream os;
    os << "101" << 'a' << "0101";
    istringstream is{os.str()};
    b3 = BitArray<> {};
    is >> b3;
    test_(b3.to_string() == "101");
    is.get();
    is >> b3;
    test_(b3.to_string() == "0101");
    os.str("");
    os << 'a';
    istringstream is2{os.str()};
    is2 >> b3;
    test_(!is2);
    test_(b3.to_string() == "0101");

    BitArray<> b5{"11111111111111111111111111000000000000000000000000000011"};
    test_(b5.slice(23,10) == BitArray<>("1110000000"));
    size_t n = b2.size();
    b2.insert(3,b5);
    test_(n + b5.size() == b2.size());
    b2.erase(3, b5.size());
    b2.shrink_to_fit();

    // Test comparisons
    BitArray<> b6{"10101"};
    BitArray<> b7{"101010"};
    BitArray<> b8{b7};
    test_(b6 < b7);
    test_(b6 <= b7);
    test_(b6 <= b6);
    test_(b7 > b6);
    test_(b7 >= b6);
    test_(b7 >= b7);
    test_(BitArray<>("111") > BitArray<>("10111"));

    BitArray<> b9{"11111111111111111111111111000000000000000000000000000011"};
    ostringstream ostr;
    ostr << b9;
    test_(ostr.str() == "11111111111111111111111111000000000000000000000000000011");
    test_(b9.count() == 28);
    nothrow_(b9 <<= 2);
    test_(b9.count() == 26);
    nothrow_(b9 >>=33);
    test_(b9.count() == 23);
    BitArray<> b10{"01"};
    b9[0] = b10[0] = b10[1];
    test_(b10[0]);
    test_(b9[0]);
    const BitArray<> b11{b10};
    test_(b11[0]);

    BitArray<> b12("11011111101");
    b12.erase(1,8);
    test_(b12.to_string() == "101");
    b12 += b12;
    test_(b12.to_string() == "101101");

    BitArray<> b13("");
    test_(b13.size() == 0);

    report_();
}