/src/brunsli/c/common/lehmer_code.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) Google LLC 2019 |
2 | | // |
3 | | // Use of this source code is governed by an MIT-style |
4 | | // license that can be found in the LICENSE file or at |
5 | | // https://opensource.org/licenses/MIT. |
6 | | |
7 | | #include "./lehmer_code.h" |
8 | | |
9 | | #include <utility> |
10 | | #include <vector> |
11 | | |
12 | | namespace brunsli { |
13 | | |
14 | | void ComputeLehmerCode(const uint32_t* sigma, const size_t len, |
15 | 0 | uint32_t* code) { |
16 | 0 | std::vector<uint32_t> items(len); |
17 | 0 | for (size_t i = 0; i < len; ++i) items[i] = static_cast<uint32_t>(i); |
18 | 0 | for (size_t i = 0; i < len; ++i) { |
19 | 0 | std::vector<uint32_t>::iterator it = |
20 | 0 | std::find(items.begin(), items.end(), sigma[i]); |
21 | 0 | BRUNSLI_DCHECK(it != items.end()); |
22 | 0 | code[i] = static_cast<uint32_t>(it - items.begin()); |
23 | 0 | items.erase(it); |
24 | 0 | } |
25 | 0 | } |
26 | | |
27 | 6.91k | bool DecodeLehmerCode(const uint32_t* code, size_t len, uint32_t* sigma) { |
28 | 6.91k | std::vector<uint32_t> items(len); |
29 | 449k | for (size_t i = 0; i < len; ++i) items[i] = static_cast<uint32_t>(i); |
30 | 448k | for (size_t i = 0; i < len; ++i) { |
31 | 441k | uint32_t index = code[i]; |
32 | 441k | if (index >= items.size()) return false; |
33 | 441k | const uint32_t value = items[index]; |
34 | 441k | items.erase(items.begin() + index); |
35 | 441k | sigma[i] = value; |
36 | 441k | } |
37 | 6.89k | return true; |
38 | 6.91k | } |
39 | | |
40 | | } // namespace brunsli |