[Issue 16269] New: add `aa.clear!true` method to associative array to clear and initialize it
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Tue Jul 12 06:00:05 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16269
Issue ID: 16269
Summary: add `aa.clear!true` method to associative array to
clear and initialize it
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: enhancement
Priority: P1
Component: druntime
Assignee: nobody at puremagic.com
Reporter: ketmar at ketmar.no-ip.org
q&d patch:
diff --git a/src/object.d b/src/object.d
index 40e2391..f2f29de 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 _aaClearInit(void* p, const TypeInfo_AssociativeArray ti) pure
nothrow;
// alias _dg_t = extern(D) int delegate(void*);
// int _aaApply(void* aa, size_t keysize, _dg_t dg);
@@ -1914,9 +1915,20 @@ void clear(T : Value[Key], Value, Key)(T aa)
_aaClear(*cast(void **) &aa);
}
-void clear(T : Value[Key], Value, Key)(T* aa)
+void clear(bool doalloc:true, T : Value[Key], Value, Key)(ref T aa)
{
- _aaClear(*cast(void **) aa);
+ static if (!doalloc)
+ _aaClear(*cast(void **) &aa);
+ else
+ _aaClearInit(cast(void **) &aa, typeid(T));
+}
+
+void clear(bool doalloc=false, T : Value[Key], Value, Key)(T* aa)
+{
+ static if (!doalloc)
+ _aaClear(*cast(void **) aa);
+ else
+ _aaClearInit(*cast(void **) &aa, typeid(T));
}
T rehash(T : Value[Key], Value, Key)(T aa)
diff --git a/src/rt/aaA.d b/src/rt/aaA.d
index cf8943e..2e5a3a1 100644
--- a/src/rt/aaA.d
+++ b/src/rt/aaA.d
@@ -443,6 +443,20 @@ 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 _aaClearInit(AA* aa, const TypeInfo_AssociativeArray ti)
+{
+ if (!aa.empty)
+ {
+ aa.impl.clear();
+ }
+ else if (aa.impl is null)
+ {
+ // alloc implementation
+ aa.impl = new Impl(ti);
+ }
+}
+
/// Rehash AA
extern (C) void* _aaRehash(AA* paa, in TypeInfo keyti) pure nothrow
{
--
2.9.0
--
More information about the Digitalmars-d-bugs
mailing list