现在可以到github上下载目前为止的所有文件了。
其中,hjstl_vector目前无法正常工作,后期会回来修复bug。
下面是地址:https://github.com/pandening/HJSTL
#ifndef _HJ_STL_HEAP_
#define _HJ_STL_HEAP_
/*
* Author:hujian
* Time:2016/5/4
* discription:this file is about heap.
*
* NOTICE:you should not use hjstl_vector in anywhere.
* because the hjstl_vector has too much bugs.
* it not work till now.
*
*/
#include "hjstl_iterator.h"
template<class RandomAccessIterator,class Distance,class Type>
void __hjstl_push_heap(RandomAccessIterator first, Distance holeIndex,
Distance topIndex, Type value){
//find the parent of the holeindex.
Distance parent = (holeIndex - 1) / 2;
while (parent > topIndex&&*(first + parent) < value){
*(first + holeIndex) = *(first + parent);
holeIndex = parent;
parent = (holeIndex - 1) / 2;
}
*(first + holeIndex) = value;
}
template<class RandomAccessIterator,class Distance,class Type>
void __hjstl_push_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Distance*, Type*){
__hjstl_push_heap(first, hjstl_distance((last - first) - 1), hjstl_distance(0), Type(*(last - 1)));
}
template<class RandomAccessIterator>
inline void hjstl_push_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__hjstl_push_heap_aux(first, last, hjstl_distance_type(first), hjstl_value_type(first));
}
template<class RandomAccessIterator,class Distance,class Type,class Compare>
void __hjstl_push_heap(RandomAccessIterator first, Distance holeIndex,
Distance topIndex, Type value, Compare comp){
//find the parent
Distance parent = (holeIndex - 1) / 2;
while (holeIndex > topIndex&&comp(*(first + parent), value)){
*(first + holeIndex) = *(first + parent);
holeIndex = parent;
parent = (holeIndex - 1) / 2;
}
*(first + holeIndex) = value;
}
template<class RandomAccessIterator,class Compare,class Distance,class Type>
void __hjstl_push_heap_aux(RandomAccessIterator first, RandomAccessIterator last,
Compare comp, Distance*, Type*){
__hjstl_push_heap(first, hjstl_distance((last - first) - 1), hjstl_distance(0), Type(*(last - 1)), comp);
}
template<class RandomAccessIterator,class Compare>
void hjstl_push_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp){
__hjstl_push_heap_aux(first, last, comp, hjstl_distance_type(first), hjstl_value_type(first));
}
//adjust the heap
template<class RandomAccessIterator,class Distance,class Type>
void __hjstl_adjust_heap(RandomAccessIterator first, Distance holeIndex, Distance len, Type value)
{
Distance topIndex = holeIndex;
//the right child of the holeIndex
Distance secondChild = 2 * (holeIndex + 1);
while (secondChild < len){
//the secondChild is the max value(leftchild,rightchild)
if (*(first + secondChild) < *(first + (secondChild - 1))){
secondChild--;
}
*(first + holeIndex) = *(first + secondChild);
holeIndex = secondChild;
secondChild = 2 * (secondChild + 1);
}
//if no right child.just let the holeindex=>the left child.
if (secondChild == len){
*(first + holeIndex) = *(first + (secondChild - 1));
holeIndex = secondChild - 1;
}
//push heap again
__hjstl_push_heap(first, holeIndex, topIndex, value);
}
template<class RandomAccessIterator,class Distance,class Type,class Compare>
void __hjstl_adjust_heap(RandomAccessIterator first, Distance holeIndex,
Distance len, Type value, Compare comp)
{
Distance topIndex = holeIndex;
Distance secondChild = 2 * (holeIndex + 1);
while (secondChild < len){
if (comp(*(first + secondChild), *(first + (secondChild - 1))))
secondChild--;
*(first + holeIndex) = *(first + secondChild);
holeIndex = secondChild;
//the secondChild is the right child.
secondChild = 2 * (secondChild + 1);
}
if (secondChild == len){
*(first + holeIndex) = *(first + (secondChild - 1));
holeIndex = secondChild - 1;
}
//adjust again
__hjstl_push_heap(first, holeIndex, topIndex, value, comp);
}
template<class RandomAccessIterator,class Type,class Distance>
void __hjstl_pop_heap(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator result, Type value, Distance*)
{
/*the last value is the result*/
*result = *first;
__hjstl_adjust_heap(first, Distance(0), Distance(last - first), value);
}
template<class RandomAccessIterator,class Type>
void __hjstl_pop_heap_aux(RandomAccessIterator first, RandomAccessIterator last,
Type*){
__hjstl_pop_heap(first, last - 1, last - 1, Type(*(last - 1)), hjstl_distance_type(first));
}
template<class RandomAccessIterator>
void hjstl_pop_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__hjstl_pop_heap_aux(first, last, hjstl_value_type(first));
}
///-------------
template<class RandomAccessIterator, class Type, class Distance,class Compare>
void __hjstl_pop_heap(RandomAccessIterator first, RandomAccessIterator last,
RandomAccessIterator result, Type value,Compare comp,Distance*)
{
/*the last value is the result*/
*result = *first;
__hjstl_adjust_heap(first, Distance(0), Distance(last - first), value,comp);
}
template<class RandomAccessIterator, class Type,class Compare>
void __hjstl_pop_heap_aux(RandomAccessIterator first, RandomAccessIterator last,
Type*,Compare comp){
__hjstl_pop_heap(first, last - 1, last - 1, Type(*(last - 1)),
comp,hjstl_distance_type(first));
}
template<class RandomAccessIterator,class Compare>
void hjstl_pop_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp)
{
__hjstl_pop_heap_aux(first, last, hjstl_value_type(first),comp);
}
///--make heap
template<class RandomAccessIterator,class Type,class Distance>
void __hjstl_make_heap(RandomAccessIterator first, RandomAccessIterator last,
Type*, Distance*)
{
if (last - first < 2) return;//0 1
Distance len = last - first;
//this is the first child-tree we need to make-heap's root
Distance parent = (len - 2) / 2;
while (1){
__hjstl_adjust_heap(first, parent, len, Type(*(first + parent)));
if (parent == 0) return;/*end.*/
parent--;
}
}
template<class RandomAccessIterator>
void hjstl_make_heap(RandomAccessIterator first, RandomAccessIterator last)
{
__hjstl_make_heap(first, last, hjstl_value_type(first), hjstl_distance_type(first));
}
/--
template<class RandomAccessIterator, class Type, class Distance,class Compare>
void __hjstl_make_heap(RandomAccessIterator first, RandomAccessIterator last,
Compare comp,Type*, Distance*)
{
if (last - first < 2) return;//0 1
Distance len = last - first;
//this is the first child-tree we need to make-heap's root
Distance parent = (len - 2) / 2;
while (1){
__hjstl_adjust_heap(first, parent, len, Type(*(first + parent)),comp);
if (parent == 0) return;/*end.*/
parent--;
}
}
template<class RandomAccessIterator,class Compare>
void hjstl_make_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp)
{
__hjstl_make_heap(first, last,comp,hjstl_value_type(first), hjstl_distance_type(first));
}
//sort
template<class RandomAccessIterator,class Compare>
void hjstl_sort_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp)
{
while (last - first > 1){
hjstl_pop_heap(first, last--,comp);
}
}
template<class RandomAccessIterator>
void hjstl_srot_heap(RandomAccessIterator first, RandomAccessIterator last)
{
while (last - first > 1){
hjstl_pop_heap(first, last--);
}
}
#endif /*end of hjst heap*/
///<2016/5/4 hujian /nankai.>