STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
pq_helpers.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/containers/pq_helpers.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 1999 Peter Sanders <[email protected]>
7  * Copyright (C) 2003, 2004, 2007 Roman Dementiev <[email protected]>
8  * Copyright (C) 2007, 2009 Johannes Singler <[email protected]>
9  * Copyright (C) 2007, 2008 Andreas Beckmann <[email protected]>
10  *
11  * Distributed under the Boost Software License, Version 1.0.
12  * (See accompanying file LICENSE_1_0.txt or copy at
13  * http://www.boost.org/LICENSE_1_0.txt)
14  **************************************************************************/
15 
16 #ifndef STXXL_CONTAINERS_PQ_HELPERS_HEADER
17 #define STXXL_CONTAINERS_PQ_HELPERS_HEADER
18 
19 #include <vector>
20 #include <algorithm>
21 
22 #include <stxxl/bits/deprecated.h>
31 #include <stxxl/bits/parallel.h>
34 
35 #if STXXL_PARALLEL
36 
37 #if defined(STXXL_PARALLEL_MODE) && ((__GNUC__ * 10000 + __GNUC_MINOR__ * 100) < 40400)
38 #undef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
39 #undef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_EXTERNAL
40 #undef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_DELETE_BUFFER
41 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL 0
42 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_EXTERNAL 0
43 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_DELETE_BUFFER 0
44 #endif
45 
46 // enable/disable parallel merging for certain cases, for performance tuning
47 #ifndef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
48 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL 1
49 #endif
50 #ifndef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_EXTERNAL
51 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_EXTERNAL 1
52 #endif
53 #ifndef STXXL_PARALLEL_PQ_MULTIWAY_MERGE_DELETE_BUFFER
54 #define STXXL_PARALLEL_PQ_MULTIWAY_MERGE_DELETE_BUFFER 1
55 #endif
56 
57 #endif //STXXL_PARALLEL
58 
59 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_EXTERNAL
60 #define STXXL_PQ_EXTERNAL_LOSER_TREE 0 // no loser tree for the external sequences
61 #else
62 #define STXXL_PQ_EXTERNAL_LOSER_TREE 1
63 #endif
64 
65 #if STXXL_PARALLEL && STXXL_PARALLEL_PQ_MULTIWAY_MERGE_INTERNAL
66 #define STXXL_PQ_INTERNAL_LOSER_TREE 0 // no loser tree for the internal sequences
67 #else
68 #define STXXL_PQ_INTERNAL_LOSER_TREE 1
69 #endif
70 
71 #define STXXL_VERBOSE_PQ(msg) STXXL_VERBOSE2("[" << static_cast<void*>(this) << "] priority_queue::" << msg)
72 
74 
75 //! \defgroup stlcontinternals internals
76 //! \ingroup stlcont
77 //! Supporting internal classes
78 //! \{
79 
80 /*! \internal
81  */
82 namespace priority_queue_local {
83 
84 /*!
85  * Similar to std::priority_queue, with the following differences:
86  * - Maximum size is fixed at construction time, so an array can be used.
87  * - Provides access to underlying heap, so (parallel) sorting in place is possible.
88  * - Can be cleared "at once", without reallocation.
89  */
90 template <typename _Tp, typename _Sequence = std::vector<_Tp>,
91  typename _Compare = std::less<typename _Sequence::value_type> >
93 {
94  // concept requirements
95  typedef typename _Sequence::value_type _Sequence_value_type;
96 
97 public:
98  typedef typename _Sequence::value_type value_type;
99  typedef typename _Sequence::reference reference;
100  typedef typename _Sequence::const_reference const_reference;
101  typedef typename _Sequence::size_type size_type;
102  typedef _Sequence container_type;
103 
104 protected:
105  // See queue::heap for notes on these names.
106  _Sequence heap;
107  _Compare comp;
109 
110 public:
111  //! Default constructor creates no elements.
112  explicit
114  : heap(capacity), current_size(0)
115  { }
116 
117  //! Returns true if the %queue is empty.
118  bool
119  empty() const
120  { return current_size == 0; }
121 
122  //! Returns the number of elements in the %queue.
123  size_type
124  size() const
125  { return current_size; }
126 
127  /*!
128  * Returns a read-only (constant) reference to the data at the first
129  * element of the %queue.
130  */
131  const_reference
132  top() const
133  {
134  return heap.front();
135  }
136 
137  /*!
138  * Add data to the %queue.
139  * @param __x Data to be added.
140  *
141  * This is a typical %queue operation.
142  * The time complexity of the operation depends on the underlying
143  * sequence.
144  */
145  void
146  push(const value_type& __x)
147  {
148  heap[current_size] = __x;
149  ++current_size;
150  std::push_heap(heap.begin(), heap.begin() + current_size, comp);
151  }
152 
153  /*!
154  * Removes first element.
155  *
156  * This is a typical %queue operation. It shrinks the %queue
157  * by one. The time complexity of the operation depends on the
158  * underlying sequence.
159  *
160  * Note that no data is returned, and if the first element's
161  * data is needed, it should be retrieved before pop() is
162  * called.
163  */
164  void
165  pop()
166  {
167  std::pop_heap(heap.begin(), heap.begin() + current_size, comp);
168  --current_size;
169  }
170 
171  //! Sort all contained elements, write result to @c target.
172  void sort_to(value_type* target)
173  {
176  sort(heap.begin(), heap.begin() + current_size, comp);
177  std::reverse_copy(heap.begin(), heap.begin() + current_size, target);
178  }
179 
180  //! Remove all contained elements.
181  void clear()
182  {
183  current_size = 0;
184  }
185 };
186 
187 //! Inverts the order of a comparison functor by swapping its arguments.
188 template <class Predicate, typename first_argument_type, typename second_argument_type>
190 {
191 protected:
192  Predicate pred;
193 
194 public:
195  explicit
196  invert_order(const Predicate& _pred) : pred(_pred) { }
197 
198  bool operator () (const first_argument_type& x, const second_argument_type& y) const
199  {
200  return pred(y, x);
201  }
202 };
203 
204 /*!
205  * Similar to std::stack, with the following differences:
206  * - Maximum size is fixed at compilation time, so an array can be used.
207  * - Can be cleared "at once", without reallocation.
208  */
209 template <typename Tp_, unsigned_type max_size_>
211 {
212  typedef Tp_ value_type;
214  enum { max_size = max_size_ };
215 
217  value_type array[max_size];
218 
219 public:
220  internal_bounded_stack() : size_(0) { }
221 
222  void push(const value_type& x)
223  {
224  assert(size_ < max_size);
225  array[size_++] = x;
226  }
227 
228  const value_type & top() const
229  {
230  assert(size_ > 0);
231  return array[size_ - 1];
232  }
233 
234  void pop()
235  {
236  assert(size_ > 0);
237  --size_;
238  }
239 
240  void clear()
241  {
242  size_ = 0;
243  }
244 
245  size_type size() const
246  {
247  return size_;
248  }
249 
250  bool empty() const
251  {
252  return size_ == 0;
253  }
254 };
255 
256 } // namespace priority_queue_local
257 
258 //! \}
259 
261 
262 #endif // !STXXL_CONTAINERS_PQ_HELPERS_HEADER
263 // vim: et:ts=4:sw=4
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
Inverts the order of a comparison functor by swapping its arguments.
Definition: pq_helpers.h:189
void check_sort_settings()
Definition: parallel.h:98
void sort_to(value_type *target)
Sort all contained elements, write result to target.
Definition: pq_helpers.h:172
Similar to std::stack, with the following differences:
Definition: pq_helpers.h:210
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
void push(const value_type &__x)
Add data to the queue.
Definition: pq_helpers.h:146
internal_priority_queue(size_type capacity)
Default constructor creates no elements.
Definition: pq_helpers.h:113
Similar to std::priority_queue, with the following differences:
Definition: pq_helpers.h:92
invert_order(const Predicate &_pred)
Definition: pq_helpers.h:196
size_type size() const
Returns the number of elements in the queue.
Definition: pq_helpers.h:124
bool empty() const
Returns true if the queue is empty.
Definition: pq_helpers.h:119
choose_int_types< my_pointer_size >::unsigned_type unsigned_type
Definition: types.h:67
const_reference top() const
Returns a read-only (constant) reference to the data at the first element of the queue.
Definition: pq_helpers.h:132
void clear()
Remove all contained elements.
Definition: pq_helpers.h:181
#define STXXL_END_NAMESPACE
Definition: namespace.h:17