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
@@ -0,0 +1,97 @@
// File: array_hash_map.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import "fmt"
/* キーと値の組 */
type pair struct {
key int
val string
}
/* 配列ベースのハッシュテーブル */
type arrayHashMap struct {
buckets []*pair
}
/* ハッシュテーブルを初期化 */
func newArrayHashMap() *arrayHashMap {
// 100 個のバケットを含む配列を初期化
buckets := make([]*pair, 100)
return &arrayHashMap{buckets: buckets}
}
/* ハッシュ関数 */
func (a *arrayHashMap) hashFunc(key int) int {
index := key % 100
return index
}
/* 検索操作 */
func (a *arrayHashMap) get(key int) string {
index := a.hashFunc(key)
pair := a.buckets[index]
if pair == nil {
return "Not Found"
}
return pair.val
}
/* 追加操作 */
func (a *arrayHashMap) put(key int, val string) {
pair := &pair{key: key, val: val}
index := a.hashFunc(key)
a.buckets[index] = pair
}
/* 削除操作 */
func (a *arrayHashMap) remove(key int) {
index := a.hashFunc(key)
// nil に設定し、削除を表す
a.buckets[index] = nil
}
/* すべてのキーのペアを取得する */
func (a *arrayHashMap) pairSet() []*pair {
var pairs []*pair
for _, pair := range a.buckets {
if pair != nil {
pairs = append(pairs, pair)
}
}
return pairs
}
/* すべてのキーを取得 */
func (a *arrayHashMap) keySet() []int {
var keys []int
for _, pair := range a.buckets {
if pair != nil {
keys = append(keys, pair.key)
}
}
return keys
}
/* すべての値を取得 */
func (a *arrayHashMap) valueSet() []string {
var values []string
for _, pair := range a.buckets {
if pair != nil {
values = append(values, pair.val)
}
}
return values
}
/* ハッシュテーブルを出力 */
func (a *arrayHashMap) print() {
for _, pair := range a.buckets {
if pair != nil {
fmt.Println(pair.key, "->", pair.val)
}
}
}
@@ -0,0 +1,52 @@
// File: array_hash_map_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"testing"
)
func TestArrayHashMap(t *testing.T) {
/* ハッシュテーブルを初期化 */
hmap := newArrayHashMap()
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
hmap.put(12836, "シャオハー")
hmap.put(15937, "シャオルオ")
hmap.put(16750, "シャオスワン")
hmap.put(13276, "シャオファー")
hmap.put(10583, "シャオヤ")
fmt.Println("\n追加後、ハッシュ表は\nKey -> Value")
hmap.print()
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
name := hmap.get(15937)
fmt.Println("\n学籍番号 15937 を入力し、見つかった名前は " + name)
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
hmap.remove(10583)
fmt.Println("\n10583 を削除後、ハッシュ表は\nKey -> Value")
hmap.print()
/* ハッシュテーブルを走査 */
fmt.Println("\nキーと値の組 Key->Value を走査")
for _, kv := range hmap.pairSet() {
fmt.Println(kv.key, " -> ", kv.val)
}
fmt.Println("\nキー Key を個別に走査")
for _, key := range hmap.keySet() {
fmt.Println(key)
}
fmt.Println("\n値 Value を個別に走査")
for _, val := range hmap.valueSet() {
fmt.Println(val)
}
}
@@ -0,0 +1,62 @@
// File: hash_collision_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"testing"
)
func TestHashMapChaining(t *testing.T) {
/* ハッシュテーブルを初期化 */
hmap := newHashMapChaining()
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
hmap.put(12836, "シャオハー")
hmap.put(15937, "シャオルオ")
hmap.put(16750, "シャオスワン")
hmap.put(13276, "シャオファー")
hmap.put(10583, "シャオヤ")
fmt.Println("\n追加後、ハッシュ表は\nKey -> Value")
hmap.print()
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
name := hmap.get(15937)
fmt.Println("\n学籍番号 15937 を入力し、見つかった名前は", name)
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
hmap.remove(12836)
fmt.Println("\n12836 を削除後、ハッシュ表は\nKey -> Value")
hmap.print()
}
func TestHashMapOpenAddressing(t *testing.T) {
/* ハッシュテーブルを初期化 */
hmap := newHashMapOpenAddressing()
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
hmap.put(12836, "シャオハー")
hmap.put(15937, "シャオルオ")
hmap.put(16750, "シャオスワン")
hmap.put(13276, "シャオファー")
hmap.put(10583, "シャオヤ")
fmt.Println("\n追加後、ハッシュ表は\nKey -> Value")
hmap.print()
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
name := hmap.get(13276)
fmt.Println("\n学籍番号 13276 を入力し、見つかった名前は ", name)
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
hmap.remove(16750)
fmt.Println("\n16750 を削除後、ハッシュ表は\nKey -> Value")
hmap.print()
}
@@ -0,0 +1,134 @@
// File: hash_map_chaining.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import (
"fmt"
"strconv"
"strings"
)
/* チェイン法ハッシュテーブル */
type hashMapChaining struct {
size int // キーと値のペア数
capacity int // ハッシュテーブル容量
loadThres float64 // リサイズを発動する負荷率のしきい値
extendRatio int // 拡張倍率
buckets [][]pair // バケット配列
}
/* コンストラクタ */
func newHashMapChaining() *hashMapChaining {
buckets := make([][]pair, 4)
for i := 0; i < 4; i++ {
buckets[i] = make([]pair, 0)
}
return &hashMapChaining{
size: 0,
capacity: 4,
loadThres: 2.0 / 3.0,
extendRatio: 2,
buckets: buckets,
}
}
/* ハッシュ関数 */
func (m *hashMapChaining) hashFunc(key int) int {
return key % m.capacity
}
/* 負荷率 */
func (m *hashMapChaining) loadFactor() float64 {
return float64(m.size) / float64(m.capacity)
}
/* 検索操作 */
func (m *hashMapChaining) get(key int) string {
idx := m.hashFunc(key)
bucket := m.buckets[idx]
// バケットを走査し、key が見つかれば対応する val を返す
for _, p := range bucket {
if p.key == key {
return p.val
}
}
// key が見つからない場合は空文字列を返す
return ""
}
/* 追加操作 */
func (m *hashMapChaining) put(key int, val string) {
// 負荷率がしきい値を超えたら、リサイズを実行
if m.loadFactor() > m.loadThres {
m.extend()
}
idx := m.hashFunc(key)
// バケットを走査し、指定した key が見つかれば対応する val を更新して返す
for i := range m.buckets[idx] {
if m.buckets[idx][i].key == key {
m.buckets[idx][i].val = val
return
}
}
// その key が存在しなければ、キーと値のペアを末尾に追加
p := pair{
key: key,
val: val,
}
m.buckets[idx] = append(m.buckets[idx], p)
m.size += 1
}
/* 削除操作 */
func (m *hashMapChaining) remove(key int) {
idx := m.hashFunc(key)
// バケットを走査してキーと値のペアを削除
for i, p := range m.buckets[idx] {
if p.key == key {
// スライスから削除する
m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)
m.size -= 1
break
}
}
}
/* ハッシュテーブルを拡張 */
func (m *hashMapChaining) extend() {
// 元のハッシュテーブルを一時保存
tmpBuckets := make([][]pair, len(m.buckets))
for i := 0; i < len(m.buckets); i++ {
tmpBuckets[i] = make([]pair, len(m.buckets[i]))
copy(tmpBuckets[i], m.buckets[i])
}
// リサイズ後の新しいハッシュテーブルを初期化
m.capacity *= m.extendRatio
m.buckets = make([][]pair, m.capacity)
for i := 0; i < m.capacity; i++ {
m.buckets[i] = make([]pair, 0)
}
m.size = 0
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for _, bucket := range tmpBuckets {
for _, p := range bucket {
m.put(p.key, p.val)
}
}
}
/* ハッシュテーブルを出力 */
func (m *hashMapChaining) print() {
var builder strings.Builder
for _, bucket := range m.buckets {
builder.WriteString("[")
for _, p := range bucket {
builder.WriteString(strconv.Itoa(p.key) + " -> " + p.val + " ")
}
builder.WriteString("]")
fmt.Println(builder.String())
builder.Reset()
}
}
@@ -0,0 +1,126 @@
// File: hash_map_open_addressing.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import (
"fmt"
)
/* オープンアドレス法ハッシュテーブル */
type hashMapOpenAddressing struct {
size int // キーと値のペア数
capacity int // ハッシュテーブル容量
loadThres float64 // リサイズを発動する負荷率のしきい値
extendRatio int // 拡張倍率
buckets []*pair // バケット配列
TOMBSTONE *pair // 削除済みマーク
}
/* コンストラクタ */
func newHashMapOpenAddressing() *hashMapOpenAddressing {
return &hashMapOpenAddressing{
size: 0,
capacity: 4,
loadThres: 2.0 / 3.0,
extendRatio: 2,
buckets: make([]*pair, 4),
TOMBSTONE: &pair{-1, "-1"},
}
}
/* ハッシュ関数 */
func (h *hashMapOpenAddressing) hashFunc(key int) int {
return key % h.capacity // キーに基づいてハッシュ値を計算
}
/* 負荷率 */
func (h *hashMapOpenAddressing) loadFactor() float64 {
return float64(h.size) / float64(h.capacity) // 現在の負荷率を計算
}
/* key に対応するバケットインデックスを探す */
func (h *hashMapOpenAddressing) findBucket(key int) int {
index := h.hashFunc(key) // 初期インデックスを取得
firstTombstone := -1 // 最初に遭遇した `TOMBSTONE` の位置を記録する
for h.buckets[index] != nil {
if h.buckets[index].key == key {
if firstTombstone != -1 {
// 以前に削除マークが見つかっていれば、そのインデックスへキーと値のペアを移動
h.buckets[firstTombstone] = h.buckets[index]
h.buckets[index] = h.TOMBSTONE
return firstTombstone // 移動後のバケットインデックスを返す
}
return index // 見つかったインデックスを返す
}
if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {
firstTombstone = index // 最初に遭遇した削除マークの位置を記録する
}
index = (index + 1) % h.capacity // 線形探索を行い、末尾を越えたら先頭に戻る
}
// key が存在しない場合は追加位置のインデックスを返す
if firstTombstone != -1 {
return firstTombstone
}
return index
}
/* 検索操作 */
func (h *hashMapOpenAddressing) get(key int) string {
index := h.findBucket(key) // key に対応するバケットインデックスを探す
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
return h.buckets[index].val // キーと値の組が見つかったら、対応する val を返す
}
return "" // キーと値のペアが存在しない場合は `""` を返す
}
/* 追加操作 */
func (h *hashMapOpenAddressing) put(key int, val string) {
if h.loadFactor() > h.loadThres {
h.extend() // 負荷率がしきい値を超えたら、リサイズを実行
}
index := h.findBucket(key) // key に対応するバケットインデックスを探す
if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {
h.buckets[index] = &pair{key, val} // キーと値の組が存在しない場合は、その組を追加する
h.size++
} else {
h.buckets[index].val = val // キーと値のペアが見つかった場合は、`val` を上書きする
}
}
/* 削除操作 */
func (h *hashMapOpenAddressing) remove(key int) {
index := h.findBucket(key) // key に対応するバケットインデックスを探す
if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {
h.buckets[index] = h.TOMBSTONE // キーと値の組が見つかったら、削除マーカーで上書きする
h.size--
}
}
/* ハッシュテーブルを拡張 */
func (h *hashMapOpenAddressing) extend() {
oldBuckets := h.buckets // 元のハッシュテーブルを一時保存
h.capacity *= h.extendRatio // 容量を更新
h.buckets = make([]*pair, h.capacity) // リサイズ後の新しいハッシュテーブルを初期化
h.size = 0 // サイズをリセットする
// キーと値のペアを元のハッシュテーブルから新しいハッシュテーブルへ移す
for _, pair := range oldBuckets {
if pair != nil && pair != h.TOMBSTONE {
h.put(pair.key, pair.val)
}
}
}
/* ハッシュテーブルを出力 */
func (h *hashMapOpenAddressing) print() {
for _, pair := range h.buckets {
if pair == nil {
fmt.Println("nil")
} else if pair == h.TOMBSTONE {
fmt.Println("TOMBSTONE")
} else {
fmt.Printf("%d -> %s\n", pair.key, pair.val)
}
}
}
@@ -0,0 +1,74 @@
// File: hash_map_test.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import (
"fmt"
"strconv"
"testing"
. "github.com/krahets/hello-algo/pkg"
)
func TestHashMap(t *testing.T) {
/* ハッシュテーブルを初期化 */
hmap := make(map[int]string)
/* 追加操作 */
// ハッシュテーブルにキーと値のペア (key, value) を追加
hmap[12836] = "シャオハー"
hmap[15937] = "シャオルオ"
hmap[16750] = "シャオスワン"
hmap[13276] = "シャオファー"
hmap[10583] = "シャオヤ"
fmt.Println("\n追加後、ハッシュ表は\nKey -> Value")
PrintMap(hmap)
/* 検索操作 */
// キー key をハッシュテーブルに渡し、値 value を取得
name := hmap[15937]
fmt.Println("\n学籍番号 15937 を入力し、見つかった名前は ", name)
/* 削除操作 */
// ハッシュテーブルからキーと値のペア (key, value) を削除
delete(hmap, 10583)
fmt.Println("\n10583 を削除後、ハッシュ表は\nKey -> Value")
PrintMap(hmap)
/* ハッシュテーブルを走査 */
// キーと値の組 key->value を走査する
fmt.Println("\nキーと値の組 Key->Value を走査")
for key, value := range hmap {
fmt.Println(key, "->", value)
}
// キー key のみを走査する
fmt.Println("\nキー Key を個別に走査")
for key := range hmap {
fmt.Println(key)
}
// 値 value のみを走査する
fmt.Println("\n値 Value を個別に走査")
for _, value := range hmap {
fmt.Println(value)
}
}
func TestSimpleHash(t *testing.T) {
var hash int
key := "Hello アルゴリズム"
hash = addHash(key)
fmt.Println("加算ハッシュ値は " + strconv.Itoa(hash))
hash = mulHash(key)
fmt.Println("乗算ハッシュ値は " + strconv.Itoa(hash))
hash = xorHash(key)
fmt.Println("排他的論理和ハッシュ値は " + strconv.Itoa(hash))
hash = rotHash(key)
fmt.Println("回転ハッシュ値は " + strconv.Itoa(hash))
}
@@ -0,0 +1,55 @@
// File: simple_hash.go
// Created Time: 2023-06-23
// Author: Reanon (793584285@qq.com)
package chapter_hashing
import "fmt"
/* 加算ハッシュ */
func addHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = (hash + int64(b)) % modulus
}
return int(hash)
}
/* 乗算ハッシュ */
func mulHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = (31*hash + int64(b)) % modulus
}
return int(hash)
}
/* XOR ハッシュ */
func xorHash(key string) int {
hash := 0
modulus := 1000000007
for _, b := range []byte(key) {
fmt.Println(int(b))
hash ^= int(b)
hash = (31*hash + int(b)) % modulus
}
return hash & modulus
}
/* 回転ハッシュ */
func rotHash(key string) int {
var hash int64
var modulus int64
modulus = 1000000007
for _, b := range []byte(key) {
hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus
}
return int(hash)
}