高效的将对象保存到二进制文件的方法

5
我有一个类,基本上由向量矩阵组成:vector< MyFeatVector<T> > m_vCells,其中外部向量表示矩阵。然后该矩阵中的每个元素都是一个vector(我扩展了stl vector类并将其命名为MyFeatVector<T>)。
我正在尝试编写一种有效的方法来将此类对象存储在二进制文件中。到目前为止,我需要三个嵌套循环: foutput.write( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) ); 其中this->at(dy,dx,dz)检索位于[dy,dx]位置的向量的dz元素。
是否有任何可能在不使用循环的情况下存储m_vCells私有成员?我尝试了以下内容:foutput.write(reinterpret_cast<char*>(&(this->m_vCells[0])), (this->m_vCells.size())*sizeof(CFeatureVector<T>));,但似乎不能正常工作。我们可以假设该矩阵中的所有向量具有相同的大小,尽管也欢迎更一般的解决方案 :-)
此外,根据我的嵌套循环实现,将此类对象存储在二进制文件中似乎需要比将相同对象存储在纯文本文件中使用更多的物理空间。这有点奇怪。
我试图遵循http://forum.allaboutcircuits.com/showthread.php?t=16465下的建议,但无法得出适当的解决方案。
谢谢!
以下是我的serializationunserialization方法的简化示例。
template < typename T >
bool MyFeatMatrix<T>::writeBinary( const string & ofile ){

    ofstream foutput(ofile.c_str(), ios::out|ios::binary);
    foutput.write(reinterpret_cast<char*>(&this->m_nHeight), sizeof(int));
    foutput.write(reinterpret_cast<char*>(&this->m_nWidth), sizeof(int));
    foutput.write(reinterpret_cast<char*>(&this->m_nDepth), sizeof(int));

    //foutput.write(reinterpret_cast<char*>(&(this->m_vCells[0])), nSze*sizeof(CFeatureVector<T>));
    for(register int dy=0; dy < this->m_nHeight; dy++){
       for(register int dx=0; dx < this->m_nWidth; dx++){
          for(register int dz=0; dz < this->m_nDepth; dz++){
              foutput.write( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) );
          }
       }
    }

    foutput.close();
    return true;
}

template < typename T >
bool MyFeatMatrix<T>::readBinary( const string & ifile ){

    ifstream finput(ifile.c_str(), ios::in|ios::binary);

    int nHeight, nWidth, nDepth;
    finput.read(reinterpret_cast<char*>(&nHeight), sizeof(int));
    finput.read(reinterpret_cast<char*>(&nWidth), sizeof(int));
    finput.read(reinterpret_cast<char*>(&nDepth), sizeof(int));

    this->resize(nHeight, nWidth, nDepth);

    for(register int dy=0; dy < this->m_nHeight; dy++){
        for(register int dx=0; dx < this->m_nWidth; dx++){
            for(register int dz=0; dz < this->m_nDepth; dz++){
                finput.read( reinterpret_cast<char*>( &(this->at(dy,dx,dz)) ), sizeof(T) );
            }
        }
    }
    finput.close();
    return true;
}


为什么不编写自己的矩阵类,将数据内部保存在一维数组中(使用std::array),这样你就可以直接输出了呢? - Kerrek SB
1
子类化STL容器可能不是一个好主意。它们并不是为了子类化而设计的。请注意,例如std::vector没有虚析构函数。 - R. Martinho Fernandes
你可能会对Boost.Serialization感兴趣。 - R. Martinho Fernandes
@Kerrek SB,好的建议但是我需要考虑到私有成员。 @Martinho Fernandes,我不熟悉子类化,而且由于某些原因我也不能使用boost。你有其它好的网址推荐吗? - Peter
@Peter:我不明白你指的是哪些私有成员。除了矩阵数据之外,你需要存储任何数据吗?无论如何,只需将矩阵数据存储在一维数组中,并使用适当的步幅访问它。 - Kerrek SB
显示剩余3条评论
4个回答

3
一种最有效的方法是将对象存储到数组(或连续空间)中,然后将缓冲区直接写入文件。这样做的优点是磁盘盘片不必浪费时间启动并且写入可以在连续位置而非随机位置执行。
如果这是您的性能瓶颈,您可能需要考虑使用多个线程,其中一个额外的线程来处理输出。将对象转储到缓冲区,设置一个标志,然后写入线程将处理输出,使您的主要任务能够执行更重要的任务。 编辑1:序列化示例
以下代码未编译,仅用于说明目的。
#include <fstream>
#include <algorithm>

using std::ofstream;
using std::fill;

class binary_stream_interface
{
    virtual void    load_from_buffer(const unsigned char *& buf_ptr) = 0;
    virtual size_t  size_on_stream(void) const = 0;
    virtual void    store_to_buffer(unsigned char *& buf_ptr) const = 0;
};

