From 8a99a2725c32fc5a710f60ee1a041e002b41fe5f Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Tue, 14 Jan 2020 12:46:17 +0000 Subject: [PATCH] Merge remote-tracking branch 'origin/development' # Conflicts: # include/etl/version.h # library.json # library.properties # support/Release notes.txt --- include/etl/algorithm.h | 37 +++++++++++++++++++++++++++++++++++++ include/etl/version.h | 2 +- library.json | 2 +- library.properties | 2 +- support/Release notes.txt | 4 ++++ test/test_algorithm.cpp | 30 ++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 3 deletions(-) diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 39ea178f..1cd8e4f7 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -2938,6 +2938,43 @@ namespace etl etl::insertion_sort(first, last, etl::less::value_type>()); } + //*************************************************************************** + /// Sorts the elements using heap sort. + /// Uses user defined comparison. + ///\ingroup algorithm + //*************************************************************************** + template + void heap_sort(TIterator first, TIterator last, TCompare compare) + { + if (!etl::is_heap(first, last, compare)) + { + etl::make_heap(first, last, compare); + } + + while (first != last) + { + etl::pop_heap(first, last--, compare); + } + } + + //*************************************************************************** + /// Sorts the elements using heap sort. + ///\ingroup algorithm + //*************************************************************************** + template + void heap_sort(TIterator first, TIterator last) + { + if (!etl::is_heap(first, last)) + { + etl::make_heap(first, last); + } + + while (first != last) + { + etl::pop_heap(first, last--); + } + } + //*************************************************************************** /// Returns the maximum value. //*************************************************************************** diff --git a/include/etl/version.h b/include/etl/version.h index c8ea0c5d..78e56a81 100644 --- a/include/etl/version.h +++ b/include/etl/version.h @@ -38,7 +38,7 @@ SOFTWARE. ///\ingroup utilities #define ETL_VERSION_MAJOR 16 -#define ETL_VERSION_MINOR 1 +#define ETL_VERSION_MINOR 2 #define ETL_VERSION_PATCH 0 #define ETL_VERSION ETL_STRINGIFY(ETL_VERSION_MAJOR) "." ETL_STRINGIFY(ETL_VERSION_MINOR) "." ETL_STRINGIFY(ETL_VERSION_PATCH) diff --git a/library.json b/library.json index ee63d96e..c70a67fb 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "Embedded Template Library", - "version": "16.1.0", + "version": "16.2.0", "authors": { "name": "John Wellbelove", "email": "john.wellbelove@etlcpp.com" diff --git a/library.properties b/library.properties index 936ca6dd..6f7b71d9 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Embedded Template Library -version=16.1.0 +version=16.2.0 author= John Wellbelove maintainer=John Wellbelove license=MIT diff --git a/support/Release notes.txt b/support/Release notes.txt index fccb353d..bbaa2485 100644 --- a/support/Release notes.txt +++ b/support/Release notes.txt @@ -1,3 +1,7 @@ +=============================================================================== +16.2.0 +Added etl::heap_sort to algorithms. + =============================================================================== 16.1.0 Added compile time option to make string truncation an error. diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index e5238719..d0b56114 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -1856,6 +1856,36 @@ namespace CHECK(is_same); } + //************************************************************************* + TEST(heap_sort_default) + { + std::vector initial_data = { NDC(1, 1), NDC(2, 1), NDC(3, 1), NDC(2, 2), NDC(3, 2), NDC(4, 1), NDC(2, 3), NDC(3, 3), NDC(5, 1) }; + + std::vector data1(initial_data); + std::vector data2(initial_data); + + std::sort(data1.begin(), data1.end()); + etl::heap_sort(data2.begin(), data2.end()); + + bool is_same = std::equal(data1.begin(), data1.end(), data2.begin()); + CHECK(is_same); + } + + //************************************************************************* + TEST(heap_sort_greater) + { + std::vector initial_data = { NDC(1, 1), NDC(2, 1), NDC(3, 1), NDC(2, 2), NDC(3, 2), NDC(4, 1), NDC(2, 3), NDC(3, 3), NDC(5, 1) }; + + std::vector data1(initial_data); + std::vector data2(initial_data); + + std::sort(data1.begin(), data1.end(), std::greater()); + etl::heap_sort(data2.begin(), data2.end(), std::greater()); + + bool is_same = std::equal(data1.begin(), data1.end(), data2.begin()); + CHECK(is_same); + } + //************************************************************************* TEST(multimax) {