From 14effc2b4a3f0da500e1d957cd06e454367cfd69 Mon Sep 17 00:00:00 2001 From: Tristan Brindle Date: Thu, 15 Aug 2024 12:53:19 +0100 Subject: [PATCH] Remove simple_sequence This was added very early in the life of Flux with the intention of making it easy to write Flow-style (or Rust-style) iterators with just a `next() -> optional` method. It works fine, but it's not particularly useful -- if you're doing a quick prototype then `flux::generator` is quicker and easier, and otherwise a "full" sequence implementation isn't much more work and usually gets you multipass for free. So given that it's not useful and it's not used anywhere, let's get rid of it. --- include/flux/core.hpp | 1 - include/flux/core/simple_sequence_base.hpp | 86 ----------------- test/CMakeLists.txt | 1 - test/test_simple_sequence.cpp | 104 --------------------- test/test_take.cpp | 23 ----- 5 files changed, 215 deletions(-) delete mode 100644 include/flux/core/simple_sequence_base.hpp delete mode 100644 test/test_simple_sequence.cpp diff --git a/include/flux/core.hpp b/include/flux/core.hpp index 88cec54e..6a1d57af 100644 --- a/include/flux/core.hpp +++ b/include/flux/core.hpp @@ -12,6 +12,5 @@ #include #include #include -#include #endif // FLUX_CORE_HPP_INCLUDED diff --git a/include/flux/core/simple_sequence_base.hpp b/include/flux/core/simple_sequence_base.hpp deleted file mode 100644 index 905c7bd9..00000000 --- a/include/flux/core/simple_sequence_base.hpp +++ /dev/null @@ -1,86 +0,0 @@ - -// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com) -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#ifndef FLUX_CORE_SIMPLE_SEQUENCE_BASE_HPP_INCLUDED -#define FLUX_CORE_SIMPLE_SEQUENCE_BASE_HPP_INCLUDED - -#include - -namespace flux { - -FLUX_EXPORT -template -struct simple_sequence_base : inline_sequence_base {}; - -namespace detail { - -template -concept simple_sequence = - std::derived_from> && - requires (S& s) { - { s.maybe_next() } -> optional_like; - }; - -} // namespace detail - -template -struct sequence_traits { -private: - class cursor_type { - friend struct sequence_traits; - using optional_t = decltype(FLUX_DECLVAL(S&).maybe_next()); - optional_t opt_{}; - - constexpr cursor_type() = default; - - constexpr explicit cursor_type(optional_t&& opt) - : opt_(std::move(opt)) - {} - - public: - cursor_type(cursor_type&&) = default; - cursor_type& operator=(cursor_type&&) = default; - }; - -public: - static constexpr bool is_infinite = detail::is_infinite_seq; - - static constexpr auto first(S& self) -> cursor_type - { - return cursor_type(self.maybe_next()); - } - - static constexpr auto is_last(S&, cursor_type const& cur) -> bool - { - return !static_cast(cur.opt_); - } - - static constexpr auto inc(S& self, cursor_type& cur) -> cursor_type& - { - cur.opt_ = self.maybe_next(); - return cur; - } - - static constexpr auto read_at(S&, cursor_type const& cur) -> decltype(auto) - { - return *cur.opt_; - } - - static constexpr auto for_each_while(S& self, auto&& pred) -> cursor_type - { - while (auto o = self.maybe_next()) { - if (!std::invoke(pred, *o)) { - return cursor_type(std::move(o)); - } - } - return cursor_type{}; - } -}; - -} // namespace flux - -#endif // FLUX_CORE_SIMPLE_SEQUENCE_BASE_HPP_INCLUDED - - diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b710c4b5..7e2bda49 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -15,7 +15,6 @@ add_executable(test-flux test_overflow.cpp test_optional.cpp test_predicates.cpp - test_simple_sequence.cpp test_apply.cpp test_adjacent.cpp diff --git a/test/test_simple_sequence.cpp b/test/test_simple_sequence.cpp deleted file mode 100644 index d6892b4d..00000000 --- a/test/test_simple_sequence.cpp +++ /dev/null @@ -1,104 +0,0 @@ -// Copyright (c) 2022 Tristan Brindle (tcbrindle at gmail dot com) -// Distributed under the Boost Software License, Version 1.0. (See accompanying -// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) - -#include -#include - -#include "test_utils.hpp" - -namespace { - -template -struct array_iterator : flux::simple_sequence_base> { -private: - std::array* array_; - std::size_t idx_ = 0; - -public: - constexpr explicit array_iterator(std::array& arr) - : array_(std::addressof(arr)) - {} - - constexpr auto maybe_next() -> T* - { - if (idx_ < N) { - return &array_->at(idx_++); - } else { - return nullptr; - } - } -}; - -struct ints : flux::simple_sequence_base { - int i = 0; - - static constexpr bool is_infinite = true; - - constexpr auto maybe_next() -> std::optional - { - return {i++}; - } -}; - -constexpr bool test_simple_sequence() -{ - { - std::array arr{1, 2, 3, 4, 5}; - auto iter = array_iterator(arr); - - using I = decltype(iter); -#ifndef USE_MODULES - static_assert(flux::detail::simple_sequence); -#endif - static_assert(flux::sequence); - static_assert(not flux::multipass_sequence); - static_assert(not flux::sized_sequence); - static_assert(not flux::infinite_sequence); - - static_assert(std::same_as, int&>); - static_assert(std::same_as, int>); - static_assert(std::same_as, int&&>); - - iter.fill(10); - - STATIC_CHECK(check_equal(array_iterator(arr), {10, 10, 10, 10, 10})); - } - - { -#ifndef USE_MODULES - static_assert(flux::detail::simple_sequence); -#endif - static_assert(flux::sequence); - static_assert(flux::infinite_sequence); - static_assert(not flux::multipass_sequence); - static_assert(not flux::sized_sequence); - static_assert(std::same_as, int const&>); - static_assert(std::same_as, int>); - static_assert(std::same_as, int const&&>); - - STATIC_CHECK(ints{}.take(10).sum() == 45); - } - - // Check that we can restart iteration - { - std::array arr{1, 2, 3, 4, 5}; - auto iter = array_iterator(arr); - - auto cur = iter.find(3); - auto slice = flux::slice(iter, std::move(cur), flux::last); - - STATIC_CHECK(check_equal(slice, {3, 4, 5})); - } - - return true; -} -static_assert(test_simple_sequence()); - -} - -TEST_CASE("simple_sequence") -{ - bool result = test_simple_sequence(); - REQUIRE(result); -} \ No newline at end of file diff --git a/test/test_take.cpp b/test/test_take.cpp index 97f0b599..e41fae68 100644 --- a/test/test_take.cpp +++ b/test/test_take.cpp @@ -11,16 +11,6 @@ namespace { -struct Tester : flux::simple_sequence_base { - - int i = 0; - - constexpr auto maybe_next() -> std::optional - { - return {i++}; - } -}; - constexpr bool test_take() { { @@ -57,19 +47,6 @@ constexpr bool test_take() STATIC_CHECK(check_equal(taken, {0, 1, 2})); } - { - auto taken = Tester{}.take(3); - - using T = decltype(taken); - static_assert(flux::sequence); - static_assert(not flux::multipass_sequence); - static_assert(not flux::sized_sequence); - - static_assert(not flux::sequence); - - STATIC_CHECK(check_equal(taken, {0, 1, 2})); - } - // test taking all the elements { auto arr = std::array{1, 2, 3, 4, 5};