mirror of
https://github.com/sockspls/badfish
synced 2025-05-01 09:13:08 +00:00
Remove some difficult to understand C++11 constructs
Code like this is more a case of showing off one's C++ knowledge, rather than using it adequately, IMHO. **First loop (std::generate)** Iterators are inadequate here, because they lose the key information which is idx. As a result, we need to carry a redundant idx variable, and increment it along the way. Very clumsy. Usage of std::generate and a lambda function only obfuscate the code, which is merely a simple and stupid loop over the elements of a vector. **Second loop (std::accumulate)** This code is thoroughlly incomprehensible. Restore the original, which was much simpler to understand. **Third loop (range based loop)** Again, a range based loop is inadequate, because we lose idx! To resolve this artificially created problem, the data model was made redundant (idx is a data member of db[] elements!?), which is ugly and unjustified. A simple and stupid for loop with idx does the job much better. No functional change. Resolves #313
This commit is contained in:
parent
8463fa479e
commit
60beb18efc
1 changed files with 9 additions and 9 deletions
|
@ -64,7 +64,6 @@ namespace {
|
||||||
|
|
||||||
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
|
||||||
|
|
||||||
unsigned id;
|
|
||||||
Color us;
|
Color us;
|
||||||
Square ksq[COLOR_NB], psq;
|
Square ksq[COLOR_NB], psq;
|
||||||
Result result;
|
Result result;
|
||||||
|
@ -85,20 +84,22 @@ bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
|
||||||
void Bitbases::init() {
|
void Bitbases::init() {
|
||||||
|
|
||||||
std::vector<KPKPosition> db(MAX_INDEX);
|
std::vector<KPKPosition> db(MAX_INDEX);
|
||||||
unsigned id = 0;
|
unsigned idx, repeat = 1;
|
||||||
|
|
||||||
// Initialize db with known win / draw positions
|
// Initialize db with known win / draw positions
|
||||||
std::generate(db.begin(), db.end(), [&id](){ return KPKPosition(id++); });
|
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||||
|
db[idx] = KPKPosition(idx);
|
||||||
|
|
||||||
// Iterate through the positions until none of the unknown positions can be
|
// Iterate through the positions until none of the unknown positions can be
|
||||||
// changed to either wins or draws (15 cycles needed).
|
// changed to either wins or draws (15 cycles needed).
|
||||||
while (std::accumulate(db.begin(), db.end(), false, [&](bool repeat, KPKPosition& pos)
|
while (repeat)
|
||||||
{ return (pos == UNKNOWN && pos.classify(db) != UNKNOWN) || repeat; })){}
|
for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
|
||||||
|
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
|
||||||
|
|
||||||
// Map 32 results into one KPKBitbase[] entry
|
// Map 32 results into one KPKBitbase[] entry
|
||||||
for (auto& pos : db)
|
for (idx = 0; idx < MAX_INDEX; ++idx)
|
||||||
if (pos == WIN)
|
if (db[idx] == WIN)
|
||||||
KPKBitbase[pos.id / 32] |= 1 << (pos.id & 0x1F);
|
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -106,7 +107,6 @@ namespace {
|
||||||
|
|
||||||
KPKPosition::KPKPosition(unsigned idx) {
|
KPKPosition::KPKPosition(unsigned idx) {
|
||||||
|
|
||||||
id = idx;
|
|
||||||
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
ksq[WHITE] = Square((idx >> 0) & 0x3F);
|
||||||
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
ksq[BLACK] = Square((idx >> 6) & 0x3F);
|
||||||
us = Color ((idx >> 12) & 0x01);
|
us = Color ((idx >> 12) & 0x01);
|
||||||
|
|
Loading…
Add table
Reference in a new issue