Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 12, 2016 [Issue 16269] add `aa.clear!true` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 Ketmar Dark <ketmar@ketmar.no-ip.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |ketmar@ketmar.no-ip.org -- |
July 12, 2016 [Issue 16269] add `aa.clear!true` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 Steven Schveighoffer <schveiguy@yahoo.com> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |schveiguy@yahoo.com --- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> --- Thanks. I don't love the API. aa.clear!true is not "clear" (pun not intended) on what it is doing as opposed to aa.clear. I like the idea though -- have a function that clears, and if it's not allocated, do that too. Why not use your internal name: clearInit? -- |
July 12, 2016 [Issue 16269] add `aa.clear!true` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #2 from Ketmar Dark <ketmar@ketmar.no-ip.org> --- i just thought that `.clear!true` is better. i don't know why: probably 'cause i just love D templates and want to use 'em everywhere. ;-) still, it's a matter of simple renaming, i don't have real API preferences here. something like `.clear!(init:true)` may be better, but D doesn't support that. T_T -- |
July 12, 2016 [Issue 16269] add `aa.clear!true` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #3 from Ketmar Dark <ketmar@ketmar.no-ip.org> --- p.s. i created this ER in the hope that people will talk about API too: i'm usually sux at inventig APIs. -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 Ketmar Dark <ketmar@ketmar.no-ip.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|add `aa.clear!true` method |add `aa.ensureAllocated` |to associative array to |method to associative array |clear and initialize it |to clear and initialize it -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #4 from Ketmar Dark <ketmar@ketmar.no-ip.org> --- another try. this time it adds `ensureAllocated()` method, which can be used like this: ====================================== void test (string[int] aa) { aa[42] = "42"; } void main () { // inline { string[int] aa; test(aa.ensureAllocated); assert(aa[42] == "42"); // check that AA is not cleared aa.ensureAllocated(); assert(aa[42] == "42"); } // function { string[int] bb; bb.ensureAllocated; test(bb); assert(bb[42] == "42"); // check that AA is not cleared bb.ensureAllocated(); assert(bb[42] == "42"); } } ====================================== diff --git a/src/object.d b/src/object.d index 40e2391..0d82f07 100644 --- a/src/object.d +++ b/src/object.d @@ -1876,6 +1876,7 @@ extern (C) inout(void)[] _aaKeys(inout void* p, in size_t keysize, const TypeInfo tiKeyArray) pure nothrow; void* _aaRehash(void** pp, in TypeInfo keyti) pure nothrow; void _aaClear(void* p) pure nothrow; + void _aaEnsureAllocated(void* p, const TypeInfo_AssociativeArray ti); // alias _dg_t = extern(D) int delegate(void*); // int _aaApply(void* aa, size_t keysize, _dg_t dg); @@ -1919,6 +1920,18 @@ void clear(T : Value[Key], Value, Key)(T* aa) _aaClear(*cast(void **) aa); } +T ensureAllocated(T : Value[Key], Value, Key)(ref T aa) +{ + _aaEnsureAllocated(cast(void*)&aa, typeid(T)); + return aa; +} + +T ensureAllocated(T : Value[Key], Value, Key)(T* aa) +{ + _aaEnsureAllocated(cast(void*)aa, typeid(T)); + return *aa; +} + T rehash(T : Value[Key], Value, Key)(T aa) { _aaRehash(cast(void**)&aa, typeid(Value[Key])); diff --git a/src/rt/aaA.d b/src/rt/aaA.d index cf8943e..b16b6d8 100644 --- a/src/rt/aaA.d +++ b/src/rt/aaA.d @@ -443,6 +443,16 @@ extern (C) void _aaClear(AA aa) pure nothrow } } +/// Remove all elements from AA, allocate new AA if it isn't allocated yet. +extern (C) void _aaEnsureAllocated(AA* paa, const TypeInfo_AssociativeArray ti) +{ + if (paa !is null && paa.impl is null) + { + // alloc implementation + paa.impl = new Impl(ti); + } +} + /// Rehash AA extern (C) void* _aaRehash(AA* paa, in TypeInfo keyti) pure nothrow { -- 2.9.0 -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #5 from Ketmar Dark <ketmar@ketmar.no-ip.org> --- oops. forgot to fix the comment. sorry. ;-) -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #6 from Steven Schveighoffer <schveiguy@yahoo.com> --- Thanks, this looks really good. I like the returning of itself, so you just put ensureAllocated on the call and it just works in all cases where you normally have an AA. When I get a chance, I'll make a PR. Thanks for the tests too! -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #7 from Steven Schveighoffer <schveiguy@yahoo.com> --- Just adding a note for myself when I make the PR, the AA is and always will be an implementation pointer as the only member. I will check for null pointer in wrapper rather than the runtime code to avoid extra opaque calls. -- |
July 13, 2016 [Issue 16269] add `aa.ensureAllocated` method to associative array to clear and initialize it | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=16269 --- Comment #8 from Ketmar Dark <ketmar@ketmar.no-ip.org> --- >I get a chance, I'll make a PR thank you. >I will check for null pointer in wrapper rather than the runtime code to avoid extra opaque calls. yes, this is way better, tnx again. i planned to do exactly that, but somehow put it in the wrong place. ;-) -- |
Copyright © 1999-2021 by the D Language Foundation