mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-20 18:46:06 +00:00
Re-translate the Japanese version (#1871)
* Retranslate Japanese docs with GPT-5.4 * Retranslate Japanese code with GPT-5.4
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
add_executable(array_hash_map array_hash_map.c)
|
||||
add_executable(hash_map_chaining hash_map_chaining.c)
|
||||
add_executable(hash_map_open_addressing hash_map_open_addressing.c)
|
||||
add_executable(simple_hash simple_hash.c)
|
||||
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* File: array_hash_map.c
|
||||
* Created Time: 2023-03-18
|
||||
* Author: Guanngxu (446678850@qq.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* ハッシュテーブルのデフォルトサイズ */
|
||||
#define MAX_SIZE 100
|
||||
|
||||
/* キーと値の組 int->string */
|
||||
typedef struct {
|
||||
int key;
|
||||
char *val;
|
||||
} Pair;
|
||||
|
||||
/* キーと値の組の集合 */
|
||||
typedef struct {
|
||||
void *set;
|
||||
int len;
|
||||
} MapSet;
|
||||
|
||||
/* 配列ベースのハッシュテーブル */
|
||||
typedef struct {
|
||||
Pair *buckets[MAX_SIZE];
|
||||
} ArrayHashMap;
|
||||
|
||||
/* コンストラクタ */
|
||||
ArrayHashMap *newArrayHashMap() {
|
||||
ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));
|
||||
for (int i=0; i < MAX_SIZE; i++) {
|
||||
hmap->buckets[i] = NULL;
|
||||
}
|
||||
return hmap;
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
void delArrayHashMap(ArrayHashMap *hmap) {
|
||||
for (int i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
free(hmap->buckets[i]->val);
|
||||
free(hmap->buckets[i]);
|
||||
}
|
||||
}
|
||||
free(hmap);
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(int key) {
|
||||
int index = key % MAX_SIZE;
|
||||
return index;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
const char *get(const ArrayHashMap *hmap, const int key) {
|
||||
int index = hashFunc(key);
|
||||
const Pair *Pair = hmap->buckets[index];
|
||||
if (Pair == NULL)
|
||||
return NULL;
|
||||
return Pair->val;
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(ArrayHashMap *hmap, const int key, const char *val) {
|
||||
Pair *Pair = malloc(sizeof(Pair));
|
||||
Pair->key = key;
|
||||
Pair->val = malloc(strlen(val) + 1);
|
||||
strcpy(Pair->val, val);
|
||||
|
||||
int index = hashFunc(key);
|
||||
hmap->buckets[index] = Pair;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void removeItem(ArrayHashMap *hmap, const int key) {
|
||||
int index = hashFunc(key);
|
||||
free(hmap->buckets[index]->val);
|
||||
free(hmap->buckets[index]);
|
||||
hmap->buckets[index] = NULL;
|
||||
}
|
||||
|
||||
/* すべてのキーと値のペアを取得 */
|
||||
void pairSet(ArrayHashMap *hmap, MapSet *set) {
|
||||
Pair *entries;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
/* 有効なキーと値のペア数を集計 */
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
total++;
|
||||
}
|
||||
}
|
||||
entries = malloc(sizeof(Pair) * total);
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
entries[index].key = hmap->buckets[i]->key;
|
||||
entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);
|
||||
strcpy(entries[index].val, hmap->buckets[i]->val);
|
||||
index++;
|
||||
}
|
||||
}
|
||||
set->set = entries;
|
||||
set->len = total;
|
||||
}
|
||||
|
||||
/* すべてのキーを取得 */
|
||||
void keySet(ArrayHashMap *hmap, MapSet *set) {
|
||||
int *keys;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
/* 有効なキーと値のペア数を集計 */
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
total++;
|
||||
}
|
||||
}
|
||||
keys = malloc(total * sizeof(int));
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
keys[index] = hmap->buckets[i]->key;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
set->set = keys;
|
||||
set->len = total;
|
||||
}
|
||||
|
||||
/* すべての値を取得 */
|
||||
void valueSet(ArrayHashMap *hmap, MapSet *set) {
|
||||
char **vals;
|
||||
int i = 0, index = 0;
|
||||
int total = 0;
|
||||
/* 有効なキーと値のペア数を集計 */
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
total++;
|
||||
}
|
||||
}
|
||||
vals = malloc(total * sizeof(char *));
|
||||
for (i = 0; i < MAX_SIZE; i++) {
|
||||
if (hmap->buckets[i] != NULL) {
|
||||
vals[index] = hmap->buckets[i]->val;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
set->set = vals;
|
||||
set->len = total;
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print(ArrayHashMap *hmap) {
|
||||
int i;
|
||||
MapSet set;
|
||||
pairSet(hmap, &set);
|
||||
Pair *entries = (Pair *)set.set;
|
||||
for (i = 0; i < set.len; i++) {
|
||||
printf("%d -> %s\n", entries[i].key, entries[i].val);
|
||||
}
|
||||
free(set.set);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
ArrayHashMap *hmap = newArrayHashMap();
|
||||
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
put(hmap, 12836, "シャオハー");
|
||||
put(hmap, 15937, "シャオルオ");
|
||||
put(hmap, 16750, "シャオスワン");
|
||||
put(hmap, 13276, "シャオファー");
|
||||
put(hmap, 10583, "シャオヤー");
|
||||
printf("\n追加完了後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hmap);
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
const char *name = get(hmap, 15937);
|
||||
printf("\n学籍番号 15937 を入力すると、名前 %s が見つかりました\n", name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
removeItem(hmap, 10583);
|
||||
printf("\n10583 を削除した後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hmap);
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
int i;
|
||||
|
||||
printf("\nキーと値のペア Key->Value を走査\n");
|
||||
print(hmap);
|
||||
|
||||
MapSet set;
|
||||
|
||||
keySet(hmap, &set);
|
||||
int *keys = (int *)set.set;
|
||||
printf("\nキー Key のみを個別に走査\n");
|
||||
for (i = 0; i < set.len; i++) {
|
||||
printf("%d\n", keys[i]);
|
||||
}
|
||||
free(set.set);
|
||||
|
||||
valueSet(hmap, &set);
|
||||
char **vals = (char **)set.set;
|
||||
printf("\nValue のみを個別に走査\n");
|
||||
for (i = 0; i < set.len; i++) {
|
||||
printf("%s\n", vals[i]);
|
||||
}
|
||||
free(set.set);
|
||||
|
||||
delArrayHashMap(hmap);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* File: hash_map_chaining.c
|
||||
* Created Time: 2023-10-13
|
||||
* Author: SenMing (1206575349@qq.com), krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// val の最大長を 100 と仮定する
|
||||
#define MAX_SIZE 100
|
||||
|
||||
/* キーと値の組 */
|
||||
typedef struct {
|
||||
int key;
|
||||
char val[MAX_SIZE];
|
||||
} Pair;
|
||||
|
||||
/* 連結リストノード */
|
||||
typedef struct Node {
|
||||
Pair *pair;
|
||||
struct Node *next;
|
||||
} Node;
|
||||
|
||||
/* チェイン法ハッシュテーブル */
|
||||
typedef struct {
|
||||
int size; // キーと値のペア数
|
||||
int capacity; // ハッシュテーブル容量
|
||||
double loadThres; // リサイズを発動する負荷率のしきい値
|
||||
int extendRatio; // 拡張倍率
|
||||
Node **buckets; // バケット配列
|
||||
} HashMapChaining;
|
||||
|
||||
/* コンストラクタ */
|
||||
HashMapChaining *newHashMapChaining() {
|
||||
HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));
|
||||
hashMap->size = 0;
|
||||
hashMap->capacity = 4;
|
||||
hashMap->loadThres = 2.0 / 3.0;
|
||||
hashMap->extendRatio = 2;
|
||||
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
hashMap->buckets[i] = NULL;
|
||||
}
|
||||
return hashMap;
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
void delHashMapChaining(HashMapChaining *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Node *cur = hashMap->buckets[i];
|
||||
while (cur) {
|
||||
Node *tmp = cur;
|
||||
cur = cur->next;
|
||||
free(tmp->pair);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
free(hashMap->buckets);
|
||||
free(hashMap);
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(HashMapChaining *hashMap, int key) {
|
||||
return key % hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 負荷率 */
|
||||
double loadFactor(HashMapChaining *hashMap) {
|
||||
return (double)hashMap->size / (double)hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
char *get(HashMapChaining *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
// バケットを走査し、key が見つかれば対応する val を返す
|
||||
Node *cur = hashMap->buckets[index];
|
||||
while (cur) {
|
||||
if (cur->pair->key == key) {
|
||||
return cur->pair->val;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
return ""; // key が見つからない場合は空文字列を返す
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(HashMapChaining *hashMap, int key, const char *val);
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void extend(HashMapChaining *hashMap) {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
int oldCapacity = hashMap->capacity;
|
||||
Node **oldBuckets = hashMap->buckets;
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
hashMap->capacity *= hashMap->extendRatio;
|
||||
hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
hashMap->buckets[i] = NULL;
|
||||
}
|
||||
hashMap->size = 0;
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
for (int i = 0; i < oldCapacity; i++) {
|
||||
Node *cur = oldBuckets[i];
|
||||
while (cur) {
|
||||
put(hashMap, cur->pair->key, cur->pair->val);
|
||||
Node *temp = cur;
|
||||
cur = cur->next;
|
||||
// メモリを解放する
|
||||
free(temp->pair);
|
||||
free(temp);
|
||||
}
|
||||
}
|
||||
|
||||
free(oldBuckets);
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(HashMapChaining *hashMap, int key, const char *val) {
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (loadFactor(hashMap) > hashMap->loadThres) {
|
||||
extend(hashMap);
|
||||
}
|
||||
int index = hashFunc(hashMap, key);
|
||||
// バケットを走査し、指定した key が見つかれば対応する val を更新して返す
|
||||
Node *cur = hashMap->buckets[index];
|
||||
while (cur) {
|
||||
if (cur->pair->key == key) {
|
||||
strcpy(cur->pair->val, val); // 指定した `key` に遭遇したら、対応する `val` を更新して返す
|
||||
return;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
// 該当する `key` がなければ、キーと値のペアを連結リストの先頭に追加する
|
||||
Pair *newPair = (Pair *)malloc(sizeof(Pair));
|
||||
newPair->key = key;
|
||||
strcpy(newPair->val, val);
|
||||
Node *newNode = (Node *)malloc(sizeof(Node));
|
||||
newNode->pair = newPair;
|
||||
newNode->next = hashMap->buckets[index];
|
||||
hashMap->buckets[index] = newNode;
|
||||
hashMap->size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void removeItem(HashMapChaining *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
Node *cur = hashMap->buckets[index];
|
||||
Node *pre = NULL;
|
||||
while (cur) {
|
||||
if (cur->pair->key == key) {
|
||||
// そこからキーと値の組を削除する
|
||||
if (pre) {
|
||||
pre->next = cur->next;
|
||||
} else {
|
||||
hashMap->buckets[index] = cur->next;
|
||||
}
|
||||
// メモリを解放する
|
||||
free(cur->pair);
|
||||
free(cur);
|
||||
hashMap->size--;
|
||||
return;
|
||||
}
|
||||
pre = cur;
|
||||
cur = cur->next;
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print(HashMapChaining *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Node *cur = hashMap->buckets[i];
|
||||
printf("[");
|
||||
while (cur) {
|
||||
printf("%d -> %s, ", cur->pair->key, cur->pair->val);
|
||||
cur = cur->next;
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
HashMapChaining *hashMap = newHashMapChaining();
|
||||
|
||||
/* 追加操作 */
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
put(hashMap, 12836, "シャオハー");
|
||||
put(hashMap, 15937, "シャオルオ");
|
||||
put(hashMap, 16750, "シャオスワン");
|
||||
put(hashMap, 13276, "シャオファー");
|
||||
put(hashMap, 10583, "シャオヤー");
|
||||
printf("\n追加完了後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hashMap);
|
||||
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
char *name = get(hashMap, 13276);
|
||||
printf("\n学籍番号 13276 を入力すると、名前 %s が見つかりました\n", name);
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
removeItem(hashMap, 12836);
|
||||
printf("\n学籍番号 12836 を削除した後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hashMap);
|
||||
|
||||
/* ハッシュテーブルの領域を解放する */
|
||||
delHashMapChaining(hashMap);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* File: hash_map_open_addressing.c
|
||||
* Created Time: 2023-10-6
|
||||
* Author: lclc6 (w1929522410@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* オープンアドレス法ハッシュテーブル */
|
||||
typedef struct {
|
||||
int key;
|
||||
char *val;
|
||||
} Pair;
|
||||
|
||||
/* オープンアドレス法ハッシュテーブル */
|
||||
typedef struct {
|
||||
int size; // キーと値のペア数
|
||||
int capacity; // ハッシュテーブル容量
|
||||
double loadThres; // リサイズを発動する負荷率のしきい値
|
||||
int extendRatio; // 拡張倍率
|
||||
Pair **buckets; // バケット配列
|
||||
Pair *TOMBSTONE; // 削除済みマーク
|
||||
} HashMapOpenAddressing;
|
||||
|
||||
// 関数宣言
|
||||
void extend(HashMapOpenAddressing *hashMap);
|
||||
|
||||
/* コンストラクタ */
|
||||
HashMapOpenAddressing *newHashMapOpenAddressing() {
|
||||
HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));
|
||||
hashMap->size = 0;
|
||||
hashMap->capacity = 4;
|
||||
hashMap->loadThres = 2.0 / 3.0;
|
||||
hashMap->extendRatio = 2;
|
||||
hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));
|
||||
hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));
|
||||
hashMap->TOMBSTONE->key = -1;
|
||||
hashMap->TOMBSTONE->val = "-1";
|
||||
|
||||
return hashMap;
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
void delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Pair *pair = hashMap->buckets[i];
|
||||
if (pair != NULL && pair != hashMap->TOMBSTONE) {
|
||||
free(pair->val);
|
||||
free(pair);
|
||||
}
|
||||
}
|
||||
free(hashMap->buckets);
|
||||
free(hashMap->TOMBSTONE);
|
||||
free(hashMap);
|
||||
}
|
||||
|
||||
/* ハッシュ関数 */
|
||||
int hashFunc(HashMapOpenAddressing *hashMap, int key) {
|
||||
return key % hashMap->capacity;
|
||||
}
|
||||
|
||||
/* 負荷率 */
|
||||
double loadFactor(HashMapOpenAddressing *hashMap) {
|
||||
return (double)hashMap->size / (double)hashMap->capacity;
|
||||
}
|
||||
|
||||
/* key に対応するバケットインデックスを探す */
|
||||
int findBucket(HashMapOpenAddressing *hashMap, int key) {
|
||||
int index = hashFunc(hashMap, key);
|
||||
int firstTombstone = -1;
|
||||
// 線形プロービングを行い、空バケットに達したら終了
|
||||
while (hashMap->buckets[index] != NULL) {
|
||||
// key が見つかったら、対応するバケットのインデックスを返す
|
||||
if (hashMap->buckets[index]->key == key) {
|
||||
// 以前に削除マークが見つかっていれば、そのインデックスへキーと値のペアを移動
|
||||
if (firstTombstone != -1) {
|
||||
hashMap->buckets[firstTombstone] = hashMap->buckets[index];
|
||||
hashMap->buckets[index] = hashMap->TOMBSTONE;
|
||||
return firstTombstone; // 移動後のバケットインデックスを返す
|
||||
}
|
||||
return index; // バケットのインデックスを返す
|
||||
}
|
||||
// 最初に見つかった削除マークを記録
|
||||
if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// バケットのインデックスを計算し、末尾を越えたら先頭に戻る
|
||||
index = (index + 1) % hashMap->capacity;
|
||||
}
|
||||
// key が存在しない場合は追加位置のインデックスを返す
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* 検索操作 */
|
||||
char *get(HashMapOpenAddressing *hashMap, int key) {
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(hashMap, key);
|
||||
// キーと値の組が見つかったら、対応する val を返す
|
||||
if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {
|
||||
return hashMap->buckets[index]->val;
|
||||
}
|
||||
// キーと値の組が存在しない場合は空文字列を返す
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(HashMapOpenAddressing *hashMap, int key, char *val) {
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (loadFactor(hashMap) > hashMap->loadThres) {
|
||||
extend(hashMap);
|
||||
}
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(hashMap, key);
|
||||
// キーと値の組が見つかったら、val を上書きして返す
|
||||
if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {
|
||||
free(hashMap->buckets[index]->val);
|
||||
hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));
|
||||
strcpy(hashMap->buckets[index]->val, val);
|
||||
hashMap->buckets[index]->val[strlen(val)] = '\0';
|
||||
return;
|
||||
}
|
||||
// キーと値の組が存在しない場合は、その組を追加する
|
||||
Pair *pair = (Pair *)malloc(sizeof(Pair));
|
||||
pair->key = key;
|
||||
pair->val = (char *)malloc(sizeof(strlen(val) + 1));
|
||||
strcpy(pair->val, val);
|
||||
pair->val[strlen(val)] = '\0';
|
||||
|
||||
hashMap->buckets[index] = pair;
|
||||
hashMap->size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void removeItem(HashMapOpenAddressing *hashMap, int key) {
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(hashMap, key);
|
||||
// キーと値の組が見つかったら、削除マーカーで上書きする
|
||||
if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {
|
||||
Pair *pair = hashMap->buckets[index];
|
||||
free(pair->val);
|
||||
free(pair);
|
||||
hashMap->buckets[index] = hashMap->TOMBSTONE;
|
||||
hashMap->size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを拡張 */
|
||||
void extend(HashMapOpenAddressing *hashMap) {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
Pair **bucketsTmp = hashMap->buckets;
|
||||
int oldCapacity = hashMap->capacity;
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
hashMap->capacity *= hashMap->extendRatio;
|
||||
hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));
|
||||
hashMap->size = 0;
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
for (int i = 0; i < oldCapacity; i++) {
|
||||
Pair *pair = bucketsTmp[i];
|
||||
if (pair != NULL && pair != hashMap->TOMBSTONE) {
|
||||
put(hashMap, pair->key, pair->val);
|
||||
free(pair->val);
|
||||
free(pair);
|
||||
}
|
||||
}
|
||||
free(bucketsTmp);
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print(HashMapOpenAddressing *hashMap) {
|
||||
for (int i = 0; i < hashMap->capacity; i++) {
|
||||
Pair *pair = hashMap->buckets[i];
|
||||
if (pair == NULL) {
|
||||
printf("NULL\n");
|
||||
} else if (pair == hashMap->TOMBSTONE) {
|
||||
printf("TOMBSTONE\n");
|
||||
} else {
|
||||
printf("%d -> %s\n", pair->key, pair->val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// ハッシュテーブルを初期化
|
||||
HashMapOpenAddressing *hashmap = newHashMapOpenAddressing();
|
||||
|
||||
// 追加操作
|
||||
// ハッシュテーブルにキーと値の組 (key, val) を追加する
|
||||
put(hashmap, 12836, "シャオハー");
|
||||
put(hashmap, 15937, "シャオルオ");
|
||||
put(hashmap, 16750, "シャオスワン");
|
||||
put(hashmap, 13276, "シャオファー");
|
||||
put(hashmap, 10583, "シャオヤー");
|
||||
printf("\n追加完了後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hashmap);
|
||||
|
||||
// 検索操作
|
||||
// ハッシュテーブルにキー key を入力し、値 val を得る
|
||||
char *name = get(hashmap, 13276);
|
||||
printf("\n学籍番号 13276 を入力すると、名前 %s が見つかりました\n", name);
|
||||
|
||||
// 削除操作
|
||||
// ハッシュテーブルからキーと値の組 (key, val) を削除する
|
||||
removeItem(hashmap, 16750);
|
||||
printf("\n16750 を削除した後、ハッシュテーブルは\nKey -> Value\n");
|
||||
print(hashmap);
|
||||
|
||||
// ハッシュテーブルを破棄する
|
||||
delHashMapOpenAddressing(hashmap);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
/**
|
||||
* File: simple_hash.c
|
||||
* Created Time: 2023-09-09
|
||||
* Author: Gonglja (glj0@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 加算ハッシュ */
|
||||
int addHash(char *key) {
|
||||
long long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
for (int i = 0; i < strlen(key); i++) {
|
||||
hash = (hash + (unsigned char)key[i]) % MODULUS;
|
||||
}
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* 乗算ハッシュ */
|
||||
int mulHash(char *key) {
|
||||
long long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
for (int i = 0; i < strlen(key); i++) {
|
||||
hash = (31 * hash + (unsigned char)key[i]) % MODULUS;
|
||||
}
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* XOR ハッシュ */
|
||||
int xorHash(char *key) {
|
||||
int hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
|
||||
for (int i = 0; i < strlen(key); i++) {
|
||||
hash ^= (unsigned char)key[i];
|
||||
}
|
||||
return hash & MODULUS;
|
||||
}
|
||||
|
||||
/* 回転ハッシュ */
|
||||
int rotHash(char *key) {
|
||||
long long hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
for (int i = 0; i < strlen(key); i++) {
|
||||
hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;
|
||||
}
|
||||
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
char *key = "Hello アルゴリズム";
|
||||
|
||||
int hash = addHash(key);
|
||||
printf("加算ハッシュ値は %d\n", hash);
|
||||
|
||||
hash = mulHash(key);
|
||||
printf("乗算ハッシュ値は %d\n", hash);
|
||||
|
||||
hash = xorHash(key);
|
||||
printf("XORハッシュ値は %d\n", hash);
|
||||
|
||||
hash = rotHash(key);
|
||||
printf("回転ハッシュ値は %d\n", hash);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user