Bitset for FiniteFunction
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user