所以我最近使用GLib的類型,如列表和地圖,但我遇到了一個相當麻煩的問題。GLib:哈希表沒有正確找到值
出發,我創建了哈希表,例如:
BoneIdMap = g_hash_table_new(g_direct_hash, g_str_equal);
我再嘗試一下,在一個字符串鍵插入一些uint和它完美的作品:
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);
請注意:我動態地分配指針,這是因爲我試圖傳遞靜態數組,它不起作用(原來兩者都不起作用)
然後我繼續嘗試並檢索那些提示WN行:
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)) {
但它根本沒有工作...我試着檢索所有鍵到一個數組:
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");
和它的作品! (???)
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
注意到,如果我是使用靜態字符串添加一個關鍵就在那裏除了上述的打印功能,並試圖找到它,它的工作! 這讓我很困惑...
順便說一句,MNAME是aiString(ASSIMP) -
Public Attributes
char data [MAXLEN]
String buffer.
size_t length
Binary length of the string excluding the terminal 0.
感謝您閱讀...
請分享您的'map_insert'和'map_find'代碼,因爲我假設它們都是GLib散列表函數的包裝函數。 – tgregory