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

Petr Savicky savicky at cs.cas.cz
Sun May 31 10:29:41 CEST 2009


On Sat, May 30, 2009 at 07:32:52PM +0200, Martin Maechler wrote:
> >>>>> "vQ" == Wacek Kusnierczyk <Waclaw.Marcin.Kusnierczyk at 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.



More information about the R-devel mailing list