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

indexed_value.hpp « search - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2bfbfbb86a723e060dacd320dd4114745b33974 (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
69
70
71
72
73
#pragma once

#include "std/bind.hpp"
#include "std/algorithm.hpp"
#include "std/vector.hpp"
#include "std/limits.hpp"


namespace search
{

/// Intrusive class to implement multi-index for some user-defined type.
template <size_t N> class IndexedValueBase
{
protected:
  static size_t const SIZE = N;
  size_t m_ind[N];

public:
  IndexedValueBase()
  {
    for (size_t i = 0; i < N; ++i)
      m_ind[i] = numeric_limits<size_t>::max();
  }

  void SetIndex(size_t i, size_t v) { m_ind[i] = v; }

  void SortIndex()
  {
    sort(m_ind, m_ind + N);
  }

  static bool Less(IndexedValueBase const & r1, IndexedValueBase const & r2)
  {
    for (size_t i = 0; i < N; ++i)
    {
      if (r1.m_ind[i] != r2.m_ind[i])
        return (r1.m_ind[i] < r2.m_ind[i]);
    }

    return false;
  }
};

template <class T, class CompFactory>
void SortByIndexedValue(vector<T> & vec, CompFactory factory)
{
  for (size_t i = 0; i < CompFactory::SIZE; ++i)
  {
    typename CompFactory::CompT comp = factory.Get(i);

    // sort by needed criteria
    sort(vec.begin(), vec.end(), comp);

    // assign ranks
    size_t rank = 0;
    for (size_t j = 0; j < vec.size(); ++j)
    {
      if (j > 0 && comp(vec[j-1], vec[j]))
        ++rank;

      vec[j].SetIndex(i, rank);
    }
  }

  // prepare combined criteria
  for_each(vec.begin(), vec.end(), [] (IndexedValueBase<CompFactory::SIZE> & p) { p.SortIndex(); });

  // sort results according to combined criteria
  sort(vec.begin(), vec.end(), &IndexedValueBase<CompFactory::SIZE>::Less);
}

}