mtf.hpp 576 B

12345678910111213141516171819202122232425262728293031
  1. #pragma once
  2. //move to front
  3. namespace nall::Encode {
  4. inline auto MTF(array_view<uint8_t> input) -> vector<uint8_t> {
  5. vector<uint8_t> output;
  6. output.resize(input.size());
  7. uint8_t order[256];
  8. for(uint n : range(256)) order[n] = n;
  9. for(uint offset : range(input.size())) {
  10. uint data = input[offset];
  11. for(uint index : range(256)) {
  12. uint value = order[index];
  13. if(value == data) {
  14. output[offset] = index;
  15. memory::move(&order[1], &order[0], index);
  16. order[0] = value;
  17. break;
  18. }
  19. }
  20. }
  21. return output;
  22. }
  23. }