-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbf.cpp
238 lines (161 loc) · 3.92 KB
/
tbf.cpp
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <iostream>
#include <fstream>
#include <memory>
#include <vector>
#include <algorithm>
#include <string>
#include "bf.hpp"
#include "bfhelp.hpp"
using num_t = int;
using factor_t = std::pair<num_t,num_t>;
using listfactor_t = std::vector<factor_t>;
static listfactor_t factor(num_t n)
{
listfactor_t vec;
for(num_t i=1;i<= (n/2)+1 ;++i)
{
if(!(n%i)) vec.push_back({i,n/i});
}
return vec;
}
static factor_t minfactor(const listfactor_t &vec)
{
return *std::min_element(vec.begin(),vec.end(),
[](const factor_t &a,const factor_t &b)
{
return (a.first + a.second) < (b.first + b.second);
});
}
static bool isprime(num_t n)
{
for(num_t i=2;i*i<=n;++i)
{
if(!(n%i)) return false;
}
return true;
}
static num_t findfactor(num_t n)
{
while(isprime(n) && (n>3)) --n;
return n;
}
static void appendip(ip_t &ip,const std::string &str)
{
ip.insert(ip.end(),str.begin(),str.end());
}
static ip_t a2bfA(num_t n, Tape &tape)
{
ip_t ip;
num_t a,b;
num_t m= n>*(tape) ? n-*(tape) : *(tape)-n;
std::tie(a,b) = minfactor(factor(findfactor(m)));
appendip(ip,">");++tape;
(a>*tape)? ip.insert(ip.end(),a-(*tape),'+') : ip.insert(ip.end(),(*tape)-a,'-') ;
*tape=0;
appendip(ip,"[<");--tape;
(n>*tape)? ip.insert(ip.end(),b,'+') : ip.insert(ip.end(),b,'-') ;
appendip(ip,">-]<");
if( (n>*tape) && ((*tape+a*b)<n) ) ip.insert(ip.end(),n-(*tape+a*b),'+') ;
else if((*tape>=n) && ((n+a*b)<*tape) ) ip.insert(ip.end(),*tape-(n+a*b),'-') ;
*tape=n;
appendip(ip,".");
return ip;
}
static ip_t a2bfB(num_t n, Tape &tape)
{
ip_t ip;
(n>*tape) ? ip.insert(ip.end(),n-*tape,'+') : ip.insert(ip.end(),*tape-n,'-');
*tape=n;
appendip(ip,".");
return ip;
}
static ip_t movetapeptr(Tape &tape,std::size_t index)
{
auto diff = (tape.getbaseptr()+index)-tape.getptr();
ip_t ip;
if(diff>0) for(;diff;--diff,++tape) ip.push_back('>');
else for(;diff;++diff,--tape) ip.push_back('<');
return ip;
}
static ip_t movea2bf(num_t n,Tape &tape,std::size_t index)
{
ip_t ip = movetapeptr(tape,index);
auto ip2=(( ((n>*tape)&&((n-*tape)<=11)) || ((n<=*tape)&&((*tape-n)<=11)) ) ? a2bfB : a2bfA) (n,tape);
ip.insert(ip.end(),ip2.begin(),ip2.end());
return ip;
}
static ip_t a2bf(num_t n, Tape &tape)
{
switch (n)
{
case 32: return movea2bf(n,tape,2); break;
case 10: return movea2bf(n,tape,4); break;
case 13: return movea2bf(n,tape,6); break;
default: return movea2bf(n,tape,0);
}
}
template <class T>
static const ip_t it2bf(T i,T j, Tape &tape)
{
ip_t ip;
for(;i != j; ++i)
{
auto ip2 = a2bf(*i,tape);
ip.insert(ip.end(),ip2.begin(),ip2.end());
}
return ip;
}
static unsigned int printip(std::ostream &out,const ip_t &ip,unsigned int ccol,unsigned int col)
{
for(const auto &i:ip)
{
out << i;
ccol=(ccol+1)%col;
if(!ccol) out << std::endl;
}
return ccol;
}
int main(int argc , const char *argv[])
{
if(argc<=1)
{
usage(argv[0],"a Text to Brainf**k Encoder.",TBFTAPE,sizeof(cell_t));
return 0;
}
std::ifstream fin;
if(argc > 1)
{
fin.open(argv[1],std::ios::in | std::ios::binary);
if(!fin)
{
showerr(err_fin,err,argv[1]);
return 1;
}
}
std::ofstream fout;
if(argc > 2)
{
fout.open(argv[2]);
if(!fout)
{
showerr(err_fout,err,argv[2]);
return 1;
}
}
std::ostream out(argc>2 ? fout.rdbuf() : std::cout.rdbuf() );
Tape tape(TBFTAPE);
std::unique_ptr<inst_t[]> buff(new inst_t[BSIZE+1]);
unsigned int ccol = 0;
try{
do
{
fin.read(buff.get(),BSIZE);
ccol=printip(out,it2bf(buff.get(),buff.get()+fin.gcount(),tape),ccol,COL);
}while(fin.gcount()>=BSIZE);
}catch(const std::exception &e)
{
std::cerr << std::endl << "Exception:" << std::endl
<< e.what() << std::endl;
}
return 0;
}