lz77.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // #include "simple/support/debug.hpp"
  2. #include "simple/compress/lz77.hpp"
  3. #include "simple/compress/iterator.hpp" // out_bits
  4. #include "simple/support/iterator.hpp" // offset_expander
  5. #include <cassert>
  6. #include <vector>
  7. #include <string>
  8. #include <cstdio>
  9. using namespace simple::compress;
  10. void Endecode(std::string text)
  11. // void Endecode(std::vector<unsigned> text)
  12. {
  13. std::vector<std::byte> encoded;
  14. encoded.reserve(text.size());
  15. lz77_encode(text.begin(), text.end(), out_bits(simple::support::offset_expander(encoded)));
  16. #if defined SIMPLE_SUPPORT_DEBUG_HPP
  17. simple::support::print('\n');
  18. simple::support::print("INPUT SIZE: ", text.size(), '\n');
  19. simple::support::print("COMPRESSED SIZE: ", encoded.size(), '\n');
  20. #endif
  21. std::string decoded;
  22. // std::vector<unsigned> decoded;
  23. decoded.resize(text.size());
  24. lz77_decode(encoded.begin(), decoded.begin(), decoded.end());
  25. assert(text == decoded);
  26. }
  27. int main(int argc, char const* argv[])
  28. {
  29. std::string text = "abcd aaaa bbbb cccc aaaa abcd aaaa aaaa aaaa aaaaa aaaa aaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa";
  30. // std::vector<unsigned> text(1000);
  31. if(argc > 1)
  32. {
  33. auto f = std::fopen(argv[1], "rb");
  34. std::fseek(f,0,SEEK_END);
  35. text.resize(std::ftell(f));
  36. // text.resize(std::ftell(f) / sizeof(unsigned));
  37. std::fseek(f,0,SEEK_SET);
  38. auto unused [[maybe_unused]] = std::fread(text.data(), text.size(), 1 ,f);
  39. // auto unused [[maybe_unused]] = std::fread(reinterpret_cast<std::byte*>(text.data()), text.size() * sizeof(unsigned), 1 ,f);
  40. #if defined SIMPLE_SUPPORT_DEBUG_HPP
  41. simple::support::print("s: ", text.size(), '\n');
  42. #endif
  43. }
  44. Endecode(std::move(text));
  45. return 0;
  46. }