[map] overhaul the maps, fewer parts, fewer paths, fewer knobs; same great results

This commit is contained in:
Allen Webster
2021-06-06 20:15:48 -07:00
parent d390326e17
commit fb999476fa
9 changed files with 274 additions and 335 deletions
+20 -37
View File
@@ -500,53 +500,36 @@ int main(void)
Test("Hash maps")
{
MD_String8 keys[] =
MD_String8 key_strings[] =
{
MD_S8Lit("\xed\x80\x73\x71\x78\xba\xff\xd6\x87\x83\xcd\x20\x28\xf7\x1c\xc1\x5f\xca\x98\x9c\x5a\xab\x0c\xae\x9a\x60\x57\x03\xeb\x1f\xde\x99"),
MD_S8Lit("\x4c\x80\xb7\x8b\xbf\x65\x5a\x4b\xc1\x2a\xc3\x5f\xe1\x66\xfb\x0d\x72\x83\x1c\x63\xba\xb5\x97\x02\x3f\x6a\xe0\x2a\x1b\x82\x07\x76"),
MD_S8Lit("\xd8\xfd\x11\x4b\x04\xdf\xe5\x20\x5b\xd6\x4f\x87\x00\x70\x6a\xc8\xde\xed\xc7\x79\xdb\x87\x24\x36\xa8\x7a\x31\x41\x00\x57\xbd\x8d"),
};
// NOTE(mal): True for MD_HashString is djb2
{
TestResult(MD_HashString(keys[0]) == 1 && MD_HashString(keys[1]) == 1 && MD_HashString(keys[2]) == 3);
MD_MapKey keys[MD_ArrayCount(key_strings)*2];
for (MD_u64 i = 0; i < MD_ArrayCount(key_strings); i += 1){
keys[i] = MD_MapKeyStr(key_strings[i]);
}
for (MD_u64 i = MD_ArrayCount(key_strings); i < MD_ArrayCount(keys); i += 1){
keys[i] = MD_MapKeyPtr((void *)i);
}
{
MD_MapCollisionRule rules[] = { MD_MapCollisionRule_Chain, MD_MapCollisionRule_Overwrite };
for(int i_rule = 0; i_rule < MD_ArrayCount(rules); ++i_rule)
{
MD_Map map = {0};
MD_StringMap_Insert(&map, rules[i_rule], keys[0], (void *)0);
MD_StringMap_Insert(&map, rules[i_rule], keys[1], (void *)1);
MD_StringMap_Insert(&map, rules[i_rule], keys[2], (void *)2);
MD_MapSlot *slot0 = MD_StringMap_Lookup(&map, keys[0]);
MD_MapSlot *slot1 = MD_StringMap_Lookup(&map, keys[1]);
MD_MapSlot *slot2 = MD_StringMap_Lookup(&map, keys[2]);
TestResult(slot0 && slot0->value == (void *)0 &&
slot1 && slot1->value == (void *)1 &&
slot2 && slot2->value == (void *)2);
MD_Map map = MD_MapMake();
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapInsert(&map, keys[i], (void *)i);
}
}
{
MD_MapCollisionRule rules[] = { MD_MapCollisionRule_Chain, MD_MapCollisionRule_Overwrite };
for(int i_rule = 0; i_rule < MD_ArrayCount(rules); ++i_rule)
{
MD_Map map = {0};
MD_PtrMap_Insert(&map, rules[i_rule], (void *)0, (void *)0);
MD_PtrMap_Insert(&map, rules[i_rule], (void *)1, (void *)1);
MD_PtrMap_Insert(&map, rules[i_rule], (void *)2, (void *)2);
MD_MapSlot *slot0 = MD_PtrMap_Lookup(&map, (void *)0);
MD_MapSlot *slot1 = MD_PtrMap_Lookup(&map, (void *)1);
MD_MapSlot *slot2 = MD_PtrMap_Lookup(&map, (void *)2);
TestResult(slot0 && slot0->value == (void *) 0 &&
slot1 && slot1->value == (void *) 1 &&
slot2 && slot2->value == (void *) 2);
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapSlot *slot = MD_MapLookup(&map, keys[i]);
TestResult(slot && slot->val == (void *)i);
}
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapOverwrite(&map, keys[i], (void *)(i + 10));
}
for (MD_u64 i = 0; i < MD_ArrayCount(keys); i += 1){
MD_MapSlot *slot = MD_MapLookup(&map, keys[i]);
TestResult(slot && slot->val == (void *)(i + 10));
}
}
}