digitalmars.D - Clearing associative arrays
- Martin Fuchs (8/8) Sep 18 2007 Is there a direct way to clear the content of associative arrays in one
- Bill Baxter (6/17) Sep 18 2007 "arr = null" or "arr = arr.init" works.
- Martin Fuchs (7/22) Sep 18 2007 Thanks.
-
Stewart Gordon
(6/11)
Sep 18 2007
- Oskar Linde (22/31) Sep 18 2007 If you only want to clear the reference,
Is there a direct way to clear the content of associative arrays in one step? I would expect a statement like "arr.clear();", "arr.erase();" or "arr.removeall();" to do the work, but couldn't find one. Instead it seams I would have to use the following wordy an inefficient approach: foreach(x; arr) arr.remove(x);
Sep 18 2007
Martin Fuchs wrote:Is there a direct way to clear the content of associative arrays in one step? I would expect a statement like "arr.clear();", "arr.erase();" or "arr.removeall();" to do the work, but couldn't find one. Instead it seams I would have to use the following wordy an inefficient approach: foreach(x; arr) arr.remove(x);"arr = null" or "arr = arr.init" works. http://www.prowiki.org/wiki4d/wiki.cgi?action=edit&id=DocComments/Arrays§ion=16 (Check those "Comments" link pages on the doc whenever you don't see something in the doc that you should be there) --bb
Sep 18 2007
Thanks. However your link doesn't work for me - it's using an edit action in the Wiki I would have to login before. How do I register in the Wiki, I could only find a "login" link, but no "register" function? "Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:fcnttu$m99$1 digitalmars.com...Martin Fuchs wrote:Is there a direct way to clear the content of associative arrays in one step? I would expect a statement like "arr.clear();", "arr.erase();" or "arr.removeall();" to do the work, but couldn't find one. Instead it seams I would have to use the following wordy an inefficient approach: foreach(x; arr) arr.remove(x);"arr = null" or "arr = arr.init" works. http://www.prowiki.org/wiki4d/wiki.cgi?action=edit&id=DocComments/Arrays§ion=16 (Check those "Comments" link pages on the doc whenever you don't see something in the doc that you should be there) --bb
Sep 18 2007
"Martin Fuchs" <martin-fuchs gmx.net> wrote in message news:fcnufv$n5i$1 digitalmars.com...Thanks. However your link doesn't work for me - it's using an edit action in the Wiki I would have to login before. How do I register in the Wiki, I could only find a "login" link, but no "register" function?<snip top of upside-down reply> Wiki4D doesn't have user accounts as such - you just have to give yourself a name in the preferences. Stewart.
Sep 18 2007
Martin Fuchs wrote:Is there a direct way to clear the content of associative arrays in one step? I would expect a statement like "arr.clear();", "arr.erase();" or "arr.removeall();" to do the work, but couldn't find one. Instead it seams I would have to use the following wordy an inefficient approach: foreach(x; arr) arr.remove(x);If you only want to clear the reference, arr = null; will do the trick. Otherwise, you need to resort to non-portable 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; } } use: arr.clear(); Warning: hardly tested, but should work with DMD 1.020/Phobos. Regards, -- Oskar
Sep 18 2007