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