Re: [Rd] as.numeric(levels(factor(x))) may be a decreasing sequence

From: Petr Savicky <savicky_at_cs.cas.cz>
Date: Sun, 31 May 2009 10:29:41 +0200

On Sat, May 30, 2009 at 07:32:52PM +0200, Martin Maechler wrote:
> >>>>> "vQ" == Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk@idi.ntnu.no>
> >>>>> on Sat, 30 May 2009 11:16:43 +0200 writes:

[...]

> vQ> one simple way to improve the code is as follows; instead of (simplified)
>
> vQ> const char* dropTrailing(const char* s, ...) {
> vQ> const char *p = s;
> vQ> char *replace;
> vQ> ...
> vQ> replace = (char*) p;
> vQ> ...
> vQ> return s; }
>
> vQ> ...mkChar(dropTrailing(EncodeReal(...), ...) ...
>
> vQ> you can have something like
>
> vQ> const char* dropTrailing(char* s, ...) {
> vQ> char *p = s, *replace;
> vQ> ...
> vQ> replace = p;
> vQ> ...
> vQ> return s; }
>
> vQ> ...mkChar(dropTrailing((char*)EncodeReal(...), ...) ...
>
> vQ> where it is clear, from DT's signature, that it may (as it purposefully
> vQ> does, in fact) modify the content of s. that is, you drop the
> vQ> promise-not-to-modify contract in DT, and move the need for
> vQ> deconstifying ER's return out of DT, making it more explicit.

[...]

> vQ> (3) modify petr's solution along the lines above, i.e., have the input
> vQ> in the signature non-const and deconst-cast the output from ER outside
> vQ> of the call to DT.
>
> that's what I have adopted, as I'm sure you've noticed when you
> saw the code above.

I appreciate the current version, which contains   static const char* dropTrailing0(char *s, char cdec)   ...
  mkChar(dropTrailing0((char *)EncodeReal(x, w, d, e, OutDec), ...

Here, is better visible that the cast (char *) is used than if it was hidden inside dropTrailing0(). Also, it makes dropTrailing0() more consistent.

I would like to recall the already discussed modification

            if (replace != p)
                while((*(replace++) = *(p++)))
                    ;

which saves a few instructions in the more frequent case that there are no trailing zeros.

Petr.



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun 31 May 2009 - 08:33:28 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 01 Jun 2009 - 15:04:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive