38 template<
class ContainerType>
39 using ElementTypeOf = std::remove_cvref_t<decltype(*std::declval<ContainerType>().begin())>;
44 template<
class RangeType,
class T,
class Fn = std::plus<T>>
45 [[nodiscard]] JPL_INLINE
constexpr T
Accumulate(RangeType&& range, T initialValue, Fn reduceOp = {})
47 return std::accumulate(range.begin(), range.end(), std::move(initialValue), reduceOp);
52 template <
class T =
void>
55 [[nodiscard]] JPL_INLINE
constexpr T
operator()(
const T& acc,
const T& value)
const noexcept
57 return acc + value * value;
62 template <
class T =
void>
65 [[nodiscard]] JPL_INLINE
constexpr T
operator()(
const T& acc,
const T& value)
const noexcept
72 template <
class T =
void>
77 explicit Multiply(T multiplier) : mMultiplier(multiplier) {}
79 JPL_INLINE
constexpr void operator()(T& value)
const noexcept
87 template<
class ContainerType>
92 const float invSum = 1.0f /
static_cast<float>(sum);
93 std::ranges::for_each(std::forward<ContainerType>(data),
Multiply(invSum));
97 template<
class ContainerType>
102 const float invLength =
Math::InvSqrt(
static_cast<float>(sum2));
103 std::ranges::for_each(std::forward<ContainerType>(data),
Multiply(invLength));
106 template<
class ContainerType>
107 [[nodiscard]] JPL_INLINE
constexpr bool IsNormalizedL1(
const ContainerType& data,
float tolerance = JPL_FLOAT_EPS)
114 template<
class ContainerType>
115 [[nodiscard]] JPL_INLINE
constexpr bool IsNormalizedL2(
const ContainerType& data,
float tolerance = JPL_FLOAT_EPS)
128 template<std::random_access_iterator I, std::sentinel_for<I> S,
class Gen>
requires std::permutable<I> && std::uniform_random_bit_generator<std::remove_reference_t<Gen>>
131 using diff_t = std::iter_difference_t<I>;
132 using distr_t = std::uniform_int_distribution<diff_t>;
133 using param_t =
typename distr_t::param_type;
135 const diff_t n = std::ranges::distance(first, last);
136 const diff_t k = std::min<diff_t>(
count, n);
142 std::ranges::iter_swap(first +
i, first +
j);
154 template<std::ranges::random_access_range R,
class Gen>
requires std::permutable<std::ranges::iterator_t<R>>
and std::uniform_random_bit_generator<std::remove_reference_t<Gen>>
functor for for_each (and similar) to multiply elements by a multiplier
Definition Algorithm.h:74
Multiply(T multiplier)
Definition Algorithm.h:77
JPL_INLINE constexpr void operator()(T &value) const noexcept
Definition Algorithm.h:79
std::remove_cvref_t< decltype(*std::declval< ContainerType >().begin())> ElementTypeOf
Definition Algorithm.h:39
Definition Algorithm.h:35
constexpr I PartialShuffle(I first, S last, std::size_t count, Gen &&gen)
Definition Algorithm.h:129
JPL_INLINE constexpr void NormalizeL2(ContainerType &&data)
Apply unit vector scaling, so that the magnitude of the vector = 1.
Definition Algorithm.h:98
JPL_INLINE constexpr bool IsNormalizedL1(const ContainerType &data, float tolerance=JPL_FLOAT_EPS)
Definition Algorithm.h:107
JPL_INLINE constexpr T Accumulate(RangeType &&range, T initialValue, Fn reduceOp={})
Just a wrapper for a range, to not have to type begin and end iterators.
Definition Algorithm.h:45
JPL_INLINE constexpr void NormalizeL1(ContainerType &&data)
Normalize so that the sum = 1.
Definition Algorithm.h:88
JPL_INLINE constexpr bool IsNormalizedL2(const ContainerType &data, float tolerance=JPL_FLOAT_EPS)
Definition Algorithm.h:115
JPL_INLINE constexpr T InvSqrt(T x) noexcept
Definition Math.h:283
JPL_INLINE constexpr bool IsNearlyEqual(T a, T b, T tolerance=JPL_FLOAT_EPS_V< T >) noexcept
Definition Math.h:152
JPL_INLINE constexpr auto Abs(const T &value) noexcept
Standard abs is not constexpr in C++20.
Definition Math.h:87
JPL_INLINE simd min(const simd &a, const simd &b) noexcept
Element-wise min.
Definition SIMD.h:1813
functor for std::accumulate (and similar) to accumulate absolute values
Definition Algorithm.h:64
JPL_INLINE constexpr T operator()(const T &acc, const T &value) const noexcept
Definition Algorithm.h:65
functor for std::accumulate (and similar) to accumulate sum of value squares
Definition Algorithm.h:54
JPL_INLINE constexpr T operator()(const T &acc, const T &value) const noexcept
Definition Algorithm.h:55