13 #ifndef STXXL_STREAM_HEADER
14 #define STXXL_STREAM_HEADER
16 #include <stxxl/bits/namespace.h>
17 #include <stxxl/bits/mng/buf_istream.h>
18 #include <stxxl/bits/mng/buf_ostream.h>
19 #include <stxxl/bits/common/tuple.h>
20 #include <stxxl/vector>
21 #include <stxxl/bits/compat_auto_ptr.h>
24 __STXXL_BEGIN_NAMESPACE
57 template <
class InputIterator_>
60 InputIterator_ current_, end_;
64 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
67 current_(begin), end_(end) { }
77 const value_type * operator -> ()
const
85 assert(end_ != current_);
93 return (current_ == end_);
102 template <
class InputIterator_>
109 template <
class InputIterator_>
120 template <
class InputIterator_>
123 InputIterator_ current_, end_;
124 typedef buf_istream<
typename InputIterator_::block_type,
127 typedef typename stxxl::compat_auto_ptr<buf_istream_type>::result auto_ptr_type;
128 mutable auto_ptr_type in;
139 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
142 current_(begin), end_(end)
145 typename InputIterator_::bids_container_iterator end_iter = end.bid() + ((end.block_offset()) ? 1 : 0);
147 if (end_iter - begin.bid() > 0)
150 (2 * config::get_instance()->disks_number())));
152 InputIterator_ cur = begin - begin.block_offset();
155 for ( ; cur != begin; ++cur)
168 const value_type * operator -> ()
const
176 assert(end_ != current_);
188 return (current_ == end_);
204 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
205 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
206 vector_iterator2stream<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
208 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
209 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
210 unsigned_type nbuffers = 0)
212 STXXL_VERBOSE1(
"streamify for vector_iterator range is called");
214 (begin, end, nbuffers);
217 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
218 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
219 struct streamify_traits<stxxl::
vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
221 typedef vector_iterator2stream<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
stream_type;
232 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
233 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
234 vector_iterator2stream<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
236 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
237 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
238 unsigned_type nbuffers = 0)
240 STXXL_VERBOSE1(
"streamify for const_vector_iterator range is called");
242 (begin, end, nbuffers);
245 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
246 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
247 struct streamify_traits<stxxl::
const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
249 typedef vector_iterator2stream<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
stream_type;
259 template <
class InputIterator_>
265 typedef typename InputIterator_::block_type block_type;
271 typedef typename std::iterator_traits<InputIterator_>::value_type
value_type;
275 if (end - begin < block_type::size)
277 STXXL_VERBOSE1(
"vector_iterator2stream_sr::vector_iterator2stream_sr: Choosing iterator2stream<InputIterator_>");
279 vec_it_stream = NULL;
283 STXXL_VERBOSE1(
"vector_iterator2stream_sr::vector_iterator2stream_sr: Choosing vector_iterator2stream<InputIterator_>");
297 return **vec_it_stream;
300 const value_type * operator -> ()
const
303 return &(**it_stream);
305 return &(**vec_it_stream);
325 return it_stream->empty();
327 return vec_it_stream->empty();
335 delete vec_it_stream;
340 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
341 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
342 vector_iterator2stream_sr<stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
344 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
345 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
346 unsigned_type nbuffers = 0)
348 STXXL_VERBOSE1(
"streamify_sr for vector_iterator range is called");
350 (begin, end, nbuffers);
354 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
355 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_>
356 vector_iterator2stream_sr<stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> >
358 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> begin,
359 stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> end,
360 unsigned_type nbuffers = 0)
362 STXXL_VERBOSE1(
"streamify_sr for const_vector_iterator range is called");
364 (begin, end, nbuffers);
373 template <
class OutputIterator_,
class StreamAlgorithm_>
374 OutputIterator_
materialize(StreamAlgorithm_ & in, OutputIterator_ out)
395 template <
class OutputIterator_,
class StreamAlgorithm_>
396 OutputIterator_
materialize(StreamAlgorithm_ & in, OutputIterator_ outbegin, OutputIterator_ outend)
398 while ((!in.empty()) && outend != outbegin)
419 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
420 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_,
class StreamAlgorithm_>
421 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_>
423 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> outbegin,
424 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> outend,
425 unsigned_type nbuffers = 0)
427 typedef stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ExtIterator;
428 typedef stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ConstExtIterator;
432 while (outbegin.block_offset())
435 if (in.empty() || outbegin == outend)
444 nbuffers = 2 * config::get_instance()->disks_number();
450 buf_ostream_type outstream(outbegin.bid(), nbuffers);
452 assert(outbegin.block_offset() == 0);
454 while (!in.empty() && outend != outbegin)
456 if (outbegin.block_offset() == 0)
465 ConstExtIterator const_out = outbegin;
467 while (const_out.block_offset())
469 *outstream = *const_out;
487 template <
typename Tp_,
typename AllocStr_,
typename SzTp_,
typename DiffTp_,
488 unsigned BlkSize_,
typename PgTp_,
unsigned PgSz_,
class StreamAlgorithm_>
489 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_>
491 stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> out,
492 unsigned_type nbuffers = 0)
494 typedef stxxl::vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ExtIterator;
495 typedef stxxl::const_vector_iterator<Tp_, AllocStr_, SzTp_, DiffTp_, BlkSize_, PgTp_, PgSz_> ConstExtIterator;
503 while (out.block_offset())
515 nbuffers = 2 * config::get_instance()->disks_number();
521 buf_ostream_type outstream(out.bid(), nbuffers);
523 assert(out.block_offset() == 0);
527 if (out.block_offset() == 0)
536 ConstExtIterator const_out = out;
538 while (const_out.block_offset())
540 *outstream = *const_out;
553 template <
class Generator_>
566 gen_(g), current_(gen_()) { }
576 const value_type * operator -> ()
const
598 template <
class Generator_>
617 template <
class Operation_,
class Input1_,
618 class Input2_ = Stopper,
619 class Input3_ = Stopper,
620 class Input4_ = Stopper,
621 class Input5_ = Stopper,
622 class Input6_ = Stopper
643 transform(Operation_ o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_,
644 Input5_ & i5_, Input5_ & i6_) :
645 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_), i6(i6_),
646 current(op(*i1, *i2, *i3, *i4, *i5, *i6)) { }
654 const value_type * operator -> ()
const
670 current = op(*i1, *i2, *i3, *i4, *i5, *i6);
679 return i1.empty() || i2.empty() || i3.empty() ||
680 i4.empty() || i5.empty() || i6.empty();
693 template <
class Operation_,
class Input1_>
694 class transform<Operation_, Input1_, Stopper, Stopper, Stopper, Stopper, Stopper>
708 transform(Operation_ o, Input1_ & i1_) : op(o), i1(i1_),
717 const value_type * operator -> ()
const
749 template <
class Operation_,
class Input1_,
752 class transform<Operation_, Input1_, Input2_, Stopper, Stopper, Stopper, Stopper>
767 transform(Operation_ o, Input1_ & i1_, Input2_ & i2_) : op(o), i1(i1_), i2(i2_),
768 current(op(*i1, *i2)) { }
776 const value_type * operator -> ()
const
787 current = op(*i1, *i2);
796 return i1.empty() || i2.empty();
810 template <
class Operation_,
class Input1_,
814 class transform<Operation_, Input1_, Input2_, Input3_, Stopper, Stopper, Stopper>
830 transform(Operation_ o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_) :
831 op(o), i1(i1_), i2(i2_), i3(i3_),
832 current(op(*i1, *i2, *i3)) { }
840 const value_type * operator -> ()
const
852 current = op(*i1, *i2, *i3);
861 return i1.empty() || i2.empty() || i3.empty();
876 template <
class Operation_,
class Input1_,
881 class transform<Operation_, Input1_, Input2_, Input3_, Input4_, Stopper, Stopper>
898 transform(Operation_ o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_) :
899 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_),
900 current(op(*i1, *i2, *i3, *i4)) { }
908 const value_type * operator -> ()
const
921 current = op(*i1, *i2, *i3, *i4);
930 return i1.empty() || i2.empty() || i3.empty() || i4.empty();
946 template <
class Operation_,
class Input1_,
952 class transform<Operation_, Input1_, Input2_, Input3_, Input4_, Input5_, Stopper>
970 transform(Operation_ o, Input1_ & i1_, Input2_ & i2_, Input3_ & i3_, Input4_ & i4_,
972 op(o), i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_),
973 current(op(*i1, *i2, *i3, *i4, *i5)) { }
981 const value_type * operator -> ()
const
995 current = op(*i1, *i2, *i3, *i4, *i5);
1004 return i1.empty() || i2.empty() || i3.empty() || i4.empty() || i5.empty();
1018 template <
class Input1_,
1020 class Input3_ = Stopper,
1021 class Input4_ = Stopper,
1022 class Input5_ = Stopper,
1023 class Input6_ = Stopper
1036 typedef typename stxxl::tuple<
1042 typename Input6_::value_type
1058 i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_), i6(i6_),
1059 current(
value_type(*i1, *i2, *i3, *i4, *i5, *i6)) { }
1067 const value_type * operator -> ()
const
1083 current =
value_type(*i1, *i2, *i3, *i4, *i5, *i6);
1092 return i1.empty() || i2.empty() || i3.empty() ||
1093 i4.empty() || i5.empty() || i6.empty();
1104 template <
class Input1_,
class Input2_>
1105 class make_tuple<Input1_, Input2_, Stopper, Stopper, Stopper, Stopper>
1112 typedef typename stxxl::tuple<
1114 typename Input2_::value_type
1140 const value_type * operator -> ()
const
1161 return i1.empty() || i2.empty();
1172 template <
class Input1_,
class Input2_,
class Input3_>
1173 class make_tuple<Input1_, Input2_, Input3_, Stopper, Stopper, Stopper>
1181 typedef typename stxxl::tuple<
1184 typename Input3_::value_type
1197 i1(i1_), i2(i2_), i3(i3_),
1206 const value_type * operator -> ()
const
1228 return i1.empty() || i2.empty() || i3.empty();
1240 template <
class Input1_,
1245 class make_tuple<Input1_, Input2_, Input3_, Input4_, Stopper, Stopper>
1254 typedef typename stxxl::tuple<
1258 typename Input4_::value_type
1272 i1(i1_), i2(i2_), i3(i3_), i4(i4_),
1281 const value_type * operator -> ()
const
1304 return i1.empty() || i2.empty() || i3.empty() ||
1325 class make_tuple<Input1_, Input2_, Input3_, Input4_, Input5_, Stopper>
1335 typedef typename stxxl::tuple<
1340 typename Input5_::value_type
1355 i1(i1_), i2(i2_), i3(i3_), i4(i4_), i5(i5_),
1356 current(
value_type(*i1, *i2, *i3, *i4, *i5)) { }
1364 const value_type * operator -> ()
const
1379 current =
value_type(*i1, *i2, *i3, *i4, *i5);
1388 return i1.empty() || i2.empty() || i3.empty() ||
1389 i4.empty() || i5.empty();
1394 template <
class Input_,
int Which>
1404 template <
class Input_>
1405 class choose<Input_, 1>
1409 typedef typename Input_::value_type tuple_type;
1420 choose(Input_ & in_) :
1422 current((*in_).first) { }
1430 const value_type * operator -> ()
const
1436 choose & operator ++ ()
1441 current = (*in).first;
1460 template <
class Input_>
1461 class choose<Input_, 2>
1465 typedef typename Input_::value_type tuple_type;
1476 choose(Input_ & in_) :
1478 current((*in_).second) { }
1486 const value_type * operator -> ()
const
1492 choose & operator ++ ()
1497 current = (*in).second;
1516 template <
class Input_>
1517 class choose<Input_, 3>
1521 typedef typename Input_::value_type tuple_type;
1532 choose(Input_ & in_) :
1534 current((*in_).third) { }
1542 const value_type * operator -> ()
const
1548 choose & operator ++ ()
1553 current = (*in).third;
1572 template <
class Input_>
1573 class choose<Input_, 4>
1577 typedef typename Input_::value_type tuple_type;
1588 choose(Input_ & in_) :
1590 current((*in_).fourth) { }
1598 const value_type * operator -> ()
const
1604 choose & operator ++ ()
1609 current = (*in).fourth;
1628 template <
class Input_>
1629 class choose<Input_, 5>
1633 typedef typename Input_::value_type tuple_type;
1644 choose(Input_ & in_) :
1646 current((*in_).fifth) { }
1654 const value_type * operator -> ()
const
1660 choose & operator ++ ()
1665 current = (*in).fifth;
1684 template <
class Input_>
1685 class choose<Input_, 6>
1689 typedef typename Input_::value_type tuple_type;
1700 choose(Input_ & in_) :
1702 current((*in_).sixth) { }
1710 const value_type * operator -> ()
const
1716 choose & operator ++ ()
1721 current = (*in).sixth;
1738 template <
class Input,
class BinaryPredicate = Stopper>
1742 BinaryPredicate binary_pred;
1743 typename Input::value_type current;
1746 typedef typename Input::value_type value_type;
1747 unique(Input & input_, BinaryPredicate binary_pred_) : input(input_), binary_pred(binary_pred_)
1756 value_type old_value = current;
1758 while (!input.empty() && (binary_pred(current = *input, old_value)))
1770 bool empty()
const {
return input.empty(); }
1776 template <
class Input>
1780 typename Input::value_type current;
1783 typedef typename Input::value_type value_type;
1784 unique(Input & input_) : input(input_)
1792 value_type old_value = current;
1794 while (!input.empty() && ((current = *input) == old_value))
1807 bool empty()
const {
return input.empty(); }
1814 __STXXL_END_NAMESPACE
1816 #endif // !STXXL_STREAM_HEADER