Stxxl  1.3.2
run_cursor.h
1 /***************************************************************************
2  * include/stxxl/bits/algo/run_cursor.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2003 Roman Dementiev <[email protected]>
7  * Copyright (C) 2009 Andreas Beckmann <[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_RUN_CURSOR_HEADER
15 #define STXXL_RUN_CURSOR_HEADER
16 
17 #include <cstdlib>
18 #include <stxxl/bits/common/types.h>
19 
20 
21 __STXXL_BEGIN_NAMESPACE
22 
23 template <typename block_type>
24 struct run_cursor
25 {
26  unsigned_type pos;
27  block_type * buffer;
28 
29  run_cursor() : pos(0), buffer(NULL) { }
30 
31  inline typename block_type::const_reference current() const
32  {
33  return (*buffer)[pos];
34  }
35  inline void operator ++ ()
36  {
37  ++pos;
38  }
39 };
40 
41 #ifdef STXXL_SORT_SINGLE_PREFETCHER
42 
43 template <typename must_be_void = void>
44 struct have_prefetcher
45 {
46  static void * untyped_prefetcher;
47 };
48 
49 #endif
50 
51 template <typename block_type,
52  typename prefetcher_type_>
53 struct run_cursor2 : public run_cursor<block_type>
54 #ifdef STXXL_SORT_SINGLE_PREFETCHER
55  , public have_prefetcher<>
56 #endif
57 {
58  typedef prefetcher_type_ prefetcher_type;
59  typedef run_cursor2<block_type, prefetcher_type> _Self;
60  typedef typename block_type::value_type value_type;
61 
62  using run_cursor<block_type>::pos;
63  using run_cursor<block_type>::buffer;
64 
65 #ifdef STXXL_SORT_SINGLE_PREFETCHER
66  static prefetcher_type * const prefetcher() // sorry, a hack
67  {
68  return reinterpret_cast<prefetcher_type *>(untyped_prefetcher);
69  }
70  static void set_prefetcher(prefetcher_type * pfptr)
71  {
72  untyped_prefetcher = pfptr;
73  }
74  run_cursor2() { }
75 #else
76  prefetcher_type * prefetcher_;
77  prefetcher_type * & prefetcher() // sorry, a hack
78  {
79  return prefetcher_;
80  }
81 
82  run_cursor2() { }
83  run_cursor2(prefetcher_type * p) : prefetcher_(p) { }
84 #endif
85 
86  inline bool empty() const
87  {
88  return (pos >= block_type::size);
89  }
90  inline void operator ++ ();
91  inline void make_inf()
92  {
93  pos = block_type::size;
94  }
95 };
96 
97 #ifdef STXXL_SORT_SINGLE_PREFETCHER
98 template <typename must_be_void>
99 void * have_prefetcher<must_be_void>::untyped_prefetcher = NULL;
100 #endif
101 
102 template <typename block_type,
103  typename prefetcher_type>
104 void run_cursor2<block_type, prefetcher_type>::operator ++ ()
105 {
106  assert(!empty());
107  ++pos;
108  if (UNLIKELY(pos >= block_type::size))
109  {
110  if (prefetcher()->block_consumed(buffer))
111  pos = 0;
112  }
113 }
114 
115 
116 #if 0
117 template <typename block_type>
118 struct run_cursor_cmp
119 {
120  typedef run_cursor<block_type> cursor_type;
121 
122  inline bool operator () (const cursor_type & a, const cursor_type & b) // greater or equal
123  {
124  return !((*a.buffer)[a.pos] < (*b.buffer)[b.pos]);
125  }
126 };
127 #endif
128 
129 __STXXL_END_NAMESPACE
130 
131 
132 #endif // !STXXL_RUN_CURSOR_HEADER
133 // vim: et:ts=4:sw=4