Pasha Bibko Util Library
Loading...
Searching...
No Matches
Vec< len, Ty > Struct Template Reference

Mathmatical vector class. More...

#include <Vec.h>

Inheritance diagram for Vec< len, Ty >:
Collaboration diagram for Vec< len, Ty >:

Public Member Functions

template<typename = std::enable_if_t<std::is_default_constructible_v<Ty>>>
 Vec ()
 Default constructor which default constructs all items.
 
 Vec (const Ty &value)
 Constructor to create each item in the vector with a given value.
 
template<typename... Args>
requires Internal::AllSameType<Ty, Args...> && (sizeof...(Args) == len)
 Vec (Args &&... args)
 Creates a vector with a given value for each item.
 
Tyoperator[] (std::size_t index)
 Returns a reference to the item at that index.
 
Tybegin () noexcept
 Returns a pointer to the beginning of the array.
 
Tyend ()
 Returns a pointer to the end of the vector.
 
template<typename OtherTy >
requires Internal::CanAdd<Ty, OtherTy> && std::is_same_v<Ty, Internal::AddResultT<Ty, OtherTy>>
Vecoperator+= (const Vec< len, OtherTy > &other)
 Adds another vector of the same size to itself.
 
template<typename OtherTy >
requires Internal::CanSub<Ty, OtherTy> && std::is_same_v<Ty, Internal::SubResultT<Ty, OtherTy>>
Vecoperator-= (const Vec< len, OtherTy > &other)
 Subtracts another vector of the same size to itself.
 
template<typename OtherTy >
requires Internal::CanMul<Ty, OtherTy> && std::is_same_v<Ty, Internal::MulResultT<Ty, OtherTy>>
Vecoperator*= (const Vec< len, OtherTy > &other)
 Multiplies another vector of the same size to itself.
 
template<typename OtherTy >
requires Internal::CanDiv<Ty, OtherTy> && std::is_same_v<Ty, Internal::DivResultT<Ty, OtherTy>>
Vecoperator/= (const Vec< len, OtherTy > &other)
 Divides another vector of the same size to itself.
 

Detailed Description

template<std::size_t len, typename Ty>
requires (len != 0 && len != 1) && std::is_copy_constructible_v<Ty>
struct PashaBibko::Util::Vec< len, Ty >

Mathmatical vector class.

Template Parameters
lenThe length of the array, cannot be 0 or 1.
TyThe type that the vector contains must be copyable.

The Vec class is a fixed-size, strongly-typed mathematical vector implementation that supports compile-time size checking and type constraints. It is designed for mathematical operations and performance-critical applications where a fixed size and no dynamic memory allocation are desired.

For convenience whilst using there are also multiple typedefs for easier recognition of common Vec types. For vectors between size of 2 and 4 you are able to write Vec2<Ty> / Vec3<Ty> / Vec4<Ty> where Ty is the type that you want the vector to contain. If no type is provided it will default to float.

Combined with this, there are also typedefs of these lengths with the types. Below are the Vec2 types but the number can be changed to work with Vec3 and Vec4.

  • short = Vec2s
  • int = Vec2i
  • unsigned int = Vec2u
  • long = Vec2l
  • double = Vec2d

For further convenience vectors between the lengths of 2 and 4 allow access to their members via letters:

  • x = data[0]
  • y = data[1]
  • z = data[2]
  • w = data[3]

If you are working with colours you are also able to use those letters (only available on Vec3 and Vec4):

  • r = data[0]
  • g = data[1]
  • b = data[2]
  • a = data[3]

Letters are only availble if the vector is already that long, for example: Vec2 only has access to x and y as it is only 2 elements long.

Example usage:

