From savicky at cs.cas.cz  Sun May 31 10:29:41 2009
From: savicky at cs.cas.cz (Petr Savicky)
Date: Sun, 31 May 2009 10:29:41 +0200
Subject: [Rd] as.numeric(levels(factor(x))) may be a decreasing sequence
In-Reply-To: <18977.28100.532043.734122@cmath-4.math.ethz.ch>
References: <20090523074454.GA17112@cs.cas.cz>
	<18973.42970.826686.173830@cmath-5.math.ethz.ch>
	<4A1DC057.80401@idi.ntnu.no>
	<18975.59582.885300.549159@lynne.math.ethz.ch>
	<4A203D67.8000007@idi.ntnu.no>
	<3f1d2410905291448y271fa484te82b8dbaa547017f@mail.gmail.com>
	<4A20F97B.1090406@idi.ntnu.no>
	<18977.28100.532043.734122@cmath-4.math.ethz.ch>
Message-ID: <20090531082941.GA12517@cs.cas.cz>

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.



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