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 <string>
#include <utility>
#include <iostream>
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 <<shift);
//std::cout << " -> " << (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<CellType, BASE*BASE> _results;
std::array<CellType, (BASE*BASE + 3) / 4> _results;
};