Stxxl  1.3.2
pq_helpers.h
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_PQ_HELPERS_HEADER
17 #define STXXL_PQ_HELPERS_HEADER
18 
19 #include <queue>
20 
21 __STXXL_BEGIN_NAMESPACE
22 
26 
29 namespace priority_queue_local
30 {
37  template <typename _Tp, typename _Sequence = std::vector<_Tp>,
38  typename _Compare = std::less<typename _Sequence::value_type> >
40  {
41  // concept requirements
42  typedef typename _Sequence::value_type _Sequence_value_type;
43 
44  public:
45  typedef typename _Sequence::value_type value_type;
46  typedef typename _Sequence::reference reference;
47  typedef typename _Sequence::const_reference const_reference;
48  typedef typename _Sequence::size_type size_type;
49  typedef _Sequence container_type;
50 
51  protected:
52  // See queue::heap for notes on these names.
53  _Sequence heap;
54  _Compare comp;
55  size_type current_size;
56 
57  public:
61  explicit
62  internal_priority_queue(size_type capacity)
63  : heap(capacity), current_size(0)
64  { }
65 
69  bool
70  empty() const
71  { return current_size == 0; }
72 
74  size_type
75  size() const
76  { return current_size; }
77 
82  const_reference
83  top() const
84  {
85  return heap.front();
86  }
87 
96  void
97  push(const value_type & __x)
98  {
99  heap[current_size] = __x;
100  ++current_size;
101  std::push_heap(heap.begin(), heap.begin() + current_size, comp);
102  }
103 
115  void
116  pop()
117  {
118  std::pop_heap(heap.begin(), heap.begin() + current_size, comp);
119  --current_size;
120  }
121 
125  void sort_to(value_type * target)
126  {
128  sort(heap.begin(), heap.begin() + current_size, comp);
129  std::reverse_copy(heap.begin(), heap.begin() + current_size, target);
130  }
131 
135  void clear()
136  {
137  current_size = 0;
138  }
139  };
140 
144  template <class Predicate, typename first_argument_type, typename second_argument_type>
146  {
147  protected:
148  Predicate pred;
149 
150  public:
151  explicit
152  invert_order(const Predicate & _pred) : pred(_pred) { }
153 
154  bool operator () (const first_argument_type & x, const second_argument_type & y) const
155  {
156  return pred(y, x);
157  }
158  };
159 
160 
166  template <typename Tp_, unsigned_type max_size_>
168  {
169  typedef Tp_ value_type;
170  typedef unsigned_type size_type;
171  enum { max_size = max_size_ };
172 
173  size_type size_;
174  value_type array[max_size];
175 
176  public:
177  internal_bounded_stack() : size_(0) { }
178 
179  void push(const value_type & x)
180  {
181  assert(size_ < max_size);
182  array[size_++] = x;
183  }
184 
185  const value_type & top() const
186  {
187  assert(size_ > 0);
188  return array[size_ - 1];
189  }
190 
191  void pop()
192  {
193  assert(size_ > 0);
194  --size_;
195  }
196 
197  void clear()
198  {
199  size_ = 0;
200  }
201 
202  size_type size() const
203  {
204  return size_;
205  }
206 
207  bool empty() const
208  {
209  return size_ == 0;
210  }
211  };
212 } //priority_queue_local
213 
215 
216 __STXXL_END_NAMESPACE
217 
218 #endif // !STXXL_PQ_HELPERS_HEADER
219 // vim: et:ts=4:sw=4
const_reference top() const
Definition: pq_helpers.h:83
void sort(ExtIterator_ first, ExtIterator_ last, StrictWeakOrdering_ cmp, unsigned_type M)
Sort records comparison-based.
Definition: sort.h:700
bool empty() const
Definition: pq_helpers.h:70
Inverts the order of a comparison functor by swapping its arguments.
Definition: pq_helpers.h:145
void clear()
Remove all contained elements.
Definition: pq_helpers.h:135
void sort_to(value_type *target)
Sort all contained elements, write result to target.
Definition: pq_helpers.h:125
Similar to std::priority_queue, with the following differences:
Definition: pq_helpers.h:39
internal_priority_queue(size_type capacity)
Default constructor creates no elements.
Definition: pq_helpers.h:62
void push(const value_type &__x)
Add data to the queue.
Definition: pq_helpers.h:97
Similar to std::stack, with the following differences:
Definition: pq_helpers.h:167
void pop()
Removes first element.
Definition: pq_helpers.h:116
size_type size() const
Definition: pq_helpers.h:75