-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLifeHistoryState.hpp
209 lines (176 loc) · 4.66 KB
/
LifeHistoryState.hpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include "LifeAPI.h"
#include "Parsing.hpp"
// This uses lifelib "layers" which do not match Golly's state names,
// so the parsing has to adjust for this.
struct LifeHistoryState {
LifeState state;
LifeState history;
LifeState marked;
LifeState original;
LifeHistoryState() = default;
LifeHistoryState(const LifeState &state, const LifeState &history,
const LifeState &marked, const LifeState &original)
: state{state}, history{history}, marked{marked}, original{original} {};
LifeHistoryState(const LifeState &state, const LifeState &history, const LifeState &marked)
: state{state}, history{history}, marked{marked}, original{LifeState()} {};
LifeHistoryState(const LifeState &state, const LifeState &history)
: state{state}, history{history}, marked{LifeState()},
original{LifeState()} {};
std::string RLE() const;
std::string RLEWHeader() const {
return "x = 0, y = 0, rule = LifeHistory\n" + RLE();
}
friend std::ostream& operator<<(std::ostream& os, LifeHistoryState const& self) {
return os << self.RLEWHeader();
}
static LifeHistoryState Parse(const std::string &s);
static LifeHistoryState ParseWHeader(const std::string &s);
static LifeHistoryState ParseBellman(const std::string &s);
static LifeHistoryState ParseBellmanWHeader(const std::string &s);
void Move(int x, int y) {
state.Move(x, y);
history.Move(x, y);
marked.Move(x, y);
original.Move(x, y);
}
void Move(std::pair<int, int> vec) {
Move(vec.first, vec.second);
}
};
char LifeHistoryChar(unsigned mask) {
switch (mask) {
case 0b0000: return '.';
case 0b0001: return 'A';
case 0b0010: return 'B';
case 0b0101: return 'C';
case 0b0100: return 'D';
case 0b1001: return 'E';
default: return 'F';
}
}
std::string LifeHistoryState::RLE() const {
return GenericRLE([&](int x, int y) -> char {
bool statecell = state.Get(x, y) == 1;
bool historycell = history.Get(x, y) == 1;
bool markedcell = marked.Get(x, y) == 1;
bool originalcell = original.Get(x, y) == 1;
unsigned val = statecell + (historycell << 1) + (markedcell << 2) + (originalcell << 3);
return LifeHistoryChar(val);
});
}
LifeHistoryState LifeHistoryState::Parse(const std::string &rle) {
LifeHistoryState result;
int cnt;
int x, y;
x = 0;
y = 0;
cnt = 0;
for (char const ch : rle) {
if (ch >= '0' && ch <= '9') {
cnt *= 10;
cnt += (ch - '0');
} else if (ch == '$') {
if (cnt == 0)
cnt = 1;
if (cnt == 129)
// TODO: error
return result;
y += cnt;
x = 0;
cnt = 0;
} else if (ch == '!') {
break;
} else if (ch == '\r' || ch == '\n' || ch == ' ') {
continue;
} else {
if (cnt == 0)
cnt = 1;
for (int j = 0; j < cnt; j++) {
switch(ch) {
case 'A':
result.state.Set(x, y);
break;
case 'B':
result.history.Set(x, y);
break;
case 'C':
result.state.Set(x, y);
result.marked.Set(x, y);
break;
case 'D':
result.marked.Set(x, y);
break;
case 'E':
result.state.Set(x, y);
result.original.Set(x, y);
break;
}
x++;
}
cnt = 0;
}
}
return result;
}
LifeHistoryState LifeHistoryState::ParseWHeader(const std::string &s) {
std::string rle;
std::istringstream iss(s);
for (std::string line; std::getline(iss, line); ) {
if(line[0] != 'x')
rle += line;
}
return Parse(rle);
}
LifeHistoryState LifeHistoryState::ParseBellman(const std::string &rle) {
LifeHistoryState result;
int cnt;
int x, y;
x = 0;
y = 0;
cnt = 0;
for (char const ch : rle) {
if (ch >= '0' && ch <= '9') {
cnt *= 10;
cnt += (ch - '0');
} else if (ch == '$') {
if (cnt == 0)
cnt = 1;
if (cnt == 129)
// TODO: error
return result;
y += cnt;
x = 0;
cnt = 0;
} else if (ch == '!') {
break;
} else if (ch == '\n' || ch == ' ') {
continue;
} else {
if (cnt == 0)
cnt = 1;
for (int j = 0; j < cnt; j++) {
switch(ch) {
case 'C':
result.state.Set(x, y);
break;
case 'E':
result.history.Set(x, y);
break;
}
x++;
}
cnt = 0;
}
}
return result;
}
LifeHistoryState LifeHistoryState::ParseBellmanWHeader(const std::string &s) {
std::string rle;
std::istringstream iss(s);
for (std::string line; std::getline(iss, line); ) {
if(line[0] != 'x')
rle += line;
}
return ParseBellman(rle);
}