STL源码剖析heap
来源:互联网 发布:nike淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/14 12:08
现在可以到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 heaptemplate<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 heaptemplate<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));}//sorttemplate<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.>
0 0
- STL源码剖析---heap
- STL源码剖析heap
- stl源码剖析 详细学习笔记heap
- STL源码剖析----stack、heap、queue
- STL源码剖析——heap
- STL源码剖析之heap,priority_queue【2013.11.25】
- STL源码剖析——最大堆heap
- STL源码剖析 - 第4章 序列式容器 - heap
- 《stl源码剖析》剖析
- STL源码:heap
- 【STL源码剖析读书笔记】【第4章】序列式容器之heap和priority_queue
- 【STL源码剖析读书笔记】自己实现Heap算法之MyHeap(底层容器用vector)
- STL 源码剖析读书笔记五:序列式容器之 heap、priority_queue、slist
- STL源码剖析-序列式容器之heap和priority_queue
- STL heap部分源码分析
- STL源码分析-heap部分
- STL源码剖析(1)
- STL源码剖析(2)
- String,StringBuilder和StringBuffer_思维导图总结
- 明细表删除序号及name属性索引未刷新
- 生成凭证代码
- DrawerLayout抽屉
- Linux VNC server的安装及简单配置使用
- STL源码剖析heap
- centos7+php+nginx
- Linux操作系统安装MetaQ
- Swift学习笔记一:语言特性
- StringUtils用法+StringUtils详细介绍
- 第三届河南省acm省赛 BUYING FEED
- iOS百度地图-BMK标注&覆盖物
- 165. Compare Version Numbers
- Linux-15-启动vmware无法加载安装linux系统的故障解决(L003-29)