Welcome to mirror list, hosted at ThFree Co, Russian Federation.

scale_index.hpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 096f00c200b820f1d09b65562e5f416069589702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once

#include "indexer/data_factory.hpp"
#include "indexer/interval_index.hpp"

#include "coding/var_serial_vector.hpp"

#include <cstdint>
#include <memory>
#include <vector>


/// Index bucket <--> Draw scale range.
/// Using default one-to-one mapping.
class ScaleIndexBase
{
public:
  static uint32_t GetBucketsCount() { return 18; }
  static uint32_t BucketByScale(int scale) { return static_cast<uint32_t>(scale); }
  /// @return Range like [x, y).
  static std::pair<uint32_t, uint32_t> ScaleRangeForBucket(uint32_t bucket)
  {
    return {bucket, bucket + 1};
  }
};

template <class Reader>
class ScaleIndex : public ScaleIndexBase
{
public:
  ScaleIndex() = default;

  ScaleIndex(Reader const & reader, IndexFactory const & factory) { Attach(reader, factory); }

  ~ScaleIndex()
  {
    Clear();
  }

  void Clear()
  {
    m_IndexForScale.clear();
  }

  void Attach(Reader const & reader, IndexFactory const & factory)
  {
    Clear();

    ReaderSource<Reader> source(reader);
    VarSerialVectorReader<Reader> treesReader(source);
    for (uint32_t i = 0; i < treesReader.Size(); ++i)
      m_IndexForScale.push_back(factory.CreateIndex(treesReader.SubReader(i)));
  }

  template <typename F>
  void ForEachInIntervalAndScale(F const & f, uint64_t beg, uint64_t end, int scale) const
  {
    auto const scaleBucket = BucketByScale(scale);
    if (scaleBucket < m_IndexForScale.size())
    {
      for (size_t i = 0; i <= scaleBucket; ++i)
        m_IndexForScale[i]->ForEach(f, beg, end);
    }
  }

private:
  std::vector<std::unique_ptr<IntervalIndex<Reader, uint32_t>>> m_IndexForScale;
};