From 620775e7c0117b03e73d4e7b0a393dac761f2a8a Mon Sep 17 00:00:00 2001 From: wermos <63574588+wermos@users.noreply.github.com> Date: Sun, 15 Jun 2025 13:15:59 +0530 Subject: [PATCH] Run `pre-commit` on the files to format them. --- examples/filter_int_iterator.cpp | 18 +++---- examples/repeated_chars_iterator.cpp | 27 +++++----- .../detail/stl_interfaces/config.hpp | 6 ++- .../stl_interfaces/iterator_interface.hpp | 32 +++++++----- .../iterator_interface/iterator_interface.hpp | 30 +++++------ .../iterator_interface_access.hpp | 4 +- .../iterator_interface.test.cpp | 52 +++++++++---------- 7 files changed, 85 insertions(+), 84 deletions(-) diff --git a/examples/filter_int_iterator.cpp b/examples/filter_int_iterator.cpp index a8ae97c..7a360fa 100644 --- a/examples/filter_int_iterator.cpp +++ b/examples/filter_int_iterator.cpp @@ -13,15 +13,15 @@ // filtered_int_iterator uses iterator_interface to define a forward iterator // that iterates over a sequence of integers, skipping those that do not satisfy a predicate. template -struct filtered_int_iterator - : beman::iterator_interface::ext_iterator_interface_compat, - std::forward_iterator_tag, - int> { +struct filtered_int_iterator : beman::iterator_interface::ext_iterator_interface_compat, + std::forward_iterator_tag, + int> { // Default constructor creates an end-of-range iterator. filtered_int_iterator() : m_it_begin(nullptr) {} // Constructor for the beginning of the sequence. - filtered_int_iterator(int* it_begin, int* it_end, Pred pred) : m_it_begin(it_begin), m_it_end(it_end), m_pred(std::move(pred)) { + filtered_int_iterator(int* it_begin, int* it_end, Pred pred) + : m_it_begin(it_begin), m_it_end(it_end), m_pred(std::move(pred)) { m_it_begin = std::find_if(m_it_begin, m_it_end, m_pred); } @@ -58,11 +58,11 @@ struct filtered_int_iterator int main() { // Create a filtered_int_iterator that iterates over the sequence {1, 2, 3, 4, 10, 11, 101, 200, 0}, // skipping odd numbers. 0 is not skipped, so it will be the last element in the sequence. - std::array a = {1, 2, 3, 4, 10, 11, 101, 200, 0}; + std::array a = {1, 2, 3, 4, 10, 11, 101, 200, 0}; filtered_int_iterator it{std::begin(a), std::end(a), [](int i) { return i % 2 == 0; }}; - - while (*it) { // Expected output at STDOUT: - std::cout << *it << " "; // 2 4 10 200 0 + + while (*it) { // Expected output at STDOUT: + std::cout << *it << " "; // 2 4 10 200 0 ++it; } std::cout << "\n"; diff --git a/examples/repeated_chars_iterator.cpp b/examples/repeated_chars_iterator.cpp index 5c07f76..fadc5dd 100644 --- a/examples/repeated_chars_iterator.cpp +++ b/examples/repeated_chars_iterator.cpp @@ -14,29 +14,27 @@ // repeated_chars_iterator uses iterator_interface to define a random access iterator // that iterates over a sequence of characters repeated indefinitely. class repeated_chars_iterator - : public beman::iterator_interface::ext_iterator_interface_compat { -public: + : public beman::iterator_interface:: + ext_iterator_interface_compat { + public: // Default constructor creates an end-of-range iterator. constexpr repeated_chars_iterator() : m_it_begin(nullptr), m_fixed_size(0), m_pos(0) {} - + // Constructor for the beginning of the sequence. constexpr repeated_chars_iterator(const char* it_begin, difference_type size, difference_type n) : m_it_begin(it_begin), m_fixed_size(size), m_pos(n) {} // Random access iterator requirements: - constexpr auto operator*() const { return m_it_begin[m_pos % m_fixed_size]; } + constexpr auto operator*() const { return m_it_begin[m_pos % m_fixed_size]; } constexpr repeated_chars_iterator& operator+=(std::ptrdiff_t i) { m_pos += i; return *this; } constexpr auto operator-(repeated_chars_iterator other) const { return m_pos - other.m_pos; } -private: + private: // Start of the sequence of characters. - const char* m_it_begin; + const char* m_it_begin; // Number of characters in the sequence. difference_type m_fixed_size; @@ -49,17 +47,18 @@ int main() { // Create a repeated_chars_iterator that iterates over the sequence "foo" repeated indefinitely: // "foofoofoofoofoofoo...". Will actually extract a prefix of the sequence and insert it into a std::string. constexpr const std::string_view target = "foo"; - constexpr const auto len = 7; // Number of extracted characters from the sequence. + constexpr const auto len = 7; // Number of extracted characters from the sequence. // Create iterators that go over the sequence "foofoofoofoofoofoo...". - repeated_chars_iterator it_first(target.data(), target.size(), 0); // target.size() == 3 is the length of "foo", 0 is this iterator's position. - repeated_chars_iterator it_last(target.data(), target.size(), len); // Same as above, but now the iterator's position is 7. + repeated_chars_iterator it_first( + target.data(), target.size(), 0); // target.size() == 3 is the length of "foo", 0 is this iterator's position. + repeated_chars_iterator it_last( + target.data(), target.size(), len); // Same as above, but now the iterator's position is 7. std::string extracted_result; std::copy(it_first, it_last, std::back_inserter(extracted_result)); assert(extracted_result.size() == len); - std::cout << extracted_result << "\n"; // Expected output at STDOUT: "foofoof" + std::cout << extracted_result << "\n"; // Expected output at STDOUT: "foofoof" return 0; } - diff --git a/include/beman/iterator_interface/detail/stl_interfaces/config.hpp b/include/beman/iterator_interface/detail/stl_interfaces/config.hpp index 7126fd2..cfc2c05 100644 --- a/include/beman/iterator_interface/detail/stl_interfaces/config.hpp +++ b/include/beman/iterator_interface/detail/stl_interfaces/config.hpp @@ -32,11 +32,13 @@ // multiple vI namespace alternatives exist. For example, some instances of // the v1 namespace may still be inline, if there is no v2 version of its // contents. -#if !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS && !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS +#if !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS && \ + !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 inline namespace v1 #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V2 namespace v2 #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V3 namespace v3 -#elif BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS && !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS +#elif BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS && \ + !BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 namespace v1 #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V2 inline namespace v2 #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V3 namespace v3 diff --git a/include/beman/iterator_interface/detail/stl_interfaces/iterator_interface.hpp b/include/beman/iterator_interface/detail/stl_interfaces/iterator_interface.hpp index c4d29e7..c107a10 100644 --- a/include/beman/iterator_interface/detail/stl_interfaces/iterator_interface.hpp +++ b/include/beman/iterator_interface/detail/stl_interfaces/iterator_interface.hpp @@ -28,7 +28,8 @@ namespace stl_interfaces { this template implies a copy or move of the underlying object of type `T`. */ template -#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS +#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || \ + BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS // clang-format off requires std::is_object_v #endif @@ -99,7 +100,9 @@ struct common_eq { }; template struct common_eq { - static constexpr auto call(T lhs, U rhs) { return iterator_interface_access::base(lhs) == iterator_interface_access::base(rhs); } + static constexpr auto call(T lhs, U rhs) { + return iterator_interface_access::base(lhs) == iterator_interface_access::base(rhs); + } }; template @@ -213,7 +216,7 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 { template constexpr auto operator->() const noexcept(noexcept(detail::make_pointer(*std::declval()))) - -> decltype(detail::make_pointer(*std::declval())) { + -> decltype(detail::make_pointer(*std::declval())) { return detail::make_pointer(*derived()); } @@ -249,7 +252,8 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 { } template - constexpr auto operator+=(difference_type n) noexcept(noexcept(iterator_interface_access::base(std::declval()) += n)) + constexpr auto + operator+=(difference_type n) noexcept(noexcept(iterator_interface_access::base(std::declval()) += n)) -> decltype(iterator_interface_access::base(std::declval()) += n, std::declval()) { iterator_interface_access::base(derived()) += n; return derived(); @@ -263,8 +267,8 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 { retval += i; return retval; } - friend BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_HIDDEN_FRIEND_CONSTEXPR Derived operator+(difference_type i, - Derived it) noexcept { + friend BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_HIDDEN_FRIEND_CONSTEXPR Derived + operator+(difference_type i, Derived it) noexcept { return it + i; } @@ -299,8 +303,10 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 { template constexpr auto operator-(D other) const - noexcept(noexcept(iterator_interface_access::base(std::declval()) - iterator_interface_access::base(other))) - -> decltype(iterator_interface_access::base(std::declval()) - iterator_interface_access::base(other)) { + noexcept(noexcept(iterator_interface_access::base(std::declval()) - + iterator_interface_access::base(other))) + -> decltype(iterator_interface_access::base(std::declval()) - + iterator_interface_access::base(other)) { return iterator_interface_access::base(derived()) - iterator_interface_access::base(other); } @@ -402,7 +408,8 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V1 { } // namespace stl_interfaces } // namespace beman::iterator_interface::detail -#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS +#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || \ + BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_CONCEPTS namespace beman::iterator_interface::detail { namespace stl_interfaces { @@ -727,7 +734,8 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V2 { #endif -#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS +#if defined(BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_DOXYGEN) || \ + BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_USE_DEDUCED_THIS namespace beman::iterator_interface::detail { namespace stl_interfaces { @@ -954,7 +962,7 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V3 { #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_STATIC_ASSERT_CONCEPT(iter, concept_name) #endif -#define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS_IMPL( \ +#define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS_IMPL( \ iter, category, value_t, ref, ptr, diff_t) \ static_assert(std::is_same::value_type, value_t>::value, ""); \ static_assert(std::is_same::reference, ref>::value, ""); \ @@ -962,7 +970,7 @@ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_NAMESPACE_V3 { static_assert(std::is_same::difference_type, diff_t>::value, ""); #define BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS( \ - iter, category, concept, value_type, reference, pointer, difference_type) \ + iter, category, concept, value_type, reference, pointer, difference_type) \ BEMAN_ITERATOR_INTERFACE_DETAIL_STL_INTERFACES_STATIC_ASSERT_ITERATOR_TRAITS_IMPL( \ iter, category, value_type, reference, pointer, difference_type) #endif diff --git a/include/beman/iterator_interface/iterator_interface.hpp b/include/beman/iterator_interface/iterator_interface.hpp index 94592f0..1e5f1fc 100644 --- a/include/beman/iterator_interface/iterator_interface.hpp +++ b/include/beman/iterator_interface/iterator_interface.hpp @@ -142,31 +142,27 @@ struct iter_cat {}; template struct iter_cat { - using iterator_category = - std::conditional_t< - !std::is_reference_v, - std::input_iterator_tag, - std::conditional_t< - std::is_base_of_v, - std::random_access_iterator_tag, - std::conditional_t< - std::is_base_of_v, - std::bidirectional_iterator_tag, - std::forward_iterator_tag>>>; + using iterator_category = std::conditional_t< + !std::is_reference_v, + std::input_iterator_tag, + std::conditional_t, + std::random_access_iterator_tag, + std::conditional_t, + std::bidirectional_iterator_tag, + std::forward_iterator_tag>>>; }; } // namespace detail template class iterator_interface - : detail::iter_cat> -{ + : detail::iter_cat> { public: using iterator_concept = IteratorConcept; - using value_type = remove_const_t; - using reference = Reference; - using pointer = conditional_t, void, Pointer>; - using difference_type = DifferenceType; + using value_type = remove_const_t; + using reference = Reference; + using pointer = conditional_t, void, Pointer>; + using difference_type = DifferenceType; constexpr decltype(auto) operator*(this auto&& self) requires requires { *iterator_interface_access::base(self); } diff --git a/include/beman/iterator_interface/iterator_interface_access.hpp b/include/beman/iterator_interface/iterator_interface_access.hpp index 989b5d0..ea88d8e 100644 --- a/include/beman/iterator_interface/iterator_interface_access.hpp +++ b/include/beman/iterator_interface/iterator_interface_access.hpp @@ -21,6 +21,6 @@ struct iterator_interface_access { return d.base_reference(); } }; -} -} +} // namespace iterator_interface +} // namespace beman #endif diff --git a/tests/beman/iterator_interface/iterator_interface.test.cpp b/tests/beman/iterator_interface/iterator_interface.test.cpp index 4856420..86968f7 100644 --- a/tests/beman/iterator_interface/iterator_interface.test.cpp +++ b/tests/beman/iterator_interface/iterator_interface.test.cpp @@ -60,25 +60,25 @@ TEST(IteratorTest, TestRepeatedChars) { TEST(IteratorTest, TestDistance) { auto lambda = [&] { repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. - repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. + repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. std::string result; std::copy(first, last, std::back_inserter(result)); CONSTEXPR_EXPECT_EQ(std::distance(first, last), 3); }; static_assert((lambda(), true)); - lambda(); + lambda(); } TEST(IteratorTest, TestNext) { auto lambda = [&] { repeated_chars_iterator first("foo", 3, 0); // 3 is the length of "foo", 0 is this iterator's position. - repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. + repeated_chars_iterator last("foo", 3, 3); // 3 is the length of "foo", 3 is this iterator's position. CONSTEXPR_EXPECT_EQ(std::next(first, 3), last); }; static_assert((lambda(), true)); - lambda(); + lambda(); } TEST(IteratorTest, TestConcepts) { @@ -174,30 +174,26 @@ TEST(IteratorTest, OperatorArrow) { ASSERT_EQ(ai->f(), 3); } -struct dummy_input_iterator : - public ext_iterator_interface_compat< - dummy_input_iterator, std::input_iterator_tag, int, int const&, void, std::ptrdiff_t> { - constexpr dummy_input_iterator() { } - dummy_input_iterator(dummy_input_iterator const&) = delete; - dummy_input_iterator& operator=(dummy_input_iterator const&) = delete; - dummy_input_iterator(dummy_input_iterator&&) = default; - dummy_input_iterator& operator=(dummy_input_iterator&&) = default; - constexpr reference operator*() const { - return foo; - } - constexpr dummy_input_iterator& operator++() { - return *this; - } - constexpr void operator++(int) {} - - friend constexpr bool operator==(std::default_sentinel_t const&, - dummy_input_iterator const&) { - return true; - } - - friend beman::iterator_interface::iterator_interface_access; - - int foo = 0; +struct dummy_input_iterator : public ext_iterator_interface_compat { + constexpr dummy_input_iterator() {} + dummy_input_iterator(const dummy_input_iterator&) = delete; + dummy_input_iterator& operator=(const dummy_input_iterator&) = delete; + dummy_input_iterator(dummy_input_iterator&&) = default; + dummy_input_iterator& operator=(dummy_input_iterator&&) = default; + constexpr reference operator*() const { return foo; } + constexpr dummy_input_iterator& operator++() { return *this; } + constexpr void operator++(int) {} + + friend constexpr bool operator==(const std::default_sentinel_t&, const dummy_input_iterator&) { return true; } + + friend beman::iterator_interface::iterator_interface_access; + + int foo = 0; }; static_assert(std::input_iterator);