Stxxl  1.3.2
buf_istream.h
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_BUF_ISTREAM_HEADER
14 #define STXXL_BUF_ISTREAM_HEADER
15 
16 #include <stxxl/bits/mng/config.h>
17 #include <stxxl/bits/mng/block_prefetcher.h>
18 #include <stxxl/bits/algo/async_schedule.h>
19 
20 
21 __STXXL_BEGIN_NAMESPACE
22 
25 
26 
27 // a paranoid check
28 #define BUF_ISTREAM_CHECK_END
29 
30 
35 template <typename BlkTp_, typename BIDIteratorTp_>
37 {
38  typedef BlkTp_ block_type;
39  typedef BIDIteratorTp_ bid_iterator_type;
40 
41  buf_istream() { }
42 
43 protected:
45  prefetcher_type * prefetcher;
46  bid_iterator_type begin_bid, end_bid;
47  int_type current_elem;
48  block_type * current_blk;
49  int_type * prefetch_seq;
50 #ifdef BUF_ISTREAM_CHECK_END
51  bool not_finished;
52 #endif
53 
54 public:
55  typedef typename block_type::reference reference;
57 
62  buf_istream(bid_iterator_type _begin, bid_iterator_type _end, int_type nbuffers) :
63  current_elem(0)
64 #ifdef BUF_ISTREAM_CHECK_END
65  , not_finished(true)
66 #endif
67  {
68  //int_type i;
69  const unsigned_type ndisks = config::get_instance()->disks_number();
70  const int_type seq_length = _end - _begin;
71  prefetch_seq = new int_type[seq_length];
72 
73  // obvious schedule
74  //for(int_type i = 0; i< seq_length; ++i)
75  // prefetch_seq[i] = i;
76 
77  // optimal schedule
78  nbuffers = STXXL_MAX(2 * ndisks, unsigned_type(nbuffers - 1));
79  compute_prefetch_schedule(_begin, _end, prefetch_seq,
80  nbuffers, ndisks);
81 
82 
83  prefetcher = new prefetcher_type(_begin, _end, prefetch_seq, nbuffers);
84 
85  current_blk = prefetcher->pull_block();
86  }
87 
92  _Self & operator >> (reference record)
93  {
94 #ifdef BUF_ISTREAM_CHECK_END
95  assert(not_finished);
96 #endif
97 
98  record = current_blk->elem[current_elem++];
99 
100  if (current_elem >= block_type::size)
101  {
102  current_elem = 0;
103 #ifdef BUF_ISTREAM_CHECK_END
104  not_finished = prefetcher->block_consumed(current_blk);
105 #else
106  prefetcher->block_consumed(current_blk);
107 #endif
108  }
109 
110  return (*this);
111  }
112 
115  reference current() /* const */
116  {
117  return current_blk->elem[current_elem];
118  }
119 
122  reference operator * () /* const */
123  {
124  return current_blk->elem[current_elem];
125  }
126 
130  {
131 #ifdef BUF_ISTREAM_CHECK_END
132  assert(not_finished);
133 #endif
134 
135  current_elem++;
136 
137  if (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 
150  virtual ~buf_istream()
151  {
152  delete prefetcher;
153  delete[] prefetch_seq;
154  }
155 };
156 
158 
159 __STXXL_END_NAMESPACE
160 
161 #endif // !STXXL_BUF_ISTREAM_HEADER
bool block_consumed(block_type *&buffer)
Exchanges buffers between prefetcher and application.
Definition: block_prefetcher.h:159
Buffered input stream.
Definition: buf_istream.h:36
_Self & operator>>(reference record)
Input stream operator, reads in record.
Definition: buf_istream.h:92
_Self & operator++()
Moves to the next record in the stream.
Definition: buf_istream.h:129
block_type * pull_block()
Pulls next unconsumed block from the consumption sequence.
Definition: block_prefetcher.h:149
reference operator*()
Returns reference to the current record in the stream.
Definition: buf_istream.h:122
virtual ~buf_istream()
Frees used internal objects.
Definition: buf_istream.h:150
Encapsulates asynchronous prefetching engine.
Definition: block_prefetcher.h:54
buf_istream(bid_iterator_type _begin, bid_iterator_type _end, int_type nbuffers)
Constructs input stream object.
Definition: buf_istream.h:62
reference current()
Returns reference to the current record in the stream.
Definition: buf_istream.h:115