1#ifndef NEFORCE_COMPRESS_LZ4_COMPRESS_HPP__
2#define NEFORCE_COMPRESS_LZ4_COMPRESS_HPP__
13#if defined(NEFORCE_SUPPORT_LZ4) || defined(NEXUSFORCE_ENABLE_DOXYGEN)
17NEFORCE_BEGIN_NAMESPACE__
31struct lz4_exception final : thirdparty_exception {
32 explicit lz4_exception(
const char* info =
"LZ4 Operation Failed.",
const char*
type = static_type,
33 const int code = 0) noexcept :
34 thirdparty_exception(info,
type,
code) {}
36 explicit lz4_exception(
const exception& e) :
37 thirdparty_exception(e) {}
39 ~lz4_exception()
override =
default;
40 static constexpr auto static_type =
"lz4_exception";
89 size_t estimated_original_size);
106 template <
typename Iterator>
113 return compress_data(
data, data_size, level);
128 return compress_data(
reinterpret_cast<const byte_t*
>(
data.data()),
data.size(), level);
144 template <
typename T>
146 static_assert(
sizeof(T) == 1,
"Iterator must point to byte-sized elements");
148 return lz4_compressor::compress_data(
reinterpret_cast<const byte_t*
>(
data.data()),
data.size() *
sizeof(T),
163 template <
typename Iterator, enable_if_t<is_ranges_cot_iter_v<Iterator>,
int> = 0>
165 const size_t estimated_original_size = 0) {
171 return decompress_data(
data, data_size, estimated_original_size);
182 return decompress_data(
data.data(),
data.size(), estimated_original_size);
195 ::LZ4_stream_t* stream_ =
nullptr;
196 bool use_hc_ =
false;
198 size_t bytes_input_ = 0;
199 size_t bytes_output_ = 0;
200 bool finished_ =
false;
277 NEFORCE_NODISCARD
size_t bytes_input() const noexcept {
return bytes_input_; }
283 NEFORCE_NODISCARD
size_t bytes_output() const noexcept {
return bytes_output_; }
290 return bytes_input_ > 0 ?
static_cast<double>(bytes_output_) / bytes_input_ : 0.0;
303 ::LZ4_streamDecode_t* stream_ =
nullptr;
304 size_t bytes_input_ = 0;
305 size_t bytes_output_ = 0;
306 bool finished_ =
false;
362 NEFORCE_NODISCARD
size_t bytes_input() const noexcept {
return bytes_input_; }
368 NEFORCE_NODISCARD
size_t bytes_output() const noexcept {
return bytes_output_; }
375 return bytes_input_ > 0 ?
static_cast<double>(bytes_output_) / bytes_input_ : 0.0;
382NEFORCE_END_NAMESPACE__
stream_compressor(stream_compressor &&other) noexcept
移动构造函数
void reset(int level=0)
重置压缩器状态
size_t bytes_output() const noexcept
获取输出字节数
size_t bytes_input() const noexcept
获取输入字节数
byte_vector compress(const cbyte_view &data, bool finish=false)
压缩数据
double compression_ratio() const noexcept
获取压缩率
stream_compressor(int level=0)
构造函数
byte_vector compress(string_view data, bool finish=false)
压缩字符串视图
stream_compressor & operator=(stream_compressor &&other) noexcept
移动赋值运算符
byte_vector finish()
完成压缩并返回剩余数据
stream_decompressor(stream_decompressor &&other) noexcept
移动构造函数
double expansion_ratio() const noexcept
获取扩展率
~stream_decompressor()
析构函数
byte_vector finish()
完成解压缩并返回剩余数据
size_t bytes_input() const noexcept
获取输入字节数
size_t bytes_output() const noexcept
获取输出字节数
stream_decompressor()
构造函数
stream_decompressor & operator=(stream_decompressor &&other) noexcept
移动赋值运算符
byte_vector decompress(const byte_view &data, bool finish=false)
解压缩数据
static constexpr size_t block_size
最大块大小常量
static byte_vector decompress(const cbyte_view &data, const size_t estimated_original_size=0)
解压缩常量字节视图
static byte_vector compress(const vector< T > &data, const int level=0)
压缩字节向量
static byte_vector compress(Iterator begin, Iterator end, const int level=0)
压缩迭代器范围中的数据
static byte_vector decompress(Iterator begin, Iterator end, const size_t estimated_original_size=0)
解压缩迭代器范围中的数据
static byte_vector compress(const string_view data, const int level=0)
压缩字符串视图
vector< byte_t > byte_vector
字节向量类型别名
unsigned char byte_t
字节类型,定义为无符号字符
NEFORCE_INLINE17 constexpr bool is_ranges_cot_iter_v
检查是否为范围连续迭代器
constexpr iter_difference_t< Iterator > distance(Iterator first, Iterator last)
计算两个迭代器之间的距离
typename iterator_traits< Iterator >::value_type iter_value_t
获取迭代器的值类型
memory_view< byte_t > byte_view
字节视图类型别名
memory_view< const byte_t > cbyte_view
常量字节视图类型别名
basic_string_view< char > string_view
字符字符串视图
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) begin(Container &cont) noexcept(noexcept(cont.begin()))
获取容器的起始迭代器
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) end(Container &cont) noexcept(noexcept(cont.end()))
获取容器的结束迭代器
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) size(const Container &cont) noexcept(noexcept(cont.size()))
获取容器的大小
NEFORCE_NODISCARD NEFORCE_ALWAYS_INLINE constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型