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

move_to_front.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6091c3f2324863d2a08d78cb077b42a2400fbecf (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
#include "base/move_to_front.hpp"

#include "base/assert.hpp"

#include <algorithm>
#include <cstring>

using namespace std;

namespace base
{
// static
size_t constexpr MoveToFront::kNumBytes;

MoveToFront::MoveToFront()
{
  for (size_t i = 0; i < kNumBytes; ++i)
    m_order[i] = i;
}

uint8_t MoveToFront::Transform(uint8_t b)
{
  auto const it = find(m_order.begin(), m_order.end(), b);
  ASSERT(it != m_order.end(), ());

  size_t const result = distance(m_order.begin(), it);
  ASSERT_LESS(result, kNumBytes, ());

  rotate(m_order.begin(), it, it + 1);
  ASSERT_EQUAL(m_order[0], b, ());
  return static_cast<uint8_t>(result);
}
}  // namespace base