http://stxxl.sourceforge.net
<dementiev@mpi-sb.mpg.de>
<beckmann@cs.uni-frankfurt.de>
http://www.boost.org/LICENSE_1_0.txt
#ifndef STXXL_SCAN_HEADER
#define STXXL_SCAN_HEADER
#include <stxxl/bits/namespace.h>
#include <stxxl/bits/mng/buf_istream.h>
#include <stxxl/bits/mng/buf_ostream.h>
__STXXL_BEGIN_NAMESPACE
template <typename _ExtIterator, typename _UnaryFunction>
_UnaryFunction for_each(_ExtIterator _begin, _ExtIterator _end, _UnaryFunction _functor, int_type nbuffers)
{
if (_begin == _end)
return _functor;
typedef buf_istream<typename _ExtIterator::block_type, typename _ExtIterator::bids_container_iterator> buf_istream_type;
_begin.flush();
buf_istream_type in(_begin.bid(), _end.bid() + ((_end.block_offset()) ? 1 : 0), nbuffers);
_ExtIterator _cur = _begin - _begin.block_offset();
for ( ; _cur != _begin; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
}
for ( ; _cur != _end; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
_functor(tmp);
}
if (_end.block_offset())
{
_ExtIterator _last_block_end = _end - _end.block_offset() + _ExtIterator::block_type::size;
for ( ; _cur != _last_block_end; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
}
}
return _functor;
}
template <typename _ExtIterator, typename _UnaryFunction>
_UnaryFunction for_each_m(_ExtIterator _begin, _ExtIterator _end, _UnaryFunction _functor, int_type nbuffers)
{
if (_begin == _end)
return _functor;
typedef buf_istream<typename _ExtIterator::block_type, typename _ExtIterator::bids_container_iterator> buf_istream_type;
typedef buf_ostream<typename _ExtIterator::block_type, typename _ExtIterator::bids_container_iterator> buf_ostream_type;
_begin.flush();
buf_istream_type in(_begin.bid(), _end.bid() + ((_end.block_offset()) ? 1 : 0), nbuffers / 2);
buf_ostream_type out(_begin.bid(), nbuffers / 2);
_ExtIterator _cur = _begin - _begin.block_offset();
for ( ; _cur != _begin; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
out << tmp;
}
for ( ; _cur != _end; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
_functor(tmp);
out << tmp;
}
if (_end.block_offset())
{
_ExtIterator _last_block_end = _end - _end.block_offset() + _ExtIterator::block_type::size;
for ( ; _cur != _last_block_end; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
out << tmp;
}
}
return _functor;
}
template <typename _ExtIterator, typename _Generator>
void generate(_ExtIterator _begin, _ExtIterator _end, _Generator _generator, int_type nbuffers)
{
typedef typename _ExtIterator::block_type block_type;
typedef buf_ostream<block_type, typename _ExtIterator::bids_container_iterator> buf_ostream_type;
while (_begin.block_offset())
{
if (_begin == _end)
return;
*_begin = _generator();
++_begin;
}
_begin.flush();
buf_ostream_type outstream(_begin.bid(), nbuffers);
assert(_begin.block_offset() == 0);
typename _ExtIterator::const_iterator prev_block = _begin;
while (_end != _begin)
{
if (_begin.block_offset() == 0) {
if (prev_block != _begin) {
prev_block.block_externally_updated();
prev_block = _begin;
}
}
*outstream = _generator();
++_begin;
++outstream;
}
typename _ExtIterator::const_iterator out = _begin;
while (out.block_offset())
{
*outstream = *out;
++out;
++outstream;
}
if (prev_block != out)
prev_block.block_externally_updated();
_begin.flush();
}
template <typename _ExtIterator, typename _EqualityComparable>
_ExtIterator find(_ExtIterator _begin, _ExtIterator _end, const _EqualityComparable & _value, int_type nbuffers)
{
if (_begin == _end)
return _end;
typedef buf_istream<typename _ExtIterator::block_type, typename _ExtIterator::bids_container_iterator> buf_istream_type;
_begin.flush();
buf_istream_type in(_begin.bid(), _end.bid() + ((_end.block_offset()) ? 1 : 0), nbuffers);
_ExtIterator _cur = _begin - _begin.block_offset();
for ( ; _cur != _begin; ++_cur)
++in;
for ( ; _cur != _end; ++_cur)
{
typename _ExtIterator::value_type tmp;
in >> tmp;
if (tmp == _value)
return _cur;
}
return _cur;
}
__STXXL_END_NAMESPACE
#endif