Skip to content

Commit

Permalink
Fix potentially invalid use of C++20 input iterators as C++17 iterators.
Browse files Browse the repository at this point in the history
  • Loading branch information
bluescarni committed Sep 20, 2024
1 parent cde3faf commit 997f19d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
51 changes: 51 additions & 0 deletions include/heyoka/detail/rng_to_vec.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2020, 2021, 2022, 2023, 2024 Francesco Biscani ([email protected]), Dario Izzo ([email protected])
//
// This file is part of the heyoka library.
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef HEYOKA_DETAIL_RNG_TO_VEC_HPP
#define HEYOKA_DETAIL_RNG_TO_VEC_HPP

#include <ranges>
#include <type_traits>
#include <utility>
#include <vector>

#include <heyoka/config.hpp>

HEYOKA_BEGIN_NAMESPACE

namespace detail
{

// Helper to convert an input range R into a vector.
// The value type is deduced from the reference type of R.
template <typename R>
auto rng_to_vec(R &&r)
{
// Deduce the value type.
using value_type = std::remove_cvref_t<std::ranges::range_reference_t<R>>;

// Prepare the return value, reserving the appropriate
// size if R is a sized range.
std::vector<value_type> retval;
if constexpr (std::ranges::sized_range<R>) {
retval.reserve(static_cast<decltype(retval.size())>(std::ranges::size(r)));
}

// Add r's values into retval.
for (auto &&val : r) {
retval.push_back(std::forward<decltype(val)>(val));
}

return retval;
}

} // namespace detail

HEYOKA_END_NAMESPACE

#endif
10 changes: 5 additions & 5 deletions include/heyoka/expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/igor.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/rng_to_vec.hpp>
#include <heyoka/detail/type_traits.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/func.hpp>
Expand Down Expand Up @@ -461,8 +462,8 @@ class HEYOKA_DLL_PUBLIC dtens
[[nodiscard]] size_type index_of(const iterator &) const;

[[nodiscard]] auto get_derivatives(std::uint32_t) const -> decltype(std::ranges::subrange(begin(), end()));
[[nodiscard]] auto get_derivatives(std::uint32_t,
std::uint32_t) const -> decltype(std::ranges::subrange(begin(), end()));
[[nodiscard]] auto get_derivatives(std::uint32_t, std::uint32_t) const
-> decltype(std::ranges::subrange(begin(), end()));
[[nodiscard]] std::vector<expression> get_gradient() const;
[[nodiscard]] std::vector<expression> get_jacobian() const;
[[nodiscard]] std::vector<expression> get_hessian(std::uint32_t) const;
Expand Down Expand Up @@ -846,9 +847,8 @@ class HEYOKA_DLL_PUBLIC_INLINE_CLASS cfunc
template <typename R>
static auto rng_to_vecex(R &&r)
{
auto tv = r | std::views::transform([]<typename U>(U &&x) { return expression{std::forward<U>(x)}; });

return std::vector(std::ranges::begin(tv), std::ranges::end(tv));
return detail::rng_to_vec(
r | std::views::transform([]<typename U>(U &&x) { return expression{std::forward<U>(x)}; }));
}

public:
Expand Down
3 changes: 2 additions & 1 deletion include/heyoka/llvm_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

#include <heyoka/detail/fwd_decl.hpp>
#include <heyoka/detail/llvm_fwd.hpp>
#include <heyoka/detail/rng_to_vec.hpp>
#include <heyoka/detail/type_traits.hpp>
#include <heyoka/detail/visibility.hpp>
#include <heyoka/kw.hpp>
Expand Down Expand Up @@ -390,7 +391,7 @@ class HEYOKA_DLL_PUBLIC llvm_multi_state
requires std::ranges::input_range<R>
&& std::same_as<llvm_state, std::remove_cvref_t<std::ranges::range_reference_t<R>>>
explicit llvm_multi_state(R &&rng, bool parjit = detail::default_parjit)
: llvm_multi_state(std::vector(std::ranges::begin(rng), std::ranges::end(rng)), parjit)
: llvm_multi_state(detail::rng_to_vec(std::forward<R>(rng)), parjit)
{
}
llvm_multi_state(const llvm_multi_state &);
Expand Down
5 changes: 3 additions & 2 deletions src/taylor_02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include <heyoka/detail/llvm_helpers.hpp>
#include <heyoka/detail/llvm_vector_type.hpp>
#include <heyoka/detail/logging_impl.hpp>
#include <heyoka/detail/rng_to_vec.hpp>
#include <heyoka/detail/string_conv.hpp>
#include <heyoka/detail/type_traits.hpp>
#include <heyoka/detail/visibility.hpp>
Expand Down Expand Up @@ -1200,8 +1201,8 @@ std::vector<llvm_state> taylor_compute_jet_multi(llvm_state &main_state, llvm::T
// the custom transform:
//
// https://en.cppreference.com/w/cpp/ranges/as_rvalue_view
auto sview = states | std::views::transform([](auto &s) -> auto && { return std::move(s); });
return std::vector(std::ranges::begin(sview), std::ranges::end(sview));
auto sview = states | std::views::transform([](llvm_state &s) -> llvm_state && { return std::move(s); });
return rng_to_vec(sview);
}

// Helper for the computation of a jet of derivatives in compact mode,
Expand Down

0 comments on commit 997f19d

Please sign in to comment.