Change to index lists

This commit is contained in:
Nikola Petrov 2025-01-04 21:05:20 +01:00
parent adb0b2b5da
commit 42c4b88f49
2 changed files with 23 additions and 18 deletions

View File

@ -30,10 +30,10 @@ private:
uint128 randSeed; uint128 randSeed;
uint128 id; uint128 id;
std::vector<Dna> vector; std::vector<Dna> vector;
std::list<Dna *> queued; std::list<int> queued;
std::list<Dna *> showed; std::list<int> showed;
std::vector<Dna *> liked; std::vector<int> liked;
std::vector<Dna *> disliked; std::vector<int> disliked;
void newGen(); void newGen();
}; };

View File

@ -38,7 +38,7 @@ void DnaManager::init()
for (std::size_t i = 0; i < NUM_PER_GEN; i++) for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{ {
DNA::newDna(&vector[i], &randSeed); DNA::newDna(&vector[i], &randSeed);
queued.push_back(&vector[i]); queued.push_back(i);
} }
} }
@ -55,22 +55,23 @@ void DnaManager::deinit()
Unit DnaManager::next() Unit DnaManager::next()
{ {
Dna *ret = queued.front(); int index = queued.front();
queued.pop_front(); queued.pop_front();
showed.push_back(ret); showed.push_back(index);
Dna *ret = &vector[index];
return {ret, Liked::tbd}; return {ret, Liked::tbd};
} }
void DnaManager::like(Unit unit) void DnaManager::like(Unit unit)
{ {
Dna *found = nullptr; int found = -1;
for (auto &&i : showed) for (auto &&i : showed)
{ {
if (i == unit.dna) if (&vector[i] == unit.dna)
found = unit.dna; found = i;
} }
if (found == nullptr) if (found == -1)
{ {
TraceLog(LOG_ERROR, "NOT FOUND UNIT"); TraceLog(LOG_ERROR, "NOT FOUND UNIT");
return; return;
@ -104,7 +105,7 @@ void DnaManager::newGen()
for (std::size_t i = 0; i < NUM_PER_GEN; i++) for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{ {
DNA::newDna(&vector[i], &randSeed); DNA::newDna(&vector[i], &randSeed);
queued.push_back(&vector[i]); queued.push_back(i);
} }
disliked.clear(); disliked.clear();
return; return;
@ -117,11 +118,11 @@ void DnaManager::newGen()
if (liked.size() == 1) if (liked.size() == 1)
{ {
Dna *front = liked.front(); int front = liked.front();
for (auto &&i : disliked) for (auto &&i : disliked)
{ {
DNA::clone(front, i, &randSeed); DNA::clone(&vector[front], &vector[i], &randSeed);
queued.push_back(i); queued.push_back(i);
} }
@ -141,9 +142,13 @@ void DnaManager::newGen()
p2 = mrand::getValue(0, liked.size(), &randSeed); p2 = mrand::getValue(0, liked.size(), &randSeed);
} }
Dna *p1p = liked[p1]; p1 = liked[p1];
Dna *p2p = liked[p2]; p2 = liked[p2];
DNA::makeChild(p1p, p2p, i, &randSeed); Dna *p1p = &vector[p1];
Dna *p2p = &vector[p2];
Dna *c = &vector[i];
DNA::makeChild(p1p, p2p, c, &randSeed);
queued.push_back(i); queued.push_back(i);
} }
} }
@ -152,7 +157,7 @@ void DnaManager::newGen()
{ {
for (auto &&i : queued) for (auto &&i : queued)
{ {
DNA::mutate(i, NUM_OF_MUT, &randSeed); DNA::mutate(&vector[i], NUM_OF_MUT, &randSeed);
} }
return; return;
} }