| 1 |
/** |
| 2 |
Turn an Associative Array into a binary compatible struct for static initialization. |
| 3 |
|
| 4 |
This does not implement all the pieces of |
| 5 |
the associative array type in druntime, just enough to create an AA from an |
| 6 |
existing range of key/value pairs. |
| 7 |
|
| 8 |
Copyright: Copyright Digital Mars 2000 - 2015, Steven Schveighoffer 2022. |
| 9 |
License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 10 |
Authors: Martin Nowak, Steven Schveighoffer |
| 11 |
*/ |
| 12 |
module core.internal.newaa; |
| 13 |
|
| 14 |
import core.memory; |
| 15 |
|
| 16 |
// grow threshold |
| 17 |
private enum GROW_NUM = 4; |
| 18 |
private enum GROW_DEN = 5; |
| 19 |
// shrink threshold |
| 20 |
private enum SHRINK_NUM = 1; |
| 21 |
private enum SHRINK_DEN = 8; |
| 22 |
// grow factor |
| 23 |
private enum GROW_FAC = 4; |
| 24 |
// growing the AA doubles it's size, so the shrink threshold must be |
| 25 |
// smaller than half the grow threshold to have a hysteresis |
| 26 |
static assert(GROW_FAC * SHRINK_NUM * GROW_DEN < GROW_NUM * SHRINK_DEN); |
| 27 |
// initial load factor (for literals), mean of both thresholds |
| 28 |
private enum INIT_NUM = (GROW_DEN * SHRINK_NUM + GROW_NUM * SHRINK_DEN) / 2; |
| 29 |
private enum INIT_DEN = SHRINK_DEN * GROW_DEN; |
| 30 |
|
| 31 |
private enum INIT_NUM_BUCKETS = 8; |
| 32 |
// magic hash constants to distinguish empty, deleted, and filled buckets |
| 33 |
private enum HASH_EMPTY = 0; |
| 34 |
private enum HASH_FILLED_MARK = size_t(1) << 8 * size_t.sizeof - 1; |
| 35 |
|
| 36 |
private struct Bucket |
| 37 |
{ |
| 38 |
size_t hash; |
| 39 |
void *entry; |
| 40 |
} |
| 41 |
|
| 42 |
struct Impl |
| 43 |
{ |
| 44 |
Bucket[] buckets; |
| 45 |
uint used; |
| 46 |
uint deleted; |
| 47 |
const(TypeInfo) entryTI; |
| 48 |
uint firstUsed; |
| 49 |
immutable uint keysz; |
| 50 |
immutable uint valsz; |
| 51 |
immutable uint valoff; |
| 52 |
Flags flags; |
| 53 |
size_t delegate(scope const void*) nothrow hashFn; |
| 54 |
|
| 55 |
enum Flags : ubyte |
| 56 |
{ |
| 57 |
none = 0x0, |
| 58 |
keyHasPostblit = 0x1, |
| 59 |
hasPointers = 0x2, |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
private struct AAShell |
| 64 |
{ |
| 65 |
Impl *impl; |
| 66 |
} |
| 67 |
|
| 68 |
private size_t mix(size_t h) @safe pure nothrow @nogc |
| 69 |
{ |
| 70 |
// final mix function of MurmurHash2 |
| 71 |
enum m = 0x5bd1e995; |
| 72 |
h ^= h >> 13; |
| 73 |
h *= m; |
| 74 |
h ^= h >> 15; |
| 75 |
return h; |
| 76 |
} |
| 77 |
|
| 78 |
// create a binary-compatible AA structure that can be used directly as an |
| 79 |
// associative array. |
| 80 |
// NOTE: this must only be called during CTFE |
| 81 |
AAShell makeAA(K, V)(V[K] src) @trusted |
| 82 |
{ |
| 83 |
assert(__ctfe, "makeAA Must only be called at compile time"); |
| 84 |
immutable srclen = src.length; |
| 85 |
assert(srclen <= uint.max); |
| 86 |
alias E = TypeInfo_AssociativeArray.Entry!(K, V); |
| 87 |
if (srclen == 0) |
| 88 |
return AAShell.init; |
| 89 |
// first, determine the size that would be used if we grew the bucket list |
| 90 |
// one element at a time using the standard AA algorithm. |
| 91 |
size_t dim = INIT_NUM_BUCKETS; |
| 92 |
while (srclen * GROW_DEN > dim * GROW_NUM) |
| 93 |
dim = dim * GROW_FAC; |
| 94 |
|
| 95 |
// used during runtime. |
| 96 |
typeof(Impl.hashFn) hashFn = (scope const void* val) { |
| 97 |
auto x = cast(K*)val; |
| 98 |
return hashOf(*x); |
| 99 |
}; |
| 100 |
|
| 101 |
Bucket[] buckets; |
| 102 |
// Allocate and fill the buckets |
| 103 |
if (__ctfe) |
| 104 |
buckets = new Bucket[dim]; |
| 105 |
else |
| 106 |
assert(0); |
| 107 |
|
| 108 |
assert(buckets.length >= dim); |
| 109 |
|
| 110 |
immutable mask = dim - 1; |
| 111 |
assert((dim & mask) == 0); // must be a power of 2 |
| 112 |
|
| 113 |
Bucket* findSlotInsert(immutable size_t hash) |
| 114 |
{ |
| 115 |
for (size_t i = hash & mask, j = 1;; ++j) |
| 116 |
{ |
| 117 |
if (buckets[i].hash == HASH_EMPTY) |
| 118 |
return &buckets[i]; |
| 119 |
i = (i + j) & mask; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
uint firstUsed = cast(uint) buckets.length; |
| 124 |
foreach (k, v; src) |
| 125 |
{ |
| 126 |
immutable h = hashOf(k).mix | HASH_FILLED_MARK; |
| 127 |
auto location = findSlotInsert(h); |
| 128 |
immutable nfu = cast(uint) (location - &buckets[0]); |
| 129 |
if (nfu < firstUsed) |
| 130 |
firstUsed = nfu; |
| 131 |
*location = Bucket(h, new E(k, v)); |
| 132 |
} |
| 133 |
|
| 134 |
enum flags = () { |
| 135 |
import core.internal.traits; |
| 136 |
Impl.Flags flags; |
| 137 |
static if (__traits(hasPostblit, K)) |
| 138 |
flags |= flags.keyHasPostblit; |
| 139 |
static if (hasIndirections!E) |
| 140 |
flags |= flags.hasPointers; |
| 141 |
return flags; |
| 142 |
} (); |
| 143 |
// return the new implementation |
| 144 |
return AAShell(new Impl(buckets, cast(uint)srclen, 0, typeid(E), firstUsed, |
| 145 |
K.sizeof, V.sizeof, E.value.offsetof, flags, hashFn)); |
| 146 |
} |
| 147 |
|
| 148 |
unittest |
| 149 |
{ |
| 150 |
static struct Foo |
| 151 |
{ |
| 152 |
ubyte x; |
| 153 |
double d; |
| 154 |
} |
| 155 |
static int[Foo] utaa = [Foo(1, 2.0) : 5]; |
| 156 |
auto k = Foo(1, 2.0); |
| 157 |
// verify that getHash doesn't match hashOf for Foo |
| 158 |
assert(typeid(Foo).getHash(&k) != hashOf(k)); |
| 159 |
assert(utaa[Foo(1, 2.0)] == 5); |
| 160 |
} |