Stxxl
1.4.0
|
00001 /*************************************************************************** 00002 * include/stxxl/bits/containers/pq_helpers.h 00003 * 00004 * Part of the STXXL. See http://stxxl.sourceforge.net 00005 * 00006 * Copyright (C) 1999 Peter Sanders <sanders@mpi-sb.mpg.de> 00007 * Copyright (C) 2003, 2004, 2007 Roman Dementiev <dementiev@mpi-sb.mpg.de> 00008 * Copyright (C) 2007, 2009 Johannes Singler <singler@ira.uka.de> 00009 * Copyright (C) 2007, 2008 Andreas Beckmann <beckmann@cs.uni-frankfurt.de> 00010 * 00011 * Distributed under the Boost Software License, Version 1.0. 00012 * (See accompanying file LICENSE_1_0.txt or copy at 00013 * http://www.boost.org/LICENSE_1_0.txt) 00014 **************************************************************************/ 00015 00016 #ifndef STXXL_PQ_HELPERS_HEADER 00017 #define STXXL_PQ_HELPERS_HEADER 00018 00019 #include <queue> 00020 00021 __STXXL_BEGIN_NAMESPACE 00022 00023 //! \addtogroup stlcontinternals 00024 //! 00025 //! \{ 00026 00027 /*! \internal 00028 */ 00029 namespace priority_queue_local 00030 { 00031 /** 00032 * @brief Similar to std::priority_queue, with the following differences: 00033 * - Maximum size is fixed at construction time, so an array can be used. 00034 * - Provides access to underlying heap, so (parallel) sorting in place is possible. 00035 * - Can be cleared "at once", without reallocation. 00036 */ 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 // concept requirements 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 // See queue::heap for notes on these names. 00053 _Sequence heap; 00054 _Compare comp; 00055 size_type current_size; 00056 00057 public: 00058 /** 00059 * @brief Default constructor creates no elements. 00060 */ 00061 explicit 00062 internal_priority_queue(size_type capacity) 00063 : heap(capacity), current_size(0) 00064 { } 00065 00066 /** 00067 * Returns true if the %queue is empty. 00068 */ 00069 bool 00070 empty() const 00071 { return current_size == 0; } 00072 00073 /** Returns the number of elements in the %queue. */ 00074 size_type 00075 size() const 00076 { return current_size; } 00077 00078 /** 00079 * Returns a read-only (constant) reference to the data at the first 00080 * element of the %queue. 00081 */ 00082 const_reference 00083 top() const 00084 { 00085 return heap.front(); 00086 } 00087 00088 /** 00089 * @brief Add data to the %queue. 00090 * @param __x Data to be added. 00091 * 00092 * This is a typical %queue operation. 00093 * The time complexity of the operation depends on the underlying 00094 * sequence. 00095 */ 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 00104 /** 00105 * @brief Removes first element. 00106 * 00107 * This is a typical %queue operation. It shrinks the %queue 00108 * by one. The time complexity of the operation depends on the 00109 * underlying sequence. 00110 * 00111 * Note that no data is returned, and if the first element's 00112 * data is needed, it should be retrieved before pop() is 00113 * called. 00114 */ 00115 void 00116 pop() 00117 { 00118 std::pop_heap(heap.begin(), heap.begin() + current_size, comp); 00119 --current_size; 00120 } 00121 00122 /** 00123 * @brief Sort all contained elements, write result to @c target. 00124 */ 00125 void sort_to(value_type * target) 00126 { 00127 check_sort_settings(); 00128 potentially_parallel:: 00129 sort(heap.begin(), heap.begin() + current_size, comp); 00130 std::reverse_copy(heap.begin(), heap.begin() + current_size, target); 00131 } 00132 00133 /** 00134 * @brief Remove all contained elements. 00135 */ 00136 void clear() 00137 { 00138 current_size = 0; 00139 } 00140 }; 00141 00142 /** 00143 * @brief Inverts the order of a comparison functor by swapping its arguments. 00144 */ 00145 template <class Predicate, typename first_argument_type, typename second_argument_type> 00146 class invert_order 00147 { 00148 protected: 00149 Predicate pred; 00150 00151 public: 00152 explicit 00153 invert_order(const Predicate & _pred) : pred(_pred) { } 00154 00155 bool operator () (const first_argument_type & x, const second_argument_type & y) const 00156 { 00157 return pred(y, x); 00158 } 00159 }; 00160 00161 00162 /** 00163 * \brief Similar to std::stack, with the following differences: 00164 * - Maximum size is fixed at compilation time, so an array can be used. 00165 * - Can be cleared "at once", without reallocation. 00166 */ 00167 template <typename Tp_, unsigned_type max_size_> 00168 class internal_bounded_stack 00169 { 00170 typedef Tp_ value_type; 00171 typedef unsigned_type size_type; 00172 enum { max_size = max_size_ }; 00173 00174 size_type size_; 00175 value_type array[max_size]; 00176 00177 public: 00178 internal_bounded_stack() : size_(0) { } 00179 00180 void push(const value_type & x) 00181 { 00182 assert(size_ < max_size); 00183 array[size_++] = x; 00184 } 00185 00186 const value_type & top() const 00187 { 00188 assert(size_ > 0); 00189 return array[size_ - 1]; 00190 } 00191 00192 void pop() 00193 { 00194 assert(size_ > 0); 00195 --size_; 00196 } 00197 00198 void clear() 00199 { 00200 size_ = 0; 00201 } 00202 00203 size_type size() const 00204 { 00205 return size_; 00206 } 00207 00208 bool empty() const 00209 { 00210 return size_ == 0; 00211 } 00212 }; 00213 } //priority_queue_local 00214 00215 //! \} 00216 00217 __STXXL_END_NAMESPACE 00218 00219 #endif // !STXXL_PQ_HELPERS_HEADER 00220 // vim: et:ts=4:sw=4