Skip to main content
added 271 characters in body
Source Link
kiner_shah
  • 575
  • 2
  • 9

I saw one piece of code being repeated across two functions, maybe create a third function and call this function from those two functions.

Also, you were accepting input array by value, better to take them by reference and avoid unnecessary copy.

template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr std::array<ElementT, Count> get_normalized_input( const std::array<ElementT, Count>& input, const ProbabilityType& sum) { std::array<ProbabilityType, Count> output{}; for (std::size_t i = 0; i < Count; ++i) { output[i] = static_cast<ProbabilityType>(input[i]) / sum; } return output; } template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr static auto normalize_histogram(const std::array<ElementT, Count>& input) { auto sum = std::reduce(std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } // normalize_histogram template function implementation for std::array (with Execution Policy) template<class ExecutionPolicy, class ElementT, std::size_t Count, class ProbabilityType = double> requires(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>) constexpr static auto normalize_histogram(ExecutionPolicy execution_policy, const std::array<ElementT, Count>& input) { auto sum = std::reduce(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } 

Also, I suggest you create a scope-based Timer class so that you can avoid repeating code for start and end times in your usage example code. The Timer class can be simple like capture start time in constructor and compute end time and difference in destructor.

Also, since you have tagged the post as C++23, do try using std::print and std::format instead of using std::cout.

I saw one piece of code being repeated across two functions, maybe create a third function and call this function from those two functions.

Also, you were accepting input array by value, better to take them by reference and avoid unnecessary copy.

template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr std::array<ElementT, Count> get_normalized_input( const std::array<ElementT, Count>& input, const ProbabilityType& sum) { std::array<ProbabilityType, Count> output{}; for (std::size_t i = 0; i < Count; ++i) { output[i] = static_cast<ProbabilityType>(input[i]) / sum; } return output; } template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr static auto normalize_histogram(const std::array<ElementT, Count>& input) { auto sum = std::reduce(std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } // normalize_histogram template function implementation for std::array (with Execution Policy) template<class ExecutionPolicy, class ElementT, std::size_t Count, class ProbabilityType = double> requires(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>) constexpr static auto normalize_histogram(ExecutionPolicy execution_policy, const std::array<ElementT, Count>& input) { auto sum = std::reduce(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } 

I saw one piece of code being repeated across two functions, maybe create a third function and call this function from those two functions.

Also, you were accepting input array by value, better to take them by reference and avoid unnecessary copy.

template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr std::array<ElementT, Count> get_normalized_input( const std::array<ElementT, Count>& input, const ProbabilityType& sum) { std::array<ProbabilityType, Count> output{}; for (std::size_t i = 0; i < Count; ++i) { output[i] = static_cast<ProbabilityType>(input[i]) / sum; } return output; } template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr static auto normalize_histogram(const std::array<ElementT, Count>& input) { auto sum = std::reduce(std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } // normalize_histogram template function implementation for std::array (with Execution Policy) template<class ExecutionPolicy, class ElementT, std::size_t Count, class ProbabilityType = double> requires(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>) constexpr static auto normalize_histogram(ExecutionPolicy execution_policy, const std::array<ElementT, Count>& input) { auto sum = std::reduce(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } 

Also, I suggest you create a scope-based Timer class so that you can avoid repeating code for start and end times in your usage example code. The Timer class can be simple like capture start time in constructor and compute end time and difference in destructor.

Also, since you have tagged the post as C++23, do try using std::print and std::format instead of using std::cout.

Source Link
kiner_shah
  • 575
  • 2
  • 9

I saw one piece of code being repeated across two functions, maybe create a third function and call this function from those two functions.

Also, you were accepting input array by value, better to take them by reference and avoid unnecessary copy.

template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr std::array<ElementT, Count> get_normalized_input( const std::array<ElementT, Count>& input, const ProbabilityType& sum) { std::array<ProbabilityType, Count> output{}; for (std::size_t i = 0; i < Count; ++i) { output[i] = static_cast<ProbabilityType>(input[i]) / sum; } return output; } template<class ElementT, std::size_t Count, class ProbabilityType = double> constexpr static auto normalize_histogram(const std::array<ElementT, Count>& input) { auto sum = std::reduce(std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); } // normalize_histogram template function implementation for std::array (with Execution Policy) template<class ExecutionPolicy, class ElementT, std::size_t Count, class ProbabilityType = double> requires(std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>>) constexpr static auto normalize_histogram(ExecutionPolicy execution_policy, const std::array<ElementT, Count>& input) { auto sum = std::reduce(execution_policy, std::ranges::cbegin(input), std::ranges::cend(input)); return get_normalized_input(input, static_cast<ProbabilityType>(sum)); }