Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 01, 2008 How to clear associative array | ||||
---|---|---|---|---|
| ||||
As in subject. Type [KeyType] array; array[new KeyType(...)] = new Type(...); ... delete array; // error |
August 01, 2008 Re: How to clear associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zarathustra | "Zarathustra"
> As in subject.
>
> Type [KeyType] array;
>
> array[new KeyType(...)] = new Type(...);
> ...
>
> delete array; // error
array = null;
-Steve
|
August 01, 2008 Re: How to clear associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Zarathustra | Reply to Zarathustra, > As in subject. > > Type [KeyType] array; > > array[new KeyType(...)] = new Type(...); > ... > delete array; // error > I'm not sure how array=null will interact with other references to the same AA. This should clear them as well (but is more costly). foreach(k;array.keys)array.delete(k); |
August 01, 2008 Re: How to clear associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to BCS | "BCS" wrote
> Reply to Zarathustra,
>
>> As in subject.
>>
>> Type [KeyType] array;
>>
>> array[new KeyType(...)] = new Type(...);
>> ...
>> delete array; // error
>>
>
> I'm not sure how array=null will interact with other references to the same AA.
AA's are objects, so multiple references to the same AA would result in the data being retained for the references that you did not set to null.
It would be nice to have a .clear function so you could deal with removing all elements when multiple references are used.
-Steve
|
August 01, 2008 Re: How to clear associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Reply to Steven,
> "BCS" wrote
>
>> Reply to Zarathustra,
>>
>>> As in subject.
>>>
>>> Type [KeyType] array;
>>>
>>> array[new KeyType(...)] = new Type(...);
>>> ...
>>> delete array; // error
>> I'm not sure how array=null will interact with other references to
>> the same AA.
>>
> AA's are objects, so multiple references to the same AA would result
> in the data being retained for the references that you did not set to
> null.
>
> It would be nice to have a .clear function so you could deal with
> removing all elements when multiple references are used.
>
> -Steve
>
void clear(T)(T toClear) {foreach(key; toClear.keys) toClear.remove(key); }
?? O(n) (vs. O(1) for an internal version)
|
August 01, 2008 Re: How to clear associative array | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Fri, 1 Aug 2008 15:47:35 -0400, "Steven Schveighoffer" <schveiguy@yahoo.com> wrote:
>"BCS" wrote
>> Reply to Zarathustra,
>>
>>> As in subject.
>>>
>>> Type [KeyType] array;
>>>
>>> array[new KeyType(...)] = new Type(...);
>>> ...
>>> delete array; // error
>>>
>>
>> I'm not sure how array=null will interact with other references to the same AA.
>
>AA's are objects, so multiple references to the same AA would result in the data being retained for the references that you did not set to null.
>
>It would be nice to have a .clear function so you could deal with removing all elements when multiple references are used.
>
>-Steve
>
It would be also nice to be able to initialize an AA reference without adding a value to the array.
int[int] a;
int[int] b = a;
a[1] = 2;
auto x = b[1]; // oops
|
Copyright © 1999-2021 by the D Language Foundation