STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buf_istream_reverse.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/buf_istream_reverse.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  * Copyright (C) 2013 Timo Bingmann <[email protected]>
8  *
9  * Distributed under the Boost Software License, Version 1.0.
10  * (See accompanying file LICENSE_1_0.txt or copy at
11  * http://www.boost.org/LICENSE_1_0.txt)
12  **************************************************************************/
13 
14 #ifndef STXXL_MNG_BUF_ISTREAM_REVERSE_HEADER
15 #define STXXL_MNG_BUF_ISTREAM_REVERSE_HEADER
16 
17 #include <stxxl/bits/mng/config.h>
18 #include <stxxl/bits/mng/bid.h>
20 #include <stxxl/bits/noncopyable.h>
22 
23 
25 
26 //! \addtogroup schedlayer
27 //! \{
28 
29 
30 // a paranoid check
31 #define BUF_ISTREAM_CHECK_END
32 
33 
34 //! Buffered input stream, reading the items in the blocks in reverse order.
35 //!
36 //! Reads data records from the stream of blocks in reverse order.
37 //! \remark Reading performed in the background, i.e. with overlapping of I/O and computation
38 template <typename BlockType, typename BIDIteratorType>
40 {
41 public:
42  typedef BlockType block_type;
43  typedef BIDIteratorType bid_iterator_type;
44 
45  //-tb note that we redefine the BID type here, because there is no way to
46  //-derive it from BIDIteratorType (which is usually just a POD pointer).
48 
49 private:
51 
52 protected:
58 #ifdef BUF_ISTREAM_CHECK_END
60 #endif
62 
63 public:
64  typedef typename block_type::reference reference;
66 
67  //! Constructs input stream object, reading [first,last) blocks in reverse.
68  //! \param begin \c bid_iterator pointing to the first block of the stream
69  //! \param end \c bid_iterator pointing to the ( \b last + 1 ) block of the stream
70  //! \param nbuffers number of buffers for internal use
72  : current_elem(0),
74  not_finished(true),
75 #endif
76  bids_(end - begin)
77  {
78  // copy list of bids in reverse
79  std::reverse_copy(begin, end, bids_.begin());
80 
81  // calculate prefetch sequence
82  const unsigned_type ndisks = config::get_instance()->disks_number();
83 
84  prefetch_seq = new int_type[bids_.size()];
85 
86  // optimal schedule
87  nbuffers = STXXL_MAX(2 * ndisks, unsigned_type(nbuffers - 1));
88  compute_prefetch_schedule(bids_.begin(), bids_.end(), prefetch_seq,
89  nbuffers, ndisks);
90 
91  // create stream prefetcher
92  prefetcher = new prefetcher_type(bids_.begin(), bids_.end(), prefetch_seq, nbuffers);
93 
94  // fetch block: last in sequence
95  current_blk = prefetcher->pull_block();
96  current_elem = block_type::size - 1;
97  }
98 
99  //! Input stream operator, reads in \c record.
100  //! \param record reference to the block record type,
101  //! contains value of the next record in the stream after the call of the operator
102  //! \return reference to itself (stream object)
103  _Self& operator >> (reference record)
104  {
105 #ifdef BUF_ISTREAM_CHECK_END
106  assert(not_finished);
107 #endif
108 
109  record = current_blk->elem[current_elem--];
110 
111  if (UNLIKELY(current_elem < 0))
112  {
113  current_elem = block_type::size - 1;
114 #ifdef BUF_ISTREAM_CHECK_END
115  not_finished = prefetcher->block_consumed(current_blk);
116 #else
117  prefetcher->block_consumed(current_blk);
118 #endif
119  }
120 
121  return (*this);
122  }
123 
124  //! Returns reference to the current record in the stream.
125  reference current() /* const */
126  {
127  return current_blk->elem[current_elem];
128  }
129 
130  //! Returns reference to the current record in the stream.
131  reference operator * () /* const */
132  {
133  return current_blk->elem[current_elem];
134  }
135 
136  //! Moves to the _previous_ record in the stream.
137  //! \return reference to itself after the advance
139  {
140 #ifdef BUF_ISTREAM_CHECK_END
141  assert(not_finished);
142 #endif
143 
144  current_elem--;
145 
146  if (UNLIKELY(current_elem < 0))
147  {
148  current_elem = block_type::size - 1;
149 #ifdef BUF_ISTREAM_CHECK_END
150  not_finished = prefetcher->block_consumed(current_blk);
151 #else
152  prefetcher->block_consumed(current_blk);
153 #endif
154  }
155  return *this;
156  }
157 
158  //! Frees used internal objects.
160  {
161  delete prefetcher;
162  delete[] prefetch_seq;
163  }
164 };
165 
166 //! \}
167 
169 
170 #endif // !STXXL_MNG_BUF_ISTREAM_REVERSE_HEADER
block_type::reference reference
#define BUF_ISTREAM_CHECK_END
block_type * pull_block()
Pulls next unconsumed block from the consumption sequence.
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
Encapsulates asynchronous prefetching engine.
const Tp & STXXL_MAX(const Tp &a, const Tp &b)
Definition: utils.h:154
choose_int_types< my_pointer_size >::int_type int_type
Definition: types.h:66
buf_istream_reverse< block_type, bid_iterator_type > _Self
Buffered input stream, reading the items in the blocks in reverse order.
#define UNLIKELY(c)
Definition: utils.h:226
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
buf_istream_reverse(bid_iterator_type begin, bid_iterator_type end, int_type nbuffers)
Constructs input stream object, reading [first,last) blocks in reverse.
reference current()
Returns reference to the current record in the stream.
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
void compute_prefetch_schedule(const int_type *first, const int_type *last, int_type *out_first, int_type m, int_type D)
~buf_istream_reverse()
Frees used internal objects.
block_prefetcher< block_type, typename bid_vector_type::iterator > prefetcher_type
BIDArray< block_type::raw_size > bid_vector_type
#define STXXL_END_NAMESPACE
Definition: namespace.h:17