Initial version
This commit is contained in:
62
al_function.hpp
Normal file
62
al_function.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef _AL_FUNCTION_HPP
|
||||
#define _AL_FUNCTION_HPP
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include <string>
|
||||
|
||||
template<class STORAGE, size_t VALUES_COUNT>
|
||||
class Function {
|
||||
public:
|
||||
Function(): _function_values(0) {}
|
||||
explicit Function(STORAGE val): _function_values(val) {}
|
||||
explicit Function(std::string val): _function_values(0) {
|
||||
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
|
||||
Reference in New Issue
Block a user