📄 mstl_algorithm_heap.hpp
字号:
/*
The young Library
Copyright (c) 2005 by 杨桓
Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/
/*
* This file is derived from software bearing the following
* restrictions:
*
* Copyright (c) 1994
* Hewlett-Packard Company
*
* Permission to use, copy, modify, distribute and sell this
* software and its documentation for any purpose is hereby
* granted without fee, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation.
* Hewlett-Packard Company makes no representations about the
* suitability of this software for any purpose. It is provided
* "as is" without express or implied warranty.
*/
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_ALGORITHM_HEAP_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_ALGORITHM_HEAP_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "../mstl_iterator.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename RandomAccessIterator >
inline
void push_heap( RandomAccessIterator first, RandomAccessIterator last )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
push_heap_aux( first, diff_t((last - first) - 1),
diff_t(0), value_t(*(last - 1)) );
}
template< typename RandomAccessIterator, typename Integer, typename T >
void push_heap_aux( RandomAccessIterator first, Integer holeIndex,
Integer topIndex, T value )
{
Integer parent = (holeIndex - 1) / 2;
//没有至根节点并且新值大于父节点时,向上调整插入点索引和父节点索引
while( (holeIndex > topIndex) && (*(first + parent) < value) )
{
*(first + holeIndex) = *(first + parent);
holeIndex = parent;
parent = (holeIndex - 1) / 2;
}
*(first + holeIndex) = value;
}
template< typename RandomAccessIterator, typename StrictWeakOrdering >
inline
void push_heap( RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
push_heap_aux( first, diff_t((last - first) - 1),
diff_t(0), value_t(*(last - 1)), comp );
}
template< typename RandomAccessIterator, typename Integer, typename T,
typename StrictWeakOrdering >
void push_heap_aux( RandomAccessIterator first, Integer holeIndex,
Integer topIndex, T value, StrictWeakOrdering comp )
{
Integer 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< typename RandomAccessIterator >
inline
void pop_heap( RandomAccessIterator first, RandomAccessIterator last )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
value_t value = *(last - 1); //要调整的值
*(last - 1) = *first; //被弹出的值
adjust_heap( first, diff_t(0), diff_t(last - 1 - first), value );
}
template< typename RandomAccessIterator, typename Integer, typename T >
void adjust_heap( RandomAccessIterator first, Integer holeIndex,
Integer len, T value )
{
Integer topIndex = holeIndex;
Integer 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_aux( first, holeIndex, topIndex, value );
}
template< typename RandomAccessIterator, typename StrictWeakOrdering >
inline
void pop_heap( RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
value_t value = *(last - 1);
*(last - 1) = *first;
adjust_heap( first, diff_t(0), diff_t(last - 1 - first),
value, comp );
}
template< typename RandomAccessIterator, typename Integer, typename T,
typename StrictWeakOrdering >
void adjust_heap( RandomAccessIterator first, Integer holeIndex,
Integer len, T value, StrictWeakOrdering comp )
{
Integer topIndex = holeIndex;
Integer 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_aux( first, holeIndex, topIndex, value, comp );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename RandomAccessIterator >
void make_heap( RandomAccessIterator first, RandomAccessIterator last )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
if( last - first < 2 )
return;
diff_t len = last - first;
diff_t holeIndex = (len - 2) / 2; //最后一个非树叶节点的索引
while(1)
{
adjust_heap( first, holeIndex, len, value_t(*(first + holeIndex)) );
if( holeIndex == 0 )
return;
--holeIndex;
}
}
template< typename RandomAccessIterator, typename StrictWeakOrdering >
void make_heap( RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp )
{
typedef typename iterator_traits<RandomAccessIterator>::value_type
value_t;
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
if( last - first < 2 )
return;
diff_t len = last - first;
diff_t holeIndex = (len - 2) / 2;
while(1)
{
adjust_heap( first, holeIndex, len, value_t(*(first + holeIndex)), comp );
if( holeIndex == 0 )
return;
--holeIndex;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename RandomAccessIterator >
void sort_heap( RandomAccessIterator first, RandomAccessIterator last )
{
while( (last - first) > 1 )
{
pop_heap( first, last );
--last;
}
}
template< typename RandomAccessIterator, typename StrictWeakOrdering >
void sort_heap( RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp )
{
while( (last - first) > 1 )
{
pop_heap( first, last, comp );
--last;
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template< typename RandomAccessIterator >
bool is_heap( RandomAccessIterator first, RandomAccessIterator last )
{
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
diff_t len = last - first;
if( len < 2 )
return true;
diff_t parent = 0;
for( diff_t child = 1; child < len; ++child )
{
if( *(first + parent) < *(first + child) )
return false;
if( child % 2 == 0 )
++parent;
}
return true;
}
template< typename RandomAccessIterator, typename StrictWeakOrdering >
bool is_heap( RandomAccessIterator first, RandomAccessIterator last,
StrictWeakOrdering comp )
{
typedef typename iterator_traits<RandomAccessIterator>::difference_type
diff_t;
diff_t len = last - first;
if( len < 2 )
return true;
diff_t parent = 0;
for( diff_t child = 1; child < len; ++child )
{
if( comp(*(first + parent), *(first + child)) )
return false;
if( child % 2 == 0 )
++parent;
}
return true;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_END_NAMESPACE__
#endif
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -