|
Boost Users : |
Subject: [Boost-users] large enum and native array indexing
From: Hicham Mouline (hicham_at_[hidden])
Date: 2009-01-14 13:18:52
hello,
we have a 3rd party lib which provides an enum OP with some hundred members,
but each member is defined with numbers that are not contiguous.
enum OP { op1=56, op2=6, op3=5 ..... };
We have a performance critical function f that looks like this:
status f( double& result, OP , int thing );
typical usage calls f( result0, op0 ) then f(result1, op1) .... f(resultn, opn)
on the same thing.
these calls, if merged, could have some speedup as there is some repetitive work
done for say op3 and op7.
I then thought of doing
void f( std::map< Op, pair<status, result> >& output, const std::set<OP>&, int
thing ) to collapse the above calls to 1.
Inside this f, we set results in output with the [] operator.
then I compared this to
void f( double results[], status statues[], const std::set<OP>&, int thing ),
thanks to the native array contiguous access, it was 4 times faster.
therefore i wrote a numerical pseudo-metafunction "Position" to take the enum OP
as an input and return a size_t, reflecting the index for the array.
A call would be like:
std::set<OP> opset; opset.set( op1 ); opset.set( op2 ); ... opset.set( op7 );
double results[7]; status statuses[7];
f( results, statuses, opset, thing );
// then to read output
results[ Position<OP5>::value ];
Now, the question is about defining the metafunction Position.
template< OP op > struct Position {};
template<> struct Position<OP5> { const size_t value=4; };
....
The 3rd party enum OP in the header file I can't change.
Is there a way to generate my metafunction automatically?
Any facility in mpl or fusion instead of defining my own metafunction?
regards,
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net