Re: [eigen] (Const)FixedSegmentReturnType with Compile-time known offset

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hello
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
....
so I can do something like
namedVec[Pressure] = 0.5;

I recently published a paper where the same API is used for simulation development (dx.doi.org/10.5194/gmd-8-473-2015) so I'd say this is definitely worth pursuing :) But why use index calculations at all? Can't you just overload [] for each type to return the data corresponding to that type without needing any indices, as done in listing 1 in the above paper?

One of the referees showed how to get something similar with Blitz++ (listing B in http://www.geosci-model-dev-discuss.net/7/C2172/2014/gmdd-7-C2172-2014-supplement.pdf) but that method requires indices and makes the user responsible for them which is unnecessary with type indexing. With type indexing there's also no need to change existing indices when adding/removing new types.

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.

Does operator [] return a reference to the data or is that hidden elsewhere? I've been thinking about how to return a tuple of references to the partial contents of another tuple and the above looks interesting but it's not the same thing is it?

Ilja



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/