qxLib
transform.inl
Go to the documentation of this file.
1 /**
2 
3  @file transform.inl
4  @author Khrapov
5  @date 1.10.2023
6  @copyright © Nick Khrapov, 2023. All right reserved.
7 
8 **/
9 
10 namespace qx
11 {
12 
13 namespace details
14 {
15 
16 template<class T>
17 concept has_reserve_func = requires(T t) {
18  {
19  t.reserve(size_t())
20  };
21 };
22 
23 template<class T>
24 concept has_size_func = requires(T t) {
25  {
26  t.size()
27  } -> std::convertible_to<size_t>;
28 };
29 
30 } // namespace details
31 
32 template<
33  template<class...>
34  class result_container_t,
35  class input_container_t,
36  class transform_callable_t,
37  class... result_container_rest_t>
38 auto transform_return(input_container_t&& input, const transform_callable_t& transformCallable)
39 {
40  using result_type = decltype(transformCallable(*input.begin()));
41  using result_container_full_t = result_container_t<result_type, result_container_rest_t...>;
42  result_container_full_t result;
43 
44  if constexpr (details::has_reserve_func<result_container_full_t> && details::has_size_func<input_container_t>)
45  result.reserve(input.size());
46 
47  for (auto&& inputElement : input)
48  result.insert(result.end(), transformCallable(std::forward<decltype(inputElement)>(inputElement)));
49 
50  return result;
51 }
52 
53 } // namespace qx
auto transform_return(input_container_t &&input, const transform_callable_t &transformCallable)
std::transform equivalent that makes the code a bit shorter
Definition: transform.inl:38