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
This commit is contained in:
Roland Reichwein 2025-04-30 17:49:02 +02:00 committed by John Wellbelove
parent 2fd4e171ba
commit 49acd2d2ab
11 changed files with 60 additions and 68 deletions

View File

@ -44,6 +44,8 @@ SOFTWARE.
#include "functional.h"
#include "utility.h"
#include "gcd.h"
#include "error_handler.h"
#include "exception.h"
#include <stdint.h>
#include <string.h>
@ -82,6 +84,27 @@ namespace etl
template <typename TIterator, typename TCompare>
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<TInputIterator>::difference_type;
using d_size_type = typename iterator_traits<TOutputIterator>::difference_type;
using min_size_type = typename etl::common_type<s_size_type, d_size_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<min_size_type>(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<TInputIterator>::difference_type;
using d_size_type = typename iterator_traits<TOutputIterator>::difference_type;
using min_size_type = typename etl::common_type<s_size_type, d_size_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<min_size_type>(s_size, d_size);
return etl::move(i_begin, i_begin + size, o_begin);
}

View File

@ -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

View File

@ -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.
//*************************************************************************

View File

@ -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_)
{
}
};

View File

@ -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_)
{
}
};

View File

@ -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"

View File

@ -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_)
{
}
};

View File

@ -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_)
{
}
};

View File

@ -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_)
{
}
};

View File

@ -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)
{

View File

@ -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<ItemNDCNode> 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 = &it;
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)
{