hcreate_rãªã©ã使ã£ã¦ã¿ã
ç¾å¨ä½æä¸ã®ããã°ã©ã ã§ããã·ã¥è¡¨ã使ããããªã¼ã¨æã£ã¦ãããPOSIX, GNUæ¡å¼µã«åå¨ããããããã¨ãããã¨ã§èª¿ã¹ã¦ã¿ã¾ããã
ãã¡ããåºæ¬ã¯man hcreateãèªããã¨ã§ããã
ä»åã¯è¤æ°ã®ããã·ã¥è¡¨ã使ãå¿
è¦ãããã®ã§ãGNUæ¡å¼µã®hcreate_rãªã©ã®reentrantçã使ãå¿
è¦ãããã¾ãã
int hcreate_r(size_t nel, struct hsearch_data *tab);3 ã¤ã®é¢æ° hcreate_r(), hsearch_r(), hdestroy_r() ã¯ãªã¨ã³ãã©ã³ããªé¢æ°ã§ã2 ã¤ä»¥ä¸ã®ãã¼ãã«ã使ç¨ãããã¨ãã§ãããæå¾ã®å¼ãæ°ã¯ãã¼ãã«ãèå¥ããã®ã«ä½¿ãããããããæã示ãæ§é ä½ã¯ãåã㦠hcreate_r() ãå¼ã³åºãåã« 0 ã«ãã¦ãããªããã°ãªããªãã
ã¨ããããã®reentrantçã®ç¨ä¾ã¯man pageã«ã¯è¼ã£ã¦ãã¾ãããå®ã¯ä½¿ãæ¹ããããããã¾ããã§ããã
æåã¯
struct hsearch_data* tab;
ãªã©ãæçµå¼æ°ã«ä»ãã¦ãã¦ãhcreate_rã®æç¹ã§è¦äºã«å¤±æãã¾ããã
次ã«
struct hsearch_data tab;
ã§æçµå¼æ°ã¯&tabã«ãã¾ãããã¨ãããããã§ãhcreate_rã0ãè¿ãã¦ãã¾ããï¼å¤±æã示ãã¦ãã¾ãï¼ã
manpageã«ã¯tabã«é¢ãã¦ã0ã«ãã¦ãããªããã°ããªããªããã¨ãããã¦ãã¾ããæå³ãããããããªãã£ãã®ã§æåã«åè ã§
struct hsearch_data* tab = 0;
ãªã©ããæå³ãªãããªãã»ã»ã»ãã¨ãæããªãã失æãã¦ãã¾ããã
ã§ãå®éã«ããã¹ããã¨ã¯ããã(2008/12/31 id:hayamiz ã®ææã«ããä¿®æ£ããã£ã¡ã®æ¹ãçãããå°æ¥æ§é ä½ãå¤æ´ããã¦ãã³ã¼ãä¿®æ£ãä¸è¦ã«ãªãã®ã§è¯ãã§ãã)
/** ä¿®æ£å¾ */ struct hsearch_data tab; /* INITIALIZE */ memset(&tab, 0, sizeof(tab)); ret = hcreate_r(INIT_TABLE_SIZE, &tab);
/** ããã¯æ§ãã¼ã¸ã§ã³ */ struct hsearch_data tab; /* INITIALIZE */ tab.size = 0; tab.filled = 0; tab.table = NULL; ret = hcreate_r(INIT_TABLE_SIZE, &tab);
å®ã¯ç§ãèªãã§ããmanpageï¼Ubuntuã®ããã±ã¼ã¸ãªã®ã§ããï¼ã¯å¤ããã®ã§ãã(2003/11/28ç)ãæ°ããmanpage(ç¾æç¹ã§2008/10/06ç)ã¯JM Projectã§å
¬éããã¦ãã¾ããman hcreate
ããã§ã¯å°ã表ç¾ãå¤ãã£ã¦ãã¾ãã
int hcreate_r(size_t nel, struct hsearch_data *htab);hcreate_r() é¢æ°ã¯ hcreate() ã¨åãåä½ãããããæ§é ä½ *htab ã§ç¤ºããããã¼ãã«ã対象ã¨ãã¦åä½ããã htab ãæã示ãæ§é ä½ã¯ã hcreate_r() ãåãã¦å¼ã³åºãåã« 0 ã§åãã¦ãããªããã°ãªããªãã
ï¼ããã¯è§£æ±ºãã¦ããç¥ã£ãè¨è¿°ã§ããï¼
追è¨ã«å®éã«åããã¦ãã¹ããããhcreate_r,hsearch_ré¢æ°ã®ä½¿ç¨ä¾ã示ãã¾ããåºæ¬çãªåä½ã¯manpageã«ãã£ãhcreate,hsearchã®åä½ã¨åãã§ãã
ã¾ãç§ã®ç°å¢ã§/usr/include/search.hãèªãã ã®ã§ããã©ãã«ã
# ifdef _GNU_SOURCE
ãªã表ç¾ã¯è¦åããããã
# ifdef __USE_GNU
ãããªã®ã°ã£ããã ã£ãã®ã§å®æ°__USE_GNUãå®ç¾©ãã¦ãã¾ãã
/* ** hash.c - Sample Program for using hcreate_r, hsearch_r ** Made by Limit */ #include <stdio.h> #include <stdlib.h> #include <string.h> /* for memset() */ #include <errno.h> #define __USE_GNU /* for reentrant functions hsearch_r etc. */ #include <search.h> char *data[] = { "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel", "india", "juliet", "kilo", "lima", "mike", "november", "oscar", "papa", "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whisky", "x-ray", "yankee", "zulu" }; #define INIT_TABLE_SIZE ((size_t) 128) int main(void) { ENTRY e; ENTRY* search_result; int ret; int i; struct hsearch_data tab; /* INITIALIZE */ memset(&tab, 0, sizeof(tab)); /* tab.size = 0; tab.filled = 0; tab.table = NULL; */ ret = hcreate_r(INIT_TABLE_SIZE, &tab); if(!ret) { /* ããã¯ããªãå¼ã£ããã£ã */ if(errno == ENOMEM) { /* ããã«ã¯å¼ã£ããããªãã£ã */ printf("NOMEM\n"); } printf("ERROR\n"); return 0; } /* REGISTER DATA */ for(i = 0; i < 24; i++) { e.key = data[i]; e.data = (void*)i; ret = hsearch_r(e, ENTER, &search_result, &tab); if(ret == 0) { printf("Hash table is full\n"); return 0; } printf("Registered %s:%d\n", e.key, (int)e.data); } /* SEARCH TEST: WILL HIT */ for (i = 22; i < 24; i++) { e.key = data[i]; ret = hsearch_r(e, FIND, &search_result, &tab); printf("%s : %s:%d\n", e.key, ret ? search_result->key : "NULL", ret ? (int)(search_result->data) : 0); } /* SEARCH TEST: NO HIT */ e.key = data[24]; ret = hsearch_r(e, FIND, &search_result, &tab); printf("%s : %s:%d\n", e.key, ret ? search_result->key : "NULL", ret ? (int)(search_result->data) : 0); return 0; }