Reputation: 942
So I've been recently using GLib's types, such as lists and maps, but I've encountered a rather troublesome issue.
Starting off, I created my hashtable as such:
BoneIdMap = g_hash_table_new(g_direct_hash, g_str_equal);
I then try it out, insert some uint at a string key, and it works perfectly:
char* string = alloq_calloc(&model->dynamic, strlen(aimesh->mBones[x]->mName.data) + 1, sizeof(char));
strcpy(string, aimesh->mBones[x]->mName.
if(map_find(&model->BoneIdMap, string, &handle)) {
index = *handle;
} else {
index = model->BoneIdMap.size;
}
map_insert(&model->BoneIdMap, &index, sizeof(uint), string);
Take note: that I dynamically allocate the pointer, that's because I tried just passing the static array and it doesn't work (turns out both don't work)
I then proceed to try and retrieve those uints later down the line:
char* string = alloq_calloc(&model->dynamic, strlen(ainode->mName.data) + 1, sizeof(char));
strcpy(string, ainode->mName.data);
/* Time to do some debugging */
if(map_find(&model->BoneIdMap, string, &handle)) {
But It simply doesn't work... I tried retrieving all the keys into an array:
uint len;
char** arr = g_hash_table_get_keys_as_array(model->BoneIdMap.table, &len);
for(int i = 0; i < len; ++i) {
if(arr[i]) printf("Key: %s\n", arr[i]);
if(!strcmp(arr[i], ainode->mName.data)) printf("Yes!\n");
}
printf("----------------------------\n");
And It works!!! (???)
Key: pelvis
Key: toe.L
Yes!
Key: sheath
Key: lamp
----------------------------
Key: toe.R
Key: shin.L
Key: fingerstip.R
Key: neck
Key: thigh.R
Key: fingers.L
Key: shin.R
Key: spine
Key: lamp
Key: head
Key: fingerstip.L
Key: thm_end.L
Key: thm_end.R
Key: tiptoe.L
Yes!
Key: upperarm.R
Take note that if I were to add a key using a static string right there besides the above printing function and try to find it, it would work! Which leaves me quite confused ...
By the way, mName is an aiString (ASSIMP) --
Public Attributes
char data [MAXLEN]
String buffer.
size_t length
Binary length of the string excluding the terminal 0.
Thank you for reading...
Upvotes: 0
Views: 586
Reputation: 564
You are using g_direct_hash
hash function which is meant to be used for gpointer for string keys, try changing it to g_str_hash
.
// g_direct_hash implementation seems to be this
guint
g_direct_hash (gconstpointer v)
{
return GPOINTER_TO_UINT (v);
}
As for why does it work with static string literal, I would say it's compiler optimizing for binary space and collapsing two literals with equal content into a single DATA segment record hence it hashes the same pointer that is why it looks like it works correctly although it does not.
Upvotes: 1