Created
July 19, 2012 09:05
-
-
Save hadashiA/3142158 to your computer and use it in GitHub Desktop.
uthashのつかいかた(ポインタをキーに)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include "uthash.h" | |
typedef enum { | |
kJigokuTileFlagNone = 0, | |
kJigokuTileFlagCollision = (1 << 0), | |
kJigokuTileFlagCharacter = (1 << 1), | |
kJigokuTileFlagItem = (1 << 2), | |
kJigokuTileFlagPlayer = (1 << 3), | |
} JigokuTileFlags; | |
typedef unsigned long int Entity; | |
typedef struct _jigoku_tile { | |
JigokuTileFlags flags; | |
Entity *entity; | |
UT_hash_handle hh; | |
} jigoku_tile; | |
jigoku_tile *entity_table = NULL; | |
void jigoku_tile_print(jigoku_tile *tile_p) { | |
if (tile_p) { | |
Entity *e = tile_p->entity; | |
printf("%p e:%ld\n", tile_p, *e); | |
} else { | |
printf("%p\n", tile_p); | |
} | |
} | |
void print_table() { | |
printf("---------------- \n"); | |
jigoku_tile *current; | |
for (current = entity_table; current != NULL; current = current->hh.next) { | |
jigoku_tile_print(current); | |
} | |
printf("---------------- \n\n"); | |
} | |
int main(int argc, char **argv) { | |
jigoku_tile *tile_p = (jigoku_tile *)malloc(sizeof(jigoku_tile) * 3); | |
Entity *e = (Entity *)malloc(sizeof(Entity) * 3); | |
int i; | |
for (i = 0; i < 3; ++i) { | |
*(e+i) = (i+1) * 11111111111111; | |
tile_p[i].entity = e+i; | |
printf("tile_p[%d]:", i); | |
jigoku_tile_print(&(tile_p[i])); | |
} | |
HASH_ADD_PTR(entity_table, entity, tile_p); | |
HASH_ADD_PTR(entity_table, entity, (tile_p+1)); | |
print_table(); | |
jigoku_tile *result; | |
HASH_FIND_PTR(entity_table, &(tile_p[2].entity), result); | |
jigoku_tile_print(result); | |
/* 存在してるキーを追加しようとすると、なんかおかしくなるので、事前に確認必要 */ | |
/* 値の更新は、削除・追加を連続でするしかないみたいなので、 */ | |
/* そういう使い方をするよりはたぶん、値自体のメンバを書き換えていく方針のほうが良さそう */ | |
/* HASH_ADD_PTR(entity_table, entity, tile_p); */ | |
/* 存在しない値を削除するとなんかおかしくなるので、事前に確認必要 */ | |
/* HASH_DEL(entity_table, tile_p+2); */ | |
/* print_table(); */ | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment