-
Notifications
You must be signed in to change notification settings - Fork 0
/
BFMachine.h
69 lines (53 loc) · 1.59 KB
/
BFMachine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* 2020 by Stephan Roslen */
#ifndef BFTMP__BFMACHINE_H_
#define BFTMP__BFMACHINE_H_
#include <array>
#include <cstdint>
#include <cstdio>
#include <type_traits>
#include <utility>
namespace bf {
class Machine {
public:
using ElemType = unsigned char;
using SignedElemType = std::make_signed_t<ElemType>;
static_assert(std::is_unsigned_v<ElemType>);
using IdxType = uint16_t;
using SignedIdxType = std::make_signed_t<IdxType>;
static_assert(std::is_unsigned_v<IdxType>);
private:
template<typename T, size_t n, size_t... idx>
static constexpr std::array<T, n> makeFilledArrayHelper(T v, std::index_sequence<idx...>) {
return std::array<T, n>{(static_cast<void>(idx), v)...};
}
template<typename T, size_t n>
static constexpr std::array<T, n> makeFilledArray(T v) {
return makeFilledArrayHelper<T, n>(v, std::make_index_sequence<n>{});
}
constexpr static size_t kCells{30000};
std::array<ElemType, kCells> mData{makeFilledArray<ElemType, kCells>(0)};
uint16_t mRef{0}; // index of current element in mData
ElemType mValue{0};// cached value of current element in mData - synced by left/right calls
public:
void add(SignedElemType n) {
mValue += static_cast<ElemType>(n);
}
void read() {
mValue = std::getchar();
}
void write() const {
std::putchar(mValue);
}
void move(SignedIdxType n) {
mData[mRef] = mValue;
mRef += static_cast<IdxType>(n);
mValue = mData[mRef];
};
template<typename Morphism>
void loop(Morphism &&morphism) const {
while (0 != mValue)
morphism();
}
};
}// namespace bf
#endif// BFTMP__BFMACHINE_H_