/src/brunsli/c/common/lehmer_code.cc
Line | Count | Source |
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 <brunsli/types.h> |
10 | | |
11 | | #include <algorithm> |
12 | | #include <vector> |
13 | | |
14 | | #include "./platform.h" |
15 | | |
16 | | namespace brunsli { |
17 | | |
18 | | void ComputeLehmerCode(const uint32_t* sigma, const size_t len, |
19 | 0 | uint32_t* code) { |
20 | 0 | std::vector<uint32_t> items(len); |
21 | 0 | for (size_t i = 0; i < len; ++i) items[i] = static_cast<uint32_t>(i); |
22 | 0 | for (size_t i = 0; i < len; ++i) { |
23 | 0 | std::vector<uint32_t>::iterator it = |
24 | 0 | std::find(items.begin(), items.end(), sigma[i]); |
25 | 0 | BRUNSLI_DCHECK(it != items.end()); |
26 | 0 | code[i] = static_cast<uint32_t>(it - items.begin()); |
27 | 0 | items.erase(it); |
28 | 0 | } |
29 | 0 | } |
30 | | |
31 | 5.01k | bool DecodeLehmerCode(const uint32_t* code, size_t len, uint32_t* sigma) { |
32 | 5.01k | std::vector<uint32_t> items(len); |
33 | 325k | for (size_t i = 0; i < len; ++i) items[i] = static_cast<uint32_t>(i); |
34 | 325k | for (size_t i = 0; i < len; ++i) { |
35 | 320k | uint32_t index = code[i]; |
36 | 320k | if (index >= items.size()) return false; |
37 | 320k | const uint32_t value = items[index]; |
38 | 320k | items.erase(items.begin() + index); |
39 | 320k | sigma[i] = value; |
40 | 320k | } |
41 | 5.00k | return true; |
42 | 5.01k | } |
43 | | |
44 | | } // namespace brunsli |