15 #ifndef STXXL_STREAM_HEADER
16 #define STXXL_STREAM_HEADER
18 #include <stxxl/bits/namespace.h>
19 #include <stxxl/bits/mng/buf_istream.h>
20 #include <stxxl/bits/mng/buf_ostream.h>
21 #include <stxxl/bits/common/tuple.h>
22 #include <stxxl/vector>
23 #include <stxxl/bits/compat_unique_ptr.h>
26 #ifndef STXXL_VERBOSE_MATERIALIZE
27 #define STXXL_VERBOSE_MATERIALIZE STXXL_VERBOSE3
31 __STXXL_BEGIN_NAMESPACE
69 template <
class InputIterator_>
72 InputIterator_ current_, end_;
76 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
79 current_(begin), end_(end) { }
89 const value_type * operator -> ()
const
97 assert(end_ != current_);
105 return (current_ == end_);
114 template <
class InputIterator_>
121 template <
class InputIterator_>
132 template <
class InputIterator_>
135 InputIterator_ current_, end_;
136 typedef buf_istream<
typename InputIterator_::block_type,
139 typedef typename stxxl::compat_unique_ptr<buf_istream_type>::result buf_istream_unique_ptr_type;
140 mutable buf_istream_unique_ptr_type in;
151 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
157 typename InputIterator_::bids_container_iterator end_iter = end.bid() + ((end.block_offset()) ? 1 : 0);
159 if (end_iter - begin.bid() > 0)
162 (2 * config::get_instance()->disks_number())));
164 InputIterator_ cur = begin - begin.block_offset();
167 for ( ; cur != begin; ++cur)
173 current_(a.current_), end_(a.end_), in(a.in.release()) { }
181 const value_type * operator -> ()
const
189 assert(end_ != current_);
201 return (current_ == end_);
217 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
218 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
219 vector_iterator2stream<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
221 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
222 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
223 unsigned_type nbuffers = 0)
225 STXXL_VERBOSE1(
"streamify for vector_iterator range is called");
227 (begin, end, nbuffers);
230 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
231 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
232 struct streamify_traits<stxxl::
vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
234 typedef vector_iterator2stream<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
stream_type;
245 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
246 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
247 vector_iterator2stream<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
249 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
250 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
251 unsigned_type nbuffers = 0)
253 STXXL_VERBOSE1(
"streamify for const_vector_iterator range is called");
255 (begin, end, nbuffers);
258 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
259 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
260 struct streamify_traits<stxxl::
const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
262 typedef vector_iterator2stream<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
stream_type;
272 template <
class InputIterator_>
278 typedef typename InputIterator_::block_type block_type;
284 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
288 if (end - begin < block_type::size)
290 STXXL_VERBOSE1(
"vector_iterator2stream_sr::vector_iterator2stream_sr: Choosing iterator2stream<InputIterator_>");
292 vec_it_stream = NULL;
296 STXXL_VERBOSE1(
"vector_iterator2stream_sr::vector_iterator2stream_sr: Choosing vector_iterator2stream<InputIterator_>");
310 return **vec_it_stream;
313 const value_type * operator -> ()
const
316 return &(**it_stream);
318 return &(**vec_it_stream);
338 return it_stream->empty();
340 return vec_it_stream->empty();
348 delete vec_it_stream;
353 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
354 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
355 vector_iterator2stream_sr<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
357 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
358 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
359 unsigned_type nbuffers = 0)
361 STXXL_VERBOSE1(
"streamify_sr for vector_iterator range is called");
363 (begin, end, nbuffers);
367 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
368 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
369 vector_iterator2stream_sr<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
371 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
372 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
373 unsigned_type nbuffers = 0)
375 STXXL_VERBOSE1(
"streamify_sr for const_vector_iterator range is called");
377 (begin, end, nbuffers);
391 template <
class OutputIterator_,
class StreamAlgorithm_>
392 OutputIterator_
materialize(StreamAlgorithm_ & in, OutputIterator_ out)
394 STXXL_VERBOSE_MATERIALIZE(STXXL_PRETTY_FUNCTION_NAME);
414 template <
class OutputIterator_,
class StreamAlgorithm_>
415 OutputIterator_
materialize(StreamAlgorithm_ & in, OutputIterator_ outbegin, OutputIterator_ outend)
417 STXXL_VERBOSE_MATERIALIZE(STXXL_PRETTY_FUNCTION_NAME);
418 while ((!in.empty()) && outend != outbegin)
439 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
440 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_,
class StreamAlgorithm_>
441 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_>
443 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> outbegin,
444 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> outend,
445 unsigned_type nbuffers = 0)
447 STXXL_VERBOSE_MATERIALIZE(STXXL_PRETTY_FUNCTION_NAME);
448 typedef stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ExtIterator;
449 typedef stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ConstExtIterator;
453 while (outbegin.block_offset())
456 if (in.empty() || outbegin == outend)
465 nbuffers = 2 * config::get_instance()->disks_number();
470 buf_ostream_type outstream(outbegin.bid(), nbuffers);
472 assert(outbegin.block_offset() == 0);
476 ConstExtIterator prev_block = outbegin;
478 while (!in.empty() && outend != outbegin)
480 if (outbegin.block_offset() == 0) {
481 if (prev_block != outbegin) {
482 prev_block.block_externally_updated();
483 prev_block = outbegin;
493 ConstExtIterator const_out = outbegin;
495 while (const_out.block_offset())
497 *outstream = *const_out;
502 if (prev_block != outbegin)
503 prev_block.block_externally_updated();
519 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
520 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_,
class StreamAlgorithm_>
521 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_>
523 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> out,
524 unsigned_type nbuffers = 0)
526 STXXL_VERBOSE_MATERIALIZE(STXXL_PRETTY_FUNCTION_NAME);
527 typedef stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ExtIterator;
528 typedef stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ConstExtIterator;
536 while (out.block_offset())
548 nbuffers = 2 * config::get_instance()->disks_number();
554 buf_ostream_type outstream(out.bid(), nbuffers);
556 assert(out.block_offset() == 0);
560 ConstExtIterator prev_block = out;
564 if (out.block_offset() == 0) {
565 if (prev_block != out) {
566 prev_block.block_externally_updated();
578 ConstExtIterator const_out = out;
580 while (const_out.block_offset())
582 *outstream = *const_out;
587 if (prev_block != out)
588 prev_block.block_externally_updated();
600 template <
class StreamAlgorithm_>
618 template <
class Generator_,
typename T =
typename Generator_::value_type>
631 gen_(g), current_(gen_()) { }
641 const value_type * operator -> ()
const
663 template <
class Generator_>
686 template <
class Operation_,
688 class Input2_ = Stopper,
689 class Input3_ = Stopper,
690 class Input4_ = Stopper,
691 class Input5_ = Stopper,
692 class Input6_ = Stopper
713 transform(Operation_ & o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_,
714 Input5_ & i5_, Input5_ & i6_) :
715 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_), i6(i6_)
718 current = op(*i1, *i2, *i3, *i4, *i5, *i6);
727 const value_type * operator -> ()
const
742 current = op(*i1, *i2, *i3, *i4, *i5, *i6);
750 return i1.empty() || i2.empty() || i3.empty() ||
751 i4.empty() || i5.empty() || i6.empty();
767 template <
class Operation_,
770 class transform<Operation_, Input1_, Stopper, Stopper, Stopper, Stopper, Stopper>
784 transform(Operation_ & o, Input1_ & i1_) : op(o), i1(i1_)
796 const value_type * operator -> ()
const
830 template <
class Operation_,
834 class transform<Operation_, Input1_, Input2_, Stopper, Stopper, Stopper, Stopper>
849 transform(Operation_ & o, Input1_ & i1_, Input2_ & i2_) : op(o), i1(i1_), i2(i2_)
852 current = op(*i1, *i2);
861 const value_type * operator -> ()
const
872 current = op(*i1, *i2);
880 return i1.empty() || i2.empty();
897 template <
class Operation_,
902 class transform<Operation_, Input1_, Input2_, Input3_, Stopper, Stopper, Stopper>
918 transform(Operation_ & o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_) :
919 op(o), i1(i1_), i2(i2_), i3(i3_)
922 current = op(*i1, *i2, *i3);
931 const value_type * operator -> ()
const
943 current = op(*i1, *i2, *i3);
951 return i1.empty() || i2.empty() || i3.empty();
969 template <
class Operation_,
975 class transform<Operation_, Input1_, Input2_, Input3_, Input4_, Stopper, Stopper>
992 transform(Operation_ & o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_) :
993 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_)
996 current = op(*i1, *i2, *i3, *i4);
1005 const value_type * operator -> ()
const
1018 current = op(*i1, *i2, *i3, *i4);
1026 return i1.empty() || i2.empty() || i3.empty() || i4.empty();
1045 template <
class Operation_,
1052 class transform<Operation_, Input1_, Input2_, Input3_, Input4_, Input5_, Stopper>
1070 transform(Operation_ & o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_,
1072 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_)
1075 current = op(*i1, *i2, *i3, *i4, *i5);
1084 const value_type * operator -> ()
const
1098 current = op(*i1, *i2, *i3, *i4, *i5);
1106 return i1.empty() || i2.empty() || i3.empty() || i4.empty() || i5.empty();
1123 template <
class Input1_,
1125 class Input3_ = Stopper,
1126 class Input4_ = Stopper,
1127 class Input5_ = Stopper,
1128 class Input6_ = Stopper
1141 typedef typename stxxl::tuple<
1147 typename Input6_::value_type
1163 i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_), i6(i6_),
1164 current(
value_type(*i1, *i2, *i3, *i4, *i5, *i6)) { }
1172 const value_type * operator -> ()
const
1188 current =
value_type(*i1, *i2, *i3, *i4, *i5, *i6);
1196 return i1.empty() || i2.empty() || i3.empty() ||
1197 i4.empty() || i5.empty() || i6.empty();
1207 template <
class Input1_,
1210 class make_tuple<Input1_, Input2_, Stopper, Stopper, Stopper, Stopper>
1217 typedef typename stxxl::tuple<
1219 typename Input2_::value_type
1245 const value_type * operator -> ()
const
1265 return i1.empty() || i2.empty();
1275 template <
class Input1_,
1279 class make_tuple<Input1_, Input2_, Input3_, Stopper, Stopper, Stopper>
1287 typedef typename stxxl::tuple<
1290 typename Input3_::value_type
1303 i1(i1_), i2(i2_), i3(i3_),
1312 const value_type * operator -> ()
const
1333 return i1.empty() || i2.empty() || i3.empty();
1344 template <
class Input1_,
1349 class make_tuple<Input1_, Input2_, Input3_, Input4_, Stopper, Stopper>
1358 typedef typename stxxl::tuple<
1362 typename Input4_::value_type
1376 i1(i1_), i2(i2_), i3(i3_), i4(i4_),
1385 const value_type * operator -> ()
const
1407 return i1.empty() || i2.empty() || i3.empty() ||
1427 class make_tuple<Input1_, Input2_, Input3_, Input4_, Input5_, Stopper>
1437 typedef typename stxxl::tuple<
1442 typename Input5_::value_type
1457 i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_),
1458 current(
value_type(*i1, *i2, *i3, *i4, *i5)) { }
1466 const value_type * operator -> ()
const
1481 current =
value_type(*i1, *i2, *i3, *i4, *i5);
1489 return i1.empty() || i2.empty() || i3.empty() ||
1490 i4.empty() || i5.empty();
1498 __STXXL_END_NAMESPACE
1501 #include <stxxl/bits/stream/choose.h>
1502 #include <stxxl/bits/stream/unique.h>
1505 #endif // !STXXL_STREAM_HEADER