[eigen] (Const)FixedSegmentReturnType with Compile-time known offset |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: <eigen@xxxxxxxxxxxxxxxxxxx>
- Subject: [eigen] (Const)FixedSegmentReturnType with Compile-time known offset
- From: <Daniel.Vollmer@xxxxxx>
- Date: Thu, 19 Mar 2015 17:18:46 +0000
- Accept-language: de-DE, en-US
- Thread-index: AdBiZvni9Ijbsi23Qyqb8GQ03EDJJg==
- Thread-topic: (Const)FixedSegmentReturnType with Compile-time known offset
Hello all,
not sure whether this is of general interest (and whether it is worthwhile pursuing at all).
I built a NamedVector on top of fixed size Eigen vectors. This class has a method which translates
names (which are represented by types) to compile-time known indices
// pseudo-code, the real lookup produces the index differently
template<class Arg>
T &operator[](const Arg &arg)
{
return _eigenVec[Arg::Index];
}
so I can do something like
namedVec[Pressure] = 0.5;
Then, I did the same for tuples of names (types)
template<typename FirstIndexType, typename ...MoreIndexTypes>
decltype(std::declval<BaseVector>().template segment<1+sizeof...(MoreIndexTypes)>(0u))
operator[](const std::tuple<FirstIndexType, MoreIndexTypes ...> &i)
{
constexpr auto firstIndex = TH::FirstIndexOfKeyInTuple<FirstIndexType, FormatT>::value;
static_assert(std::is_same<
std::tuple<FirstIndexType, MoreIndexTypes ...>,
TH::TupleNElementsAtOffset<1+sizeof...(MoreIndexTypes), firstIndex, FormatT>
>::value, "Cannot access non-contiguous values as sub-vector!");
return BaseVector::template segment<1+sizeof...(MoreIndexTypes)>(firstIndex);
}
which returns a FixedSegmentReturnType<N>.
Now I can do something like
// MomentumVector is an instance of std::tuple<MomentumX, MomentumY, MomentumZ>
namedVec[MomentumVector] = Vector3d::Zero();
which is sort of nifty.
But in my case, even the offset could be a template parameter of FixedSegmentReturnType.
Would this be worthwhile pursuing in your opinion?
Some testing shows that replacing the explicit references to MomentumX with the vector
variant can sometimes lead to slower code (or at least hit inlining limits earlier).
Best regards
Daniel Vollmer
--------------------------
Deutsches Zentrum für Luft- und Raumfahrt e.V. (DLR)
German Aerospace Center
Institute of Aerodynamics and Flow Technology | Lilienthalplatz 7 | 38108 Braunschweig | Germany
Daniel Vollmer | AS C²A²S²E
www.DLR.de