STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sorter.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/sorter.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2012 Timo Bingmann <[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_CONTAINERS_SORTER_HEADER
14 #define STXXL_CONTAINERS_SORTER_HEADER
15 
16 #include <stxxl/bits/deprecated.h>
18 
20 
21 #ifndef STXXL_VERBOSE_SORTER
22 #define STXXL_VERBOSE_SORTER STXXL_VERBOSE2
23 #endif
24 
25 //! \addtogroup stlcont
26 //! \{
27 
28 //! External sorter container. \n
29 //! <b> Introduction </b> to sorter container: see \ref tutorial_sorter tutorial. \n
30 //! <b> Design and Internals </b> of sorter container: see \ref design_sorter
31 
32 /**
33  * \brief External Sorter: use stream package objects to keep a sorted
34  * container.
35  *
36  * This sorter container combines the two functions of runs_creator and
37  * runs_merger from the stream packages into a two-phase container.
38  *
39  * In the first phase the container is filled with unordered items via push(),
40  * which are presorted internally into runs of size M. When the internal memory
41  * overflows a runs is written to external memory in blocks of block_size.
42  *
43  * When sort() is called the container enters the output phase and push() is
44  * disallowed. After calling sort() the items can be read in sorted order using
45  * operator*() to get the top item, operator++() to advance to the next one and
46  * empty() to check for end of stream. This is exactly the stream interface.
47  *
48  * In the output phase the sorter can be returned to the beginning of the
49  * stream using rewind() and everything is read again in sorted order.
50  *
51  * Using clear() the object can be reset into input state and all items are
52  * destroyed.
53  *
54  * Added in STXXL 1.4
55  *
56  * \tparam ValueType type of the contained objects (POD with no references to internal memory)
57  * \tparam CompareType type of comparison object used for sorting the runs
58  * \tparam BlockSize size of the external memory block in bytes, default is \c STXXL_DEFAULT_BLOCK_SIZE(ValTp)
59  * \tparam AllocStr parallel disk allocation strategy, default is \c STXXL_DEFAULT_ALLOC_STRATEGY
60  */
61 template <typename ValueType,
62  typename CompareType,
63  unsigned BlockSize = STXXL_DEFAULT_BLOCK_SIZE(ValueType),
64  class AllocStrategy = STXXL_DEFAULT_ALLOC_STRATEGY>
65 class sorter : private noncopyable
66 {
67 public:
68  // *** Template Parameters
69 
70  typedef ValueType value_type;
71  typedef CompareType cmp_type;
72  enum {
73  block_size = BlockSize
74  };
75  typedef AllocStrategy alloc_strategy_type;
76 
77  // *** Constructed Types
78 
79  //! runs creator type with push() method
82 
83  //! corresponding runs merger type
86 
87 protected:
88  // *** Object Attributes
89 
90  //! current state of sorter
91  enum { STATE_INPUT, STATE_OUTPUT } m_state;
92 
93  //! runs creator object holding all items
95 
96  //! runs merger reading items when in STATE_OUTPUT
98 
99 public:
100  //! \name Constructors
101  //! \{
102 
103 
104  //! Constructor allocation memory_to_use bytes in ram for sorted runs.
105  sorter(const cmp_type& cmp, unsigned_type memory_to_use)
106  : m_state(STATE_INPUT),
107  m_runs_creator(cmp, memory_to_use),
108  m_runs_merger(cmp, memory_to_use)
109  { }
110 
111  //! Constructor variant with differently sizes runs_creator and runs_merger
112  sorter(const cmp_type& cmp, unsigned_type creator_memory_to_use, unsigned_type merger_memory_to_use)
113  : m_state(STATE_INPUT),
114  m_runs_creator(cmp, creator_memory_to_use),
115  m_runs_merger(cmp, merger_memory_to_use)
116 
117  { }
118 
119  //! \}
120 
121  //! \name Modifiers
122  //! \{
123 
124  //! Remove all items and return to input state.
125  void clear()
126  {
127  if (m_state == STATE_OUTPUT)
128  m_runs_merger.deallocate();
129 
130  m_runs_creator.allocate();
131  m_state = STATE_INPUT;
132  }
133 
134  //! Push another item (only callable during input state).
135  void push(const value_type& val)
136  {
137  assert(m_state == STATE_INPUT);
138  m_runs_creator.push(val);
139  }
140 
141  //! \}
142 
143  //! \name Modus
144  //! \{
145 
146  //! Finish push input state and deallocate input buffer.
147  void finish()
148  {
149  if (m_state == STATE_OUTPUT)
150  {
151  m_runs_merger.deallocate();
152  }
153 
154  m_runs_creator.deallocate();
155  }
156 
157  //! Deallocate buffers and clear result.
159  {
160  if (m_state == STATE_OUTPUT)
161  {
162  m_runs_merger.deallocate();
163  m_runs_creator.result()->clear();
164  }
165 
166  m_runs_creator.deallocate();
167  }
168 
169  //! \}
170 
171  //! \name Modifiers
172  //! \{
173 
174  //! Switch to output state, rewind() in case the output was already sorted.
175  void sort()
176  {
177  if (m_state == STATE_OUTPUT)
178  {
179  m_runs_merger.deallocate();
180  }
181 
182  m_runs_creator.deallocate();
183  m_runs_merger.initialize(m_runs_creator.result());
184  m_state = STATE_OUTPUT;
185  }
186 
187  //! Switch to output state, rewind() in case the output was already sorted.
188  void sort(unsigned_type merger_memory_to_use)
189  {
190  m_runs_merger.set_memory_to_use(merger_memory_to_use);
191  sort();
192  }
193 
194  //! \}
195 
196  //! \name Modus
197  //! \{
198 
199  //! Switch to output state, rewind() in case the output was already sorted.
200  void sort_reuse()
201  {
202  assert(m_state == STATE_INPUT);
203 
204  m_runs_merger.initialize(m_runs_creator.result());
205  m_state = STATE_OUTPUT;
206  }
207 
208  //! Rewind output stream to beginning.
209  void rewind()
210  {
211  assert(m_state == STATE_OUTPUT);
212 
213  m_runs_merger.deallocate();
214 
215  m_state = STATE_INPUT;
216  return sort();
217  }
218 
219  //! \}
220 
221  //! Change runs_merger memory usage
222  void set_merger_memory_to_use(unsigned_type merger_memory_to_use)
223  {
224  m_runs_merger.set_memory_to_use(merger_memory_to_use);
225  }
226 
227  //! \}
228 
229  //! \name Capacity
230  //! \{
231 
232  //! Number of items pushed or items remaining to be read.
234  {
235  if (m_state == STATE_INPUT)
236  return m_runs_creator.size();
237  else
238  return m_runs_merger.size();
239  }
240  //! Standard stream method
241  bool empty() const
242  {
243  assert(m_state == STATE_OUTPUT);
244  return m_runs_merger.empty();
245  }
246 
247  //! \}
248 
249  //! \name Operators
250  //! \{
251 
252  //! Standard stream method
253  const value_type& operator * () const
254  {
255  assert(m_state == STATE_OUTPUT);
256  return *m_runs_merger;
257  }
258 
259  //! Standard stream method
260  const value_type* operator -> () const
261  {
262  return &(operator * ());
263  }
264 
265  //! Standard stream method (preincrement operator)
267  {
268  assert(m_state == STATE_OUTPUT);
269  ++m_runs_merger;
270  return *this;
271  }
272 
273  //! \}
274 };
275 
276 //! \}
277 
279 
280 #endif // !STXXL_CONTAINERS_SORTER_HEADER
281 // vim: et:ts=4:sw=4
sorter(const cmp_type &cmp, unsigned_type creator_memory_to_use, unsigned_type merger_memory_to_use)
Constructor variant with differently sizes runs_creator and runs_merger.
Definition: sorter.h:112
void finish()
Finish push input state and deallocate input buffer.
Definition: sorter.h:147
AllocStrategy alloc_strategy_type
Definition: sorter.h:75
#define STXXL_DEFAULT_BLOCK_SIZE(type)
stream::runs_merger< typename runs_creator_type::sorted_runs_type, cmp_type, alloc_strategy_type > runs_merger_type
corresponding runs merger type
Definition: sorter.h:85
runs_merger_type m_runs_merger
runs merger reading items when in STATE_OUTPUT
Definition: sorter.h:97
void sort(ExtIterator first, ExtIterator last, StrictWeakOrdering cmp, unsigned_type M)
Sort records comparison-based, see stxxl::sort -- Sorting Comparison-Based.
Definition: sort.h:673
runs_creator_type m_runs_creator
runs creator object holding all items
Definition: sorter.h:94
#define STXXL_DEFAULT_ALLOC_STRATEGY
Definition: block_alloc.h:259
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
void clear()
Remove all items and return to input state.
Definition: sorter.h:125
void finish_clear()
Deallocate buffers and clear result.
Definition: sorter.h:158
External sorter container. Introduction to sorter container: see STXXL Sorter tutorial. Design and Internals of sorter container: see Sorter.
Definition: sorter.h:65
unsigned_type size() const
Number of items pushed or items remaining to be read.
Definition: sorter.h:233
void sort(unsigned_type merger_memory_to_use)
Switch to output state, rewind() in case the output was already sorted.
Definition: sorter.h:188
Merges sorted runs.
Definition: sort_stream.h:1456
void rewind()
Rewind output stream to beginning.
Definition: sorter.h:209
CompareType cmp_type
Definition: sorter.h:71
Forms sorted runs of data from a stream.
Definition: sort_stream.h:349
void push(const value_type &val)
Push another item (only callable during input state).
Definition: sorter.h:135
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
bool empty() const
Standard stream method.
Definition: sorter.h:241
void set_merger_memory_to_use(unsigned_type merger_memory_to_use)
Change runs_merger memory usage.
Definition: sorter.h:222
stream::runs_creator< stream::use_push< ValueType >, cmp_type, block_size, alloc_strategy_type > runs_creator_type
runs creator type with push() method
Definition: sorter.h:81
void sort()
Switch to output state, rewind() in case the output was already sorted.
Definition: sorter.h:175
sorter(const cmp_type &cmp, unsigned_type memory_to_use)
Constructor allocation memory_to_use bytes in ram for sorted runs.
Definition: sorter.h:105
ValueType value_type
Definition: sorter.h:70
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
void sort_reuse()
Switch to output state, rewind() in case the output was already sorted.
Definition: sorter.h:200