1def compress_ints_lossless(x, *, method: str = "zstd") -> bytes:
2 import zstandard as zstd
4 if method == "zstd":
5 cctx = zstd.ZstdCompressor(level=22)
6 return cctx.compress(x.tobytes())
7 elif method == "zlib":
8 import zlib
9 return zlib.compress(x.tobytes(), level=9)
10 elif method == "lzma":
11 import lzma
12 return lzma.compress(x.tobytes(), preset=9)
13 elif method == "simple_ans":
14 from simple_ans import ans_encode
15 encoding = ans_encode(x)
16 return encoding.bitstream + encoding.symbol_counts.tobytes() + encoding.symbol_values.tobytes()
17 else:
18 raise ValueError(f"Unknown method: {method}")