From 023dd3a21217676c6eba56405f29ebc9987234ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=93=E8=88=AA?= <1210603696@qq.com> Date: Tue, 14 Oct 2014 16:19:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90pop=5Fheap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TinySTL/Algorithm.h | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/TinySTL/Algorithm.h b/TinySTL/Algorithm.h index e2dd94f..deb34f2 100644 --- a/TinySTL/Algorithm.h +++ b/TinySTL/Algorithm.h @@ -73,19 +73,19 @@ namespace TinySTL{ } //********** [make_heap] *************** //********* [Algorithm Complexity: O(N)] **************** - template - //heap上溯算法 - static void up(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last] - if (first != last){ - auto range = last - first + 1; - for (auto cur = last; rait > first; range /= 2){ - auto parent = first + (range / 2 - 1); - if (comp(*parent, *cur)) - TinySTL::swap(*parent, *cur); - cur = parent; - } - } - } + //template + ////heap上溯算法 + //static void up(RandomAccessIterator first, RandomAccessIterator last, Compare comp){//[first, last] + // if (first != last){ + // auto range = last - first + 1; + // for (auto cur = last; rait > first; range /= 2){ + // auto parent = first + (range / 2 - 1); + // if (comp(*parent, *cur)) + // TinySTL::swap(*parent, *cur); + // cur = parent; + // } + // } + //} template //heap下降算法 static void down(RandomAccessIterator first, RandomAccessIterator last, @@ -125,9 +125,16 @@ namespace TinySTL{ void push_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); //********* [pop_heap] *************** template - void pop_heap(RandomAccessIterator first, RandomAccessIterator last); + void pop_heap(RandomAccessIterator first, RandomAccessIterator last){ + TinySTL::pop_heap(first, last, + TinySTL::less::value_type>()); + } template - void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp); + void pop_heap(RandomAccessIterator first, RandomAccessIterator last, Compare comp){ + TinySTL::swap(*first, *(last - 1)); + if (last - first >= 2) + down(first, last - 2, first, comp); + } //********* [sort_heap] *************** template void sort_heap(RandomAccessIterator first, RandomAccessIterator last);