AA key conversion woes

SomeDude lovelydear at mailmetrash.com
Fri Apr 20 08:34:05 PDT 2012


On Friday, 20 April 2012 at 15:06:42 UTC, H. S. Teoh wrote:
> On Fri, Apr 20, 2012 at 02:07:05PM +0200, SomeDude wrote:
>> On Thursday, 19 April 2012 at 19:31:36 UTC, H. S. Teoh wrote:
> [...]
>> >Believe me, I would rather not have immutable keys if I could 
>> >help
>> >it, because it makes things so much simpler. However:
>> >
>> >(1) It leads to subtle breakages due to unintended aliasing:
>> >
>> >	int[int[]] aa;
>> >	int[] key1 = [1,2,3];
>> >	aa[key1] = 123;
>> >	key1[0] = 2;	// uh oh!
>> >	assert(aa[[1,2,3]] == 123);	// assertion fails, key has
>> >					// changed without AA's knowledge
>> >
>> 
>> Yeah, I thought about that, but then it's the programmer's
>> responsibility to not shoot himself in the foot, and if he 
>> wants to be
>> safe, he can still declare his keys immutable from the start. 
>> My
>> concern is imposing a useless copy upon the programmer who has 
>> mutable
>> keys for the sake of using the AA. There should be zero 
>> overhead. If
>> for some reason he is forced to do that, he will end up 
>> writing his
>> own implementation.
>
> Copying is only done when actually adding a new key to the AA. 
> For
> example, since char[], const(char)[], and string 
> (==immutable(char)[])
> are all mutually comparable, nothing needs to be copied until 
> the key
> needs to be put inside the container, at which point it seems 
> reasonable
> to copy it. Pure lookups and stuff have no overhead.
>

OK, thank you for clearing up my mind.


More information about the Digitalmars-d mailing list