[generator] Refactored PlaceNode.

This commit is contained in:
Maksim Andrianov 2019-10-09 12:58:04 +03:00 committed by mpimenov
parent be99807812
commit f4cc853349
8 changed files with 103 additions and 100 deletions

View file

@ -124,7 +124,6 @@ set(
osm_o5m_source.hpp
osm_source.cpp
osm_xml_source.hpp
place_node.hpp
place_processor.cpp
place_processor.hpp
platform_helpers.cpp
@ -207,6 +206,7 @@ set(
translator_world.hpp
translators_pool.cpp
translators_pool.hpp
tree_node.hpp
type_helper.cpp
type_helper.hpp
ugc_db.cpp

View file

@ -100,7 +100,7 @@ MAIN_WITH_ERROR_HANDLING([](int argc, char ** argv) {
if (FLAGS_debug)
{
finalProcessor->SetPrintFunction(
static_cast<std::string (*)(generator::hierarchy::HierarchyLine const &)>(
static_cast<std::string (*)(generator::hierarchy::HierarchyEntry const &)>(
generator::hierarchy::DebugPrint));
}
rawGenerator.GenerateCustom(translator, finalProcessor);

View file

@ -546,7 +546,7 @@ std::shared_ptr<hierarchy::HierarchyLineEnricher> ComplexFinalProcessor::CreateE
void ComplexFinalProcessor::Process()
{
ThreadPool pool(m_threadsCount);
std::vector<std::future<std::vector<hierarchy::HierarchyLine>>> futures;
std::vector<std::future<std::vector<hierarchy::HierarchyEntry>>> futures;
ForEachCountry(m_mwmTmpPath, [&](auto const & filename) {
auto future = pool.Submit([&, filename]() {
auto countryName = filename;
@ -567,7 +567,7 @@ void ComplexFinalProcessor::Process()
});
futures.emplace_back(std::move(future));
});
std::vector<hierarchy::HierarchyLine> allLines;
std::vector<hierarchy::HierarchyEntry> allLines;
for (auto & f : futures)
{
auto const lines = f.get();
@ -576,7 +576,7 @@ void ComplexFinalProcessor::Process()
WriteLines(allLines);
}
void ComplexFinalProcessor::WriteLines(std::vector<hierarchy::HierarchyLine> const & lines)
void ComplexFinalProcessor::WriteLines(std::vector<hierarchy::HierarchyEntry> const & lines)
{
std::ofstream stream;
stream.exceptions(std::fstream::failbit | std::fstream::badbit);

View file

@ -141,7 +141,7 @@ public:
private:
std::shared_ptr<hierarchy::HierarchyLineEnricher> CreateEnricher(
std::string const & countryName) const;
void WriteLines(std::vector<hierarchy::HierarchyLine> const & lines);
void WriteLines(std::vector<hierarchy::HierarchyEntry> const & lines);
hierarchy::PrintFunction m_printFunction = hierarchy::PrintDefault;
std::string m_mwmTmpPath;

View file

@ -53,7 +53,7 @@ uint32_t GetTypeDefault(FeatureParams::Types const &) { return ftype::GetEmptyVa
std::string GetNameDefault(StringUtf8Multilang const &) { return {}; }
std::string PrintDefault(HierarchyLine const &) { return {}; }
std::string PrintDefault(HierarchyEntry const &) { return {}; }
HierarchyPlace::HierarchyPlace(FeatureBuilder const & fb)
: m_id(MakeCompositeId(fb))
@ -145,8 +145,7 @@ HierarchyLinker::Node::PtrList HierarchyLinker::Link()
if (!parentPlace)
continue;
parentPlace->AddChild(node);
node->SetParent(parentPlace);
tree_node::Link(node, parentPlace);
}
return m_nodes;
}
@ -206,7 +205,7 @@ boost::optional<m2::PointD> HierarchyLineEnricher::GetFeatureCenter(CompositeId
return ftPtr ? feature::GetCenter(*ftPtr) : boost::optional<m2::PointD>();
}
std::string DebugPrint(HierarchyLine const & line)
std::string DebugPrint(HierarchyEntry const & line)
{
std::stringstream stream;
stream << std::fixed << std::setprecision(7);
@ -243,9 +242,9 @@ void HierarchyLinesBuilder::SetHierarchyLineEnricher(
m_enricher = enricher;
}
std::vector<HierarchyLine> HierarchyLinesBuilder::GetHierarchyLines()
std::vector<HierarchyEntry> HierarchyLinesBuilder::GetHierarchyLines()
{
std::vector<HierarchyLine> lines;
std::vector<HierarchyEntry> lines;
lines.reserve(m_nodes.size());
std::transform(std::cbegin(m_nodes), std::cend(m_nodes), std::back_inserter(lines),
[&](auto const & n) { return Transform(n); });
@ -262,9 +261,9 @@ m2::PointD HierarchyLinesBuilder::GetCenter(HierarchyBuilder::Node::Ptr const &
return optCenter ? *optCenter : data.GetCenter();
}
HierarchyLine HierarchyLinesBuilder::Transform(HierarchyBuilder::Node::Ptr const & node)
HierarchyEntry HierarchyLinesBuilder::Transform(HierarchyBuilder::Node::Ptr const & node)
{
HierarchyLine line;
HierarchyEntry line;
auto const & data = node->GetData();
line.m_id = data.GetCompositeId();
auto const parent = node->GetParent();
@ -300,7 +299,7 @@ uint32_t GetMainType(FeatureParams::Types const & types)
std::string GetName(StringUtf8Multilang const & str) { return GetRussianName(str); }
std::string Print(HierarchyLine const & line)
std::string Print(HierarchyEntry const & line)
{
std::stringstream stream;
stream << std::fixed << std::setprecision(7);

View file

@ -2,8 +2,8 @@
#include "generator/feature_builder.hpp"
#include "generator/gen_mwm_info.hpp"
#include "generator/place_node.hpp"
#include "generator/platform_helpers.hpp"
#include "generator/tree_node.hpp"
#include "generator/utils.hpp"
#include "indexer/classificator.hpp"
@ -31,16 +31,16 @@ namespace generator
{
namespace hierarchy
{
struct HierarchyLine;
struct HierarchyEntry;
using GetMainType = std::function<uint32_t(FeatureParams::Types const &)>;
using GetName = std::function<std::string(StringUtf8Multilang const &)>;
using PrintFunction = std::function<std::string(HierarchyLine const &)>;
using PrintFunction = std::function<std::string(HierarchyEntry const &)>;
// These are dummy functions.
uint32_t GetTypeDefault(FeatureParams::Types const &);
std::string GetNameDefault(StringUtf8Multilang const &);
std::string PrintDefault(HierarchyLine const &);
std::string PrintDefault(HierarchyEntry const &);
// The HierarchyPlace class is an abstraction of FeatureBuilder to build a hierarchy of objects.
// It allows you to work with the geometry of points and areas.
@ -78,7 +78,7 @@ private:
class HierarchyLinker
{
public:
using Node = PlaceNode<HierarchyPlace>;
using Node = tree_node::TreeNode<HierarchyPlace>;
using Tree4d = m4::Tree<Node::Ptr>;
explicit HierarchyLinker(Node::PtrList && nodes);
@ -128,7 +128,7 @@ private:
};
// Intermediate view for hierarchy node.
struct HierarchyLine
struct HierarchyEntry
{
CompositeId m_id;
boost::optional<CompositeId> m_parentId;
@ -139,7 +139,7 @@ struct HierarchyLine
uint32_t m_type = ftype::GetEmptyValue();
};
std::string DebugPrint(HierarchyLine const & line);
std::string DebugPrint(HierarchyEntry const & line);
class HierarchyLinesBuilder
{
@ -151,11 +151,11 @@ public:
void SetCountryName(std::string const & name);
void SetHierarchyLineEnricher(std::shared_ptr<HierarchyLineEnricher> const & enricher);
std::vector<HierarchyLine> GetHierarchyLines();
std::vector<HierarchyEntry> GetHierarchyLines();
private:
m2::PointD GetCenter(HierarchyBuilder::Node::Ptr const & node);
HierarchyLine Transform(HierarchyBuilder::Node::Ptr const & node);
HierarchyEntry Transform(HierarchyBuilder::Node::Ptr const & node);
HierarchyBuilder::Node::PtrList m_nodes;
GetMainType m_getMainType = GetTypeDefault;
@ -168,7 +168,7 @@ namespace popularity
{
uint32_t GetMainType(FeatureParams::Types const & types);
std::string GetName(StringUtf8Multilang const & str);
std::string Print(HierarchyLine const & line);
std::string Print(HierarchyEntry const & line);
} // namespace popularity
} // namespace hierarchy
} // namespace generator

View file

@ -1,75 +0,0 @@
#pragma once
#include <memory>
#include <utility>
#include <vector>
namespace generator
{
template <typename Data>
class PlaceNode;
namespace place_node_types
{
template <typename Data>
using Ptr = std::shared_ptr<PlaceNode<Data>>;
template <typename Data>
using WeakPtr = std::weak_ptr<PlaceNode<Data>>;
template <typename Data>
using PtrList = std::vector<Ptr<Data>>;
} // place_node_types
// This is a tree node, where each node can have many children. Now it is used to build a hierarchy
// of places.
template <typename Data>
class PlaceNode
{
public:
using Ptr = place_node_types::Ptr<Data>;
using WeakPtr = place_node_types::WeakPtr<Data>;
using PtrList = place_node_types::PtrList<Data>;
explicit PlaceNode(Data && data) : m_data(std::move(data)) {}
void AddChild(Ptr child) { m_children.push_back(child); }
void SetParent(Ptr parent) { m_parent = parent; }
void SetChildren(PtrList && children) { m_children = std::move(children); }
void RemoveChildren() { m_children.clear(); }
bool HasChildren() const { return !m_children.empty(); }
bool HasParent() const { return m_parent.lock() != nullptr; }
PtrList const & GetChildren() const { return m_children; }
PtrList & GetChildren() { return m_children; }
Ptr GetParent() const { return m_parent.lock(); }
Data & GetData() { return m_data; }
Data const & GetData() const { return m_data; }
private:
Data m_data;
PtrList m_children;
WeakPtr m_parent;
};
template <typename Data>
size_t GetDepth(place_node_types::Ptr<Data> node)
{
size_t depth = 0;
while (node)
{
node = node->GetParent();
++depth;
}
return depth;
}
template <typename Data, typename Visitor>
void Visit(place_node_types::Ptr<Data> const & tree, Visitor && visitor)
{
visitor(tree);
for (auto const & subtree : tree->GetChildren())
Visit(subtree, visitor);
}
} // namespace generator

79
generator/tree_node.hpp Normal file
View file

@ -0,0 +1,79 @@
#pragma once
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
#include "base/stl_helpers.hpp"
namespace tree_node
{
template <typename Data>
class TreeNode;
namespace types
{
template <typename Data>
using Ptr = std::shared_ptr<TreeNode<Data>>;
template <typename Data>
using WeakPtr = std::weak_ptr<TreeNode<Data>>;
template <typename Data>
using PtrList = std::vector<Ptr<Data>>;
} // namespace types
template <typename Data>
class TreeNode
{
public:
using Ptr = types::Ptr<Data>;
using WeakPtr = types::WeakPtr<Data>;
using PtrList = types::PtrList<Data>;
explicit TreeNode(Data && data) : m_data(std::move(data)) {}
bool HasChildren() const { return !m_children.empty(); }
PtrList const & GetChildren() const { return m_children; }
void AddChild(Ptr const & child) { m_children.push_back(child); }
void AddChildren(PtrList && children)
{
std::move(std::begin(children), std::end(children), std::end(m_children));
}
void SetChildren(PtrList && children) { m_children = std::move(children); }
void RemoveChildren() { m_children.clear(); }
bool HasParent() const { return m_parent.lock() != nullptr; }
Ptr GetParent() const { return m_parent.lock(); }
void SetParent(Ptr const & parent) { m_parent = parent; }
Data & GetData() { return m_data; }
Data const & GetData() const { return m_data; }
private:
Data m_data;
PtrList m_children;
WeakPtr m_parent;
};
template <typename Data>
void Link(types::Ptr<Data> const & node, types::Ptr<Data> const & parent)
{
parent->AddChild(node);
node->SetParent(parent);
}
template <typename Data>
size_t GetDepth(types::Ptr<Data> node)
{
size_t depth = 0;
while (node)
{
node = node->GetParent();
++depth;
}
return depth;
}
} // namespace tree_node