#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TABLE_SIZE (102400)
#define HASH_TABLE_SIZE (32)
void initHashTable();
/*
* ハッシュテーブル。
*
* 各テーブルはリスト構造によって構築。
*/
/* テーブル上のデータにint型サイズでアクセスするためのもの */
union U {
int *addr;
char *ptr;
};
char table[TABLE_SIZE]; /* 全てのデータの格納 */
union U freeptr; /* データの追加ポイントへの参照 */
/* テーブルの先頭アドレスの取得 */
char *getTableHeadPtr() {
return &table[0] + sizeof(int);
}
/* データの追加場所のアドレスを登録 */
void setFreePtr(char *ptr) {
*freeptr.addr = (int)ptr;
}
/* データの追加場所のアドレスの移動 */
char *moveFreePtr(int add) {
*freeptr.addr += add;
return (char*)*freeptr.addr;
}
/* データの追加場所のアドレスの取得 */
char *getFreePtr() {
return (char*)*freeptr.addr;
}
/* ハッシュ関数 */
int hash(char *str) {
int h = 0;
int i;
for (i = 0; (*str != '\0') && (i < 10); ++i) {
h += (str[0]) * (str[1]);
++str;
}
return h % HASH_TABLE_SIZE;
}
/* ハッシュテーブルの初期化 */
void initHashTable() {
union U u;
int i;
/* テーブルの初期化 */
u.ptr = getTableHeadPtr();
for (i = 0; i < HASH_TABLE_SIZE; ++i) {
*u.addr = (int)NULL;
u.ptr += sizeof(int);
}
/* データの追加場所を記録する場所はテーブルの先頭 */
freeptr.ptr = &table[0];
/* データの追加場所の指定 */
setFreePtr(u.ptr);
}
/* テーブルへのデータの追加 */
void addHashTable(const char *key, const void *data, const int size) {
union U u, v;
int h = hash(key);
int len;
char *p;
/* リストの先頭アドレスの取得 */
u.ptr = getTableHeadPtr() + h * sizeof(int);
/* リスト上のデータ追加する場所を探す */
if (*u.addr != (int)NULL) {
do {
u.ptr = (char*)*u.addr;
p = u.ptr + sizeof(int);
printf("invalid key: %s\n", key
); /* キー重複面倒 */ return;
}
} while (*u.addr != (int)NULL);
}
/* 新しいデータのアドレス格納 */
*u.addr = (int)getFreePtr();
/* ここから新しいデータの編集 */
/* 次のデータへのポインタを0(NULL)で初期化 */
v.ptr = getFreePtr();
*v.addr = (int)NULL;
moveFreePtr(sizeof(int));
/* キーの登録 */
memcpy(getFreePtr
(), key
, len
); moveFreePtr(len);
/* データサイズの登録 */
/* 不精なのでunion Uを代用 */
v.ptr = getFreePtr();
*v.addr = size;
moveFreePtr(sizeof(int));
/* データの登録 */
memcpy(getFreePtr
(), data
, size
); moveFreePtr(size);
}
void getAllData(void (*func)(const char*, const void*, const int)) {
union U u, v, w;
int i, len;
char *p;
u.ptr = getTableHeadPtr();
for (i = 0; i < HASH_TABLE_SIZE; ++i) {
if (*u.addr != (int)NULL) {
v = u;
do {
v.ptr = (char*)*v.addr;
p = v.ptr + sizeof(int);
w.ptr = p + len + 1;
(*func)(p, w.ptr + sizeof(int), *w.addr);
} while (*v.addr != (int)NULL);
}
u.ptr += sizeof(int);
}
}
void myfunc(const char* key, const void* data, const int size) {
printf("key: %s, size: %d, data address: %d\n", key
, size
, data
); }
int main(void) {
int v = 0xABCDEF12;
initHashTable();
addHashTable("abcde", &v, sizeof(v));
addHashTable("abcde", &v, sizeof(v));
addHashTable("abcde", &v, sizeof(v));
v = 0xAABBCC;
addHashTable("XYZ", &v, sizeof(v));
v = 0x112233;
addHashTable("ABCDE", &v, sizeof(v));
getAllData(myfunc);
for (v = 0; v < 256; ++v) {
if (v % 8 == 0) {
if (v % 16 == 0) {
} else {
}
}
printf("%02x ", 0xFF & table
[v
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgoKI2RlZmluZSBUQUJMRV9TSVpFICgxMDI0MDApCiNkZWZpbmUgSEFTSF9UQUJMRV9TSVpFICgzMikKCnZvaWQgaW5pdEhhc2hUYWJsZSgpOwoKLyoKICog44OP44OD44K344Ol44OG44O844OW44Or44CCCiAqCiAqIOWQhOODhuODvOODluODq+OBr+ODquOCueODiOani+mAoOOBq+OCiOOBo+OBpuani+evieOAggogKi8KCi8qIOODhuODvOODluODq+S4iuOBruODh+ODvOOCv+OBq2ludOWei+OCteOCpOOCuuOBp+OCouOCr+OCu+OCueOBmeOCi+OBn+OCgeOBruOCguOBriAqLwp1bmlvbiBVIHsKCWludCAqYWRkcjsKCWNoYXIgKnB0cjsKfTsKCmNoYXIgdGFibGVbVEFCTEVfU0laRV07IC8qIOWFqOOBpuOBruODh+ODvOOCv+OBruagvOe0jSAqLwp1bmlvbiBVIGZyZWVwdHI7IC8qIOODh+ODvOOCv+OBrui/veWKoOODneOCpOODs+ODiOOBuOOBruWPgueFpyAqLwoKLyog44OG44O844OW44Or44Gu5YWI6aCt44Ki44OJ44Os44K544Gu5Y+W5b6XICovCmNoYXIgKmdldFRhYmxlSGVhZFB0cigpIHsKCXJldHVybiAmdGFibGVbMF0gKyBzaXplb2YoaW50KTsKfQoKLyog44OH44O844K/44Gu6L+95Yqg5aC05omA44Gu44Ki44OJ44Os44K544KS55m76YyyICovCnZvaWQgc2V0RnJlZVB0cihjaGFyICpwdHIpIHsKCSpmcmVlcHRyLmFkZHIgPSAoaW50KXB0cjsKfQoKLyog44OH44O844K/44Gu6L+95Yqg5aC05omA44Gu44Ki44OJ44Os44K544Gu56e75YuVICovCmNoYXIgKm1vdmVGcmVlUHRyKGludCBhZGQpIHsKCSpmcmVlcHRyLmFkZHIgKz0gYWRkOwoJcmV0dXJuIChjaGFyKikqZnJlZXB0ci5hZGRyOwp9CgovKiDjg4fjg7zjgr/jga7ov73liqDloLTmiYDjga7jgqLjg4njg6zjgrnjga7lj5blvpcgKi8KY2hhciAqZ2V0RnJlZVB0cigpIHsKCXJldHVybiAoY2hhciopKmZyZWVwdHIuYWRkcjsKfQoKLyog44OP44OD44K344Ol6Zai5pWwICovCmludCBoYXNoKGNoYXIgKnN0cikgewoJaW50IGggPSAwOwoJaW50IGk7Cglmb3IgKGkgPSAwOyAoKnN0ciAhPSAnXDAnKSAmJiAoaSA8IDEwKTsgKytpKSB7CgkJaCArPSAoc3RyWzBdKSAqIChzdHJbMV0pOwoJCSsrc3RyOwoJfQoJcmV0dXJuIGggJSBIQVNIX1RBQkxFX1NJWkU7Cn0KCi8qIOODj+ODg+OCt+ODpeODhuODvOODluODq+OBruWIneacn+WMliAqLwp2b2lkIGluaXRIYXNoVGFibGUoKSB7Cgl1bmlvbiBVIHU7CglpbnQgaTsKCQoJLyog44OG44O844OW44Or44Gu5Yid5pyf5YyWICovCgl1LnB0ciA9IGdldFRhYmxlSGVhZFB0cigpOwoJZm9yIChpID0gMDsgaSA8IEhBU0hfVEFCTEVfU0laRTsgKytpKSB7CgkJKnUuYWRkciA9IChpbnQpTlVMTDsKCQl1LnB0ciArPSBzaXplb2YoaW50KTsKCX0KCQoJLyog44OH44O844K/44Gu6L+95Yqg5aC05omA44KS6KiY6Yyy44GZ44KL5aC05omA44Gv44OG44O844OW44Or44Gu5YWI6aCtICovCglmcmVlcHRyLnB0ciA9ICZ0YWJsZVswXTsKCQoJLyog44OH44O844K/44Gu6L+95Yqg5aC05omA44Gu5oyH5a6aICovCglzZXRGcmVlUHRyKHUucHRyKTsKfQoKLyog44OG44O844OW44Or44G444Gu44OH44O844K/44Gu6L+95YqgICovCnZvaWQgYWRkSGFzaFRhYmxlKGNvbnN0IGNoYXIgKmtleSwgY29uc3Qgdm9pZCAqZGF0YSwgY29uc3QgaW50IHNpemUpIHsKCXVuaW9uIFUgdSwgdjsKCWludCBoID0gaGFzaChrZXkpOwoJaW50IGxlbjsKCWNoYXIgKnA7CgkKCS8qIOODquOCueODiOOBruWFiOmgreOCouODieODrOOCueOBruWPluW+lyAqLwoJdS5wdHIgPSBnZXRUYWJsZUhlYWRQdHIoKSArIGggKiBzaXplb2YoaW50KTsKCQoJLyog44Oq44K544OI5LiK44Gu44OH44O844K/6L+95Yqg44GZ44KL5aC05omA44KS5o6i44GZICovCglpZiAoKnUuYWRkciAhPSAoaW50KU5VTEwpIHsKCQlkbyB7CgkJCXUucHRyID0gKGNoYXIqKSp1LmFkZHI7Cg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