Stxxl  1.4.0
mng/test_buf_streams.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *  mng/test_buf_streams.cpp
00003  *
00004  *  Part of the STXXL. See http://stxxl.sourceforge.net
00005  *
00006  *  Copyright (C) 2002 Roman Dementiev <dementiev@mpi-sb.mpg.de>
00007  *
00008  *  Distributed under the Boost Software License, Version 1.0.
00009  *  (See accompanying file LICENSE_1_0.txt or copy at
00010  *  http://www.boost.org/LICENSE_1_0.txt)
00011  **************************************************************************/
00012 
00013 //! \example mng/test_buf_streams.cpp
00014 //! This is an example of use of \c stxxl::buf_istream and \c stxxl::buf_ostream
00015 
00016 #include <iostream>
00017 #include <stxxl/mng>
00018 #include <stxxl/bits/mng/buf_ostream.h>
00019 #include <stxxl/bits/mng/buf_istream.h>
00020 
00021 
00022 #define BLOCK_SIZE (1024 * 512)
00023 
00024 typedef stxxl::typed_block<BLOCK_SIZE, unsigned> block_type;
00025 typedef stxxl::buf_ostream<block_type, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_ostream_type;
00026 typedef stxxl::buf_istream<block_type, stxxl::BIDArray<BLOCK_SIZE>::iterator> buf_istream_type;
00027 
00028 int main()
00029 {
00030     const unsigned nblocks = 64;
00031     const unsigned nelements = nblocks * block_type::size;
00032     stxxl::BIDArray<BLOCK_SIZE> bids(nblocks);
00033 
00034     stxxl::block_manager * bm = stxxl::block_manager::get_instance();
00035     bm->new_blocks(stxxl::striping(), bids.begin(), bids.end());
00036     {
00037         buf_ostream_type out(bids.begin(), 2);
00038         for (unsigned i = 0; i < nelements; i++)
00039             out << i;
00040     }
00041     {
00042         buf_istream_type in(bids.begin(), bids.end(), 2);
00043         for (unsigned i = 0; i < nelements; i++)
00044         {
00045             unsigned value;
00046             in >> value;
00047             if (value != i)
00048             {
00049                 STXXL_ERRMSG("Error at position " << std::hex << i << " (" << value << ") block " << (i / block_type::size));
00050             }
00051         }
00052     }
00053     bm->delete_blocks(bids.begin(), bids.end());
00054 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines