diff --git a/finite_function.hpp b/finite_function.hpp index 1a76d9b..d78d1c0 100644 --- a/finite_function.hpp +++ b/finite_function.hpp @@ -1,7 +1,7 @@ #include #include #include - +#include typedef uint8_t CellType; // Пока положим, что только два аргумента, чтобы упростить реализацию @@ -27,7 +27,11 @@ class FiniteFunction { for (auto ch: text_repr) { if ( !isdigit(ch) ) continue; - _results.at(cur_ind) = std::stoi(std::string(1, ch)); + set_result( + cur_ind / BASE, + cur_ind % BASE, + std::stoi(std::string(1, ch)) + ); ++cur_ind; } update_num(); @@ -36,11 +40,21 @@ class FiniteFunction { // Устанавливает результат функции по двум аргументам void set_result(CellType first, CellType second, CellType result) { - _results[get_index(first, second)] = result; + //std::cout << "setting " << (int)first << "," << (int)second << " to " << (int)result << std::endl; + + uint8_t byte_num = get_index(first, second) / 4; + uint8_t shift = 2*(get_index(first, second) % 4); + //std::cout << "before: " << (int)_results[byte_num]; + _results[byte_num] = (_results[byte_num] ^ (((_results[byte_num] >> shift) + & 0x03) << shift)) | (result < " << (int)_results[byte_num] << std::endl; } int operator() (CellType first, CellType second) const { - return _results[get_index(first, second)]; + uint8_t byte_num = get_index(first, second) / 4; + uint8_t shift = 2*(get_index(first, second) % 4); + // возьмём последние два бита + return (_results[byte_num] >> shift) & 0x03; } bool operator < (const FiniteFunction &f) const { @@ -142,12 +156,12 @@ class FiniteFunction { void update_num() { _num = 0; for (auto&& val: _results) { - _num *= BASE; + _num *= 256; _num += val; } } uint32_t _num; - std::array _results; + std::array _results; };