Skip to content
Snippets Groups Projects
Commit 3019af00 authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Improvements to TinyVector

One can write for instance
TinyVector<3> x = zero;
TinyVector<2> y(1,0);

Added Types.hpp which defines
- zero
- identity
parent 6707abab
No related branches found
No related tags found
2 merge requests!2Develop,!1Develop
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include <cassert> #include <cassert>
#include <iostream> #include <iostream>
#include <Types.hpp>
template <size_t N, typename T=double> template <size_t N, typename T=double>
class TinyVector class TinyVector
{ {
...@@ -11,6 +13,18 @@ private: ...@@ -11,6 +13,18 @@ private:
T m_values[N]; T m_values[N];
static_assert((N>0),"TinyVector size must be strictly positive"); static_assert((N>0),"TinyVector size must be strictly positive");
void _unpackVariadicInput(const T& t)
{
m_values[N-1] = t;
}
template <typename... Args>
void _unpackVariadicInput(const T& t, Args&&... args)
{
m_values[N-1-sizeof...(args)] = t;
this->_unpackVariadicInput(args...);
}
public: public:
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
T operator,(const TinyVector& v) T operator,(const TinyVector& v)
...@@ -81,15 +95,6 @@ public: ...@@ -81,15 +95,6 @@ public:
return *this; return *this;
} }
KOKKOS_INLINE_FUNCTION
TinyVector& operator=(const T& t)
{
for (size_t i=0; i<N; ++i) {
m_values[i] = t;
}
return *this;
}
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
T& operator[](const size_t& i) T& operator[](const size_t& i)
{ {
...@@ -104,6 +109,15 @@ public: ...@@ -104,6 +109,15 @@ public:
return m_values[i]; return m_values[i];
} }
KOKKOS_INLINE_FUNCTION
TinyVector& operator=(const ZeroType& z)
{
for (size_t i=0; i<N; ++i) {
m_values[i] = 0;
}
return *this;
}
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
const TinyVector& operator=(const TinyVector& v) const TinyVector& operator=(const TinyVector& v)
{ {
...@@ -116,6 +130,14 @@ public: ...@@ -116,6 +130,14 @@ public:
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
TinyVector& operator=(TinyVector&& v) = default; TinyVector& operator=(TinyVector&& v) = default;
template <typename... Args>
KOKKOS_INLINE_FUNCTION
TinyVector(const T& t, Args&&... args)
{
static_assert(sizeof...(args)==N-1, "wrong number of parameters");
this->_unpackVariadicInput(t, args...);
}
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
TinyVector() TinyVector()
{ {
...@@ -123,10 +145,10 @@ public: ...@@ -123,10 +145,10 @@ public:
} }
KOKKOS_INLINE_FUNCTION KOKKOS_INLINE_FUNCTION
TinyVector(const T& t) TinyVector(const ZeroType& z)
{ {
for (size_t i=0; i<N; ++i) { for (size_t i=0; i<N; ++i) {
m_values[i] = t; m_values[i] = 0;
} }
} }
......
...@@ -133,8 +133,10 @@ int main(int argc, char *argv[]) ...@@ -133,8 +133,10 @@ int main(int argc, char *argv[])
<< rang::style::reset << '\n'; << rang::style::reset << '\n';
} }
TinyVector<2> x=1; TinyVector<2> x=zero;
TinyVector<2> y=2; TinyVector<2> y{1,2};
x=TinyVector<2>{3,2};
std::cout << x << "-" << y << "=" << x-y << std::endl; std::cout << x << "-" << y << "=" << x-y << std::endl;
std::cout << x << "+" << y << "=" << x+y << std::endl; std::cout << x << "+" << y << "=" << x+y << std::endl;
......
#ifndef TYPES_HPP
#define TYPES_HPP
enum class ZeroType { zero };
constexpr ZeroType zero=zero;
enum class IdentityType { identity };
constexpr IdentityType identity=identity;
#endif // TYPES_HPP
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment