|
Boost : |
From: Neal D. Becker (nbecker_at_[hidden])
Date: 2004-01-13 15:23:18
Jens Maurer wrote:
>
> Neal D. Becker wrote:
>> For example, In my pnseq_generator class, I need to cache the result of
>> calling the underlying engine. What is the type of the cache? We may
>> want the cache type to match Engine::result_type, but the Engine type
>> isn't known when pnseq_generator is constructed.
>
> A normal_distribution<> also needs to cache a value from the underlying
> engine. You may want to have a look at its implementation.
Thanks. Yes, that is interesting. normal_distribution caches the output
value, not the Engine::result_type, so it avoids my problem.
>
> In general, engines specify both their input and output type requirements
> and variate_generator<> satisfies those.
>
Not sure what you're getting at.
Here is the code in question:
namespace boost {
template <int width=1, class cache_type=int, class IntType=int>
class pnseq_generator {
public:
typedef IntType result_type;
typedef IntType input_type;
explicit pnseq_generator() : count (0) {}
BOOST_STATIC_CONSTANT (cache_type, mask = ~(unsigned(-1) << width));
template<class Engine>
int operator() (Engine& eng) {
if (count <= width-1) {
cache = eng();
count = std::min (std::numeric_limits<typename
Engine::result_type>::digits, std::numeric_limits<cache_type>::digits);
}
result_type bits = cache & mask;
cache >>= width;
count -= width;
return bits;
}
private:
int count;
cache_type cache;
};
}
The problem is that it we don't know in advance what cache_type should be:
it should really match Engine::result_type, but we don't know what that is
till operator() is used. It's not a major problem, but it does make the
code a bit ugly.
Any suggestions on improving this code are welcomed.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk