SolidUtils
VectorMath.hpp
Go to the documentation of this file.
1 
30 #ifndef SOLIDUTILS_INCLUDE_VECTORMATH_HPP
31 #define SOLIDUTILS_INCLUDE_VECTORMATH_HPP
32 
33 
34 #include "Array.hpp"
35 
36 #include <type_traits>
37 
38 
39 namespace sl
40 {
41 
42 
49 {
50  public:
60  template <typename T>
61  static T sum(
62  T * const data,
63  size_t const size) noexcept
64  {
65  T sum = 0;
66  for (size_t i = 0; i < size; ++i) {
67  sum += data[i];
68  }
69  return sum;
70  }
71 
72 
82  template <typename T>
83  static void increment(
84  T * const data,
85  size_t const size,
86  T const start = 0,
87  T const inc = 1) noexcept
88  {
89  T val = start;
90  for (size_t i = 0; i < size; ++i) {
91  data[i] = val;
92  val += inc;
93  }
94  }
95 
96 
104  template <typename T>
105  static void prefixSumExclusive(
106  T * const data,
107  size_t const size) noexcept
108  {
109  prefixSumExclusive(data, data+size);
110  }
111 
112 
120  template <typename T>
121  static void prefixSumExclusive(
122  T const begin,
123  T const end) noexcept
124  {
125  using V = typename std::remove_reference<decltype(*begin)>::type;
126  V sum = 0;
127  for (T iter = begin; iter != end; ++iter) {
128  V const val = *iter;
129  *iter = sum;
130  sum += val;
131  }
132  }
133 };
134 
135 
136 
137 
138 }
139 
140 
141 
142 
143 #endif
Definition: Alloc.hpp:40
static void increment(T *const data, size_t const size, T const start=0, T const inc=1) noexcept
Set all entries int the array to the given sequence.
Definition: VectorMath.hpp:83
static void prefixSumExclusive(T const begin, T const end) noexcept
Perform a prefix sum on an array.
Definition: VectorMath.hpp:121
A mutable array structure for storing self-allocated memory.
static T sum(T *const data, size_t const size) noexcept
Sum the elements of an array.
Definition: VectorMath.hpp:61
The VectorMath class contains static functions for manipulating numerical data in vectors...
Definition: VectorMath.hpp:48
static void prefixSumExclusive(T *const data, size_t const size) noexcept
Perform a prefix sum on an array.
Definition: VectorMath.hpp:105