memoize -- AAs don't work for ubyte[4] keys

Steven Schveighoffer schveiguy at yahoo.com
Tue Jan 4 11:45:26 PST 2011


On Tue, 04 Jan 2011 14:16:27 -0500, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 1/4/11 12:44 PM, Manfred_Nowak wrote:
>> Andrei Alexandrescu wrote:
>>
>>> Since a few releases ago
>>
>> upps. sorry for not reading the docs.
>>
>> -manfred
>
> BTW I think the rgb2cmyk implementation in Higher Order Perl (which I  
> copied) is a bit convoluted. A simplified version is:
>
> ubyte[4] rgb2cmyk(ubyte[3] rgb)
> {
>      immutable m = max(rgb[0], rgb[1], rgb[2]);
>      return [ cast(ubyte)(m - rgb[0]), cast(ubyte)(m - rgb[1]),
>              cast(ubyte)(m - rgb[2]), ~m ];
> }
>
> Two nice typing touches: max does not necessitate a cast because it's  
> implemented to return ubyte for ubytes, and ~m also doesn't need a cast  
> thanks to value range propagation.
>
> I don't know how to get rid of the remaining casts.

Note that in the current compiler, this produces a heap allocation.

But if that is fixed, the following at least reduces the casts:

ubyte[4] rbg2cmyk(ubyte[3] rgb)
{
     immutable m = max(rgb[0], rgb[1], rgb[2]);
     return cast(ubyte[4])[m-rgb[0], m-rgb[1], m-rgb[2], ~m];
}

-Steve


More information about the Digitalmars-d mailing list