#include <iostream>
int main()
{
// Create a 3D vector with all elements default-initialized to 0
// Create a vector where all elements are set to 5.0f
Vec3 b(5.0f);
// Create a vector from individual values
Vec3 c(1.0f, 2.0f, 3.0f);
// Access and modify an element
c.x = 10.0f;
// Iterate over vector elements
for (float value : c)
{
std::cout << value << " ";
}
std::cout << "\n";
// Add two vectors
c += b; // element-wise addition
// Print result
for (float value : c)
{
std::cout << value << " ";
}
std::cout << "\n";
return 0;
}
Mathmatical vector class.
Definition Vec.h:216
Vec()
Default constructor which default constructs all items.
Definition Vec.h:224

Constructor & Destructor Documentation

◆ Vec() [1/3]

template<std::size_t len, typename Ty >
template<typename = std::enable_if_t<std::is_default_constructible_v<Ty>>>
Vec ( )
inline

Default constructor which default constructs all items.

Will only be available if Ty has a default constructor to avoid compile-time errors.

◆ Vec() [2/3]

template<std::size_t len, typename Ty >
Vec ( const Ty value)
inlineexplicit

Constructor to create each item in the vector with a given value.

Parameters
valueThe value that will be copied to all values within the vector.

◆ Vec() [3/3]

template<std::size_t len, typename Ty >
template<typename... Args>
requires Internal::AllSameType<Ty, Args...> && (sizeof...(Args) == len)
Vec ( Args &&...  args)
inlineexplicit

Creates a vector with a given value for each item.

Parameters
argsThe arguments that will be copied to the contents of the vector.

Requires all arguments to be the same type as Ty and have the same length as the array or will have a compile-time error.

Member Function Documentation

◆ begin()

template<std::size_t len, typename Ty >
Ty * begin ( )
inlinenoexcept

Returns a pointer to the beginning of the array.

Used by C++ to allow the data type to be iterated over by a range for loop.

◆ end()

template<std::size_t len, typename Ty >
Ty * end ( )
inline

Returns a pointer to the end of the vector.

Used by C++ to allow the data type to be iterated over by a range for loop.

◆ operator*=()

template<std::size_t len, typename Ty >
template<typename OtherTy >
requires Internal::CanMul<Ty, OtherTy> && std::is_same_v<Ty, Internal::MulResultT<Ty, OtherTy>>
Vec & operator*= ( const Vec< len, OtherTy > &  other)
inline

Multiplies another vector of the same size to itself.

Requires Ty to be able to be multipled by OtherTy, otherwise it will not compile and the result type to be the same as Ty.

◆ operator+=()

template<std::size_t len, typename Ty >
template<typename OtherTy >
requires Internal::CanAdd<Ty, OtherTy> && std::is_same_v<Ty, Internal::AddResultT<Ty, OtherTy>>
Vec & operator+= ( const Vec< len, OtherTy > &  other)
inline

Adds another vector of the same size to itself.

Requires OtherTy to be able to be added to Ty, otherwise it will not compile and the result type to be the same as Ty.

◆ operator-=()

template<std::size_t len, typename Ty >
template<typename OtherTy >
requires Internal::CanSub<Ty, OtherTy> && std::is_same_v<Ty, Internal::SubResultT<Ty, OtherTy>>
Vec & operator-= ( const Vec< len, OtherTy > &  other)
inline

Subtracts another vector of the same size to itself.

Requires OtherTy to be able to be subtracted from Ty, otherwise it will not compile and the result type to be the same as Ty.

◆ operator/=()

template<std::size_t len, typename Ty >
template<typename OtherTy >
requires Internal::CanDiv<Ty, OtherTy> && std::is_same_v<Ty, Internal::DivResultT<Ty, OtherTy>>
Vec & operator/= ( const Vec< len, OtherTy > &  other)
inline

Divides another vector of the same size to itself.

Requires Ty to be able to be divided by OtherTy, otherwise it will not compile and the result type to be the same as Ty.

◆ operator[]()

template<std::size_t len, typename Ty >
Ty & operator[] ( std::size_t  index)
inline

Returns a reference to the item at that index.

Parameters
indexThe index that it will find the item of.
Warning
The function does not check if the index is within the bounds of the vector. Accessing elements not within the bounds is classified as UB.

The documentation for this struct was generated from the following file: