mathieu wrote:
I am trying to rewrite this very slow function (due to AFAIK the
extra copy):
void DoAction(std::istream &is, std::ostream &os)
{
uint16_t c;
while( is.read((char*)&c,2) )
{
c =
(c >> (16 - 12 - 1)) & pmask;
os.write((char*)&c, 2 );
}
}
I was thinking of doing:
os.rdbuf( is.rdbuf() )
and then working directly with a ostreambuf_iterator, but I cannot
read from a ostreambuf_iterator can I ?
Don't reassign the output streambuf. Use transform, or a loop on the
streambuf iterators.
i.e.:
struct convert
{
uint16_t operator()(const uint16 c) const
{
return (c >> shift) & pmask);
}
convert(uint16_t& pmask_) : pmask(pmask_) { }
private:
static const shift = (16 - 12 - 1);
uint16_t pmask;
};
void DoAction(std::istream& is, std::ostream& os)
{
std::transform(std::istreambuf_iterator<uint16_t>(is),
std::istreambuf_iterator<uint16_t>(),
std::ostreambuf_iterator<uint16_t>(os));