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:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions

View File

@@ -0,0 +1,128 @@
/**
* File: array_hash_map.js
* Created Time: 2022-12-26
* Author: Justin (xiefahit@gmail.com)
*/
/* キーと値の組 Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* 配列ベースのハッシュテーブル */
class ArrayHashMap {
#buckets;
constructor() {
// 100 個のバケットを含む配列を初期化
this.#buckets = new Array(100).fill(null);
}
/* ハッシュ関数 */
#hashFunc(key) {
return key % 100;
}
/* 検索操作 */
get(key) {
let index = this.#hashFunc(key);
let pair = this.#buckets[index];
if (pair === null) return null;
return pair.val;
}
/* 追加操作 */
set(key, val) {
let index = this.#hashFunc(key);
this.#buckets[index] = new Pair(key, val);
}
/* 削除操作 */
delete(key) {
let index = this.#hashFunc(key);
// null に設定し、削除を表す
this.#buckets[index] = null;
}
/* すべてのキーと値のペアを取得 */
entries() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i]);
}
}
return arr;
}
/* すべてのキーを取得 */
keys() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i].key);
}
}
return arr;
}
/* すべての値を取得 */
values() {
let arr = [];
for (let i = 0; i < this.#buckets.length; i++) {
if (this.#buckets[i]) {
arr.push(this.#buckets[i].val);
}
}
return arr;
}
/* ハッシュテーブルを出力 */
print() {
let pairSet = this.entries();
for (const pair of pairSet) {
console.info(`${pair.key} -> ${pair.val}`);
}
}
}
/* Driver Code */
/* ハッシュテーブルを初期化 */
const map = new ArrayHashMap();
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
map.set(12836, 'シャオハー');
map.set(15937, 'シャオルオ');
map.set(16750, 'シャオスワン');
map.set(13276, 'シャオファー');
map.set(10583, 'シャオヤー');
console.info('\n追加完了後、ハッシュ表は\nKey -> Value');
map.print();
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
let name = map.get(15937);
console.info('\n学籍番号 15937 を入力すると、氏名 ' + name);
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
map.delete(10583);
console.info('\n10583 を削除した後、ハッシュ表は\nKey -> Value');
map.print();
/* ハッシュテーブルを走査 */
console.info('\nキーと値のペア Key->Value を走査');
for (const pair of map.entries()) {
if (!pair) continue;
console.info(pair.key + ' -> ' + pair.val);
}
console.info('\nキー Key を個別に走査');
for (const key of map.keys()) {
console.info(key);
}
console.info('\n値 Value を個別に走査');
for (const val of map.values()) {
console.info(val);
}

View File

@@ -0,0 +1,44 @@
/**
* File: hash_map.js
* Created Time: 2022-12-26
* Author: Justin (xiefahit@gmail.com)
*/
/* Driver Code */
/* ハッシュテーブルを初期化 */
const map = new Map();
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
map.set(12836, 'シャオハー');
map.set(15937, 'シャオルオ');
map.set(16750, 'シャオスワン');
map.set(13276, 'シャオファー');
map.set(10583, 'シャオヤー');
console.info('\n追加完了後、ハッシュ表は\nKey -> Value');
console.info(map);
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
let name = map.get(15937);
console.info('\n学籍番号 15937 を入力すると、氏名 ' + name);
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
map.delete(10583);
console.info('\n10583 を削除した後、ハッシュ表は\nKey -> Value');
console.info(map);
/* ハッシュテーブルを走査 */
console.info('\nキーと値のペア Key->Value を走査');
for (const [k, v] of map.entries()) {
console.info(k + ' -> ' + v);
}
console.info('\nキー Key を個別に走査');
for (const k of map.keys()) {
console.info(k);
}
console.info('\n値 Value を個別に走査');
for (const v of map.values()) {
console.info(v);
}

View File

