Bitset for FiniteFunction

This commit is contained in:
2018-04-26 02:34:30 +03:00
parent 98bbd51a87
commit 35712e46d1

View File

@@ -1,7 +1,7 @@
#include <array> #include <array>
#include <string> #include <string>
#include <utility> #include <utility>
#include <iostream>
typedef uint8_t CellType; typedef uint8_t CellType;
// Пока положим, что только два аргумента, чтобы упростить реализацию // Пока положим, что только два аргумента, чтобы упростить реализацию
@@ -27,7 +27,11 @@ class FiniteFunction {
for (auto ch: text_repr) { for (auto ch: text_repr) {
if ( !isdigit(ch) ) if ( !isdigit(ch) )
continue; 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; ++cur_ind;
} }
update_num(); update_num();
@@ -36,11 +40,21 @@ class FiniteFunction {
// Устанавливает результат функции по двум аргументам // Устанавливает результат функции по двум аргументам
void set_result(CellType first, CellType second, CellType result) { 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 <<shift);
//std::cout << " -> " << (int)_results[byte_num] << std::endl;
} }
int operator() (CellType first, CellType second) const { 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 { bool operator < (const FiniteFunction &f) const {
@@ -142,12 +156,12 @@ class FiniteFunction {
void update_num() { void update_num() {
_num = 0; _num = 0;
for (auto&& val: _results) { for (auto&& val: _results) {
_num *= BASE; _num *= 256;
_num += val; _num += val;
} }
} }
uint32_t _num; uint32_t _num;
std::array<CellType, BASE*BASE> _results; std::array<CellType, (BASE*BASE + 3) / 4> _results;
}; };