Introduce DisplaceManager class.

This commit is contained in:
Lev Dragunov 2015-12-01 14:04:51 +03:00 committed by Sergey Yershov
parent e3e4ad8001
commit 1c69f0b270
2 changed files with 101 additions and 63 deletions

View file

@ -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<CellFeaturePair>::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<CellFeatureBucketTuple>::value, "");
#endif
template <class TSorter>
class DisplacementManager
{
public:
DisplacementManager(TSorter & sorter) : m_sorter(sorter) {}
template <class TFeature>
void Add(vector<int64_t> 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 <class TFeature>
bool IsDisplacable(TFeature const & /*ft*/) const noexcept
{
return false;
}
TSorter & m_sorter;
};
} // namespace indexer

View file

@ -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<CellFeaturePair>::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<CellFeatureBucketTuple>::value, "");
#endif
template <class TSorter>
template <class TDisplacementManager>
class FeatureCoverer
{
public:
FeatureCoverer(feature::DataHeader const & header, TSorter & sorter,
FeatureCoverer(feature::DataHeader const & header, TDisplacementManager & manager,
vector<uint32_t> & featuresInBucket, vector<uint32_t> & 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<int64_t> 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<uint32_t> & m_featuresInBucket;
vector<uint32_t> & m_cellsInBucket;
@ -208,11 +152,13 @@ void IndexScales(feature::DataHeader const & header, TFeaturesVector const & fea
FileWriter cellsToFeaturesAllBucketsWriter(cellsToFeatureAllBucketsFile);
using TSorter = FileSorter<CellFeatureBucketTuple, WriterFunctor<FileWriter>>;
using TDisplacementManager = DisplacementManager<TSorter>;
WriterFunctor<FileWriter> out(cellsToFeaturesAllBucketsWriter);
TSorter sorter(1024 * 1024 /* bufferBytes */, tmpFilePrefix + CELL2FEATURE_TMP_EXT, out);
TDisplacementManager manager(sorter);
vector<uint32_t> featuresInBucket(bucketsCount);
vector<uint32_t> cellsInBucket(bucketsCount);
features.ForEach(FeatureCoverer<TSorter>(header, sorter, featuresInBucket, cellsInBucket));
features.ForEach(FeatureCoverer<TDisplacementManager>(header, manager, featuresInBucket, cellsInBucket));
sorter.SortAndFinish();
for (uint32_t bucket = 0; bucket < bucketsCount; ++bucket)