@@ -0,0 +1,142 @@
/**
* File: hash_map_chaining.js
* Created Time: 2023-08-06
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* キーと値の組 Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* チェイン法ハッシュテーブル */
class HashMapChaining {
#size; // キーと値のペア数
#capacity; // ハッシュテーブル容量
#loadThres; // リサイズを発動する負荷率のしきい値
#extendRatio; // 拡張倍率
#buckets; // バケット配列
/* コンストラクタ */
constructor() {
this.#size = 0;
this.#capacity = 4;
this.#loadThres = 2.0 / 3.0;
this.#extendRatio = 2;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
}
/* ハッシュ関数 */
#hashFunc(key) {
return key % this.#capacity;
}
/* 負荷率 */
#loadFactor() {
return this.#size / this.#capacity;
}
/* 検索操作 */
get(key) {
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// バケットを走査し、key が見つかれば対応する val を返す
for (const pair of bucket) {
if (pair.key === key) {
return pair.val;
}
}
// key が見つからない場合は null を返す
return null;
}
/* 追加操作 */
put(key, val) {
// 負荷率がしきい値を超えたら、リサイズを実行
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
const index = this.#hashFunc(key);
const bucket = this.#buckets[index];
// バケットを走査し、指定した key が見つかれば対応する val を更新して返す
for (const pair of bucket) {
if (pair.key === key) {
pair.val = val;
return;
}
}
// その key が存在しなければ、キーと値のペアを末尾に追加
const pair = new Pair(key, val);
bucket.push(pair);
this.#size++;
}
/* 削除操作 */
remove(key) {
const index = this.#hashFunc(key);
let bucket = this.#buckets[index];
// バケットを走査してキーと値のペアを削除
for (let i = 0; i < bucket.length; i++) {
if (bucket[i].key === key) {
bucket.splice(i, 1);
this.#size--;
break;
}
}
}
/* ハッシュテーブルを拡張 */
#extend() {
// 元のハッシュテーブルを一時保存
const bucketsTmp = this.#buckets;
// リサイズ後の新しいハッシュテーブルを初期化
this.#capacity *= this.#extendRatio;
this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);
this.#size = 0;
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for (const bucket of bucketsTmp) {
for (const pair of bucket) {
this.put(pair.key, pair.val);
}
}
}
/* ハッシュテーブルを出力 */
print() {
for (const bucket of this.#buckets) {
let res = [];
for (const pair of bucket) {
res.push(pair.key + ' -> ' + pair.val);
}
console.log(res);
}
}
}
/* Driver Code */
/* ハッシュテーブルを初期化 */
const map = new HashMapChaining();
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
map.put(12836, 'シャオハー');
map.put(15937, 'シャオルオ');
map.put(16750, 'シャオスワン');
map.put(13276, 'シャオファー');
map.put(10583, 'シャオヤー');
console.log('\n追加完了後、ハッシュ表は\nKey -> Value');
map.print();
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
const name = map.get(13276);
console.log('\n学籍番号 13276 を入力すると、名前 ' + name);
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
map.remove(12836);
console.log('\n12836 を削除した後、ハッシュテーブルは\nKey -> Value');
map.print();

View File

