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_MAKE_HEAP_HPP
00008 #define INSTIGATE_STL_MAKE_HEAP_HPP
00009
00059
00060 #include "concept.hpp"
00061 #include "_basis.hpp"
00062 #include "_pop_heap.hpp"
00063
00064
00065 #include <generic/base.hpp>
00066
00067
00068
00069
00070
00071 namespace instigate {
00072 namespace stl {
00073 template <typename I>
00074 void make_heap(I, I);
00075
00076 template <typename I, typename C>
00077 void make_heap(I, I, C);
00078 }
00079 }
00080
00114 template <typename I>
00115 void instigate::stl::make_heap(I b, I e)
00116 {
00117 typedef typename stl::random_access_iterator::interface<I>::
00118 difference_type D;
00119 typedef typename stl::readable_iterator::interface<I>::value_type V;
00120 CHECK(stl::random_access_iterator::requirements<I>);
00121 CHECK(stl::lvalue_iterator::requirements<I>);
00122 CHECK(instigate::generic::less_than_comparable::requirements<V>);
00123 if (stl::distance(b, e) < 2) {
00124 return;
00125 }
00126 D l = stl::distance(b, e);
00127 D p = (l - 2) / 2;
00128 char buf[sizeof(I)];
00129 I* buf_p = 0;
00130 assign(buf_p, initialize<I>(buf));
00131 while (true) {
00132 assert(0 != buf_p);
00133 assign(*buf_p, b);
00134 stl::advance(*buf_p, p);
00135 implementation::adjust_heap_aux(b, p, l,
00136 const_dereference(*buf_p));
00137 if (0 == p) {
00138 return;
00139 }
00140 --p;
00141 }
00142 }
00143
00167 template <typename I, typename C>
00168 void instigate::stl::make_heap(I b, I e, C c)
00169 {
00170 typedef typename stl::random_access_iterator::interface<I>::
00171 difference_type D;
00172 CHECK(stl::random_access_iterator::requirements<I>);
00173 CHECK(stl::lvalue_iterator::requirements<I>);
00174 CHECK(stl::binary_predicate::requirements<C>);
00175 if (stl::distance(b, e) < 2) {
00176 return;
00177 }
00178 D l = stl::distance(b, e);
00179 D p = (l - 2) / 2;
00180 char buf[sizeof(I)];
00181 I* buf_p = 0;
00182 assign(buf_p, initialize<I>(buf));
00183 while (true) {
00184 assert(0 != buf_p);
00185 assign(*buf_p, b);
00186 stl::advance(*buf_p, p);
00187 implementation::adjust_heap_aux(b, p, l,
00188 const_dereference(*buf_p), c);
00189 if (0 == p) {
00190 return;
00191 }
00192 --p;
00193 }
00194 }
00195
00196
00197
00198 #endif // INSTIGATE_STL_MAKE_HEAP_HPP