gzip_compressor in boost does not seem to save and load compressed streams properly

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>namespace io = boost::iostreams;
void saveCompressedData(const std::string& file_path, const std::vector<double>& bid, const std::vector<double>& ask, const std::vector<double>& close_prices, const std::vector<int>& tick_numbers) {
try {
// Create an output file stream and filtering stream
std::ofstream file(file_path, std::ios_base::out | std::ios_base::binary);
if (!file) {
std::cerr << "Error opening compressed file for writing: " << file_path << std::endl;
return;
}
io::filtering_ostream out;
out.push(io::gzip_compressor());
out.push(file);
// Write the data to the compressed file
uint64_t size = bid.size();
out.write((const char* ) (&size), sizeof(size));
out.write(reinterpret_cast<const char*>(bid.data()), size * sizeof(double));
out.write(reinterpret_cast<const char*>(ask.data()), size * sizeof(double));
size_t close_size = close_prices.size();
out.write(reinterpret_cast<const char*>(&close_size), sizeof(close_size));
out.write(reinterpret_cast<const char*>(close_prices.data()), close_size * sizeof(double));
size_t tick_size = tick_numbers.size();
out.write(reinterpret_cast<const char*>(&tick_size), sizeof(tick_size));
out.write(reinterpret_cast<const char*>(tick_numbers.data()), tick_size * sizeof(int));
out.pop();
}
catch (const std::exception& e) {
std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
}
}
void loadCompressedData(const std::string& file_path, std::vector<double>& bid, std::vector<double>& ask, std::vector<double>& close_prices, std::vector<int>& tick_numbers) {
try {
// Create an input file stream and filtering stream
std::ifstream file(file_path, std::ios_base::in | std::ios_base::binary);
if (!file) {
std::cerr << "Error opening compressed file for reading: " << file_path << std::endl;
return;
}
io::filtering_istream in;
in.push(io::gzip_decompressor());
in.push(file);
// Read the data from the compressed file
uint64_t size;
in.read((char*)(&size), sizeof(size));
bid.resize(size);
ask.resize(size);
in.read(reinterpret_cast<char*>(bid.data()), size * sizeof(double));
in.read(reinterpret_cast<char*>(ask.data()), size * sizeof(double));
size_t close_size;
in.read(reinterpret_cast<char*>(&close_size), sizeof(close_size));
close_prices.resize(close_size);
in.read(reinterpret_cast<char*>(close_prices.data()), close_size * sizeof(double));
size_t tick_size;
in.read(reinterpret_cast<char*>(&tick_size), sizeof(tick_size));
tick_numbers.resize(tick_size);
in.read(reinterpret_cast<char*>(tick_numbers.data()), tick_size * sizeof(int));
}
catch (const std::exception& e) {
std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
}
}
</code>
<code>namespace io = boost::iostreams; void saveCompressedData(const std::string& file_path, const std::vector<double>& bid, const std::vector<double>& ask, const std::vector<double>& close_prices, const std::vector<int>& tick_numbers) { try { // Create an output file stream and filtering stream std::ofstream file(file_path, std::ios_base::out | std::ios_base::binary); if (!file) { std::cerr << "Error opening compressed file for writing: " << file_path << std::endl; return; } io::filtering_ostream out; out.push(io::gzip_compressor()); out.push(file); // Write the data to the compressed file uint64_t size = bid.size(); out.write((const char* ) (&size), sizeof(size)); out.write(reinterpret_cast<const char*>(bid.data()), size * sizeof(double)); out.write(reinterpret_cast<const char*>(ask.data()), size * sizeof(double)); size_t close_size = close_prices.size(); out.write(reinterpret_cast<const char*>(&close_size), sizeof(close_size)); out.write(reinterpret_cast<const char*>(close_prices.data()), close_size * sizeof(double)); size_t tick_size = tick_numbers.size(); out.write(reinterpret_cast<const char*>(&tick_size), sizeof(tick_size)); out.write(reinterpret_cast<const char*>(tick_numbers.data()), tick_size * sizeof(int)); out.pop(); } catch (const std::exception& e) { std::cerr << "Exception while writing compressed data: " << e.what() << std::endl; } } void loadCompressedData(const std::string& file_path, std::vector<double>& bid, std::vector<double>& ask, std::vector<double>& close_prices, std::vector<int>& tick_numbers) { try { // Create an input file stream and filtering stream std::ifstream file(file_path, std::ios_base::in | std::ios_base::binary); if (!file) { std::cerr << "Error opening compressed file for reading: " << file_path << std::endl; return; } io::filtering_istream in; in.push(io::gzip_decompressor()); in.push(file); // Read the data from the compressed file uint64_t size; in.read((char*)(&size), sizeof(size)); bid.resize(size); ask.resize(size); in.read(reinterpret_cast<char*>(bid.data()), size * sizeof(double)); in.read(reinterpret_cast<char*>(ask.data()), size * sizeof(double)); size_t close_size; in.read(reinterpret_cast<char*>(&close_size), sizeof(close_size)); close_prices.resize(close_size); in.read(reinterpret_cast<char*>(close_prices.data()), close_size * sizeof(double)); size_t tick_size; in.read(reinterpret_cast<char*>(&tick_size), sizeof(tick_size)); tick_numbers.resize(tick_size); in.read(reinterpret_cast<char*>(tick_numbers.data()), tick_size * sizeof(int)); } catch (const std::exception& e) { std::cerr << "Exception while reading compressed data: " << e.what() << std::endl; } } </code>
namespace io = boost::iostreams;    

void saveCompressedData(const std::string& file_path, const std::vector<double>& bid, const std::vector<double>& ask, const std::vector<double>& close_prices, const std::vector<int>& tick_numbers) {
        try {
            // Create an output file stream and filtering stream
            std::ofstream file(file_path, std::ios_base::out | std::ios_base::binary);
            if (!file) {
                std::cerr << "Error opening compressed file for writing: " << file_path << std::endl;
                return;
            }
    
            io::filtering_ostream out;
            out.push(io::gzip_compressor());
            out.push(file);
    
            // Write the data to the compressed file
            uint64_t size = bid.size();
            out.write((const char* ) (&size), sizeof(size));
            out.write(reinterpret_cast<const char*>(bid.data()), size * sizeof(double));
            out.write(reinterpret_cast<const char*>(ask.data()), size * sizeof(double));
    
            size_t close_size = close_prices.size();
            out.write(reinterpret_cast<const char*>(&close_size), sizeof(close_size));
            out.write(reinterpret_cast<const char*>(close_prices.data()), close_size * sizeof(double));
    
            size_t tick_size = tick_numbers.size();
            out.write(reinterpret_cast<const char*>(&tick_size), sizeof(tick_size));
            out.write(reinterpret_cast<const char*>(tick_numbers.data()), tick_size * sizeof(int));
            out.pop();
    
        }
        catch (const std::exception& e) {
            std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
        }
    }
    
    void loadCompressedData(const std::string& file_path, std::vector<double>& bid, std::vector<double>& ask, std::vector<double>& close_prices, std::vector<int>& tick_numbers) {
        try {
            // Create an input file stream and filtering stream
            std::ifstream file(file_path, std::ios_base::in | std::ios_base::binary);
            if (!file) {
                std::cerr << "Error opening compressed file for reading: " << file_path << std::endl;
                return;
            }
    
            io::filtering_istream in;
            in.push(io::gzip_decompressor());
            in.push(file);
    
            // Read the data from the compressed file
            uint64_t size;
            in.read((char*)(&size), sizeof(size));
            bid.resize(size);
            ask.resize(size);
            in.read(reinterpret_cast<char*>(bid.data()), size * sizeof(double));
            in.read(reinterpret_cast<char*>(ask.data()), size * sizeof(double));
    
            size_t close_size;
            in.read(reinterpret_cast<char*>(&close_size), sizeof(close_size));
            close_prices.resize(close_size);
            in.read(reinterpret_cast<char*>(close_prices.data()), close_size * sizeof(double));
    
            size_t tick_size;
            in.read(reinterpret_cast<char*>(&tick_size), sizeof(tick_size));
            tick_numbers.resize(tick_size);
            in.read(reinterpret_cast<char*>(tick_numbers.data()), tick_size * sizeof(int));
    
        }
        catch (const std::exception& e) {
            std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
        }
    }

I tested this but uint64_t size = bid.size(); reads bad bytes.. what could be the reason?

2

I cannot reproduce that. I’d suggest generalizing the code so you can write it more straight-forwardly:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using Amount = double;
using Amounts = std::vector<Amount>;
using Tick = int;
using Ticks = std::vector<Tick>;
struct Data {
Amounts bid, ask, close_prices;
Ticks tick_numbers;
auto operator<=>(Data const&) const = default;
void save(std::ostream& os) const {
assert(ask.size() == bid.size());
WriteRaw( //
os, bid.size(), bid, ask, //
close_prices.size(), close_prices, //
tick_numbers.size(), tick_numbers //
); //
}
void load(std::istream& is) {
auto n = ReadVector(is, bid);
ReadVector(is, ask, n);
ReadVector(is, close_prices);
ReadVector(is, tick_numbers);
}
};
void saveCompressedData(std::string const& file_path, Data const& data) try {
std::ofstream file(file_path, std::ios::binary);
io::filtering_ostream out;
out.push(io::gzip_compressor{});
out.push(file);
data.save(out);
} catch (std::exception const& e) {
std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
}
void loadCompressedData(std::string const& file_path, Data& data) try {
std::ifstream file(file_path, std::ios::binary);
io::filtering_istream in;
in.push(io::gzip_decompressor{});
in.push(file);
data.load(in);
} catch (std::exception const& e) {
std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
}
</code>
<code>using Amount = double; using Amounts = std::vector<Amount>; using Tick = int; using Ticks = std::vector<Tick>; struct Data { Amounts bid, ask, close_prices; Ticks tick_numbers; auto operator<=>(Data const&) const = default; void save(std::ostream& os) const { assert(ask.size() == bid.size()); WriteRaw( // os, bid.size(), bid, ask, // close_prices.size(), close_prices, // tick_numbers.size(), tick_numbers // ); // } void load(std::istream& is) { auto n = ReadVector(is, bid); ReadVector(is, ask, n); ReadVector(is, close_prices); ReadVector(is, tick_numbers); } }; void saveCompressedData(std::string const& file_path, Data const& data) try { std::ofstream file(file_path, std::ios::binary); io::filtering_ostream out; out.push(io::gzip_compressor{}); out.push(file); data.save(out); } catch (std::exception const& e) { std::cerr << "Exception while writing compressed data: " << e.what() << std::endl; } void loadCompressedData(std::string const& file_path, Data& data) try { std::ifstream file(file_path, std::ios::binary); io::filtering_istream in; in.push(io::gzip_decompressor{}); in.push(file); data.load(in); } catch (std::exception const& e) { std::cerr << "Exception while reading compressed data: " << e.what() << std::endl; } </code>
using Amount  = double;
using Amounts = std::vector<Amount>;
using Tick    = int;
using Ticks   = std::vector<Tick>;

struct Data {
    Amounts bid, ask, close_prices;
    Ticks   tick_numbers;

    auto operator<=>(Data const&) const = default;

    void save(std::ostream& os) const {
        assert(ask.size() == bid.size());
        WriteRaw(                              //
            os, bid.size(), bid, ask,          //
            close_prices.size(), close_prices, //
            tick_numbers.size(), tick_numbers  //
        );                                     //
    }

    void load(std::istream& is) {
        auto n = ReadVector(is, bid);
        ReadVector(is, ask, n);
        ReadVector(is, close_prices);
        ReadVector(is, tick_numbers);
    }
};

void saveCompressedData(std::string const& file_path, Data const& data) try {
    std::ofstream         file(file_path, std::ios::binary);
    io::filtering_ostream out;
    out.push(io::gzip_compressor{});
    out.push(file);

    data.save(out);
} catch (std::exception const& e) {
    std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
}

void loadCompressedData(std::string const& file_path, Data& data) try {
    std::ifstream         file(file_path, std::ios::binary);
    io::filtering_istream in;
    in.push(io::gzip_decompressor{});
    in.push(file);

    data.load(in);
} catch (std::exception const& e) {
    std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
}

The read/write helpers using spans of bytes to avoid programming errors.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>namespace /* filestatic */ {
void WriteRaw(std::ostream& os, std::span<std::byte const> v);
void ReadRaw(std::istream& is, std::span<std::byte> v);
// write helpers
template <typename T> void WriteRaw(std::ostream& os, std::vector<T> const& v);
template <typename T> void WriteRaw(std::ostream& os, T const& v);
template <typename... T> void WriteRaw(std::ostream& os, T const&... vs);
// read helpers
template <typename T> void ReadRaw(std::istream& is, T& v);
template <typename... T> void ReadRaw(std::istream& is, T&... vs);
template <typename T> void ReadVector(std::istream& is, std::vector<T>& v, size_t size);
template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v);
} // namespace
</code>
<code>namespace /* filestatic */ { void WriteRaw(std::ostream& os, std::span<std::byte const> v); void ReadRaw(std::istream& is, std::span<std::byte> v); // write helpers template <typename T> void WriteRaw(std::ostream& os, std::vector<T> const& v); template <typename T> void WriteRaw(std::ostream& os, T const& v); template <typename... T> void WriteRaw(std::ostream& os, T const&... vs); // read helpers template <typename T> void ReadRaw(std::istream& is, T& v); template <typename... T> void ReadRaw(std::istream& is, T&... vs); template <typename T> void ReadVector(std::istream& is, std::vector<T>& v, size_t size); template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v); } // namespace </code>
namespace /* filestatic */ {
    void WriteRaw(std::ostream& os, std::span<std::byte const> v);
    void ReadRaw(std::istream& is, std::span<std::byte> v);

    // write helpers
    template <typename T> void    WriteRaw(std::ostream& os, std::vector<T> const& v);
    template <typename T> void    WriteRaw(std::ostream& os, T const& v);
    template <typename... T> void WriteRaw(std::ostream& os, T const&... vs);

    // read helpers
    template <typename T> void    ReadRaw(std::istream& is, T& v);
    template <typename... T> void ReadRaw(std::istream& is, T&... vs);

    template <typename T> void   ReadVector(std::istream& is, std::vector<T>& v, size_t size);
    template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v);
} // namespace

The implementations should also make sure that the element values are bitwise copyable.

Full Demo

Live On Coliru

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <fstream>
#include <iostream>
#include <span>
namespace io = boost::iostreams;
namespace /* filestatic */ {
void WriteRaw(std::ostream& os, std::span<std::byte const> v) {
os.write(reinterpret_cast<char const*>(v.data()), v.size());
}
void ReadRaw(std::istream& is, std::span<std::byte> v) {
is.read(reinterpret_cast<char*>(v.data()), v.size());
}
// write helpers
template <typename T> void WriteRaw(std::ostream& os, std::vector<T> const& v) {
static_assert(std::is_trivially_copyable_v<T>);
WriteRaw(os, std::as_bytes(std::span(v)));
}
template <typename T> void WriteRaw(std::ostream& os, T const& v) {
static_assert(std::is_trivially_copyable_v<T>);
WriteRaw(os, std::as_bytes(std::span(&v, 1)));
}
template <typename... T> void WriteRaw(std::ostream& os, T const&... vs) {
(WriteRaw(os, vs), ...);
}
// read helpers
template <typename T> void ReadRaw(std::istream& is, T& v) {
static_assert(std::is_trivially_copyable_v<T>);
ReadRaw(is, std::as_writable_bytes(std::span(&v, 1)));
}
template <typename... T> void ReadRaw(std::istream& is, T&... vs) {
(ReadRaw(is, vs), ...);
}
template <typename T> void ReadVector(std::istream& is, std::vector<T>& v, size_t size) {
static_assert(std::is_trivially_copyable_v<T>);
v.resize(size);
ReadRaw(is, std::as_writable_bytes(std::span(v)));
}
template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v) {
static_assert(std::is_trivially_copyable_v<T>);
size_t tmpsize;
ReadRaw(is, tmpsize);
ReadVector(is, v, tmpsize);
return tmpsize;
}
}
using Amount = double;
using Amounts = std::vector<Amount>;
using Tick = int;
using Ticks = std::vector<Tick>;
struct Data {
Amounts bid, ask, close_prices;
Ticks tick_numbers;
auto operator<=>(Data const&) const = default;
void save(std::ostream& os) const {
assert(ask.size() == bid.size());
WriteRaw( //
os, bid.size(), bid, ask, //
close_prices.size(), close_prices, //
tick_numbers.size(), tick_numbers //
); //
}
void load(std::istream& is) {
auto n = ReadVector(is, bid);
ReadVector(is, ask, n);
ReadVector(is, close_prices);
ReadVector(is, tick_numbers);
}
};
void saveCompressedData(std::string const& file_path, Data const& data) try {
std::ofstream file(file_path, std::ios::binary);
io::filtering_ostream out;
out.push(io::gzip_compressor{});
out.push(file);
data.save(out);
} catch (std::exception const& e) {
std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
}
void loadCompressedData(std::string const& file_path, Data& data) try {
std::ifstream file(file_path, std::ios::binary);
io::filtering_istream in;
in.push(io::gzip_decompressor{});
in.push(file);
data.load(in);
} catch (std::exception const& e) {
std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
}
#include <fmt/ranges.h>
int main() {
using L = std::numeric_limits<Amount>;
for (Data const& data : {
Data{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}, {10, 11, 12}},
Data{{1.1111e11, 2.2222e11, 3.3333e11, 4.4444e11},
{5.5555e11, 6.6666e11, 7.7777e11, 8.8888e11},
{7.7777e11},
{10, 11, 12, 13, 14}},
Data{{}, {}, {0.10, L::infinity(), -L::infinity(), 10.2, 10.3, 10.4}, {}},
Data{},
// nan is always "not equal" to itself, but also roundtrips
Data{{}, {}, {L::quiet_NaN()}, {}},
}) {
saveCompressedData("test.gz", data);
Data roundtrip;
loadCompressedData("test.gz", roundtrip);
auto& [bid, ask, close_prices, tick_numbers] = roundtrip;
fmt::print ("bid: {}nask: {}nclose_prices: {}ntick_numbers: {}n", bid, ask, close_prices, tick_numbers);
std::cout << "Roundtrip: " << (data == roundtrip ? "OK" : "FAIL") << "n---------------n" << std::endl;
}
}
</code>
<code>#include <boost/iostreams/filter/gzip.hpp> #include <boost/iostreams/filtering_stream.hpp> #include <fstream> #include <iostream> #include <span> namespace io = boost::iostreams; namespace /* filestatic */ { void WriteRaw(std::ostream& os, std::span<std::byte const> v) { os.write(reinterpret_cast<char const*>(v.data()), v.size()); } void ReadRaw(std::istream& is, std::span<std::byte> v) { is.read(reinterpret_cast<char*>(v.data()), v.size()); } // write helpers template <typename T> void WriteRaw(std::ostream& os, std::vector<T> const& v) { static_assert(std::is_trivially_copyable_v<T>); WriteRaw(os, std::as_bytes(std::span(v))); } template <typename T> void WriteRaw(std::ostream& os, T const& v) { static_assert(std::is_trivially_copyable_v<T>); WriteRaw(os, std::as_bytes(std::span(&v, 1))); } template <typename... T> void WriteRaw(std::ostream& os, T const&... vs) { (WriteRaw(os, vs), ...); } // read helpers template <typename T> void ReadRaw(std::istream& is, T& v) { static_assert(std::is_trivially_copyable_v<T>); ReadRaw(is, std::as_writable_bytes(std::span(&v, 1))); } template <typename... T> void ReadRaw(std::istream& is, T&... vs) { (ReadRaw(is, vs), ...); } template <typename T> void ReadVector(std::istream& is, std::vector<T>& v, size_t size) { static_assert(std::is_trivially_copyable_v<T>); v.resize(size); ReadRaw(is, std::as_writable_bytes(std::span(v))); } template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v) { static_assert(std::is_trivially_copyable_v<T>); size_t tmpsize; ReadRaw(is, tmpsize); ReadVector(is, v, tmpsize); return tmpsize; } } using Amount = double; using Amounts = std::vector<Amount>; using Tick = int; using Ticks = std::vector<Tick>; struct Data { Amounts bid, ask, close_prices; Ticks tick_numbers; auto operator<=>(Data const&) const = default; void save(std::ostream& os) const { assert(ask.size() == bid.size()); WriteRaw( // os, bid.size(), bid, ask, // close_prices.size(), close_prices, // tick_numbers.size(), tick_numbers // ); // } void load(std::istream& is) { auto n = ReadVector(is, bid); ReadVector(is, ask, n); ReadVector(is, close_prices); ReadVector(is, tick_numbers); } }; void saveCompressedData(std::string const& file_path, Data const& data) try { std::ofstream file(file_path, std::ios::binary); io::filtering_ostream out; out.push(io::gzip_compressor{}); out.push(file); data.save(out); } catch (std::exception const& e) { std::cerr << "Exception while writing compressed data: " << e.what() << std::endl; } void loadCompressedData(std::string const& file_path, Data& data) try { std::ifstream file(file_path, std::ios::binary); io::filtering_istream in; in.push(io::gzip_decompressor{}); in.push(file); data.load(in); } catch (std::exception const& e) { std::cerr << "Exception while reading compressed data: " << e.what() << std::endl; } #include <fmt/ranges.h> int main() { using L = std::numeric_limits<Amount>; for (Data const& data : { Data{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}, {10, 11, 12}}, Data{{1.1111e11, 2.2222e11, 3.3333e11, 4.4444e11}, {5.5555e11, 6.6666e11, 7.7777e11, 8.8888e11}, {7.7777e11}, {10, 11, 12, 13, 14}}, Data{{}, {}, {0.10, L::infinity(), -L::infinity(), 10.2, 10.3, 10.4}, {}}, Data{}, // nan is always "not equal" to itself, but also roundtrips Data{{}, {}, {L::quiet_NaN()}, {}}, }) { saveCompressedData("test.gz", data); Data roundtrip; loadCompressedData("test.gz", roundtrip); auto& [bid, ask, close_prices, tick_numbers] = roundtrip; fmt::print ("bid: {}nask: {}nclose_prices: {}ntick_numbers: {}n", bid, ask, close_prices, tick_numbers); std::cout << "Roundtrip: " << (data == roundtrip ? "OK" : "FAIL") << "n---------------n" << std::endl; } } </code>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <fstream>
#include <iostream>
#include <span>

namespace io = boost::iostreams;

namespace /* filestatic */ {
    void WriteRaw(std::ostream& os, std::span<std::byte const> v) {
        os.write(reinterpret_cast<char const*>(v.data()), v.size());
    }
    void ReadRaw(std::istream& is, std::span<std::byte> v) {
        is.read(reinterpret_cast<char*>(v.data()), v.size());
    }

    // write helpers
    template <typename T> void WriteRaw(std::ostream& os, std::vector<T> const& v) {
        static_assert(std::is_trivially_copyable_v<T>);
        WriteRaw(os, std::as_bytes(std::span(v)));
    }
    template <typename T> void WriteRaw(std::ostream& os, T const& v) {
        static_assert(std::is_trivially_copyable_v<T>);
        WriteRaw(os, std::as_bytes(std::span(&v, 1)));
    }
    template <typename... T> void WriteRaw(std::ostream& os, T const&... vs) {
        (WriteRaw(os, vs), ...);
    }

    // read helpers
    template <typename T> void ReadRaw(std::istream& is, T& v) {
        static_assert(std::is_trivially_copyable_v<T>);
        ReadRaw(is, std::as_writable_bytes(std::span(&v, 1)));
    }
    template <typename... T> void ReadRaw(std::istream& is, T&... vs) {
        (ReadRaw(is, vs), ...);
    }

    template <typename T> void ReadVector(std::istream& is, std::vector<T>& v, size_t size) {
        static_assert(std::is_trivially_copyable_v<T>);
        v.resize(size);
        ReadRaw(is, std::as_writable_bytes(std::span(v)));
    }
    template <typename T> size_t ReadVector(std::istream& is, std::vector<T>& v) {
        static_assert(std::is_trivially_copyable_v<T>);
        size_t tmpsize;
        ReadRaw(is, tmpsize);
        ReadVector(is, v, tmpsize);
        return tmpsize;
    }
}

using Amount  = double;
using Amounts = std::vector<Amount>;
using Tick    = int;
using Ticks   = std::vector<Tick>;

struct Data {
    Amounts bid, ask, close_prices;
    Ticks   tick_numbers;

    auto operator<=>(Data const&) const = default;

    void save(std::ostream& os) const {
        assert(ask.size() == bid.size());
        WriteRaw(                              //
            os, bid.size(), bid, ask,          //
            close_prices.size(), close_prices, //
            tick_numbers.size(), tick_numbers  //
        );                                     //
    }

    void load(std::istream& is) {
        auto n = ReadVector(is, bid);
        ReadVector(is, ask, n);
        ReadVector(is, close_prices);
        ReadVector(is, tick_numbers);
    }
};

void saveCompressedData(std::string const& file_path, Data const& data) try {
    std::ofstream         file(file_path, std::ios::binary);
    io::filtering_ostream out;
    out.push(io::gzip_compressor{});
    out.push(file);

    data.save(out);
} catch (std::exception const& e) {
    std::cerr << "Exception while writing compressed data: " << e.what() << std::endl;
}

void loadCompressedData(std::string const& file_path, Data& data) try {
    std::ifstream         file(file_path, std::ios::binary);
    io::filtering_istream in;
    in.push(io::gzip_decompressor{});
    in.push(file);

    data.load(in);
} catch (std::exception const& e) {
    std::cerr << "Exception while reading compressed data: " << e.what() << std::endl;
}

#include <fmt/ranges.h>
int main() {
    using L = std::numeric_limits<Amount>;
    for (Data const& data : {
             Data{{1.0, 2.0, 3.0}, {4.0, 5.0, 6.0}, {7.0, 8.0, 9.0}, {10, 11, 12}},
             Data{{1.1111e11, 2.2222e11, 3.3333e11, 4.4444e11},
                  {5.5555e11, 6.6666e11, 7.7777e11, 8.8888e11},
                  {7.7777e11},
                  {10, 11, 12, 13, 14}},
             Data{{}, {}, {0.10, L::infinity(), -L::infinity(), 10.2, 10.3, 10.4}, {}},
             Data{},
             // nan is always "not equal" to itself, but also roundtrips
             Data{{}, {}, {L::quiet_NaN()}, {}},
         }) {
        saveCompressedData("test.gz", data);

        Data roundtrip;
        loadCompressedData("test.gz", roundtrip);

        auto& [bid, ask, close_prices, tick_numbers] = roundtrip;
        fmt::print ("bid: {}nask: {}nclose_prices: {}ntick_numbers: {}n", bid, ask, close_prices, tick_numbers);
        std::cout << "Roundtrip: " << (data == roundtrip ? "OK" : "FAIL") << "n---------------n" << std::endl;
    }
}

Printing

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>bid: [1, 2, 3]
ask: [4, 5, 6]
close_prices: [7, 8, 9]
tick_numbers: [10, 11, 12]
Roundtrip: OK
---------------
bid: [111110000000, 222220000000, 333330000000, 444440000000]
ask: [555550000000, 666660000000, 777770000000, 888880000000]
close_prices: [777770000000]
tick_numbers: [10, 11, 12, 13, 14]
Roundtrip: OK
---------------
bid: []
ask: []
close_prices: [0.1, inf, -inf, 10.2, 10.3, 10.4]
tick_numbers: []
Roundtrip: OK
---------------
bid: []
ask: []
close_prices: []
tick_numbers: []
Roundtrip: OK
---------------
bid: []
ask: []
close_prices: [nan]
tick_numbers: []
Roundtrip: FAIL
---------------
</code>
<code>bid: [1, 2, 3] ask: [4, 5, 6] close_prices: [7, 8, 9] tick_numbers: [10, 11, 12] Roundtrip: OK --------------- bid: [111110000000, 222220000000, 333330000000, 444440000000] ask: [555550000000, 666660000000, 777770000000, 888880000000] close_prices: [777770000000] tick_numbers: [10, 11, 12, 13, 14] Roundtrip: OK --------------- bid: [] ask: [] close_prices: [0.1, inf, -inf, 10.2, 10.3, 10.4] tick_numbers: [] Roundtrip: OK --------------- bid: [] ask: [] close_prices: [] tick_numbers: [] Roundtrip: OK --------------- bid: [] ask: [] close_prices: [nan] tick_numbers: [] Roundtrip: FAIL --------------- </code>
bid: [1, 2, 3]
ask: [4, 5, 6]
close_prices: [7, 8, 9]
tick_numbers: [10, 11, 12]
Roundtrip: OK
---------------

bid: [111110000000, 222220000000, 333330000000, 444440000000]
ask: [555550000000, 666660000000, 777770000000, 888880000000]
close_prices: [777770000000]
tick_numbers: [10, 11, 12, 13, 14]
Roundtrip: OK
---------------

bid: []
ask: []
close_prices: [0.1, inf, -inf, 10.2, 10.3, 10.4]
tick_numbers: []
Roundtrip: OK
---------------

bid: []
ask: []
close_prices: []
tick_numbers: []
Roundtrip: OK
---------------

bid: []
ask: []
close_prices: [nan]
tick_numbers: []
Roundtrip: FAIL
---------------

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật