00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef STXXL_PQ_HELPERS_HEADER
00017 #define STXXL_PQ_HELPERS_HEADER
00018
00019 #include <queue>
00020
00021 __STXXL_BEGIN_NAMESPACE
00022
00026
00029 namespace priority_queue_local
00030 {
00037 template <typename _Tp, typename _Sequence = std::vector<_Tp>,
00038 typename _Compare = std::less<typename _Sequence::value_type> >
00039 class internal_priority_queue
00040 {
00041
00042 typedef typename _Sequence::value_type _Sequence_value_type;
00043
00044 public:
00045 typedef typename _Sequence::value_type value_type;
00046 typedef typename _Sequence::reference reference;
00047 typedef typename _Sequence::const_reference const_reference;
00048 typedef typename _Sequence::size_type size_type;
00049 typedef _Sequence container_type;
00050
00051 protected:
00052
00053 _Sequence heap;
00054 _Compare comp;
00055 size_type current_size;
00056
00057 public:
00061 explicit
00062 internal_priority_queue(size_type capacity)
00063 : heap(capacity), current_size(0)
00064 { }
00065
00069 bool
00070 empty() const
00071 { return current_size == 0; }
00072
00074 size_type
00075 size() const
00076 { return current_size; }
00077
00082 const_reference
00083 top() const
00084 {
00085 return heap.front();
00086 }
00087
00096 void
00097 push(const value_type & __x)
00098 {
00099 heap[current_size] = __x;
00100 ++current_size;
00101 std::push_heap(heap.begin(), heap.begin() + current_size, comp);
00102 }
00103
00115 void
00116 pop()
00117 {
00118 std::pop_heap(heap.begin(), heap.begin() + current_size, comp);
00119 --current_size;
00120 }
00121
00125 void sort_to(value_type * target)
00126 {
00127 std::sort(heap.begin(), heap.begin() + current_size, comp);
00128 std::reverse_copy(heap.begin(), heap.begin() + current_size, target);
00129 }
00130
00134 void clear()
00135 {
00136 current_size = 0;
00137 }
00138 };
00139
00143 template <class Predicate, typename first_argument_type, typename second_argument_type>
00144 class invert_order
00145 {
00146 protected:
00147 Predicate pred;
00148
00149 public:
00150 explicit
00151 invert_order(const Predicate & _pred) : pred(_pred) { }
00152
00153 bool operator () (const first_argument_type & x, const second_argument_type & y) const
00154 {
00155 return pred(y, x);
00156 }
00157 };
00158
00159
00165 template <typename Tp_, unsigned_type max_size_>
00166 class internal_bounded_stack
00167 {
00168 typedef Tp_ value_type;
00169 typedef unsigned_type size_type;
00170 enum { max_size = max_size_ };
00171
00172 size_type size_;
00173 value_type array[max_size];
00174
00175 public:
00176 internal_bounded_stack() : size_(0) { }
00177
00178 void push(const value_type & x)
00179 {
00180 assert(size_ < max_size);
00181 array[size_++] = x;
00182 }
00183
00184 const value_type & top() const
00185 {
00186 assert(size_ > 0);
00187 return array[size_ - 1];
00188 }
00189
00190 void pop()
00191 {
00192 assert(size_ > 0);
00193 --size_;
00194 }
00195
00196 void clear()
00197 {
00198 size_ = 0;
00199 }
00200
00201 size_type size() const
00202 {
00203 return size_;
00204 }
00205
00206 bool empty() const
00207 {
00208 return size_ == 0;
00209 }
00210 };
00211 }
00212
00214
00215 __STXXL_END_NAMESPACE
00216
00217 #endif // !STXXL_PQ_HELPERS_HEADER
00218