newaa ~master
Associative Array library that mirrors druntime, but is fully templated
To use this package, run the following command in your project's root directory:
Manual usage
Put the following dependency into your project's dependences section:
Library version of druntime AA
This library is a template-library version of the AA implementation used by the d programming language. It is binary compatbile, so reinterpret-casting a Hash!(K, V)
to/from a V[K]
is valid.
If any changes are made to the druntime AA implementation, this library may stop being binary compatible (or will be tied to specific versions). The latest D version tested with this library is 2.099.1
Differences
- Special mechanisms are not possible with library code. For instance, the outer hash of nested hashes knowing that it is being used for an assignment operation.
- For now, only
opIndexAssign
is defined for adding a value to aHash
. This means things likehash[unassignedKey]++
will not work. - Not all "methods" of AAs are implemented yet, e.g.
require
. These will be added. - You can build a
Hash!(K, V)
at compile time, and at runtime use it as aV[K]
via theasAA
UFCS method.
Compatibility/conversion to language AA
The code is built to use the same exact layout and hash implementation as the builtin language AA. There is a difference in that Hash
uses compile-time information about the types to compute hashes, and uses the language to properly manage lifetimes. The druntime AA implementation is all based on TypeInfo
, and therefore must handle all these mechanisms manually.
In particular, the AA builds a fake TypeInfo
for the Entry
struct that contains a key and value (a thing that has no formal type). Because it is using compile-time introspection, a Hash
does not use the TypeInfo
for this information, and therefore doesn't need it.
However, when converting to an AA, it ensures the TypeInfo
is properly populated, but instead of using a fake TypeInfo
, the actual TypeInfo
for the Entry
struct is used. This should be binary compatible with the AA, because the AA's fake is attempting to construct a fake TypeInfo
that would mimic what a real Entry
struct's TypeInfo
would be.
Use the asAA
UFCS method to extract an associative array from a given hash.
You can just assign a Hash
from a compatible AA. By compatible, I mean that the AA's keys convert to the Hash
's keys, and the AA's values convert to the Hash
's values. If the keys and values are the same type, the Hash
will use the AA's internal representation directly.
Tests
Tests are sparse at this time. Just enough to ensure the binary compatibility is working, and that the hash works as expected. I plan to add more.
Future plans
- Try and ensure all methods/properties are identical to builtin AAs
- Improve tests
- flesh out
@safe
and other attributes, make sure they are inferred correctly. - Investigate ways to prove the layout/code of the builtin AA has not changed (even if done via CI).
- ~master released a year ago
- schveiguy/newaa
- Boost-1.0
- Copyright © 2022, Steven Schveighoffer, Digital Mars
- Authors:
- Dependencies:
- none
- Versions:
-
0.0.1 2022-May-11 ~master 2023-Aug-09 - Download Stats:
-
-
0 downloads today
-
0 downloads this week
-
0 downloads this month
-
24 downloads total
-
- Score:
- 0.9
- Short URL:
- newaa.dub.pm