完成is_heap
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
namespace TinySTL{
|
namespace TinySTL{
|
||||||
//********* [fill] ********************
|
//********* [fill] ********************
|
||||||
|
//********* [Algorithm Complexity: O(N)] ****************
|
||||||
template<class ForwardIterator, class T>
|
template<class ForwardIterator, class T>
|
||||||
void fill(ForwardIterator first, ForwardIterator last, const T& value)
|
void fill(ForwardIterator first, ForwardIterator last, const T& value)
|
||||||
{
|
{
|
||||||
@@ -20,6 +21,7 @@ namespace TinySTL{
|
|||||||
memset(first, static_cast<unsigned char>(value), (last - first) * sizeof(wchar_t));
|
memset(first, static_cast<unsigned char>(value), (last - first) * sizeof(wchar_t));
|
||||||
}
|
}
|
||||||
//********* [fill_n] ********************
|
//********* [fill_n] ********************
|
||||||
|
//********* [Algorithm Complexity: O(N)] ****************
|
||||||
template<class OutputIterator, class Size, class T>
|
template<class OutputIterator, class Size, class T>
|
||||||
OutputIterator fill_n(OutputIterator first, Size n, const T& value)
|
OutputIterator fill_n(OutputIterator first, Size n, const T& value)
|
||||||
{
|
{
|
||||||
@@ -40,6 +42,7 @@ namespace TinySTL{
|
|||||||
return first + n;
|
return first + n;
|
||||||
}
|
}
|
||||||
//************ [find] ****************
|
//************ [find] ****************
|
||||||
|
//********* [Algorithm Complexity: O(N)] ****************
|
||||||
template<class InputIterator, class T>
|
template<class InputIterator, class T>
|
||||||
InputIterator find(InputIterator first, InputIterator last, const T& val){
|
InputIterator find(InputIterator first, InputIterator last, const T& val){
|
||||||
for (; first != last; ++first){
|
for (; first != last; ++first){
|
||||||
@@ -49,6 +52,7 @@ namespace TinySTL{
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
//*********** [min] ********************
|
//*********** [min] ********************
|
||||||
|
//********* [Algorithm Complexity: O(1)] ****************
|
||||||
template <class T>
|
template <class T>
|
||||||
const T& min(const T& a, const T& b){
|
const T& min(const T& a, const T& b){
|
||||||
return !(b < a) ? a : b;
|
return !(b < a) ? a : b;
|
||||||
@@ -58,6 +62,7 @@ namespace TinySTL{
|
|||||||
return !comp(b, a) ? a : b;
|
return !comp(b, a) ? a : b;
|
||||||
}
|
}
|
||||||
//*********** [max] ********************
|
//*********** [max] ********************
|
||||||
|
//********* [Algorithm Complexity: O(1)] ****************
|
||||||
template <class T>
|
template <class T>
|
||||||
const T& max(const T& a, const T& b){
|
const T& max(const T& a, const T& b){
|
||||||
return (a < b) ? b : a;
|
return (a < b) ? b : a;
|
||||||
@@ -67,7 +72,9 @@ namespace TinySTL{
|
|||||||
return (copm(a, b)) ? b : a;
|
return (copm(a, b)) ? b : a;
|
||||||
}
|
}
|
||||||
//********** [make_heap] ***************
|
//********** [make_heap] ***************
|
||||||
|
//********* [Algorithm Complexity: O(N)] ****************
|
||||||
template<class RandomAccessIterator, class Compare>
|
template<class RandomAccessIterator, class Compare>
|
||||||
|
//heap<61><70><EFBFBD><EFBFBD><EFBFBD>㷨
|
||||||
static void up(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last]
|
static void up(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last]
|
||||||
if (first != last){
|
if (first != last){
|
||||||
auto range = last - first + 1;
|
auto range = last - first + 1;
|
||||||
@@ -80,20 +87,21 @@ namespace TinySTL{
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
template<class RandomAccessIterator, class Compare>
|
template<class RandomAccessIterator, class Compare>
|
||||||
static void down(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last]
|
//heap<61>½<EFBFBD><C2BD>㷨
|
||||||
|
static void down(RandomAccessIterator first, RandomAccessIterator last,
|
||||||
|
RandomAccessIterator head, Compare comp){//1.[first, last], 2.headr points the header of the heap
|
||||||
if (first != last){
|
if (first != last){
|
||||||
decltype(last - first) range = 1;
|
const auto range = last - head + 1;
|
||||||
for (auto cur = first; cur < last;range = cur - first + 1){
|
auto index = first - head;
|
||||||
auto child = first + (range * 2 - 1);//get the left child
|
auto leftChildIndex = index * 2 + 1;
|
||||||
if (child <= last){
|
for (auto cur = first; leftChildIndex < range && cur < last; leftChildIndex = index * 2 + 1){
|
||||||
if ((child + 1) <= last && *(child + 1) > *child)
|
auto child = head + leftChildIndex;//get the left child
|
||||||
child = child + 1;//right child
|
if ((child + 1) <= last && *(child + 1) > *child)//cur has a right child
|
||||||
if (comp(*cur, *child))
|
child = child + 1;
|
||||||
TinySTL::swap(*cur, *child);
|
if (comp(*cur, *child))
|
||||||
cur = child;
|
TinySTL::swap(*cur, *child);
|
||||||
}
|
cur = child;
|
||||||
else
|
index = cur - head;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -105,8 +113,9 @@ namespace TinySTL{
|
|||||||
template <class RandomAccessIterator, class Compare>
|
template <class RandomAccessIterator, class Compare>
|
||||||
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
|
void make_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
|
||||||
const auto range = last - first;
|
const auto range = last - first;
|
||||||
for (auto rait = first + range - 1; rait > first; --rait){
|
for (auto cur = first + range / 2 - 1; cur >= first; --cur){
|
||||||
down(first, rait, comp);
|
down(cur, last - 1, first, comp);
|
||||||
|
if (cur == first) return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//********* [push_heap] ***************
|
//********* [push_heap] ***************
|
||||||
@@ -126,9 +135,23 @@ namespace TinySTL{
|
|||||||
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
|
void sort_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
|
||||||
//********* [is_heap] ***************
|
//********* [is_heap] ***************
|
||||||
template <class RandomAccessIterator>
|
template <class RandomAccessIterator>
|
||||||
bool is_heap(RandomAccessIterator first, RandomAccessIterator last);
|
bool is_heap(RandomAccessIterator first, RandomAccessIterator last){
|
||||||
|
return TinySTL::is_heap(first, last,
|
||||||
|
TinySTL::less<typename TinySTL::iterator_traits<RandomAccessIterator>::value_type>());
|
||||||
|
}
|
||||||
template <class RandomAccessIterator, class Compare>
|
template <class RandomAccessIterator, class Compare>
|
||||||
bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp);
|
bool is_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){
|
||||||
|
const auto range = last - first;
|
||||||
|
auto index = range / 2 - 1;
|
||||||
|
for (auto cur = first + range / 2 - 1; cur >= first; --cur, --index){
|
||||||
|
if (*(first + (index * 2 + 1)) > *cur ||//left child > cur
|
||||||
|
((first + (index * 2 + 2)) <= last && *(first + (index * 2 + 2)) > *cur))//right child > cur
|
||||||
|
return false;
|
||||||
|
if (cur == first)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user