This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
ArrayHelpers.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include "Array.hpp"
6 
7 #if __cplusplus >= 201400L
8 #define USE_CONSTEXPR_ARRAY_HELPERS constexpr
9 #else
10 #define USE_CONSTEXPR_ARRAY_HELPERS
11 #endif
12 
14 template <class ForwardIt, class Generator>
15 USE_CONSTEXPR_ARRAY_HELPERS void generate(ForwardIt first, ForwardIt last,
16  Generator g) {
17  while (first != last)
18  *first++ = g();
19 }
20 
30 template <class T, class V>
31 class Incrementor {
32  public:
34  : value(start), increment(increment) {}
36  T temp = value;
37  value += increment;
38  return temp;
39  }
40 
41  private:
42  T value;
43  const V increment;
44 };
45 
48 
64 template <class T, size_t N, class G>
66  Array<T, N> array{};
67  generate(array.begin(), array.end(), generator);
68  return array;
69 }
70 
84 template <size_t N, class G>
86  -> Array<decltype(generator()), N> {
87  Array<decltype(generator()), N> array{};
88  generate(array.begin(), array.end(), generator);
89  return array;
90 }
91 
105 template <class T, size_t N, class U>
107  Array<T, N> dest{};
108  for (size_t i = 0; i < N; ++i)
109  dest[i] = src[i];
110  return dest;
111 }
112 
113 template <class T, size_t N, class... Args>
115  return generateArray<N>([&]() { return T{args...}; });
116 }
117 
150 template <class T, size_t N, class U, class V = U>
152 generateIncrementalArray(U start = 0, V increment = 1) {
153  Incrementor<U, V> g(start, increment);
154  return generateArray<T, N>(g);
155 }
156 
172 template <class T, size_t M, size_t N>
174  const Array<T, N> &b) {
175  Array<T, M + N> result = {};
176  size_t r = 0;
177  for (size_t i = 0; i < M; ++i, ++r)
178  result[r] = a[i];
179  for (size_t i = 0; i < N; ++i, ++r)
180  result[r] = b[i];
181  return result;
182 }
183 
184 /// @}
MIDI_Notes::G
constexpr int8_t G
Definition: Notes.hpp:25
copyAs
Array< T, N > copyAs(const Array< U, N > &src)
Copy an Array to an Array of a different type.
Definition: ArrayHelpers.hpp:106
Incrementor::increment
const V increment
Definition: ArrayHelpers.hpp:43
generateIncrementalArray
Array< T, N > generateIncrementalArray(U start=0, V increment=1)
Generate an array where the first value is given, and the subsequent values are calculated as the pre...
Definition: ArrayHelpers.hpp:152
Array
An array wrapper for easy copying, comparing, and iterating.
Definition: Array.hpp:20
USE_CONSTEXPR_ARRAY_HELPERS
#define USE_CONSTEXPR_ARRAY_HELPERS
Definition: ArrayHelpers.hpp:10
Array.hpp
Incrementor::Incrementor
Incrementor(T start=0, V increment=1)
Definition: ArrayHelpers.hpp:33
fillArray
Array< T, N > fillArray(Args... args)
Definition: ArrayHelpers.hpp:114
generate
void generate(ForwardIt first, ForwardIt last, Generator g)
https://en.cppreference.com/w/cpp/algorithm/generate
Definition: ArrayHelpers.hpp:15
Incrementor::value
T value
Definition: ArrayHelpers.hpp:42
generateArray
Array< T, N > generateArray(G generator)
Generate an array using the given generator.
Definition: ArrayHelpers.hpp:65
Incrementor
Utility class that acts as a functor to return incremental values.
Definition: ArrayHelpers.hpp:31
Incrementor::operator()
T operator()()
Definition: ArrayHelpers.hpp:35
cat
Array< T, M+N > cat(const Array< T, M > &a, const Array< T, N > &b)
Concatenate two arrays.
Definition: ArrayHelpers.hpp:173