Vectorize

Automatically convert some struct S to a vector of homogeneous data type, optionally expanding enum values to one-hot encodings. Useful for writing unit data or score data to numpy arrays for ML training.

Author

Bryce Ferenczi

Version

0.1

Date

2024-08-10

Copyright

Copyright (c) 2024

namespace cvt

Functions

template<typename T>
constexpr auto getVectorizedSize(bool onehotEnum) -> std::size_t

Get the size of the struct if its vectorized with vectorize.

Template Parameters:

T – Type to query

Parameters:

onehotEnum – Flag if oneHotEncodings are expanded or not

Returns:

Number of elements of the vectorized type

template<typename S, typename It>
auto vectorize(S s, It it, bool onehotEnum = false) -> It

Vectorize Struct of data to mutable output iterator.

Template Parameters:
  • S – struct type

  • It – output iterator type

Parameters:
  • s – Struct data to vectorize

  • it – Output iterator

  • onehotEnum – Flag to expand enum types to onehot encoding (default: false)

Returns:

Incremented output iterator

template<typename T, typename S>
auto vectorize(S s, bool onehotEnum = false) -> std::vector<T>

Vectorize Struct of data to vector.

Template Parameters:
  • T – Output arithmetic type of vector

  • S – Struct type to vectorize

Parameters:
  • s – struct data to vectorize

  • onehotEnum – Flag to expand enum types to onehot encoding (default: false)

Returns:

Vectorized struct data as type T

namespace detail

Functions

template<typename T, typename It>
auto vectorize_helper(T d, It it, bool) -> It

Vectorize single arithmetic element by writing to output iterator It and incrementing.

Template Parameters:
  • T – Arithmetic type

  • It – Output iterator type

Parameters:
  • d – Data to vectorize

  • it – Output iterator

  • onehotEnum – flag to expand enum types to onehot

Returns:

Incremented output iterator

template<std::ranges::range T, typename It>
auto vectorize_helper(const T &d, It it, bool) -> It

Vectorize range of arithmetic elements by writing to and incrementing output iterator.

Template Parameters:
  • T – Range type

  • It – Output iterator type

Parameters:
  • d – Range data to vectorize

  • it – Output iterator

  • onehotEnum – flag to expand enum types to onehot

Returns:

Incremented output iterator

template<typename T, typename It>
auto vectorize_helper(T d, It it, bool onehotEnum) -> It

Vectorize enum type and optionally expand to onehot encoding to and incrementing output iterator.

Template Parameters:
  • T – Enum type

  • It – Output iterator type

Parameters:
  • d – Enum data to vectorize

  • it – Output iterator

  • onehotEnum – flag to expand enum types to onehot

Returns:

Incremented output iterator

template<typename T, typename It>
auto vectorize_helper(T d, It it, bool onehotEnum) -> It

Vectorize struct (aggregate type) to data.

Template Parameters:
  • T – Struct type

  • It – Output iterator type

Parameters:
  • d – Struct to vectorize

  • it – Output iterator

  • onehotEnum – flag to expand enum types to onehot

Returns:

Incremented output iterator

template<typename T, bool oneHotEnum>
consteval auto vectorizedSizeHelper() -> std::size_t

Does the main lifting for determining the size of a vectorized struct.

Template Parameters:
  • T – Type to understand vectorization size

  • oneHotEnum – Flag to expand enums to onehot

Returns:

std::size_t The size of the vectorized type