struct Pet
    : public binary_stream_interface,
    max_name_length(32)
{
    std::string     name;
    unsigned int    age;
    const unsigned int  max_name_length;

    void    load_from_buffer(const unsigned char *& buf_ptr)
        {
            age = *((unsigned int *) buf_ptr);
            buf_ptr += sizeof(unsigned int);
            name = std::string((char *) buf_ptr);
            buf_ptr += max_name_length;
            return;
        }
    size_t  size_on_stream(void) const
    {
        return sizeof(unsigned int) + max_name_length;
    }
    void    store_to_buffer(unsigned char *& buf_ptr) const
    {
        *((unsigned int *) buf_ptr) = age;
        buf_ptr += sizeof(unsigned int);
        std::fill(buf_ptr, 0, max_name_length);
        strncpy((char *) buf_ptr, name.c_str(), max_name_length);
        buf_ptr += max_name_length;
        return;
    }
};


int main(void)
{
    Pet dog;
    dog.name = "Fido";
    dog.age = 5;
    ofstream    data_file("pet_data.bin", std::ios::binary);

    // Determine size of buffer
    size_t  buffer_size = dog.size_on_stream();

    // Allocate the buffer
    unsigned char * buffer = new unsigned char [buffer_size];
    unsigned char * buf_ptr = buffer;

    // Write / store the object into the buffer.
    dog.store_to_buffer(buf_ptr);

    // Write the buffer to the file / stream.
    data_file.write((char *) buffer, buffer_size);

    data_file.close();
    delete [] buffer;
    return 0;
}

编辑2: 拥有字符串向量的类

class Many_Strings
    : public binary_stream_interface
{
    enum {MAX_STRING_SIZE = 32};

    size_t    size_on_stream(void) const
    {
        return m_string_container.size() * MAX_STRING_SIZE  // Total size of strings.
               + sizeof(size_t); // with room for the quantity variable.
    }

    void      store_to_buffer(unsigned char *& buf_ptr) const
    {
        // Treat the vector<string> as a variable length field.
        // Store the quantity of strings into the buffer,
        //     followed by the content.
        size_t string_quantity = m_string_container.size();
        *((size_t *) buf_ptr) = string_quantity;
        buf_ptr += sizeof(size_t);

        for (size_t i = 0; i < string_quantity; ++i)
        {
            // Each string is a fixed length field.
            // Pad with '\0' first, then copy the data.
            std::fill((char *)buf_ptr, 0, MAX_STRING_SIZE);
            strncpy(buf_ptr, m_string_container[i].c_str(), MAX_STRING_SIZE);
            buf_ptr += MAX_STRING_SIZE;
        }
    }
    void load_from_buffer(const unsigned char *& buf_ptr)
    {
        // The actual coding is left as an exercise for the reader.
        // Psuedo code:
        //     Clear / empty the string container.
        //     load the quantity variable.
        //     increment the buffer variable by the size of the quantity variable.
        //     for each new string (up to the quantity just read)
        //        load a temporary string from the buffer via buffer pointer.
        //        push the temporary string into the vector
        //        increment the buffer pointer by the MAX_STRING_SIZE.
        //      end-for
     }
     std::vector<std::string> m_string_container;
};

所以我可以创建一个临时字符数组,首先将所有向量的所有元素都倒入其中。然后,使用一次write函数调用将该数组存储到文件中。这样正确吗? - Peter
@Peter:是的,你说得对。我在数组中使用了“unsigned char”。我还创建了一个接口,用于将数据存储到缓冲区、从缓冲区加载数据并返回缓冲区中的大小。这样可以轻松地将对象连接成一个大缓冲区。大小函数有助于确定所需的缓冲区大小。 - Thomas Matthews
谢谢!我理解了,那您可能有一些示例代码吗? - Peter
我添加了一个序列化接口的示例。该接口允许嵌套对象和多个对象。首先使用 size_on_stream 确定缓冲区的大小。其次,使用结果值分配缓冲区。第三,将对象存储到缓冲区中。最后,将缓冲区写入流中。 - Thomas Matthews
谢谢您的说明。至于size_on_stream()部分,您使用了max_name_length变量,该变量手动设置为32,我猜这是您的结构体大小,对吗?因此,我们是否应该考虑像sizeof(myClass)这样的内容来避免硬编码这个变量呢? - Peter
显示剩余5条评论

