diff --git a/indexer/displacement_manager.hpp b/indexer/displacement_manager.hpp new file mode 100644 index 0000000000..eb9d4fdd19 --- /dev/null +++ b/indexer/displacement_manager.hpp @@ -0,0 +1,92 @@ +#pragma once + +#include "indexer/cell_id.hpp" + +#include "std/vector.hpp" + +namespace covering +{ +class CellFeaturePair +{ +public: + CellFeaturePair() = default; + CellFeaturePair(uint64_t cell, uint32_t feature) + : m_CellLo(UINT64_LO(cell)), m_CellHi(UINT64_HI(cell)), m_Feature(feature) + { + } + + bool operator<(CellFeaturePair const & rhs) const + { + if (m_CellHi != rhs.m_CellHi) + return m_CellHi < rhs.m_CellHi; + if (m_CellLo != rhs.m_CellLo) + return m_CellLo < rhs.m_CellLo; + return m_Feature < rhs.m_Feature; + } + + uint64_t GetCell() const { return UINT64_FROM_UINT32(m_CellHi, m_CellLo); } + uint32_t GetFeature() const { return m_Feature; } +private: + uint32_t m_CellLo; + uint32_t m_CellHi; + uint32_t m_Feature; +}; +static_assert(sizeof(CellFeaturePair) == 12, ""); +#ifndef OMIM_OS_LINUX +static_assert(is_trivially_copyable::value, ""); +#endif + +class CellFeatureBucketTuple +{ +public: + CellFeatureBucketTuple() = default; + CellFeatureBucketTuple(CellFeaturePair const & p, uint32_t bucket) : m_pair(p), m_bucket(bucket) + { + } + + bool operator<(CellFeatureBucketTuple const & rhs) const + { + if (m_bucket != rhs.m_bucket) + return m_bucket < rhs.m_bucket; + return m_pair < rhs.m_pair; + } + + CellFeaturePair const & GetCellFeaturePair() const { return m_pair; } + uint32_t GetBucket() const { return m_bucket; } +private: + CellFeaturePair m_pair; + uint32_t m_bucket; +}; +static_assert(sizeof(CellFeatureBucketTuple) == 16, ""); +#ifndef OMIM_OS_LINUX +static_assert(is_trivially_copyable::value, ""); +#endif + +template +class DisplacementManager +{ +public: + DisplacementManager(TSorter & sorter) : m_sorter(sorter) {} + template + void Add(vector const & cells, uint32_t bucket, TFeature const & ft, uint32_t index) + { + if (!IsDisplacable(ft)) + { + for (auto cell : cells) + m_sorter.Add(CellFeatureBucketTuple(CellFeaturePair(cell, index), bucket)); + return; + } + // TODO(ldragunov) Put feature to displacement store. + } + +private: + // TODO write displacement determination. + template + bool IsDisplacable(TFeature const & /*ft*/) const noexcept + { + return false; + } + + TSorter & m_sorter; +}; +} // namespace indexer diff --git a/indexer/scale_index_builder.hpp b/indexer/scale_index_builder.hpp index 18b04bcf7b..1dbfb758df 100644 --- a/indexer/scale_index_builder.hpp +++ b/indexer/scale_index_builder.hpp @@ -1,6 +1,7 @@ #pragma once #include "indexer/cell_id.hpp" #include "indexer/data_header.hpp" +#include "indexer/displacement_manager.hpp" #include "indexer/feature.hpp" #include "indexer/feature_covering.hpp" #include "indexer/feature_visibility.hpp" @@ -26,72 +27,16 @@ namespace covering { -class CellFeaturePair -{ -public: - CellFeaturePair() = default; - CellFeaturePair(uint64_t cell, uint32_t feature) - : m_CellLo(UINT64_LO(cell)), m_CellHi(UINT64_HI(cell)), m_Feature(feature) {} - - bool operator< (CellFeaturePair const & rhs) const - { - if (m_CellHi != rhs.m_CellHi) - return m_CellHi < rhs.m_CellHi; - if (m_CellLo != rhs.m_CellLo) - return m_CellLo < rhs.m_CellLo; - return m_Feature < rhs.m_Feature; - } - - uint64_t GetCell() const { return UINT64_FROM_UINT32(m_CellHi, m_CellLo); } - uint32_t GetFeature() const { return m_Feature; } - -private: - uint32_t m_CellLo; - uint32_t m_CellHi; - uint32_t m_Feature; -}; -static_assert(sizeof(CellFeaturePair) == 12, ""); -#ifndef OMIM_OS_LINUX -static_assert(is_trivially_copyable::value, ""); -#endif - -class CellFeatureBucketTuple -{ -public: - CellFeatureBucketTuple() = default; - CellFeatureBucketTuple(CellFeaturePair const & p, uint32_t bucket) : m_pair(p), m_bucket(bucket) - { - } - - bool operator<(CellFeatureBucketTuple const & rhs) const - { - if (m_bucket != rhs.m_bucket) - return m_bucket < rhs.m_bucket; - return m_pair < rhs.m_pair; - } - - CellFeaturePair const & GetCellFeaturePair() const { return m_pair; } - uint32_t GetBucket() const { return m_bucket; } - -private: - CellFeaturePair m_pair; - uint32_t m_bucket; -}; -static_assert(sizeof(CellFeatureBucketTuple) == 16, ""); -#ifndef OMIM_OS_LINUX -static_assert(is_trivially_copyable::value, ""); -#endif - -template +template class FeatureCoverer { public: - FeatureCoverer(feature::DataHeader const & header, TSorter & sorter, + FeatureCoverer(feature::DataHeader const & header, TDisplacementManager & manager, vector & featuresInBucket, vector & cellsInBucket) : m_header(header), m_scalesIdx(0), m_bucketsCount(header.GetLastScale() + 1), - m_sorter(sorter), + m_displacement(manager), m_codingDepth(covering::GetCodingDepth(header.GetLastScale())), m_featuresInBucket(featuresInBucket), m_cellsInBucket(cellsInBucket) @@ -120,8 +65,7 @@ public: } vector const cells = covering::CoverFeature(ft, m_codingDepth, 250); - for (int64_t cell : cells) - m_sorter.Add(CellFeatureBucketTuple(CellFeaturePair(cell, index), bucket)); + m_displacement.Add(cells, bucket, ft, index); m_featuresInBucket[bucket] += 1; m_cellsInBucket[bucket] += cells.size(); @@ -169,7 +113,7 @@ private: mutable size_t m_scalesIdx; uint32_t m_bucketsCount; - TSorter & m_sorter; + TDisplacementManager & m_displacement; int m_codingDepth; vector & m_featuresInBucket; vector & m_cellsInBucket; @@ -208,11 +152,13 @@ void IndexScales(feature::DataHeader const & header, TFeaturesVector const & fea FileWriter cellsToFeaturesAllBucketsWriter(cellsToFeatureAllBucketsFile); using TSorter = FileSorter>; + using TDisplacementManager = DisplacementManager; WriterFunctor out(cellsToFeaturesAllBucketsWriter); TSorter sorter(1024 * 1024 /* bufferBytes */, tmpFilePrefix + CELL2FEATURE_TMP_EXT, out); + TDisplacementManager manager(sorter); vector featuresInBucket(bucketsCount); vector cellsInBucket(bucketsCount); - features.ForEach(FeatureCoverer(header, sorter, featuresInBucket, cellsInBucket)); + features.ForEach(FeatureCoverer(header, manager, featuresInBucket, cellsInBucket)); sorter.SortAndFinish(); for (uint32_t bucket = 0; bucket < bucketsCount; ++bucket)