http://stxxl.sourceforge.net
<dementiev@ira.uka.de>
http://www.boost.org/LICENSE_1_0.txt
#include <stxxl/io>
#include <stxxl/vector>
#include <stxxl/bits/mng/buf_ostream.h>
template <class VectorType>
class write_vector
{
typedef VectorType vector_type;
typedef typename vector_type::size_type size_type;
typedef typename vector_type::value_type value_type;
typedef typename vector_type::block_type block_type;
typedef typename vector_type::iterator ExtIterator;
typedef typename vector_type::const_iterator ConstExtIterator;
typedef stxxl::buf_ostream<block_type, typename ExtIterator::bids_container_iterator> buf_ostream_type;
vector_type & Vec;
size_type RealSize;
unsigned nbuffers;
buf_ostream_type * outstream;
public:
write_vector(vector_type & Vec_,
unsigned nbuffers_
) : Vec(Vec_), RealSize(0), nbuffers(nbuffers_)
{
assert(Vec.empty());
Vec.resize(2 * block_type::size);
outstream = new buf_ostream_type(Vec.begin().bid(), nbuffers);
}
void push_back(const value_type & val)
{
++RealSize;
if (Vec.size() < RealSize)
{
delete outstream;
Vec.resize(2 * Vec.size());
outstream = new buf_ostream_type((Vec.begin() + RealSize - 1).bid(), nbuffers);
}
ExtIterator it = Vec.begin() + RealSize - 1;
if (it.block_offset() == 0)
it.block_externally_updated();
**outstream = val;
++(*outstream);
}
void finish()
{
ExtIterator out = Vec.begin() + RealSize;
ConstExtIterator const_out = out;
while (const_out.block_offset())
{
**outstream = *const_out;
++const_out;
++(*outstream);
}
out.flush();
delete outstream;
outstream = NULL;
Vec.resize(RealSize);
}
virtual ~write_vector()
{
if (outstream)
finish();
}
};
typedef unsigned char my_type;
using stxxl::syscall_file;
using stxxl::file;
int main(int argc, char * argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0] << " input_file output_file " << std::endl;
return -1;
}
unlink(argv[2]);
syscall_file InputFile(argv[1], file::RDONLY);
syscall_file OutputFile(argv[2], file::RDWR | file::CREAT);
typedef stxxl::vector<my_type> vector_type;
std::cout << "Copying file " << argv[1] << " to " << argv[2] << std::endl;
vector_type InputVector(&InputFile);
vector_type OutputVector(&OutputFile);
std::cout << "File " << argv[1] << " has size " << InputVector.size() << " bytes." << std::endl;
vector_type::const_iterator it = InputVector.begin();
write_vector<vector_type> Writer(OutputVector, 6);
for ( ; it != InputVector.end(); ++it)
{
Writer.push_back(*it);
}
Writer.finish();
return 0;
}