khashtable.c
最終更新:2009/12/28
001: /* khashtable.c
002: * ハッシュテーブルの例
003: * HASHTABLE: apr_hash_make / apr_hash_set / apr_hash_get / apr_hash_count / apr_hash_first / apr_hash_next / apr_hash_this
004: */
005:
006: #include "apr_general.h"
007: #include "apr_file_io.h"
008:
009: #include "apr_hash.h"
010:
011: #include "mystab.h"
012:
013: void disp_k_v(const char *key, int kLen, const char *value, void *ctx) {
014: apr_file_t *out = (apr_file_t*)ctx;
015: apr_file_printf(out, "ITER [%s]=[%s]\n", key, value);
016: }
017:
018: /*
019: * ハッシュテーブルのイテレータ関数
020: */
021: void apr_hash_foreach (apr_pool_t *pool, apr_hash_t *ht, void *f, void *ctx) {
022: void (*func)(const char *, int, const char *, void *) = f;
023: const void *k;
024: void *v;
025: apr_ssize_t kLen;
026: apr_hash_index_t *hi;
027: for (hi = apr_hash_first(pool, ht); hi; hi=apr_hash_next(hi)) {
028: apr_hash_this(hi, &k, &kLen, &v);
029: (*func)((const char*)k, kLen, (const char*)v, ctx);
030: }
031: }
032:
033: int apr_my_main (int ac, char **av, apr_file_t * astdin, apr_file_t * astdout, apr_file_t * astderr, apr_pool_t * pool) {
034:
035: char *keys[] = {
036: "key0"
037: , "key1"
038: , "key2"
039: , "key3"
040: };
041:
042: char *values[] = {
043: "value0"
044: , "value1"
045: , "value2"
046: , "value3"
047: };
048:
049: /* apr_hash_make / apr_hash_set / apr_hash_get / apr_hash_count / apr_hash_first / apr_hash_next / apr_hash_this
050: * INCLUDE: apr_hash.h
051: * LIBS: libapr-1.lib
052: */
053: {
054: apr_hash_t * ht;
055: int i;
056:
057: /* ハッシュテーブルの生成 */
058:
059: ht = apr_hash_make(pool);
060:
061: /* キー・値の追加 */
062:
063: for (i=0; i<4; i++) {
064: apr_hash_set(ht, keys[i], APR_HASH_KEY_STRING, values[i]);
065: }
066:
067: apr_hash_set(ht, "FOO", APR_HASH_KEY_STRING, "VALUE0");
068: apr_hash_set(ht, "BAR", APR_HASH_KEY_STRING, "VALUE1");
069: apr_hash_set(ht, "QUUX", APR_HASH_KEY_STRING, "VALUE2");
070:
071: /* キーに対応する値の表示 */
072:
073: apr_file_printf(astdout, "[%s]=>[%s]\n", "QUUX", (char*)apr_hash_get(ht, "QUUX", APR_HASH_KEY_STRING));
074:
075: for (i=0; i<4; i++) {
076: apr_file_printf(astdout, "[%s]=>[%s]\n", keys[i], (char*)apr_hash_get(ht, keys[i], APR_HASH_KEY_STRING));
077: }
078:
079: /* 格納されたキー・値の個数の表示 */
080:
081: apr_file_printf(astdout, "size=[%d]\n", apr_hash_count(ht));
082:
083: /* 格納されたキー・値の一覧表示 */
084:
085: apr_hash_foreach(pool, ht, disp_k_v, astdout);
086:
087: /* 値の変更 */
088: apr_hash_set(ht, "QUUX", APR_HASH_KEY_STRING, "ANOTHER VALUE");
089: apr_hash_set(ht, "key3", APR_HASH_KEY_STRING, "ANOTHER VALUE2");
090:
091: /* キーに対応する値の表示 */
092:
093: apr_file_printf(astdout, "[%s]=>[%s]\n", "QUUX", (char*)apr_hash_get(ht, "QUUX", APR_HASH_KEY_STRING));
094:
095: for (i=0; i<4; i++) {
096: apr_file_printf(astdout, "[%s]=>[%s]\n", keys[i], (char*)apr_hash_get(ht, keys[i], APR_HASH_KEY_STRING));
097: }
098:
099: /* 格納されたキー・値の個数の表示 */
100:
101: apr_file_printf(astdout, "size=[%d]\n", apr_hash_count(ht));
102:
103: /* 格納されたキー・値の一覧表示 */
104:
105: apr_hash_foreach(pool, ht, disp_k_v, astdout);
106:
107: /* キー・値の削除 */
108: apr_hash_set(ht, "QUUX", APR_HASH_KEY_STRING, NULL);
109:
110: /* キーに対応する値の表示 */
111:
112: apr_file_printf(astdout, "[%s]=>[%s]\n", "QUUX", (char*)apr_hash_get(ht, "QUUX", APR_HASH_KEY_STRING));
113:
114: for (i=0; i<4; i++) {
115: apr_file_printf(astdout, "[%s]=>[%s]\n", keys[i], (char*)apr_hash_get(ht, keys[i], APR_HASH_KEY_STRING));
116: }
117:
118: /* 格納されたキー・値の個数の表示 */
119:
120: apr_file_printf(astdout, "size=[%d]\n", apr_hash_count(ht));
121:
122: /* 格納されたキー・値の一覧表示 */
123:
124: apr_hash_foreach(pool, ht, disp_k_v, astdout);
125:
126: /* ハッシュテーブルのクリア */
127: apr_hash_clear(ht);
128:
129: /* キーに対応する値の表示 */
130:
131: apr_file_printf(astdout, "[%s]=>[%s]\n", "QUUX", (char*)apr_hash_get(ht, "QUUX", APR_HASH_KEY_STRING));
132:
133: for (i=0; i<4; i++) {
134: apr_file_printf(astdout, "[%s]=>[%s]\n", keys[i], (char*)apr_hash_get(ht, keys[i], APR_HASH_KEY_STRING));
135: }
136:
137: /* 格納されたキー・値の個数の表示 */
138:
139: apr_file_printf(astdout, "size=[%d]\n", apr_hash_count(ht));
140:
141: /* 格納されたキー・値の一覧表示 */
142:
143: apr_hash_foreach(pool, ht, disp_k_v, astdout);
144:
145: }
146:
147: apr_file_printf(astdout, "\ndone.\n");
148: return 0; /* 正常終了 */
149: }
![]() | KAKU PROJECT (2009) |