inline void __push_heap_aux(_RandomAccessIterator __first, _RandomAccessIterator __last, _Distance*, _Tp*) { __push_heap(__first, _Distance((__last - __first) - 1), _Distance(0), _Tp(*(__last - 1))); }
inline void __push_heap_aux(_RandomAccessIterator __first, _RandomAccessIterator __last, _Distance*, _Tp*) { //__last - __first) - 1表示插入后元素的个数,也是容器的最后一个下标数字 //新插入的元素必须位于容器的末尾 __push_heap(__first, _Distance((__last - __first) - 1), _Distance(0), _Tp(*(__last - 1))); }
void __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex, _Distance __len, _Tp __value) { _Distance __topIndex = __holeIndex; _Distance __secondChild = 2 * __holeIndex + 2; while (__secondChild < __len) { if (*(__first + __secondChild) < *(__first + (__secondChild - 1))) __secondChild--; *(__first + __holeIndex) = *(__first + __secondChild); __holeIndex = __secondChild; __secondChild = 2 * (__secondChild + 1); } if (__secondChild == __len) { *(__first + __holeIndex) = *(__first + (__secondChild - 1)); __holeIndex = __secondChild - 1; } __push_heap(__first, __holeIndex, __topIndex, __value); }
void __adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex, _Distance __len, _Tp __value) { _Distance __topIndex = __holeIndex;//根节点标号 _Distance __secondChild = 2 * __holeIndex + 2;//获取子节点 while (__secondChild < __len) {//若子节点标号比总的标号数小 if (*(__first + __secondChild) < *(__first + (__secondChild - 1))) __secondChild--;//找出堆中最大关键字值的节点 //若堆中存在比新根节点元素(即原始堆最后节点关键字值)大的节点,则交换位置 *(__first + __holeIndex) = *(__first + __secondChild); __holeIndex = __secondChild;//更新父节点 __secondChild = 2 * (__secondChild + 1);//更新子节点 } if (__secondChild == __len) { *(__first + __holeIndex) = *(__first + (__secondChild - 1)); __holeIndex = __secondChild - 1; } __push_heap(__first, __holeIndex, __topIndex, __value); }
void __adjust_heap(_Iterator __first, _Distance __holeIndex, _Distance __len, _Tp __value) { const _Distance __topIndex = __holeIndex; _Distance __secondChild = __holeIndex; while (__secondChild < (__len - 1) / 2) { __secondChild = 2 * (__secondChild + 1); if (comp(*(__first + __secondChild), *(__first + (__secondChild - 1)))) __secondChild--; *(__first + __holeIndex) = *(__first + __secondChild); __holeIndex = __secondChild; } if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2) { __secondChild = 2 * (__secondChild + 1); *(__first + __holeIndex) = *(__first + __secondChild - 1); __holeIndex = __secondChild - 1; } __push_heap(__first, __holeIndex, __topIndex, __value); }
void push_heap(_Iterator __first, _Iterator __last) { _Value __value = *(__last - 1); __push_heap(__first, _Distance((__last - __first) - 1), _Distance(0), __value); }