digitalmars.D - How to remove all items in an associative array?
- Yang Bo (5/5) Oct 18 2007 http://www.digitalmars.com/d/statement.html said:
- Bill Baxter (4/11) Oct 18 2007 I don't understand the body, but the answer to the question in the
- Oskar Linde (27/39) Oct 18 2007 Since quite a while ago, the D associative arrays have become pure
- Bill Baxter (3/47) Oct 18 2007 Interesting tidbit. Thanks for the info and for updating that page.
- Gregor Richards (9/56) Oct 18 2007 Unless the performance is a huge hit, it would probably be wiser to use
- Matti Niemenmaa (4/10) Oct 18 2007 I believe aa.keys is a duplicate already, meaning there is no need for t...
- Oskar Linde (12/21) Oct 19 2007 You are both right. I updated the DocComments page.
http://www.digitalmars.com/d/statement.html said: The aggregate must be loop invariant, meaning that elements to the aggregate cannot be added or removed from it in the NoScopeNonEmptyStatement. So, how can I remove them?
Oct 18 2007
Yang Bo wrote:http://www.digitalmars.com/d/statement.html said: The aggregate must be loop invariant, meaning that elements to the aggregate cannot be added or removed from it in the NoScopeNonEmptyStatement. So, how can I remove them?I don't understand the body, but the answer to the question in the subject line is AA = null; or AA = AA.init; http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/Arrays
Oct 18 2007
Bill Baxter wrote:Yang Bo wrote:Since quite a while ago, the D associative arrays have become pure reference types. While the above has the apparent effect of clearing the array, it does in fact just reassign the current reference. If you have several references to the same associative array, the above will not clear the actual array referred to. To clear the actual array, you need to resort to hackery. Something like this should definitely be included in the compiler/runtime. private struct BB { void*[]b; size_t nodes; } private union ToPtr(T) {T x; void * ptr; } void clear(T,E)(T[E] aa) { ToPtr!(typeof(aa)) toptr; toptr.x = aa; BB* b = cast(BB*) toptr.ptr; if (b) { b.b = null; b.nodes = 0; } }http://www.digitalmars.com/d/statement.html said: The aggregate must be loop invariant, meaning that elements to the aggregate cannot be added or removed from it in the NoScopeNonEmptyStatement. So, how can I remove them?I don't understand the body, but the answer to the question in the subject line is AA = null; or AA = AA.init;http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/ArraysI rewrote that section in a way that hopefully explains the semantics better. This question comes up frequently enough to merit an addition to the official documentation and a clear/deleteAll implementation (like the one above) should definitely be added to the language and phobos/internal/aaA.d. -- Oskar
Oct 18 2007
Oskar Linde wrote:Bill Baxter wrote:Interesting tidbit. Thanks for the info and for updating that page. --bbYang Bo wrote:Since quite a while ago, the D associative arrays have become pure reference types. While the above has the apparent effect of clearing the array, it does in fact just reassign the current reference. If you have several references to the same associative array, the above will not clear the actual array referred to. To clear the actual array, you need to resort to hackery. Something like this should definitely be included in the compiler/runtime. private struct BB { void*[]b; size_t nodes; } private union ToPtr(T) {T x; void * ptr; } void clear(T,E)(T[E] aa) { ToPtr!(typeof(aa)) toptr; toptr.x = aa; BB* b = cast(BB*) toptr.ptr; if (b) { b.b = null; b.nodes = 0; } }http://www.digitalmars.com/d/statement.html said: The aggregate must be loop invariant, meaning that elements to the aggregate cannot be added or removed from it in the NoScopeNonEmptyStatement. So, how can I remove them?I don't understand the body, but the answer to the question in the subject line is AA = null; or AA = AA.init;http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/ArraysI rewrote that section in a way that hopefully explains the semantics better. This question comes up frequently enough to merit an addition to the official documentation and a clear/deleteAll implementation (like the one above) should definitely be added to the language and phobos/internal/aaA.d.
Oct 18 2007
Bill Baxter wrote:Oskar Linde wrote:Unless the performance is a huge hit, it would probably be wiser to use code that isn't dependent on the current implementation of AAs: foreach (k; aa.keys.dup) { aa.remove(k); } I think that's in O(nlogn). The other code is free, but implementation-specific. Ideally there would just be a .clear property :) - Gregor RichardsBill Baxter wrote:Interesting tidbit. Thanks for the info and for updating that page. --bbYang Bo wrote:Since quite a while ago, the D associative arrays have become pure reference types. While the above has the apparent effect of clearing the array, it does in fact just reassign the current reference. If you have several references to the same associative array, the above will not clear the actual array referred to. To clear the actual array, you need to resort to hackery. Something like this should definitely be included in the compiler/runtime. private struct BB { void*[]b; size_t nodes; } private union ToPtr(T) {T x; void * ptr; } void clear(T,E)(T[E] aa) { ToPtr!(typeof(aa)) toptr; toptr.x = aa; BB* b = cast(BB*) toptr.ptr; if (b) { b.b = null; b.nodes = 0; } }http://www.digitalmars.com/d/statement.html said: The aggregate must be loop invariant, meaning that elements to the aggregate cannot be added or removed from it in the NoScopeNonEmptyStatement. So, how can I remove them?I don't understand the body, but the answer to the question in the subject line is AA = null; or AA = AA.init;http://www.prowiki.org/wiki4d/wiki.cgi?DocComments/ArraysI rewrote that section in a way that hopefully explains the semantics better. This question comes up frequently enough to merit an addition to the official documentation and a clear/deleteAll implementation (like the one above) should definitely be added to the language and phobos/internal/aaA.d.
Oct 18 2007
Gregor Richards wrote:Unless the performance is a huge hit, it would probably be wiser to use code that isn't dependent on the current implementation of AAs: foreach (k; aa.keys.dup) { aa.remove(k); }I believe aa.keys is a duplicate already, meaning there is no need for the .dup. -- E-mail address: matti.niemenmaa+news, domain is iki (DOT) fi
Oct 18 2007
Matti Niemenmaa wrote:Gregor Richards wrote:You are both right. I updated the DocComments page. Actually, the proper way to efficiently clear an aa might be to explicitly clear or delete the referred aaA*[] array. Leaving the GC a potentially quite large array full of pointers to dead objects might be a source of memory leaks in an imprecise GC implementation. The same potential memory leak exists in _aaRehash() today. Deleting, clearing or even mark-as-not-containing-pointers the old array when rehashing/resizing the AA should, I believe, improve the situation for the GC. -- OskarUnless the performance is a huge hit, it would probably be wiser to use code that isn't dependent on the current implementation of AAs: foreach (k; aa.keys.dup) { aa.remove(k); }I believe aa.keys is a duplicate already, meaning there is no need for the .dup.
Oct 19 2007