00001
00002 #ifndef _INSTIGATE_STL_INTERNAL_HEADER_IN_ALGORITHM
00003 #error This is an internal header file used to implement Instigate STL.\
00004 It should never be included directly by a program.
00005 #endif
00006
00007 #ifndef INSTIGATE_STL_PARTIAL_SORT_HPP
00008 #define INSTIGATE_STL_PARTIAL_SORT_HPP
00009
00058
00059 #include "concept.hpp"
00060 #include "_make_heap.hpp"
00061 #include "_sort_heap.hpp"
00062
00063
00064 #include <generic/base.hpp>
00065
00066
00067
00068
00069 namespace instigate {
00070 namespace stl {
00071 template <typename R>
00072 inline void partial_sort(R b, R m, R e);
00073
00074 template <typename R, typename BP>
00075 inline void partial_sort(R b, R m, R e, BP p);
00076
00077 namespace implementation {
00078 template <typename R, typename TP>
00079 inline void partial_sort_aux(R b, R m, R e, TP*);
00080
00081 template <typename R, typename TP, typename BP>
00082 inline void partial_sort_aux(R b, R m, R e, TP*, BP p);
00083 }
00084 }
00085 }
00086
00121 template <typename R>
00122 inline void instigate::stl::partial_sort(R b, R m, R e)
00123 {
00124 namespace RI = instigate::stl::readable_iterator;
00125 typedef typename RI::interface<R>::value_type value_type;
00126 value_type* vt = 0;
00127 implementation::partial_sort_aux(b, m, e, vt);
00128 }
00129
00131 template <typename R, typename TP>
00132 inline void instigate::stl::implementation::partial_sort_aux(R b, R m, R e, TP*)
00133 {
00134 instigate::stl::make_heap(b, m);
00135 char k[sizeof(R)];
00136 R* i = 0;
00137 assign(i, copy_constructor(k, m));
00138 assert(0 != i);
00139 for(; less_than(*i, e); increment(*i)) {
00140 if(less_than(const_dereference(*i), const_dereference(b))) {
00141 instigate::stl::implementation::pop_heap_aux(
00142 b, m, *i, TP(*(*i)));
00143 }
00144 }
00145 instigate::stl::sort_heap(b, m);
00146 }
00147
00174 template <typename R, typename BP>
00175 inline void instigate::stl::partial_sort(R b, R m, R e, BP p)
00176 {
00177 namespace RI = instigate::stl::readable_iterator;
00178 typedef typename RI::interface<R>::value_type value_type;
00179 value_type* vt = 0;
00180 implementation::partial_sort_aux(b, m, e, vt, p);
00181 }
00182
00184 template <typename R, typename TP, typename BP>
00185 inline void instigate::stl::implementation::partial_sort_aux(R b, R m, R e,
00186 TP*, BP p)
00187 {
00188 namespace RA = instigate::stl::random_access_iterator;
00189 instigate::stl::make_heap(b, m, p);
00190 char* k = new char[sizeof(R)];
00191 assert(0 != k);
00192 R* i = 0;
00193 assign(i, copy_constructor(k, m));
00194 assert(0 != i);
00195 typedef typename RA::interface<R>::difference_type difference_type;
00196 for(; less_than(*i, e); increment(*i)) {
00197 if(less_than(const_dereference(*i), const_dereference(b))) {
00198 instigate::stl::implementation::pop_heap_aux(
00199 b, m, *i, TP(*(*i)), p);
00200 }
00201 }
00202 instigate::stl::sort_heap(b, m, p);
00203 }
00204
00205
00206
00207 #endif // INSTIGATE_STL_PARTIAL_SORT_HPP