mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-17 15:48:52 +08: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:
6
ja/codes/cpp/chapter_hashing/CMakeLists.txt
Normal file
6
ja/codes/cpp/chapter_hashing/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
add_executable(hash_map hash_map.cpp)
|
||||
add_executable(array_hash_map_test array_hash_map_test.cpp)
|
||||
add_executable(hash_map_chaining hash_map_chaining.cpp)
|
||||
add_executable(hash_map_open_addressing hash_map_open_addressing.cpp)
|
||||
add_executable(simple_hash simple_hash.cpp)
|
||||
add_executable(built_in_hash built_in_hash.cpp)
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* キー値ペア */
|
||||
/* キーと値の組 */
|
||||
struct Pair {
|
||||
public:
|
||||
int key;
|
||||
@@ -17,19 +17,19 @@ struct Pair {
|
||||
}
|
||||
};
|
||||
|
||||
/* 配列実装に基づくハッシュテーブル */
|
||||
/* 配列ベースのハッシュテーブル */
|
||||
class ArrayHashMap {
|
||||
private:
|
||||
vector<Pair *> buckets;
|
||||
|
||||
public:
|
||||
ArrayHashMap() {
|
||||
// 配列を初期化、100個のバケットを含む
|
||||
// 100 個のバケットを含む配列を初期化
|
||||
buckets = vector<Pair *>(100);
|
||||
}
|
||||
|
||||
~ArrayHashMap() {
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
for (const auto &bucket : buckets) {
|
||||
delete bucket;
|
||||
}
|
||||
@@ -42,7 +42,7 @@ class ArrayHashMap {
|
||||
return index;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
/* 検索操作 */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
Pair *pair = buckets[index];
|
||||
@@ -61,12 +61,12 @@ class ArrayHashMap {
|
||||
/* 削除操作 */
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
// メモリを解放してnullptrに設定
|
||||
// メモリを解放して nullptr に設定する
|
||||
delete buckets[index];
|
||||
buckets[index] = nullptr;
|
||||
}
|
||||
|
||||
/* すべてのキー値ペアを取得 */
|
||||
/* すべてのキーと値のペアを取得 */
|
||||
vector<Pair *> pairSet() {
|
||||
vector<Pair *> pairSet;
|
||||
for (Pair *pair : buckets) {
|
||||
@@ -99,7 +99,7 @@ class ArrayHashMap {
|
||||
return valueSet;
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print() {
|
||||
for (Pair *kv : pairSet()) {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
@@ -107,4 +107,4 @@ class ArrayHashMap {
|
||||
}
|
||||
};
|
||||
|
||||
// テストケースはarray_hash_map_test.cppを参照
|
||||
// テストケースは `array_hash_map_test.cpp` を参照
|
||||
|
||||
@@ -6,47 +6,47 @@
|
||||
|
||||
#include "./array_hash_map.cpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
ArrayHashMap map = ArrayHashMap();
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map.put(12836, "シャオハー");
|
||||
map.put(15937, "シャオルオ");
|
||||
map.put(16750, "シャオスワン");
|
||||
map.put(13276, "シャオファー");
|
||||
map.put(10583, "シャオヤー");
|
||||
cout << "\n追加完了後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string name = map.get(15937);
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
cout << "\n学籍番号 15937 を入力すると、氏名 " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.remove(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\n10583 を削除した後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
cout << "\nTraverse key-value pairs Key->Value" << endl;
|
||||
cout << "\nキーと値のペア Key->Value を走査" << endl;
|
||||
for (auto kv : map.pairSet()) {
|
||||
cout << kv->key << " -> " << kv->val << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse keys Key" << endl;
|
||||
cout << "\nキー Key のみを走査" << endl;
|
||||
for (auto key : map.keySet()) {
|
||||
cout << key << endl;
|
||||
}
|
||||
|
||||
cout << "\nIndividually traverse values Value" << endl;
|
||||
cout << "\n値 Value のみを走査" << endl;
|
||||
for (auto val : map.valueSet()) {
|
||||
cout << val << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,24 +6,24 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int num = 3;
|
||||
size_t hashNum = hash<int>()(num);
|
||||
cout << "The hash value of integer " << num << " is " << hashNum << "\n";
|
||||
cout << "整数 " << num << " のハッシュ値は " << hashNum << "\n";
|
||||
|
||||
bool bol = true;
|
||||
size_t hashBol = hash<bool>()(bol);
|
||||
cout << "The hash value of boolean " << bol << " is " << hashBol << "\n";
|
||||
cout << "真偽値 " << bol << " のハッシュ値は " << hashBol << "\n";
|
||||
|
||||
double dec = 3.14159;
|
||||
size_t hashDec = hash<double>()(dec);
|
||||
cout << "The hash value of decimal " << dec << " is " << hashDec << "\n";
|
||||
cout << "小数 " << dec << " のハッシュ値は " << hashDec << "\n";
|
||||
|
||||
string str = "Hello algorithm";
|
||||
string str = "Hello アルゴリズム";
|
||||
size_t hashStr = hash<string>()(str);
|
||||
cout << "The hash value of string " << str << " is " << hashStr << "\n";
|
||||
cout << "文字列 " << str << " のハッシュ値は " << hashStr << "\n";
|
||||
|
||||
// C++では、組み込みのstd:hash()は基本データ型のハッシュ値のみを提供
|
||||
// 配列やオブジェクトのハッシュ値計算は手動で実装する必要がある
|
||||
}
|
||||
// C++ では、組み込みの std::hash() は基本データ型のハッシュ値計算しか提供しない
|
||||
// 配列やオブジェクトのハッシュ値計算は自分で実装する必要がある
|
||||
}
|
||||
|
||||
@@ -6,41 +6,41 @@
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
unordered_map<int, string> map;
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map[12836] = "Ha";
|
||||
map[15937] = "Luo";
|
||||
map[16750] = "Suan";
|
||||
map[13276] = "Fa";
|
||||
map[10583] = "Ya";
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map[12836] = "シャオハー";
|
||||
map[15937] = "シャオルオ";
|
||||
map[16750] = "シャオスワン";
|
||||
map[13276] = "シャオファー";
|
||||
map[10583] = "シャオヤー";
|
||||
cout << "\n追加完了後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string name = map[15937];
|
||||
cout << "\nEnter student ID 15937, found name " << name << endl;
|
||||
cout << "\n学籍番号 15937 を入力すると、氏名 " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.erase(10583);
|
||||
cout << "\nAfter removing 10583, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\n10583 を削除した後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
printHashMap(map);
|
||||
|
||||
/* ハッシュテーブルを走査 */
|
||||
cout << "\nTraverse key-value pairs Key->Value" << endl;
|
||||
cout << "\nキーと値のペア Key->Value を走査" << endl;
|
||||
for (auto kv : map) {
|
||||
cout << kv.first << " -> " << kv.second << endl;
|
||||
}
|
||||
cout << "\nIterate through Key->Value using an iterator" << endl;
|
||||
cout << "\nイテレータで Key->Value を走査" << endl;
|
||||
for (auto iter = map.begin(); iter != map.end(); iter++) {
|
||||
cout << iter->first << "->" << iter->second << endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,9 @@
|
||||
/* チェイン法ハッシュテーブル */
|
||||
class HashMapChaining {
|
||||
private:
|
||||
int size; // キー値ペアの数
|
||||
int capacity; // ハッシュテーブルの容量
|
||||
double loadThres; // 拡張をトリガーする負荷率の閾値
|
||||
int size; // キーと値のペア数
|
||||
int capacity; // ハッシュテーブル容量
|
||||
double loadThres; // リサイズを発動する負荷率のしきい値
|
||||
int extendRatio; // 拡張倍率
|
||||
vector<vector<Pair *>> buckets; // バケット配列
|
||||
|
||||
@@ -21,11 +21,11 @@ class HashMapChaining {
|
||||
buckets.resize(capacity);
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
/* デストラクタメソッド */
|
||||
~HashMapChaining() {
|
||||
for (auto &bucket : buckets) {
|
||||
for (Pair *pair : bucket) {
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
@@ -41,34 +41,34 @@ class HashMapChaining {
|
||||
return (double)size / (double)capacity;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
/* 検索操作 */
|
||||
string get(int key) {
|
||||
int index = hashFunc(key);
|
||||
// バケットを走査、キーが見つかった場合、対応するvalを返却
|
||||
// バケットを走査し、key が見つかれば対応する val を返す
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
return pair->val;
|
||||
}
|
||||
}
|
||||
// キーが見つからない場合、空文字列を返却
|
||||
// key が見つからない場合は空文字列を返す
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(int key, string val) {
|
||||
// 負荷率が閾値を超えた場合、拡張を実行
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
int index = hashFunc(key);
|
||||
// バケットを走査、指定キーに遭遇した場合、対応するvalを更新して返却
|
||||
// バケットを走査し、指定した key が見つかれば対応する val を更新して返す
|
||||
for (Pair *pair : buckets[index]) {
|
||||
if (pair->key == key) {
|
||||
pair->val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
// キーが見つからない場合、キー値ペアを末尾に追加
|
||||
// その key が存在しなければ、キーと値のペアを末尾に追加
|
||||
buckets[index].push_back(new Pair(key, val));
|
||||
size++;
|
||||
}
|
||||
@@ -77,12 +77,12 @@ class HashMapChaining {
|
||||
void remove(int key) {
|
||||
int index = hashFunc(key);
|
||||
auto &bucket = buckets[index];
|
||||
// バケットを走査、キー値ペアを削除
|
||||
// バケットを走査してキーと値のペアを削除
|
||||
for (int i = 0; i < bucket.size(); i++) {
|
||||
if (bucket[i]->key == key) {
|
||||
Pair *tmp = bucket[i];
|
||||
bucket.erase(bucket.begin() + i); // キー値ペアを削除
|
||||
delete tmp; // メモリを解放
|
||||
bucket.erase(bucket.begin() + i); // そこからキーと値の組を削除する
|
||||
delete tmp; // メモリを解放する
|
||||
size--;
|
||||
return;
|
||||
}
|
||||
@@ -93,22 +93,22 @@ class HashMapChaining {
|
||||
void extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
vector<vector<Pair *>> bucketsTmp = buckets;
|
||||
// 拡張された新しいハッシュテーブルを初期化
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets.clear();
|
||||
buckets.resize(capacity);
|
||||
size = 0;
|
||||
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
for (auto &bucket : bucketsTmp) {
|
||||
for (Pair *pair : bucket) {
|
||||
put(pair->key, pair->val);
|
||||
// メモリを解放
|
||||
// メモリを解放する
|
||||
delete pair;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print() {
|
||||
for (auto &bucket : buckets) {
|
||||
cout << "[";
|
||||
@@ -120,31 +120,31 @@ class HashMapChaining {
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
/* ハッシュテーブルを初期化 */
|
||||
HashMapChaining map = HashMapChaining();
|
||||
|
||||
/* 追加操作 */
|
||||
// キー値ペア(key, value)をハッシュテーブルに追加
|
||||
map.put(12836, "Ha");
|
||||
map.put(15937, "Luo");
|
||||
map.put(16750, "Suan");
|
||||
map.put(13276, "Fa");
|
||||
map.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
// ハッシュテーブルにキーと値のペア (key, value) を追加
|
||||
map.put(12836, "シャオハー");
|
||||
map.put(15937, "シャオルオ");
|
||||
map.put(16750, "シャオスワン");
|
||||
map.put(13276, "シャオファー");
|
||||
map.put(10583, "シャオヤー");
|
||||
cout << "\n追加完了後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
/* クエリ操作 */
|
||||
// ハッシュテーブルにキーを入力、値を取得
|
||||
/* 検索操作 */
|
||||
// キー key をハッシュテーブルに渡し、値 value を取得
|
||||
string name = map.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
cout << "\n学籍番号 13276 を入力すると、氏名 " << name << endl;
|
||||
|
||||
/* 削除操作 */
|
||||
// ハッシュテーブルからキー値ペア(key, value)を削除
|
||||
// ハッシュテーブルからキーと値のペア (key, value) を削除
|
||||
map.remove(12836);
|
||||
cout << "\nAfter removing 12836, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\n12836 を削除した後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
map.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,19 +9,19 @@
|
||||
/* オープンアドレス法ハッシュテーブル */
|
||||
class HashMapOpenAddressing {
|
||||
private:
|
||||
int size; // キー値ペアの数
|
||||
int capacity = 4; // ハッシュテーブルの容量
|
||||
const double loadThres = 2.0 / 3.0; // 拡張をトリガーする負荷率の閾値
|
||||
int size; // キーと値のペア数
|
||||
int capacity = 4; // ハッシュテーブル容量
|
||||
const double loadThres = 2.0 / 3.0; // リサイズを発動する負荷率のしきい値
|
||||
const int extendRatio = 2; // 拡張倍率
|
||||
vector<Pair *> buckets; // バケット配列
|
||||
Pair *TOMBSTONE = new Pair(-1, "-1"); // 削除マーク
|
||||
Pair *TOMBSTONE = new Pair(-1, "-1"); // 削除済みマーク
|
||||
|
||||
public:
|
||||
/* コンストラクタ */
|
||||
HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {
|
||||
}
|
||||
|
||||
/* デストラクタ */
|
||||
/* デストラクタメソッド */
|
||||
~HashMapOpenAddressing() {
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair != nullptr && pair != TOMBSTONE) {
|
||||
@@ -41,68 +41,68 @@ class HashMapOpenAddressing {
|
||||
return (double)size / capacity;
|
||||
}
|
||||
|
||||
/* keyに対応するバケットインデックスを検索 */
|
||||
/* key に対応するバケットインデックスを探す */
|
||||
int findBucket(int key) {
|
||||
int index = hashFunc(key);
|
||||
int firstTombstone = -1;
|
||||
// 線形探査、空のバケットに遭遇したら中断
|
||||
// 線形プロービングを行い、空バケットに達したら終了
|
||||
while (buckets[index] != nullptr) {
|
||||
// keyに遭遇した場合、対応するバケットインデックスを返却
|
||||
// key が見つかったら、対応するバケットのインデックスを返す
|
||||
if (buckets[index]->key == key) {
|
||||
// 以前に削除マークに遭遇していた場合、キー値ペアをそのインデックスに移動
|
||||
// 以前に削除マークが見つかっていれば、そのインデックスへキーと値のペアを移動
|
||||
if (firstTombstone != -1) {
|
||||
buckets[firstTombstone] = buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
return firstTombstone; // 移動されたバケットインデックスを返却
|
||||
return firstTombstone; // 移動後のバケットインデックスを返す
|
||||
}
|
||||
return index; // バケットインデックスを返却
|
||||
return index; // バケットのインデックスを返す
|
||||
}
|
||||
// 最初に遭遇した削除マークを記録
|
||||
// 最初に見つかった削除マークを記録
|
||||
if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {
|
||||
firstTombstone = index;
|
||||
}
|
||||
// バケットインデックスを計算、末尾を超えた場合は先頭に戻る
|
||||
// バケットのインデックスを計算し、末尾を越えたら先頭に戻る
|
||||
index = (index + 1) % capacity;
|
||||
}
|
||||
// keyが存在しない場合、挿入ポイントのインデックスを返却
|
||||
// key が存在しない場合は追加位置のインデックスを返す
|
||||
return firstTombstone == -1 ? index : firstTombstone;
|
||||
}
|
||||
|
||||
/* クエリ操作 */
|
||||
/* 検索操作 */
|
||||
string get(int key) {
|
||||
// keyに対応するバケットインデックスを検索
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、対応するvalを返却
|
||||
// キーと値の組が見つかったら、対応する val を返す
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index]->val;
|
||||
}
|
||||
// キー値ペアが存在しない場合、空文字列を返却
|
||||
// キーと値の組が存在しない場合は空文字列を返す
|
||||
return "";
|
||||
}
|
||||
|
||||
/* 追加操作 */
|
||||
void put(int key, string val) {
|
||||
// 負荷率が閾値を超えた場合、拡張を実行
|
||||
// 負荷率がしきい値を超えたら、リサイズを実行
|
||||
if (loadFactor() > loadThres) {
|
||||
extend();
|
||||
}
|
||||
// keyに対応するバケットインデックスを検索
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、valを上書きして返却
|
||||
// キーと値の組が見つかったら、val を上書きして返す
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
buckets[index]->val = val;
|
||||
return;
|
||||
}
|
||||
// キー値ペアが存在しない場合、キー値ペアを追加
|
||||
// キーと値の組が存在しない場合は、その組を追加する
|
||||
buckets[index] = new Pair(key, val);
|
||||
size++;
|
||||
}
|
||||
|
||||
/* 削除操作 */
|
||||
void remove(int key) {
|
||||
// keyに対応するバケットインデックスを検索
|
||||
// key に対応するバケットインデックスを探す
|
||||
int index = findBucket(key);
|
||||
// キー値ペアが見つかった場合、削除マークで覆う
|
||||
// キーと値の組が見つかったら、削除マーカーで上書きする
|
||||
if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {
|
||||
delete buckets[index];
|
||||
buckets[index] = TOMBSTONE;
|
||||
@@ -114,11 +114,11 @@ class HashMapOpenAddressing {
|
||||
void extend() {
|
||||
// 元のハッシュテーブルを一時保存
|
||||
vector<Pair *> bucketsTmp = buckets;
|
||||
// 拡張された新しいハッシュテーブルを初期化
|
||||
// リサイズ後の新しいハッシュテーブルを初期化
|
||||
capacity *= extendRatio;
|
||||
buckets = vector<Pair *>(capacity, nullptr);
|
||||
size = 0;
|
||||
// 元のハッシュテーブルから新しいハッシュテーブルにキー値ペアを移動
|
||||
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
|
||||
for (Pair *pair : bucketsTmp) {
|
||||
if (pair != nullptr && pair != TOMBSTONE) {
|
||||
put(pair->key, pair->val);
|
||||
@@ -127,7 +127,7 @@ class HashMapOpenAddressing {
|
||||
}
|
||||
}
|
||||
|
||||
/* ハッシュテーブルを印刷 */
|
||||
/* ハッシュテーブルを出力 */
|
||||
void print() {
|
||||
for (Pair *pair : buckets) {
|
||||
if (pair == nullptr) {
|
||||
@@ -141,31 +141,31 @@ class HashMapOpenAddressing {
|
||||
}
|
||||
};
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// ハッシュテーブルを初期化
|
||||
HashMapOpenAddressing hashmap;
|
||||
|
||||
// 追加操作
|
||||
// キー値ペア(key, val)をハッシュテーブルに追加
|
||||
hashmap.put(12836, "Ha");
|
||||
hashmap.put(15937, "Luo");
|
||||
hashmap.put(16750, "Suan");
|
||||
hashmap.put(13276, "Fa");
|
||||
hashmap.put(10583, "Ya");
|
||||
cout << "\nAfter adding, the hash table is\nKey -> Value" << endl;
|
||||
// ハッシュテーブルにキーと値の組 (key, val) を追加する
|
||||
hashmap.put(12836, "シャオハー");
|
||||
hashmap.put(15937, "シャオルオ");
|
||||
hashmap.put(16750, "シャオスワン");
|
||||
hashmap.put(13276, "シャオファー");
|
||||
hashmap.put(10583, "シャオヤー");
|
||||
cout << "\n追加完了後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
// クエリ操作
|
||||
// ハッシュテーブルにキーを入力、値valを取得
|
||||
// 検索操作
|
||||
// ハッシュテーブルにキー key を入力し、値 val を得る
|
||||
string name = hashmap.get(13276);
|
||||
cout << "\nEnter student ID 13276, found name " << name << endl;
|
||||
cout << "\n学籍番号 13276 を入力すると、氏名 " << name << endl;
|
||||
|
||||
// 削除操作
|
||||
// ハッシュテーブルからキー値ペア(key, val)を削除
|
||||
// ハッシュテーブルからキーと値の組 (key, val) を削除する
|
||||
hashmap.remove(16750);
|
||||
cout << "\nAfter removing 16750, the hash table is\nKey -> Value" << endl;
|
||||
cout << "\n16750 を削除した後、ハッシュテーブルは\nKey -> Value" << endl;
|
||||
hashmap.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ int mulHash(string key) {
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* XORハッシュ */
|
||||
/* XOR ハッシュ */
|
||||
int xorHash(string key) {
|
||||
int hash = 0;
|
||||
const int MODULUS = 1000000007;
|
||||
@@ -46,21 +46,21 @@ int rotHash(string key) {
|
||||
return (int)hash;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
string key = "Hello algorithm";
|
||||
string key = "Hello アルゴリズム";
|
||||
|
||||
int hash = addHash(key);
|
||||
cout << "Additive hash value is " << hash << endl;
|
||||
cout << "加算ハッシュ値は " << hash << endl;
|
||||
|
||||
hash = mulHash(key);
|
||||
cout << "Multiplicative hash value is " << hash << endl;
|
||||
cout << "乗算ハッシュ値は " << hash << endl;
|
||||
|
||||
hash = xorHash(key);
|
||||
cout << "XOR hash value is " << hash << endl;
|
||||
cout << "XORハッシュ値は " << hash << endl;
|
||||
|
||||
hash = rotHash(key);
|
||||
cout << "Rotational hash value is " << hash << endl;
|
||||
cout << "回転ハッシュ値は " << hash << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user