2
我建议您阅读C++序列化FAQ,然后选择最适合您的内容。
当您使用结构体和类时,您需要注意两件事:
- 类中的指针 - 填充字节
这两者都可能导致输出结果不正确。在我看来,对象必须实现序列化和反序列化,因为对象可以很好地了解结构、指针数据等。因此,它可以决定哪种格式可以高效地实现。
您无论如何都必须进行迭代或包装。一旦您完成了序列化和反序列化函数的实现(可以使用运算符或函数编写),特别是当您使用流对象、重载<<和>>运算符时,传递对象会更容易。
关于您关于使用vector底层指针的问题,如果只有一个vector,那么可能会起作用。但是在其他情况下并不是一个好主意。
根据问题更新进行更新。
在重写STL成员之前,有几件事情需要注意。它们并不是很适合继承,因为它没有任何虚析构函数。如果您使用基本数据类型和POD(Plain Old Data)结构,就不会有太多问题。但是,如果您以真正面向对象的方式使用它,可能会遇到一些不愉快的行为。
关于您的代码:
  • 为什么要将其强制转换为char*?
  • 您序列化对象的方式是您的选择。在我看来,您所做的是在序列化的名义下进行的基本文件写入操作。
  • 序列化取决于对象。即您模板类中的参数“T”。如果您使用POD或基本类型,则无需特殊同步。否则,您必须谨慎选择写入对象的方式。
  • 选择文本格式或二进制格式是您的选择。文本格式始终具有成本,同时比二进制格式更容易操作。
例如,以下代码用于简单的读写操作(以文本格式)。
fstream fr("test.txt", ios_base::out | ios_base::binary );
for( int i =0;i <_countof(arr);i++)
    fr << arr[i] << ' ';

fr.close();

fstream fw("test.txt", ios_base::in| ios_base::binary);

int j = 0;
while( fw.eof() || j < _countof(arrout))
{
    fw >> arrout[j++];
}

我已经实现了自己的“序列化”和“反序列化”方法。我编辑了原始帖子并添加了代码。您认为这是建议的方式吗? - Peter
谢谢。我一直认为在处理二进制文件时需要使用read()write()函数而不是>><<运算符。这样做是否有效?此外,关于STL成员,这意味着只要我选择组合而不是继承就可以了,对吗?我没有覆盖那些类,但也在内部使用它们现有的方法。在继承中使用虚析构函数的情况下,如果我在析构函数中使用STL成员的clear()方法,这样做应该没问题,对吗? - Peter

0
首先,你看过Boost.multi_array了吗?总是好的选择使用现成的东西,而不是重新发明轮子。
话虽如此,我不确定这是否有帮助,但以下是我实现基本数据结构的方式,而且序列化也相对容易:
#include <array>

template <typename T, size_t DIM1, size_t DIM2, size_t DIM3>
class ThreeDArray
{
  typedef std::array<T, DIM1 * DIM2 * DIM3> array_t;
  array_t m_data;

public:

  inline size_t size() const { return data.size(); }
  inline size_t byte_size() const  { return sizeof(T) * data.size(); }

  inline T & operator()(size_t i, size_t j, size_t k)
  {
     return m_data[i + j * DIM1 + k * DIM1 * DIM2];
  }

  inline const T & operator()(size_t i, size_t j, size_t k) const
  {
     return m_data[i + j * DIM1 + k * DIM1 * DIM2];
  }

  inline const T * data() const { return m_data.data(); }
};

您可以直接对数据缓冲区进行序列化:

ThreeDArray<int, 4, 6 11> arr;
/* ... */
std::ofstream outfile("file.bin");
outfile.write(reinterpret_cast<char*>(arr.data()), arr.byte_size());

非常感谢!顺便问一下,如何通过重载operator()来以某种方式检索位置(i,j)处的子数组?例如,std::array<T> & operator()(size_t i, size_t j)。是否也可以完全从原始数组中删除位置(i,j)处的子数组呢? - Peter
@Peter:什么是“子数组”?选择三维空间中的二维子集有很多种方式,你想要哪一种?一般的答案是,在(i,j,k)上使用现有的运算符,并适当地固定其中一个值... - Kerrek SB
一个子数组可以被理解为R^DIM3空间中的向量。这个想法是,给定一个对应于(行,列)的坐标对(i,j),我想检索跨越深度(DIM3)的所有值。 - Peter
感谢您对Boost.multi_array的建议。至于您关于“返回向量”的问题,我需要既有副本又有只读视图。有什么建议吗? - Peter
@Peter:如果要返回一个副本,可以定义一个一维向量的类,并将相关数据复制到其中。 - Kerrek SB
显示剩余3条评论

0

对我来说,生成包含向量的二进制文件最直接的方法似乎是将其映射到内存中并将其放置在映射区域中。正如sarat所指出的那样,您需要关注类中指针的使用方式。但是,Boost-Interprocess库提供了一个教程,介绍如何使用它们的共享内存区域(包括内存映射文件)来完成这项任务。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接