Commit cc7a52eb authored by aggsol's avatar aggsol 🙉

fix parents of sections

parent 84b37bd1
......@@ -24,15 +24,16 @@
#include <cassert>
#include <iostream>
#include <map>
#include <list>
namespace bodhi
{
std::map<int, int> createSectionMapping(std::map<int, Section>& sections, unsigned seed)
std::map<int, int> createSectionMapping(const std::map<int, Section>& sections, unsigned seed)
{
std::map<int, int> resultMapping;
std::deque<int> numbers;
for(auto& s: sections)
for(const auto& s: sections)
{
if(not s.second.hasClass("fixed"))
{
......@@ -40,18 +41,22 @@ namespace bodhi
}
else
{
assert(resultMapping.end() == resultMapping.find(s.first));
resultMapping[s.first] = s.first;
}
}
std::default_random_engine generator(seed);
int minDelta = std::max<int>(3, numbers.size()/10);
int maxAttempts = std::max<int>(10, numbers.size()/10);
const int minDelta = std::max<int>(3, numbers.size()/10);
const int maxAttempts = std::max<int>(10, numbers.size()/10);
std::cout << "Shuffle: minDelta=" << minDelta << " maxAttempts=" << maxAttempts << "\n";
while(numbers.size() > 1)
{
int index = bodhi::randint(generator, 0, numbers.size()-1);
int a = numbers.at(index);
const int a = numbers.at(index);
numbers.erase(numbers.begin() + index);
index = bodhi::randint(generator, 0, numbers.size()-1);
......@@ -72,6 +77,8 @@ namespace bodhi
numbers.erase(numbers.begin() + index);
assert(a != b);
assert(resultMapping.find(a) == resultMapping.end());
assert(resultMapping.find(b) == resultMapping.end());
resultMapping[a] = b;
resultMapping[b] = a;
......@@ -82,6 +89,13 @@ namespace bodhi
resultMapping[numbers[0]] = numbers[0];
}
assert(resultMapping.size() == sections.size());
for(const auto& s: sections)
{
assert(resultMapping.at(s.first) > 0);
}
return resultMapping;
}
}
......
......@@ -152,18 +152,20 @@ int main(int argc, char* argv[])
for(auto& s: sections)
{
// Set parents
assert(s.first == s.second.m_number);
const int parent = s.first;
for(auto child: s.second.m_children)
{
auto it = sections.find(child);
if(it != sections.end())
{
it->second.m_parents.insert(child);
it->second.m_parents.insert(parent);
}
else
{
std::cerr << rang::fg::red
<< "Error : Missing section: " << child
<< " linked from section: " << s.first << "\n"
<< " linked from section: " << parent << "\n"
<< rang::fg::reset;
}
}
......@@ -206,11 +208,11 @@ int main(int argc, char* argv[])
for(auto& s: sections)
{
std::cout << "Section: " << s.second.m_number << "\n"
<< "-> Number of classes : " << s.second.m_classes.size() << "\n"
<< "-> Number of lines : " << s.second.m_lines.size() << "\n"
<< "-> Number of children: " << s.second.m_children.size() << "\n"
<< "-> Number of parents : " << s.second.m_parents.size() << "\n";
std::cout << "Section: " << s.second.m_number
<< "\t classes=" << s.second.m_classes.size()
<< "\t lines=" << s.second.m_lines.size()
<< "\t children=" << s.second.m_children.size()
<< "\t parents=" << s.second.m_parents.size() << "\n";
}
}
......
......@@ -15,10 +15,23 @@ public:
ShuffleTest()
: tiny::Unit(__FILE__)
{
tiny::Unit::registerTest(&ShuffleTest::minComplex, "minComplex");
tiny::Unit::registerTest(&ShuffleTest::basic, "basic");
tiny::Unit::registerTest(&ShuffleTest::shuffle, "shuffle");
}
static void minComplex()
{
bodhi::Parser parser("../test/mincomplex.adoc");
std::map<int, bodhi::Section> sections;
bool error = parser.parse(sections);
TINY_ASSERT_OK(not error);
auto resultMapping = bodhi::createSectionMapping(sections, 123456);
}
static void basic()
{
bodhi::Parser parser("../test/linear.adoc");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment