Unverified Commit 33a28e00 authored by Michał Karzyński's avatar Michał Karzyński Committed by GitHub

[ONNX] Simplify the get_monotonic_range helper function (#2807)

parent 8da348f5
...@@ -16,11 +16,12 @@ ...@@ -16,11 +16,12 @@
#pragma once #pragma once
#include <algorithm> // std::generate
#include <cmath> // std::floor, std::min #include <cmath> // std::floor, std::min
#include <cstddef> // std::size_t #include <cstddef> // std::size_t
#include <iterator> // std::begin, std::end #include <iterator> // std::begin, std::end
#include <memory> // std::shared_ptr, std::make_shared #include <memory> // std::shared_ptr, std::make_shared
#include <type_traits> // std::enable_if, std::is_floating_point, std::is_integral #include <type_traits> // std::enable_if
#include <vector> #include <vector>
#include "ngraph/op/constant.hpp" #include "ngraph/op/constant.hpp"
...@@ -33,38 +34,10 @@ namespace ngraph ...@@ -33,38 +34,10 @@ namespace ngraph
{ {
namespace common namespace common
{ {
namespace detail /// \brief Return a monotonic sequence.
{
namespace
{
/// \brief Fill specified range with monotonic sequence.
///
/// \param[in] first The iterator to the beginning of the range.
/// \param[in] last The iterator to the past the end of the range.
/// \param[in] init_value The initial value for sequence.
/// \param[in] step The step value for sequence.
///
/// \tparam ForwardIterator The forward iterator class type.
/// \tparam T The sequence value type.
///
template <typename ForwardIterator, typename T>
void fill_monotonic_range(ForwardIterator first,
ForwardIterator last,
T init_value,
T step)
{
for (; first != last; ++first, init_value += step)
{
*first = init_value;
}
}
} // namespace anonymous
} // namespace detail
/// \brief Return the monotonic sequence.
/// ///
/// \note Specialization for integral types. /// \note Limitations: this function may not work for very large integer values
/// (near numerical limits).
/// ///
/// \param[in] start_value The start value of the sequence. /// \param[in] start_value The start value of the sequence.
/// \param[in] end_value The end value of the sequence. /// \param[in] end_value The end value of the sequence.
...@@ -72,36 +45,21 @@ namespace ngraph ...@@ -72,36 +45,21 @@ namespace ngraph
/// ///
/// \tparam T The data value type. /// \tparam T The data value type.
/// ///
/// \return The vector with monotonic sequence. /// \return The vector with monotonic sequence
template <typename T, template <typename T>
typename std::enable_if<std::is_integral<T>::value, int>::type = 0>
std::vector<T> get_monotonic_range(T end_value, T start_value = T{0}, T step = T{1}) std::vector<T> get_monotonic_range(T end_value, T start_value = T{0}, T step = T{1})
{ {
std::size_t value_count = (end_value - start_value) / step; auto value_count =
std::vector<T> range(value_count); static_cast<std::size_t>(std::floor((end_value - start_value) / step));
detail::fill_monotonic_range(std::begin(range), std::end(range), start_value, step);
return range;
}
/// \brief Return the monotonic sequence.
///
/// \note Specialization for floating point types.
///
/// \param[in] start_value The start value of the sequence.
/// \param[in] end_value The end value of the sequence.
/// \param[in] step The step value for the sequence.
///
/// \tparam T The data value type.
///
/// \return The vector with monotonic sequence
template <typename T,
typename std::enable_if<std::is_floating_point<T>::value, int>::type = 0>
std::vector<T> get_monotonic_range(T end_value, T start_value = T{0.f}, T step = T{1.f})
{
std::size_t value_count =
reinterpret_cast<std::size_t>(std::floor((end_value - start_value) / step));
std::vector<T> range(value_count); std::vector<T> range(value_count);
detail::fill_monotonic_range(std::begin(range), std::end(range), start_value, step);
// Calculate initial value (one step below starting value)
size_t n = start_value - step;
// Generate a vector of values by adding step to previous value
std::generate(
std::begin(range), std::end(range), [&n, &step]() -> T { return n += step; });
return range; return range;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment