STL之heap实现
来源:互联网 发布:linux shell 输入 编辑:程序博客网 时间:2024/05/23 23:12
#ifndef __HEAP_H#define _HEAP_H#include"iterator_traits.h"template<class RandomAccessIterator>inline void mypush_heap(RandomAccessIterator first, RandomAccessIterator last){__push_heap_aux(first, last, distance_type(first), value_type(first));}template<class RandomAccessIterator,class Distance,class T>inline void __push_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Distance*, T*){__push_heap(first, Distance((last - first) - 1), Distance(0), T(*(last - 1)));}template<class RandomAccessIterator,class Distance,class T>inline void __push_heap(RandomAccessIterator first, Distance holeIndex, Distance topIndex, T value){Distance parent = (holeIndex - 1) / 2;while (holeIndex > topIndex&&*(first + parent) < value){*(first + holeIndex) = *(first + parent);holeIndex = parent;parent = (holeIndex - 1) / 2;}*(first + holeIndex) = value;}template<class RandomAccessIterator,class Compare>inline void mypush_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp){__push_heap_aux(first, last, comp,distance_type(first), value_type(first));}template<class RandomAccessIterator, class Compare,class Distance, class T>inline void __push_heap_aux(RandomAccessIterator first, RandomAccessIterator last, Compare comp,Distance*, T*){__push_heap(first, Distance((last - first) - 1), Distance(0), T(*(last - 1)),comp);}template<class RandomAccessIterator, class Distance, class T,class Compare>inline void __push_heap(RandomAccessIterator first, Distance holeIndex, Distance topIndex, T value,Compare comp){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>inline void mypop_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp){__pop_heap_aux(first, last, value_type(first),comp);}template<class RandomAccessIterator,class T,class Compare>inline void __pop_heap_aux(RandomAccessIterator first, RandomAccessIterator last, T*,Compare comp){__pop_heap(first, last - 1, last - 1, T(*(last - 1)),comp,distance_type(first));}template<class RandomAccessIterator,class Distance,class T,class Compare>inline void __pop_heap(RandomAccessIterator first, RandomAccessIterator last, RandomAccessIterator result, T value, Compare comp ,Distance*){*result = *first;__adjust_heap(first, Distance(0), Distance(last - first), value,comp);}template<class RandomAccessIterator,class Distance,class T,class Compare>inline void __adjust_heap(RandomAccessIterator first, Distance holeIndex, Distance len, T value,Compare comp){Distance topIndex = holeIndex;Distance secondChild = 2 * holeIndex + 2;while (secondChild < len){if (comp(*(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);}template<class RandomAccessIterator>inline void mymake_heap(RandomAccessIterator first, RandomAccessIterator last){__make_heap(first, last, value_type(first), distance_type(first));}template<class RandomAccessIterator,class T,class Distance>inline void __make_heap(RandomAccessIterator first, RandomAccessIterator last, T*, Distance*){if (last - first < 2)return;Distance len = last - first;Distance parent = (len - 2) / 2;while (true){__adjust_heap(first, parent, len, T(*(first + parent)));if (parent == 0)return;parent--;}}template<class RandomAccessIterator,class Compare>inline void mymake_heap(RandomAccessIterator first, RandomAccessIterator last,Compare comp){__make_heap(first, last, comp,value_type(first), distance_type(first));}template<class RandomAccessIterator, class Compare, class T, class Distance>inline void __make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp, T*, Distance*){if (last - first < 2)return;Distance len = last - first;Distance parent = (len - 2) / 2;while (true){__adjust_heap(first, parent, len, T(*(first + parent)),comp);if (parent == 0)return;parent--;}}template<class RandomAccessIterator,class Compare>inline void mysort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){while (last - first > 1)pop_heap(first, last--,comp);}#endif
0 0
- STL之heap实现
- 使用stl实现heap
- STL heap 实现
- STL 之 heap
- STL之heap
- STL之heap 堆
- STL之heap
- STL算法之heap
- STL之heap
- STL的 heap 堆实现
- STL系列之五heap
- STL之heap相关用法
- hdu2544最短路dij+stl 实现heap
- STL 简单 binary heap 的实现
- 【STL源码剖析读书笔记】自己实现Heap算法之MyHeap(底层容器用vector)
- STL之heap及priority_queue实现以及源码中的两个小问题
- SGI-STL学习笔记之heap算法
- STL系列之四 heap 堆
- 黑马程序员——C语言学习笔记03 变量在内存中存储的细节、位运算
- HDU-4430-Yukari's Birthday-暴力+二分
- STL之deque实现
- Android开发,连接到指定WIFI
- Jaxb
- STL之heap实现
- Android与js交互实例
- OS X 10.11 下解决Python-MysqlDB无法使用的问题
- STL之statck实现
- Best Route in a Grid
- 浅谈互联网的应聘
- 1_jingjing1
- 粒子动画处理
- STL之queue实现