Skip to content

Commit 60beb18

Browse files
lucasartzamar
authored andcommitted
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
1 parent 8463fa4 commit 60beb18

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/bitbase.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ namespace {
6464

6565
template<Color Us> Result classify(const std::vector<KPKPosition>& db);
6666

67-
unsigned id;
6867
Color us;
6968
Square ksq[COLOR_NB], psq;
7069
Result result;
@@ -85,28 +84,29 @@ bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) {
8584
void Bitbases::init() {
8685

8786
std::vector<KPKPosition> db(MAX_INDEX);
88-
unsigned id = 0;
87+
unsigned idx, repeat = 1;
8988

9089
// Initialize db with known win / draw positions
91-
std::generate(db.begin(), db.end(), [&id](){ return KPKPosition(id++); });
90+
for (idx = 0; idx < MAX_INDEX; ++idx)
91+
db[idx] = KPKPosition(idx);
9292

9393
// Iterate through the positions until none of the unknown positions can be
9494
// changed to either wins or draws (15 cycles needed).
95-
while (std::accumulate(db.begin(), db.end(), false, [&](bool repeat, KPKPosition& pos)
96-
{ return (pos == UNKNOWN && pos.classify(db) != UNKNOWN) || repeat; })){}
95+
while (repeat)
96+
for (repeat = idx = 0; idx < MAX_INDEX; ++idx)
97+
repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN);
9798

9899
// Map 32 results into one KPKBitbase[] entry
99-
for (auto& pos : db)
100-
if (pos == WIN)
101-
KPKBitbase[pos.id / 32] |= 1 << (pos.id & 0x1F);
100+
for (idx = 0; idx < MAX_INDEX; ++idx)
101+
if (db[idx] == WIN)
102+
KPKBitbase[idx / 32] |= 1 << (idx & 0x1F);
102103
}
103104

104105

105106
namespace {
106107

107108
KPKPosition::KPKPosition(unsigned idx) {
108109

109-
id = idx;
110110
ksq[WHITE] = Square((idx >> 0) & 0x3F);
111111
ksq[BLACK] = Square((idx >> 6) & 0x3F);
112112
us = Color ((idx >> 12) & 0x01);

0 commit comments

Comments
 (0)