Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 01, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Walter Bright | On Friday, 1 January 2016 at 13:59:35 UTC, Walter Bright wrote:
> http://minas-mina.com/2016/01/01/associative-arrays/
>
> https://www.reddit.com/r/programming/comments/3z03ji/hash_tables_in_the_d_programming_language/
Thanks for sharing this. I am the author. :)
|
January 01, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | On 1/1/2016 7:27 AM, Minas Mina wrote:
> On Friday, 1 January 2016 at 13:59:35 UTC, Walter Bright wrote:
>> http://minas-mina.com/2016/01/01/associative-arrays/
>>
>> https://www.reddit.com/r/programming/comments/3z03ji/hash_tables_in_the_d_programming_language/
>>
>
> Thanks for sharing this. I am the author. :)
You're welcome, and thanks for writing the nice article.
|
January 03, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | On 01/01/2016 04:27 PM, Minas Mina wrote:
> On Friday, 1 January 2016 at 13:59:35 UTC, Walter Bright wrote:
>> http://minas-mina.com/2016/01/01/associative-arrays/
>>
>> https://www.reddit.com/r/programming/comments/3z03ji/hash_tables_in_the_d_programming_language/
>>
>
> Thanks for sharing this. I am the author. :)
There is a bug.
You should never do this b/c of iterator/range invalidation.
foreach (key; aa.keys)
aa.remove(key);
|
January 04, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Nowak | On Sunday, 3 January 2016 at 19:29:05 UTC, Martin Nowak wrote:
> On 01/01/2016 04:27 PM, Minas Mina wrote:
>> On Friday, 1 January 2016 at 13:59:35 UTC, Walter Bright wrote:
>>> http://minas-mina.com/2016/01/01/associative-arrays/
>>>
>>> https://www.reddit.com/r/programming/comments/3z03ji/hash_tables_in_the_d_programming_language/
>>>
>>
>> Thanks for sharing this. I am the author. :)
>
> There is a bug.
>
> You should never do this b/c of iterator/range invalidation.
>
> foreach (key; aa.keys)
> aa.remove(key);
The reference states that keys: "Returns dynamic array, the elements of which are the keys in the associative array".
Isn't the array newly allocated?
|
January 04, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | On Monday, 4 January 2016 at 07:09:30 UTC, Minas Mina wrote: > On Sunday, 3 January 2016 at 19:29:05 UTC, Martin Nowak wrote: >> >> There is a bug. >> >> You should never do this b/c of iterator/range invalidation. >> >> foreach (key; aa.keys) >> aa.remove(key); > > The reference states that keys: "Returns dynamic array, the elements of which are the keys in the associative array". > > Isn't the array newly allocated? Hi, You are right: > void main() > { > import std.stdio; > int[int] aa; > foreach (i; 0..9) > aa[i] = i * 10; > writeln(aa); // [0:0, 6:60, 7:70, 2:20, 3:30, 1:10, 8:80, 5:50, 4:40] > > foreach (key; aa.keys) > aa.remove(key); > writeln(aa); // [] > } This would be a bug (segfault on my machine): > foreach (key; aa.byKey) > aa.remove(key); Note that, in this example, there is no need to remove every element separately, you can also just do > void main() > { > import std.stdio; > int[int] aa; > foreach (i; 0..9) > aa[i] = i * 10; > writeln(aa); // [0:0, 6:60, 7:70, 2:20, 3:30, 1:10, 8:80, 5:50, 4:40] > > aa = null; > writeln(aa); // [] > } Bastiaan. |
January 04, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bastiaan Veelo | On 01/04/2016 09:06 AM, Bastiaan Veelo wrote:
>
> This would be a bug (segfault on my machine):
>
>> foreach (key; aa.byKey)
>> aa.remove(key);
>
> Note that, in this example, there is no need to remove every element separately, you can also just do
Sorry my mistake, I never use aa.keys (instead of aa.byKey) b/c it
allocates.
So it's still sort of a bug to recommend people allocating an array ;).
|
January 05, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin Nowak | On Monday, 4 January 2016 at 19:58:03 UTC, Martin Nowak wrote:
> On 01/04/2016 09:06 AM, Bastiaan Veelo wrote:
>>
>> This would be a bug (segfault on my machine):
>>
>>> foreach (key; aa.byKey)
>>> aa.remove(key);
>>
>> Note that, in this example, there is no need to remove every element separately, you can also just do
>
> Sorry my mistake, I never use aa.keys (instead of aa.byKey) b/c it
> allocates.
> So it's still sort of a bug to recommend people allocating an array ;).
I haven't found a way to clear an AA without allocating though.
(Creating a new one doesn't count).
|
January 05, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Minas Mina | On 2016-01-05 11:19, Minas Mina wrote: > I haven't found a way to clear an AA without allocating though. > (Creating a new one doesn't count). Set it to "null", or will that allocate a new one? -- /Jacob Carlborg |
January 05, 2016 Re: Hash Tables in D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Tuesday, 5 January 2016 at 13:36:48 UTC, Jacob Carlborg wrote:
> On 2016-01-05 11:19, Minas Mina wrote:
>
>> I haven't found a way to clear an AA without allocating though.
>> (Creating a new one doesn't count).
>
> Set it to "null", or will that allocate a new one?
It won't, but to use it again you need to allocate a new one (If I'm not mistaken).
|
Copyright © 1999-2021 by the D Language Foundation