40 inline bool operator()(
const T&a,
const T&b)
const {
return (a<b); }
43 template<
class T,
class Comparator=_DefaultComparator<T> >
56 inline const T& median_of_3(
const T& a,
const T& b,
const T& c)
const {
61 else if (compare(a, c))
65 else if (compare(a, c))
67 else if (compare(b, c))
75 inline int bitlog(
int n)
const {
77 for (k = 0; n != 1; n >>= 1)
85 inline void push_heap(
int p_first,
int p_hole_idx,
int p_top_index,T p_value,T* p_array)
const {
87 int parent = (p_hole_idx - 1) / 2;
88 while (p_hole_idx > p_top_index && compare(p_array[p_first + parent], p_value)) {
90 p_array[p_first + p_hole_idx] = p_array[p_first + parent];
92 parent = (p_hole_idx - 1) / 2;
94 p_array[p_first + p_hole_idx] = p_value;
97 inline void pop_heap(
int p_first,
int p_last,
int p_result, T p_value, T* p_array)
const {
99 p_array[p_result]=p_array[p_first];
100 adjust_heap(p_first,0,p_last-p_first,p_value,p_array);
102 inline void pop_heap(
int p_first,
int p_last,T* p_array)
const {
104 pop_heap(p_first,p_last-1,p_last-1,p_array[p_last-1],p_array);
107 inline void adjust_heap(
int p_first,
int p_hole_idx,
int p_len,T p_value,T* p_array)
const {
110 int top_index = p_hole_idx;
111 int second_child = 2 * p_hole_idx + 2;
113 while (second_child < p_len) {
115 if (compare(p_array[p_first + second_child],p_array[p_first + (second_child - 1)]))
118 p_array[p_first + p_hole_idx] = p_array[p_first + second_child];
119 p_hole_idx = second_child;
120 second_child = 2 * (second_child + 1);
123 if (second_child == p_len) {
124 p_array[p_first + p_hole_idx] = p_array[p_first + (second_child - 1)];
125 p_hole_idx = second_child - 1;
127 push_heap(p_first, p_hole_idx, top_index, p_value,p_array);
130 inline void sort_heap(
int p_first,
int p_last,T* p_array)
const {
132 while(p_last-p_first > 1) {
134 pop_heap(p_first,p_last--,p_array);
138 inline void make_heap(
int p_first,
int p_last,T* p_array)
const {
139 if (p_last - p_first < 2)
141 int len = p_last - p_first;
142 int parent = (len - 2)/2;
145 adjust_heap(p_first, parent, len, p_array[p_first + parent], p_array);
152 inline void partial_sort(
int p_first,
int p_last,
int p_middle,T* p_array)
const {
154 make_heap(p_first,p_middle,p_array);
155 for(
int i=p_middle;i<p_last;i++)
156 if (compare( p_array[i],p_array[p_first]))
157 pop_heap(p_first,p_middle,i,p_array[i],p_array);
158 sort_heap(p_first,p_middle,p_array);
161 inline void partial_select(
int p_first,
int p_last,
int p_middle,T* p_array)
const {
163 make_heap(p_first,p_middle,p_array);
164 for(
int i=p_middle;i<p_last;i++)
165 if (compare( p_array[i],p_array[p_first]))
166 pop_heap(p_first,p_middle,i,p_array[i],p_array);
169 inline int partitioner(
int p_first,
int p_last, T p_pivot, T* p_array)
const {
172 while (compare(p_array[p_first],p_pivot))
175 while (compare(p_pivot,p_array[p_last]))
178 if (!(p_first < p_last))
181 SWAP(p_array[p_first],p_array[p_last]);
186 inline void introsort(
int p_first,
int p_last, T* p_array,
int p_max_depth)
const {
188 while( p_last - p_first > INTROSORT_TRESHOLD ) {
190 if (p_max_depth == 0) {
191 partial_sort(p_first,p_last,p_last,p_array);
197 int cut = partitioner(
202 p_array[p_first + (p_last-p_first)/2],
208 introsort(cut,p_last,p_array,p_max_depth);
214 inline void introselect(
int p_first,
int p_nth,
int p_last, T* p_array,
int p_max_depth)
const {
216 while( p_last - p_first > 3 ) {
218 if (p_max_depth == 0) {
219 partial_select(p_first,p_nth+1,p_last,p_array);
226 int cut = partitioner(
231 p_array[p_first + (p_last-p_first)/2],
244 insertion_sort(p_first,p_last,p_array);
247 inline void unguarded_linear_insert(
int p_last,T p_value,T* p_array)
const {
250 while (compare(p_value,p_array[next])) {
251 p_array[p_last]=p_array[next];
255 p_array[p_last] = p_value;
258 inline void linear_insert(
int p_first,
int p_last,T*p_array)
const {
260 T val = p_array[p_last];
261 if (compare(val, p_array[p_first])) {
263 for (
int i=p_last; i>p_first; i--)
264 p_array[i]=p_array[i-1];
266 p_array[p_first] = val;
268 unguarded_linear_insert(p_last, val, p_array);
271 inline void insertion_sort(
int p_first,
int p_last,T* p_array)
const {
275 for (
int i=p_first+1; i!=p_last ; i++)
276 linear_insert(p_first,i,p_array);
279 inline void unguarded_insertion_sort(
int p_first,
int p_last,T* p_array)
const {
281 for (
int i=p_first; i!=p_last ; i++)
282 unguarded_linear_insert(i,p_array[i],p_array);
285 inline void final_insertion_sort(
int p_first,
int p_last,T* p_array)
const {
287 if (p_last - p_first > INTROSORT_TRESHOLD) {
288 insertion_sort(p_first,p_first+INTROSORT_TRESHOLD,p_array);
289 unguarded_insertion_sort(p_first+INTROSORT_TRESHOLD,p_last,p_array);
292 insertion_sort(p_first,p_last,p_array);
297 inline void sort_range(
int p_first,
int p_last,T* p_array)
const {
299 if (p_first != p_last) {
300 introsort(p_first, p_last,p_array,bitlog(p_last - p_first) * 2);
301 final_insertion_sort(p_first, p_last, p_array);
305 inline void sort(T* p_array,
int p_len)
const {
307 sort_range(0,p_len,p_array);
310 inline void nth_element(
int p_first,
int p_last,
int p_nth,T* p_array)
const {
312 if (p_first==p_last || p_nth==p_last)
314 introselect(p_first,p_nth,p_last,p_array,bitlog(p_last - p_first) * 2);
Definition: typedefs.h:257