COW vs. in-place.

Kirk McDonald kirklin.mcdonald at gmail.com
Mon Jul 31 15:21:41 PDT 2006


Derek wrote:
> On Mon, 31 Jul 2006 16:40:54 -0500, Dave wrote:
> 
> 
>>Not a bad idea... The main prob. would be that there would be a lot of 
>>duplication of code.
> 
> 
> void toUpper_inplace(char[] x)
> {
>  . . .
> }
> 
> char[] toUpper(char[] x)
> {
>    char[] y = x.dup;
>    toUpper_inplace(y);
>    return y;
> }
> 

I've got one better. Say we have a whole bunch of inplace string 
functions, like the one above and this one:

void toLower_inplace(char[] x) {
     // ...
}

and others. Then we can:

char[] cow_func(alias fn)(char[] x) {
     char[] y = x.dup;
     fn(y);
     return y;
}

alias cow_func!(toUpper_inplace) toUpper;
alias cow_func!(toLower_inplace) toLower;

Etc. Obviously, you'd have to provide a different template for each 
function footprint, but the string library has a lot of repeated footprints.

-- 
Kirk McDonald
Pyd: Wrapping Python with D
http://dsource.org/projects/pyd/wiki



More information about the Digitalmars-d mailing list