@@ -0,0 +1,177 @@
/**
* File: hashMapOpenAddressing.js
* Created Time: 2023-06-13
* Author: yuan0221 (yl1452491917@gmail.com), krahets (krahets@163.com)
*/
/* キーと値の組 Number -> String */
class Pair {
constructor(key, val) {
this.key = key;
this.val = val;
}
}
/* オープンアドレス法ハッシュテーブル */
class HashMapOpenAddressing {
#size; // キーと値のペア数
#capacity; // ハッシュテーブル容量
#loadThres; // リサイズを発動する負荷率のしきい値
#extendRatio; // 拡張倍率
#buckets; // バケット配列
#TOMBSTONE; // 削除済みマーク
/* コンストラクタ */
constructor() {
this.#size = 0; // キーと値のペア数
this.#capacity = 4; // ハッシュテーブル容量
this.#loadThres = 2.0 / 3.0; // リサイズを発動する負荷率のしきい値
this.#extendRatio = 2; // 拡張倍率
this.#buckets = Array(this.#capacity).fill(null); // バケット配列
this.#TOMBSTONE = new Pair(-1, '-1'); // 削除済みマーク
}
/* ハッシュ関数 */
#hashFunc(key) {
return key % this.#capacity;
}
/* 負荷率 */
#loadFactor() {
return this.#size / this.#capacity;
}
/* key に対応するバケットインデックスを探す */
#findBucket(key) {
let index = this.#hashFunc(key);
let firstTombstone = -1;
// 線形プロービングを行い、空バケットに達したら終了
while (this.#buckets[index] !== null) {
// key が見つかったら、対応するバケットのインデックスを返す
if (this.#buckets[index].key === key) {
// 以前に削除マークが見つかっていれば、そのインデックスへキーと値のペアを移動
if (firstTombstone !== -1) {
this.#buckets[firstTombstone] = this.#buckets[index];
this.#buckets[index] = this.#TOMBSTONE;
return firstTombstone; // 移動後のバケットインデックスを返す
}
return index; // バケットのインデックスを返す
}
// 最初に見つかった削除マークを記録
if (
firstTombstone === -1 &&
this.#buckets[index] === this.#TOMBSTONE
) {
firstTombstone = index;
}
// バケットのインデックスを計算し、末尾を越えたら先頭に戻る
index = (index + 1) % this.#capacity;
}
// key が存在しない場合は追加位置のインデックスを返す
return firstTombstone === -1 ? index : firstTombstone;
}
/* 検索操作 */
get(key) {
// key に対応するバケットインデックスを探す
const index = this.#findBucket(key);
// キーと値の組が見つかったら、対応する val を返す
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
return this.#buckets[index].val;
}
// キーと値の組が存在しなければ null を返す
return null;
}
/* 追加操作 */
put(key, val) {
// 負荷率がしきい値を超えたら、リサイズを実行
if (this.#loadFactor() > this.#loadThres) {
this.#extend();
}
// key に対応するバケットインデックスを探す
const index = this.#findBucket(key);
// キーと値の組が見つかったら、val を上書きして返す
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
this.#buckets[index].val = val;
return;
}
// キーと値の組が存在しない場合は、その組を追加する
this.#buckets[index] = new Pair(key, val);
this.#size++;
}
/* 削除操作 */
remove(key) {
// key に対応するバケットインデックスを探す
const index = this.#findBucket(key);
// キーと値の組が見つかったら、削除マーカーで上書きする
if (
this.#buckets[index] !== null &&
this.#buckets[index] !== this.#TOMBSTONE
) {
this.#buckets[index] = this.#TOMBSTONE;
this.#size--;
}
}
/* ハッシュテーブルを拡張 */
#extend() {
// 元のハッシュテーブルを一時保存
const bucketsTmp = this.#buckets;
// リサイズ後の新しいハッシュテーブルを初期化
this.#capacity *= this.#extendRatio;
this.#buckets = Array(this.#capacity).fill(null);
this.#size = 0;
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for (const pair of bucketsTmp) {
if (pair !== null && pair !== this.#TOMBSTONE) {
this.put(pair.key, pair.val);
}
}
}
/* ハッシュテーブルを出力 */
print() {
for (const pair of this.#buckets) {
if (pair === null) {
console.log('null');
} else if (pair === this.#TOMBSTONE) {
console.log('TOMBSTONE');
} else {
console.log(pair.key + ' -> ' + pair.val);
}
}
}
}
/* Driver Code */
// ハッシュテーブルを初期化
const hashmap = new HashMapOpenAddressing();
// 追加操作
// ハッシュテーブルにキーと値の組 (key, val) を追加する
hashmap.put(12836, 'シャオハー');
hashmap.put(15937, 'シャオルオ');
hashmap.put(16750, 'シャオスワン');
hashmap.put(13276, 'シャオファー');
hashmap.put(10583, 'シャオヤー');
console.log('\n追加完了後、ハッシュ表は\nKey -> Value');
hashmap.print();
// 検索操作
// ハッシュテーブルにキー key を入力し、値 val を得る
const name = hashmap.get(13276);
console.log('\n学籍番号 13276 を入力すると、名前 ' + name);
// 削除操作
// ハッシュテーブルからキーと値の組 (key, val) を削除する
hashmap.remove(16750);
console.log('\n16750 を削除した後、ハッシュテーブルは\nKey -> Value');
hashmap.print();

View File

@@ -0,0 +1,60 @@
/**
* File: simple_hash.js
* Created Time: 2023-08-06
* Author: yuan0221 (yl1452491917@gmail.com)
*/
/* 加算ハッシュ */
function addHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* 乗算ハッシュ */
function mulHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = (31 * hash + c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* XOR ハッシュ */
function xorHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash ^= c.charCodeAt(0);
}
return hash % MODULUS;
}
/* 回転ハッシュ */
function rotHash(key) {
let hash = 0;
const MODULUS = 1000000007;
for (const c of key) {
hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;
}
return hash;
}
/* Driver Code */
const key = 'Hello アルゴリズム';
let hash = addHash(key);
console.log('加算ハッシュ値は ' + hash);
hash = mulHash(key);
console.log('乗算ハッシュ値は ' + hash);
hash = xorHash(key);
console.log('XORハッシュ値は ' + hash);
hash = rotHash(key);
console.log('回転ハッシュ値は ' + hash);