Permalink
Browse files

Implement simple interface for creating AA literals.

Uses essentially the same (array of keys, array of values) interface as
the current aaA.d implementation. This may change depending on further
discussion.
  • Loading branch information...
1 parent a2f5e3d commit c272909626f09829f333467499996250c2a1e8d2 @quickfur committed Mar 16, 2012
Showing with 56 additions and 8 deletions.
  1. +12 −1 TODO
  2. +44 −7 newAA.d
View
13 TODO
@@ -1,6 +1,17 @@
-- Key should be implicitly immutable?
+- Introduce toHash methods for all native types (via UFCS). This will solve the
+ stupid purity/nothrow idiocies with the current TypeInfo.getHash.
+ * Note: beware of custom getHash() methods in the pre-built TypeInfo's;
+ the toHash counterparts should return identical values.
+- Implement AA literals:
+ - We may not have compiler support for syntactic sugar ATM, but that
+ shouldn't stop us from implementing an interfacing function that can
+ construct AA's that the compiler can hook into in the future.
+- Implement compile-time instantiation of AA's:
+ - Use CTFE to build the AA at compile-time, then transform it via mixins
+ into static structs.
- Investigate how to make signature constraints work so that compiler magic
with wstring/dstring literals will still work properly.
+- Key should be implicitly immutable?
+ IMPORTANT: .get, .opIndex, and .opBinaryRight!"in" need to accept key types
that implicit convert to Key, not just Key itself.
+ Also, .opIndexAssign needs to be clever enough to NOT copy the input key
View
51 newAA.d
@@ -139,6 +139,17 @@ private:
// 8_589_934_513UL, 17_179_869_143UL
];
+ static size_t findAllocSize(size_t size) pure nothrow @safe
+ {
+ size_t i;
+ for (i=0; i < prime_list.length; i++)
+ {
+ if (size <= prime_list[i])
+ break;
+ }
+ return prime_list[i];
+ }
+
static Slot*[] alloc(size_t len) @trusted
{
auto slots = new Slot*[len];
@@ -166,6 +177,21 @@ private:
}
public:
+ static typeof(this) fromLiteral(Key[] keys, Value[] values) @safe
+ in { assert(keys.length == values.length); }
+ body
+ {
+ typeof(this) aa;
+ aa.impl = new Impl();
+ aa.impl.slots = alloc(findAllocSize(keys.length));
+
+ foreach (i; 0 .. keys.length)
+ {
+ aa[keys[i]] = values[i];
+ }
+ return aa;
+ }
+
hash_t toHash() /*nothrow pure*/ const @trusted
{
// AA hashes must:
@@ -420,13 +446,9 @@ public:
@property typeof(this) rehash() @safe
{
- size_t i;
- for (i=0; i < prime_list.length; i++)
- {
- if (impl.nodes <= prime_list[i])
- break;
- }
- size_t newlen = prime_list[i];
+ if (impl is null) return this;
+
+ size_t newlen = findAllocSize(impl.nodes);
Slot*[] newslots = alloc(newlen);
foreach (slot; impl.slots)
@@ -736,6 +758,21 @@ unittest {
assert(meta.dup == meta);
}
+// Test AA literals API
+unittest {
+ auto aa = AA!(string,int).fromLiteral(
+ ["abc", "def", "ghi"],
+ [ 123, 456, 789 ]
+ );
+
+ AA!(string,int) bb;
+ bb["abc"] = 123;
+ bb["def"] = 456;
+ bb["ghi"] = 789;
+
+ assert(aa==bb);
+}
+
// Issues 7512 & 7704
unittest {
AA!(dstring,int) aa;

0 comments on commit c272909

Please sign in to comment.