Skip to content

Commit

Permalink
switchless component access
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilipDeegan authored and nicolasaunai committed Dec 19, 2023
1 parent 94fac3e commit 78d0536
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
39 changes: 16 additions & 23 deletions src/core/data/tensorfield/tensorfield.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ template<typename NdArrayImpl, typename PhysicalQuantity, std::size_t rank_ = 1>
class TensorField
{
private:
template<std::size_t _rank = rank_>
constexpr static std::size_t dimFromRank()
{
if constexpr (rank_ == 1) // Vector field
if constexpr (_rank == 1) // Vector field
return 3;
else if constexpr (rank_ == 2) // symmetric 3x3 tensor field
else if constexpr (_rank == 2) // symmetric 3x3 tensor field
return 6;
}

auto static _get_index_for(Component component)
{
auto val = static_cast<std::underlying_type_t<Component>>(component);
if constexpr (rank == 1)
return val;
else if constexpr (rank == 2)
return val - dimFromRank<1>();
}

public:
TensorField() = delete;
TensorField(TensorField const& source) = delete;
Expand Down Expand Up @@ -116,24 +126,7 @@ class TensorField
NO_DISCARD std::string const& name() const { return name_; }


template<typename Arg>
NO_DISCARD decltype(auto) static _switcheroo(Component component, Arg& arg)
{
switch (component)
{
case Component::X: return arg[0];
case Component::Y: return arg[1];
case Component::Z: return arg[2];

case Component::XX: return arg[0];
case Component::XY: return arg[1];
case Component::XZ: return arg[2];
case Component::YY: return arg[3];
case Component::YZ: return arg[4];
case Component::ZZ: return arg[5];
}
throw std::runtime_error("Error - TensorField not usable");
}


void _check() const
{
Expand All @@ -144,7 +137,7 @@ class TensorField
NO_DISCARD field_type& getComponent(Component component)
{
_check();
return *_switcheroo(component, components_);
return *components_[_get_index_for(component)];
}


Expand All @@ -153,14 +146,14 @@ class TensorField
NO_DISCARD field_type const& getComponent(Component component) const
{
_check();
return *_switcheroo(component, components_);
return *components_[_get_index_for(component)];
}



NO_DISCARD std::string getComponentName(Component component) const
{
return _switcheroo(component, componentNames_);
return componentNames_[_get_index_for(component)];
}


Expand Down
2 changes: 1 addition & 1 deletion src/core/data/vecfield/vecfield_component.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace PHARE
{
namespace core
{
enum class Component { X, Y, Z, XX, XY, XZ, YY, YZ, ZZ };
enum class Component : std::uint16_t { X = 0, Y, Z, XX, XY, XZ, YY, YZ, ZZ };

struct Components
{
Expand Down

0 comments on commit 78d0536

Please sign in to comment.