aboutsummaryrefslogtreecommitdiff
path: root/libs/coding/move_to_front.cpp
blob: 54c8be1f64c8705251fecaffcc5521408c285f1a (plain) (blame)
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
#include "coding/move_to_front.hpp"

#include "base/assert.hpp"

#include <algorithm>
#include <cstring>
#include <numeric>

namespace coding
{
MoveToFront::MoveToFront()
{
  std::iota(m_order.begin(), m_order.end(), 0);
}

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

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

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