Hash Tables in D

Rory McGuire via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Wed Jan 6 11:52:15 PST 2016


On 06 Jan 2016 3:25 PM, "Minas Mina via Digitalmars-d-announce" <
digitalmars-d-announce at puremagic.com> wrote:
>
> On Wednesday, 6 January 2016 at 12:19:45 UTC, Jacob Carlborg wrote:
>>
>> On 2016-01-05 15:44, Minas Mina wrote:
>>
>>> It won't, but to use it again you need to allocate a new one (If I'm not
>>> mistaken).
>>
>>
>> Not explicitly. I don't know if the runtime allocates a new one. This
works:
>>
>> void main()
>> {
>>     auto foo = ["foo" : 1];
>>     foo = null;
>>     foo["bar"] = 2;
>>     assert(foo["bar"] == 2);
>> }
>
>
> I believe it does, check out this example:
> import std.stdio;
>
> class C
> {
>     int[int] squares;
> }
>
> void main()
> {
>     auto squares = [0 : 0, 1 : 1];
>
>     C c = new C();
>     c.squares = squares;
>
>     writeln(c.squares is squares); // true
>
>     squares = null;
>     squares[10] = 100;
>     writeln(c.squares is squares); // false
> }
>
> If the runtime used the same underlying memory, the second writeln()
would print true, right?
> So if I am correct, a new AA is allocated.
Probably depends on the current implementation. If you are using an
associative array you are going to be allocating at least a little.

If you used an associative array backed by two arrays you could allocate
and reuse memory when null is assigned. It would also be able to keep its
insertion order.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-announce/attachments/20160106/374109ef/attachment.html>


More information about the Digitalmars-d-announce mailing list