简体   繁体   English

通过boost :: iostreams压缩的字符串长度

[英]compressed length of a string by boost::iostreams

I have a string (of some fixed length), which I need to compress and then compare the compressed lengths (as a proxy for redundancy in the data or as a rough approximation to the Kolmogorov complexity). 我有一个字符串(长度固定),需要压缩然后比较压缩后的长度(作为数据冗余的代理或作为Kolmogorov复杂度的近似值)。 Currently, I am using boost::iostreams for compression, which seems working well. 目前,我正在使用boost :: iostreams进行压缩,这似乎效果很好。 However, I don't know how to obtain the size of the compressed data. 但是,我不知道如何获取压缩数据的大小。 Can someone help, please? 有人可以帮忙吗?

The code snippet is 该代码段是

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/filesystem.hpp>
#include <string>
#include <sstream>

namespace io = boost::iostreams;

int main() {

  std::string memblock;

  std::cout << "Input the string to be compressed:";
  std::cin >> memblock;

  std::cout << memblock << std::endl;

  io::filtering_ostream out;
  out.push(io::gzip_compressor());
  out.push(io::file_descriptor_sink("test.gz"));
  out.write (memblock.c_str(), memblock.size());

  std::cout << out.size() << std::endl;

  return 0;

}

You can try adding boost::iostreams::counter to you chain between the compressor and sink and then calling it's characters() member to get number of bytes that went through it. 您可以尝试将boost::iostreams::counter添加到压缩器和接收器之间的链上,然后调用它的characters()成员来获取经过它的字节数。

This works for me: 这对我有用:

#include <boost/iostreams/filter/counter.hpp>

... ...

io::filtering_ostream out;
out.push(io::counter());
out.push(io::gzip_compressor());
out.push(io::counter());
out.push(io::file_descriptor_sink("test.gz"));
out.write (memblock.c_str(), memblock.size());
io::close(out); // Needed for flushing the data from compressor

std::cout << "Wrote " << out.component<io::counter>(0)->characters() << " bytes to compressor, "
    << "got " << out.component<io::counter>(2)->characters() << " bytes out of it." << std::endl;

I figured out yet another (and slightly slicker) way to achieve the compressed length of a string. 我想出了另一种(略微闪烁的)方式来实现字符串的压缩长度。 I thought sharing it here, but basically it is simply passing the uncompressed string to a filtered buffer and copying the output back to a string: 我以为在这里共享它,但基本上,它只是将未压缩的字符串传递给过滤的缓冲区,然后将输出复制回字符串:

template<typename T>
inline std::string compressIt(std::vector<T> s){

    std::stringstream uncompressed, compressed;
    for (typename std::vector<T>::iterator it = s.begin();
         it != s.end(); it++)
        uncompressed << *it;

    io::filtering_streambuf<io::input> o;
    o.push(io::gzip_compressor());
    o.push(uncompressed);
    io::copy(o, compressed);

    return compressed.str();
}

Later one can easily get the size of the compressed string as 稍后可以轻松获得压缩字符串的大小,如下所示

compressIt(uncompressedString).size()

I feel it is better for it does not required me to create an output file as previously. 我感觉更好,因为它不需要像以前那样创建输出文件。

cheers, Nikhil 干杯,尼基尔

one other way would be 另一种方式是

stream<array_source> input_stream(input_data,input_data_ize);
stream<array_sink> compressed_stream(compressed_data,alloc_compressed_size);  
filtering_istreambuf out;
out.push(gzip_compressor());
out.push(input_stream);
int compressed_size = copy(out,compressed_stream);
cout << "size of compressed_stream" << compressed_size << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM