STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
buf_istream.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/mng/buf_istream.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2002-2004 Roman Dementiev <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_MNG_BUF_ISTREAM_HEADER
14 #define STXXL_MNG_BUF_ISTREAM_HEADER
15 
16 #include <stxxl/bits/mng/config.h>
18 #include <stxxl/bits/noncopyable.h>
20 
21 
23 
24 //! \addtogroup schedlayer
25 //! \{
26 
27 
28 // a paranoid check
29 #define BUF_ISTREAM_CHECK_END
30 
31 
32 //! Buffered input stream.
33 //!
34 //! Reads data records from the stream of blocks.
35 //! \remark Reading performed in the background, i.e. with overlapping of I/O and computation
36 template <typename BlockType, typename BIDIteratorType>
37 class buf_istream : private noncopyable
38 {
39 public:
40  typedef BlockType block_type;
41  typedef BIDIteratorType bid_iterator_type;
42 
43 private:
45 
46 protected:
52 #ifdef BUF_ISTREAM_CHECK_END
54 #endif
55 
56 public:
57  typedef typename block_type::reference reference;
59 
60  //! Constructs input stream object.
61  //! \param _begin \c bid_iterator pointing to the first block of the stream
62  //! \param _end \c bid_iterator pointing to the ( \b last + 1 ) block of the stream
63  //! \param nbuffers number of buffers for internal use
65  current_elem(0)
67  , not_finished(true)
68 #endif
69  {
70  //int_type i;
71  const unsigned_type ndisks = config::get_instance()->disks_number();
72  const int_type seq_length = _end - _begin;
73  prefetch_seq = new int_type[seq_length];
74 
75  // obvious schedule
76  //for(int_type i = 0; i< seq_length; ++i)
77  // prefetch_seq[i] = i;
78 
79  // optimal schedule
80  nbuffers = STXXL_MAX(2 * ndisks, unsigned_type(nbuffers - 1));
81  compute_prefetch_schedule(_begin, _end, prefetch_seq,
82  nbuffers, ndisks);
83 
84 
85  prefetcher = new prefetcher_type(_begin, _end, prefetch_seq, nbuffers);
86 
87  current_blk = prefetcher->pull_block();
88  }
89 
90  //! Input stream operator, reads in \c record.
91  //! \param record reference to the block record type,
92  //! contains value of the next record in the stream after the call of the operator
93  //! \return reference to itself (stream object)
94  _Self& operator >> (reference record)
95  {
96 #ifdef BUF_ISTREAM_CHECK_END
97  assert(not_finished);
98 #endif
99 
100  record = current_blk->elem[current_elem++];
101 
102  if (UNLIKELY(current_elem >= block_type::size))
103  {
104  current_elem = 0;
105 #ifdef BUF_ISTREAM_CHECK_END
106  not_finished = prefetcher->block_consumed(current_blk);
107 #else
108  prefetcher->block_consumed(current_blk);
109 #endif
110  }
111 
112  return (*this);
113  }
114 
115  //! Returns reference to the current record in the stream.
116  reference current() /* const */
117  {
118  return current_blk->elem[current_elem];
119  }
120 
121  //! Returns reference to the current record in the stream.
122  reference operator * () /* const */
123  {
124  return current_blk->elem[current_elem];
125  }
126 
127  //! Moves to the next record in the stream.
128  //! \return reference to itself after the advance
130  {
131 #ifdef BUF_ISTREAM_CHECK_END
132  assert(not_finished);
133 #endif
134 
135  current_elem++;
136 
137  if (UNLIKELY(current_elem >= block_type::size))
138  {
139  current_elem = 0;
140 #ifdef BUF_ISTREAM_CHECK_END
141  not_finished = prefetcher->block_consumed(current_blk);
142 #else
143  prefetcher->block_consumed(current_blk);
144 #endif
145  }
146  return *this;
147  }
148 
149  //! Frees used internal objects.
151  {
152  delete prefetcher;
153  delete[] prefetch_seq;
154  }
155 };
156 
157 //! \}
158 
160 
161 #endif // !STXXL_MNG_BUF_ISTREAM_HEADER
buf_istream< block_type, bid_iterator_type > _Self
Definition: buf_istream.h:58
int_type current_elem
Definition: buf_istream.h:49
BIDIteratorType bid_iterator_type
Definition: buf_istream.h:41
~buf_istream()
Frees used internal objects.
Definition: buf_istream.h:150
#define BUF_ISTREAM_CHECK_END
Definition: buf_istream.h:29
int_type * prefetch_seq
Definition: buf_istream.h:51
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
reference current()
Returns reference to the current record in the stream.
Definition: buf_istream.h:116
block_type * current_blk
Definition: buf_istream.h:50
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
#define UNLIKELY(c)
Definition: utils.h:226
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
Buffered input stream.
Definition: buf_istream.h:37
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(bid_iterator_type _begin, bid_iterator_type _end, int_type nbuffers)
Constructs input stream object.
Definition: buf_istream.h:64
prefetcher_type * prefetcher
Definition: buf_istream.h:48
block_type::reference reference
Definition: buf_istream.h:57
block_prefetcher< block_type, bid_iterator_type > prefetcher_type
Definition: buf_istream.h:47
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
BlockType block_type
Definition: buf_istream.h:40