Thread overview | ||||||
---|---|---|---|---|---|---|
|
April 02, 2016 Address of an element of AA | ||||
---|---|---|---|---|
| ||||
Hi ! I can't find this in specs. If i add an element to AA: aa[10] = 123; Will &aa[10] be always the same (of course i don't remove that key) ? Thanks for a reply. I think specs should be enhanced. |
April 02, 2016 Re: Address of an element of AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | On 04/02/2016 07:22 AM, Temtaime wrote:
> Hi !
> I can't find this in specs.
>
> If i add an element to AA:
> aa[10] = 123;
>
> Will &aa[10] be always the same (of course i don't remove that key) ?
>
> Thanks for a reply.
> I think specs should be enhanced.
No, the underlying buffer can be relocated when the hash table is reconstructed e.g. when there are sufficiently many elements in the array.
Ali
|
April 02, 2016 Re: Address of an element of AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | On Saturday, 2 April 2016 at 14:22:01 UTC, Temtaime wrote:
> Hi !
> I can't find this in specs.
>
> If i add an element to AA:
> aa[10] = 123;
>
> Will &aa[10] be always the same (of course i don't remove that key) ?
>
> Thanks for a reply.
> I think specs should be enhanced.
Running following
int aa[int];
aa[10] = 123;
auto p = &aa[10];
writeln(*p);
aa[11] = 121; // add Element
writeln(*p);
aa[10] = 1; // change Element
writeln(*p);
aa.rehash; // reorganize
writeln(*p);
aa.remove(10);
aa[10] = 3; // remove & add Element
writeln(*p);
results in
123 // correct
123 // correct
1 // correct
1 // correct because I'm lucky
1 // wrong
What Alis said is "rehash", a combination of remove/add or any other reorganisation, which could change memory usage and data locations.
By the way...why do you need a pointer to an AA-Element?
Regards, Ozan
|
April 02, 2016 Re: Address of an element of AA | ||||
---|---|---|---|---|
| ||||
Posted in reply to Temtaime | On 4/2/16 10:22 AM, Temtaime wrote:
> Hi !
> I can't find this in specs.
>
> If i add an element to AA:
> aa[10] = 123;
>
> Will &aa[10] be always the same (of course i don't remove that key) ?
>
> Thanks for a reply.
> I think specs should be enhanced.
Depends on your definition of "always". As long as you don't add or remove keys, it will remain at the same place. Adding other keys can cause a rehash, and removing keys (even if it isn't that one) can cause a rehash.
Note: in the current implementation, it WILL remain in the same location, even on a rehash. This is because the AA is implemented as an array of pointers to elements. A rehash does not reallocate the elements. But this is NOT guaranteed to stay this way in the future.
Note: reason the spec does not say this is because we don't want to constrain the implementation to behave a certain way.
-Steve
|
Copyright © 1999-2021 by the D Language Foundation