#if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION) #pragma once #include #include #include #include #include namespace c10 { namespace detail { // Prefer consuming an owned element when the callable accepts an rvalue. // This covers callables taking T, T&&, or const T&. Fall back to T& for // callables that can only operate on an lvalue. template inline decltype(auto) fmap_invoke_owned(T& input, const F& fn) { if constexpr (std::is_invocable_v) { return fn(std::move(input)); } else { static_assert( std::is_invocable_v, "fmap callable must accept the input element"); return fn(input); } } } // namespace detail // For non-consuming inputs, the passed function must take T by value (T), or by // const reference (const T&); taking T by non-const reference will result in an // error like: // // error: no type named 'type' in 'class std::invoke_result' // // No explicit template parameters are required. // Overload for explicit function and ArrayRef template inline auto fmap(const T& inputs, const F& fn) -> std::vector { std::vector r; r.reserve(inputs.size()); for(const auto & input : inputs) r.push_back(fn(input)); return r; } // Consuming overload for an owned vector. Prefer passing elements as rvalues // when the callable supports it. If the map produces the same element type by // value, transform in place so the result can reuse the vector's allocation. template inline auto fmap(std::vector&& inputs, const F& fn) { using raw_result_type = decltype(detail::fmap_invoke_owned( std::declval(), std::declval())); using result_type = std::remove_cvref_t; static_assert( !std::is_void_v, "fmap callable must return a value"); // Do not reuse storage for reference results. In particular, a T&& result // may alias input, turning assignment back into the same slot into a // self-move. if constexpr ( std::is_same_v && !std::is_reference_v && std::is_assignable_v) { for (auto&& input : inputs) { input = detail::fmap_invoke_owned(input, fn); } return std::move(inputs); } else { std::vector r; r.reserve(inputs.size()); for (auto&& input : inputs) { r.emplace_back(detail::fmap_invoke_owned(input, fn)); } return r; } } // C++ forbids taking an address of a constructor, so here's a workaround... // Overload for constructor (R) application template inline std::vector fmap(const T& inputs) { std::vector r; r.reserve(inputs.size()); for(auto & input : inputs) r.push_back(R(input)); return r; } // Consuming overload for constructor application. Move from each element when // R can be constructed from T&&; otherwise preserve the existing lvalue path. template inline std::vector fmap(std::vector&& inputs) { if constexpr (std::is_same_v) { return std::move(inputs); } else { std::vector r; r.reserve(inputs.size()); for (auto&& input : inputs) { if constexpr (std::is_constructible_v) { r.emplace_back(std::move(input)); } else { static_assert( std::is_constructible_v, "fmap result must be constructible from the input element"); r.emplace_back(input); } } return r; } } template inline std::vector filter(at::ArrayRef inputs, const F& fn) { std::vector r; r.reserve(inputs.size()); for(auto & input : inputs) { if (fn(input)) { r.push_back(input); } } return r; } template inline std::vector filter(const std::vector& inputs, const F& fn) { return filter(static_cast>(inputs), fn); } // TODO: Constrain this overload to move-assignable T with concepts once all // supported compilers handle C++20 constraints reliably. template inline std::vector filter(std::vector&& inputs, const F& fn) { if constexpr (std::is_move_assignable_v) { inputs.erase( std::remove_if( inputs.begin(), inputs.end(), [&](auto&& input) { return !fn(input); }), inputs.end()); return std::move(inputs); } else { return filter(static_cast&>(inputs), fn); } } } // namespace c10 #else #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined." #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)