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