所以我最近使用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