From 49acd2d2abce604218dbe2a455e3267f0c028fee Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Wed, 30 Apr 2025 17:49:02 +0200 Subject: [PATCH] Various cleanup changes (#1049) * Various Cleanup Remove remove() by pointer because erase() can be used for that Fix signed distance handling, with added check for order Add missing file ID Fix File IDs Added test for algorithm.h * Improve types # Conflicts: # include/etl/file_error_numbers.h --- include/etl/algorithm.h | 49 +++++++++++++++++++++++---- include/etl/file_error_numbers.h | 2 +- include/etl/intrusive_forward_list.h | 23 ------------- include/etl/intrusive_list.h | 2 +- include/etl/private/bitset_new.h | 4 +-- include/etl/queue.h | 2 -- include/etl/reference_flat_multiset.h | 2 +- include/etl/reference_flat_set.h | 2 +- include/etl/singleton_base.h | 2 +- test/test_algorithm.cpp | 11 ++++++ test/test_intrusive_forward_list.cpp | 29 ---------------- 11 files changed, 60 insertions(+), 68 deletions(-) diff --git a/include/etl/algorithm.h b/include/etl/algorithm.h index 5dc231bb..db5c3a2a 100644 --- a/include/etl/algorithm.h +++ b/include/etl/algorithm.h @@ -44,6 +44,8 @@ SOFTWARE. #include "functional.h" #include "utility.h" #include "gcd.h" +#include "error_handler.h" +#include "exception.h" #include #include @@ -82,6 +84,27 @@ namespace etl template ETL_CONSTEXPR14 void insertion_sort(TIterator first, TIterator last, TCompare compare); + + class algorithm_exception : public etl::exception + { + public: + + algorithm_exception(string_type reason_, string_type file_name_, numeric_type line_number_) + : exception(reason_, file_name_, line_number_) + { + } + }; + + class algorithm_error : public algorithm_exception + { + public: + + algorithm_error(string_type file_name_, numeric_type line_number_) + : algorithm_exception(ETL_ERROR_TEXT("algorithm:error", ETL_ALGORITHM_FILE_ID"A"), file_name_, line_number_) + { + } + }; + } //***************************************************************************** @@ -2262,11 +2285,17 @@ namespace etl TOutputIterator o_begin, TOutputIterator o_end) { - size_t s_size = etl::distance(i_begin, i_end); - size_t d_size = etl::distance(o_begin, o_end); - size_t size = (s_size < d_size) ? s_size : d_size; + using s_size_type = typename iterator_traits::difference_type; + using d_size_type = typename iterator_traits::difference_type; + using min_size_type = typename etl::common_type::type; - return etl::copy(i_begin, i_begin + size, o_begin); + s_size_type s_size = etl::distance(i_begin, i_end); + ETL_ASSERT(s_size >= 0, ETL_ERROR(algorithm_error)); + d_size_type d_size = etl::distance(o_begin, o_end); + ETL_ASSERT(d_size >= 0, ETL_ERROR(algorithm_error)); + min_size_type size = etl::min(s_size, d_size); + + return etl::copy(i_begin, i_begin + size, o_begin); } //*************************************************************************** @@ -2429,9 +2458,15 @@ namespace etl TOutputIterator o_begin, TOutputIterator o_end) { - size_t s_size = etl::distance(i_begin, i_end); - size_t d_size = etl::distance(o_begin, o_end); - size_t size = (s_size < d_size) ? s_size : d_size; + using s_size_type = typename iterator_traits::difference_type; + using d_size_type = typename iterator_traits::difference_type; + using min_size_type = typename etl::common_type::type; + + s_size_type s_size = etl::distance(i_begin, i_end); + ETL_ASSERT(s_size >= 0, ETL_ERROR(algorithm_error)); + d_size_type d_size = etl::distance(o_begin, o_end); + ETL_ASSERT(d_size >= 0, ETL_ERROR(algorithm_error)); + min_size_type size = etl::min(s_size, d_size); return etl::move(i_begin, i_begin + size, o_begin); } diff --git a/include/etl/file_error_numbers.h b/include/etl/file_error_numbers.h index 2369f68c..5404828a 100644 --- a/include/etl/file_error_numbers.h +++ b/include/etl/file_error_numbers.h @@ -106,5 +106,5 @@ SOFTWARE. #define ETL_SINGLETON_BASE_FILE_ID "73" #define ETL_UNALIGNED_TYPE_FILE_ID "74" #define ETL_SPAN_FILE_ID "75" - +#define ETL_ALGORITHM_FILE_ID "76" #endif diff --git a/include/etl/intrusive_forward_list.h b/include/etl/intrusive_forward_list.h index 7e6d4dba..d68dd6ad 100644 --- a/include/etl/intrusive_forward_list.h +++ b/include/etl/intrusive_forward_list.h @@ -1032,29 +1032,6 @@ namespace etl } } - //************************************************************************* - // Removes the element specified by pointer. - //************************************************************************* - void remove(const_pointer element) - { - iterator i_item = begin(); - iterator i_last_item = before_begin(); - - while (i_item != end()) - { - if (&i_item == element) - { - i_item = erase_after(i_last_item); - return; - } - else - { - ++i_item; - ++i_last_item; - } - } - } - //************************************************************************* /// Removes according to a predicate. //************************************************************************* diff --git a/include/etl/intrusive_list.h b/include/etl/intrusive_list.h index 563fadfa..970d0589 100644 --- a/include/etl/intrusive_list.h +++ b/include/etl/intrusive_list.h @@ -113,7 +113,7 @@ namespace etl public: intrusive_list_value_is_already_linked(string_type file_name_, numeric_type line_number_) - : intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:value is already linked", ETL_INTRUSIVE_LIST_FILE_ID"E"), file_name_, line_number_) + : intrusive_list_exception(ETL_ERROR_TEXT("intrusive_list:value is already linked", ETL_INTRUSIVE_LIST_FILE_ID"D"), file_name_, line_number_) { } }; diff --git a/include/etl/private/bitset_new.h b/include/etl/private/bitset_new.h index 2fcbc990..e7673f4b 100644 --- a/include/etl/private/bitset_new.h +++ b/include/etl/private/bitset_new.h @@ -141,7 +141,7 @@ namespace etl public: bitset_overflow(string_type file_name_, numeric_type line_number_) - : bitset_exception(ETL_ERROR_TEXT("bitset:overflow", ETL_BITSET_FILE_ID"C"), file_name_, line_number_) + : bitset_exception(ETL_ERROR_TEXT("bitset:overflow", ETL_BITSET_FILE_ID"B"), file_name_, line_number_) { } }; @@ -155,7 +155,7 @@ namespace etl public: bitset_invalid_buffer(string_type file_name_, numeric_type line_number_) - : bitset_exception(ETL_ERROR_TEXT("bitset:invalid buffer", ETL_BITSET_FILE_ID"D"), file_name_, line_number_) + : bitset_exception(ETL_ERROR_TEXT("bitset:invalid buffer", ETL_BITSET_FILE_ID"C"), file_name_, line_number_) { } }; diff --git a/include/etl/queue.h b/include/etl/queue.h index 99e4d50e..47e9d470 100644 --- a/include/etl/queue.h +++ b/include/etl/queue.h @@ -32,9 +32,7 @@ SOFTWARE. #define ETL_QUEUE_INCLUDED #include "platform.h" -#include "iterator.h" #include "alignment.h" -#include "array.h" #include "exception.h" #include "error_handler.h" #include "debug_count.h" diff --git a/include/etl/reference_flat_multiset.h b/include/etl/reference_flat_multiset.h index 9d43848d..78abcce9 100644 --- a/include/etl/reference_flat_multiset.h +++ b/include/etl/reference_flat_multiset.h @@ -86,7 +86,7 @@ namespace etl public: flat_multiset_iterator(string_type file_name_, numeric_type line_number_) - : flat_multiset_exception(ETL_ERROR_TEXT("flat_multiset:iterator", ETL_REFERENCE_FLAT_MULTISET_FILE_ID"C"), file_name_, line_number_) + : flat_multiset_exception(ETL_ERROR_TEXT("flat_multiset:iterator", ETL_REFERENCE_FLAT_MULTISET_FILE_ID"B"), file_name_, line_number_) { } }; diff --git a/include/etl/reference_flat_set.h b/include/etl/reference_flat_set.h index dfb65424..44bbea96 100644 --- a/include/etl/reference_flat_set.h +++ b/include/etl/reference_flat_set.h @@ -87,7 +87,7 @@ namespace etl public: flat_set_iterator(string_type file_name_, numeric_type line_number_) - : flat_set_exception(ETL_ERROR_TEXT("flat_set:iterator", ETL_REFERENCE_FLAT_SET_FILE_ID"C"), file_name_, line_number_) + : flat_set_exception(ETL_ERROR_TEXT("flat_set:iterator", ETL_REFERENCE_FLAT_SET_FILE_ID"B"), file_name_, line_number_) { } }; diff --git a/include/etl/singleton_base.h b/include/etl/singleton_base.h index eb9653b4..72f0744a 100644 --- a/include/etl/singleton_base.h +++ b/include/etl/singleton_base.h @@ -78,7 +78,7 @@ namespace etl public: singleton_base_already_created(string_type file_name_, numeric_type line_number_) - : singleton_base_exception(ETL_ERROR_TEXT("singleton_base:already created", ETL_SINGLETON_BASE_FILE_ID"A"), file_name_, line_number_) + : singleton_base_exception(ETL_ERROR_TEXT("singleton_base:already created", ETL_SINGLETON_BASE_FILE_ID"B"), file_name_, line_number_) { } }; diff --git a/test/test_algorithm.cpp b/test/test_algorithm.cpp index f1242718..2d362dd9 100644 --- a/test/test_algorithm.cpp +++ b/test/test_algorithm.cpp @@ -1364,6 +1364,17 @@ namespace CHECK(is_same); } + //************************************************************************* + TEST(copy_s_random_iterator) + { + int data1[] = { 1, 2, 3, 4, 5 }; + int out1[10]; + CHECK_THROW(etl::copy_s(std::end(data1), std::begin(data1), std::begin(out1), std::end(out1)), etl::algorithm_error); + CHECK_THROW(etl::copy_s(std::begin(data1), std::end(data1), std::end(out1), std::begin(out1)), etl::algorithm_error); + CHECK_THROW(etl::move_s(std::end(data1), std::begin(data1), std::begin(out1), std::end(out1)), etl::algorithm_error); + CHECK_THROW(etl::move_s(std::begin(data1), std::end(data1), std::end(out1), std::begin(out1)), etl::algorithm_error); + } + //************************************************************************* TEST(copy_4_parameter_non_random_iterator) { diff --git a/test/test_intrusive_forward_list.cpp b/test/test_intrusive_forward_list.cpp index 9e854486..da4d185c 100644 --- a/test/test_intrusive_forward_list.cpp +++ b/test/test_intrusive_forward_list.cpp @@ -979,35 +979,6 @@ namespace CHECK_EQUAL(sorted_data.size(), size_t(std::distance(data1.begin(), data1.end()))); } - //************************************************************************* - TEST_FIXTURE(SetupFixture, test_remove_by_pointer) - { - std::forward_list compare_data(sorted_data.begin(), sorted_data.end()); - DataNDC0 data0(sorted_data.begin(), sorted_data.end()); - DataNDC1 data1(sorted_data.begin(), sorted_data.end()); - - auto it = data0.begin(); - for (int i = 0; i < 7; ++i) - { - it++; - } - ItemNDCNode* element = ⁢ - - compare_data.remove(ItemNDCNode("7")); - data0.remove(*element); - - bool are_equal = std::equal(data0.begin(), data0.end(), compare_data.begin()); - - CHECK(are_equal); - CHECK_EQUAL(size_t(std::distance(compare_data.begin(), compare_data.end())), data0.size()); - CHECK_EQUAL(std::distance(compare_data.begin(), compare_data.end()), std::distance(data0.begin(), data0.end())); - - are_equal = std::equal(data1.begin(), data1.end(), sorted_data.begin()); - CHECK(are_equal); - CHECK_EQUAL(sorted_data.size(), data1.size()); - CHECK_EQUAL(sorted_data.size(), size_t(std::distance(data1.begin(), data1.end()))); - } - //************************************************************************* TEST_FIXTURE(SetupFixture, test_remove_if) {