Files
cmc-pseudo-polynomials/al_function.hpp

69 lines
2.2 KiB
C++

#ifndef _AL_FUNCTION_HPP
#define _AL_FUNCTION_HPP
#include <cassert>
#include <limits>
#include <string>
template<class STORAGE, size_t VALUES_COUNT>
class Function {
public:
Function(): _function_values(0) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
}
explicit Function(STORAGE val): _function_values(val) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
}
explicit Function(std::string val): _function_values(0) {
static_assert(not std::numeric_limits<STORAGE>::is_signed);
assert(("bad input string size", val.size() == VALUES_COUNT));
STORAGE cur_dig = 1;
for (auto it = val.rbegin(); it != val.rend(); ++it) {
if ( *it == '0' ) {
// nothing to do here
} else if ( *it == '1' ) {
_function_values += cur_dig;
} else {
assert(("bad input string", false));
}
cur_dig *= 2;
}
}
std::string string() const {
std::string res(VALUES_COUNT, '0');
STORAGE tmp_val = _function_values;
for (size_t ind = 0; ind < VALUES_COUNT; ++ind) {
char cur_ch = tmp_val % 2 == 0 ? '0' : '1';
res[VALUES_COUNT - ind - 1] = cur_ch;
tmp_val /= 2;
}
return res;
}
Function operator and (Function other) const {
return Function(_function_values & other._function_values);
}
Function operator or (Function other) const {
return Function(_function_values | other._function_values);
}
Function operator xor(Function other) const {
return Function(_function_values ^ other._function_values);
}
bool operator < (Function other) const {
return _function_values < other._function_values;
}
bool operator == (Function other) const {
return _function_values == other._function_values;
}
STORAGE value() const {
return _function_values;
}
private:
STORAGE _function_values;
};
#endif // _AL_FUNCTION_HPP