[bind10-dev] Name::reverse()?

JINMEI Tatuya / 神明達哉 jinmei at isc.org
Sat Feb 20 08:49:33 UTC 2010


At Fri, 19 Feb 2010 17:25:42 +0000,
Evan Hunt <each at isc.org> wrote:

> Fantastic.  I tried to figure out transform() and failed, so I came
> up with this solution instead--if there's a smarter way, let's do that.
> 
> ... I like your double-iterator approach.  However, wouldn't this work
> equally well, without the transform()?

Unfortunately not.  You also need to set retname.offsets_[0]
explicitly:

>      // Copy the original name, label by label, from tail to head.
>      vector<unsigned char>::const_reverse_iterator rit0 = offsets_.rbegin();
>      vector<unsigned char>::const_reverse_iterator rit1 = rit0 + 1;
>      string::const_iterator n0 = ndata_.begin();
       retname.offsets_.push_back(0); // THIS IS ALSO NECESSARY
>      while (rit1 != offsets_.rend()) {
>          retname.ndata_.append(n0 + *rit1, n0 + *rit0);
> +        retname.offsets_.push_back(retname.ndata_.size());
>          ++rit0;
>          ++rit1;
>      }
>      retname.ndata_.push_back(0);

(btw unittests should have been your friend:-)

That's what I meant by "by eliminating the boundary case consideration
for offsets_[0]".

Admittedly, however, the advantage of the transform() version is moot.
While (one of) the main motivation of using STL algorithms is to
eliminate the need for explicit loops, we still need the while loop
here, and transform() essentially needs a loop internally, the
transform() would probably be slightly worse in terms of efficiency.

I generally prefer a style that doesn't require an explicit and
separate boundary consideration like the above case, but in this
particular case it's probably a matter of taste.

---
JINMEI, Tatuya



More information about the bind10-dev mailing list