rshogi-core 0.1.6

A high-performance shogi engine core library with NNUE evaluation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
//! History統計
//!
//! 探索中の手の成功/失敗を記録し、手の順序付けに利用する。
//!
//! - `StatsEntry`: 範囲制限付き履歴エントリ
//! - `ButterflyHistory`: [Color][from_to] -> score
//! - `LowPlyHistory`: [ply][from_to] -> score
//! - `CapturePieceToHistory`: [piece][to][captured_pt] -> score
//! - `PieceToHistory`: [piece][to] -> score
//! - `ContinuationHistory`: [prev_pc][prev_to][pc][to] -> score
//! - `PawnHistory`: [pawn_key_idx][piece][to] -> score
//! - `CounterMoveHistory`: [piece][square] -> Move

use crate::types::{Color, Move, Piece, PieceType, Square};

use super::tt_history::TTMoveHistory;

// =============================================================================
// 定数
// =============================================================================

/// PawnHistoryのサイズ(2のべき乗)
pub const PAWN_HISTORY_SIZE: usize = 512;

/// CorrectionHistoryのサイズ(2のべき乗)
pub const CORRECTION_HISTORY_SIZE: usize = 32768;

/// CorrectionHistoryの値の制限
pub const CORRECTION_HISTORY_LIMIT: i32 = 1024;

/// LowPlyHistoryのサイズ(ルート付近のply数)
pub const LOW_PLY_HISTORY_SIZE: usize = 5;

/// from_toインデックスのサイズ
/// 将棋では from = SQUARE_NB + 0..6 で駒打ちを表す
/// (81マス + 7種の駒打ち) × 81マス
pub const FROM_TO_SIZE: usize = (Square::NUM + 7) * Square::NUM;

/// 駒種の数(PieceType::NUM相当)
const PIECE_TYPE_NUM: usize = PieceType::NUM + 1; // None含む

/// 駒の数(Piece::NUM相当、先後含む)
const PIECE_NUM: usize = Piece::NUM; // NONE含む

// =============================================================================
// YaneuraOu準拠定数
// =============================================================================

/// TTMoveHistory更新ボーナス(TT手がbest moveだった場合)
pub const TT_MOVE_HISTORY_BONUS: i32 = 811;

/// TTMoveHistory更新ペナルティ(TT手がbest moveでなかった場合)
pub const TT_MOVE_HISTORY_MALUS: i32 = -848;

/// ContinuationHistory更新の重み [(ply_back, weight)]
///
/// YaneuraOu準拠: 1,2,3,4,5,6手前の指し手と現在の指し手のペアを更新。
/// 王手中は1,2手前のみ更新。
pub const CONTINUATION_HISTORY_WEIGHTS: [(usize, i32); 6] =
    [(1, 1108), (2, 652), (3, 273), (4, 572), (5, 126), (6, 449)];

/// update_quiet_histories用のlowPlyHistory倍率
pub const LOW_PLY_HISTORY_MULTIPLIER: i32 = 771;
pub const LOW_PLY_HISTORY_OFFSET: i32 = 40;

/// update_quiet_histories用のcontinuationHistory倍率(正のボーナス時)
pub const CONTINUATION_HISTORY_POS_MULTIPLIER: i32 = 979;
/// update_quiet_histories用のcontinuationHistory倍率(負のボーナス時)
pub const CONTINUATION_HISTORY_NEG_MULTIPLIER: i32 = 842;

/// update_quiet_histories用のpawnHistory倍率(正のボーナス時)
pub const PAWN_HISTORY_POS_MULTIPLIER: i32 = 704;
/// update_quiet_histories用のpawnHistory倍率(負のボーナス時)
pub const PAWN_HISTORY_NEG_MULTIPLIER: i32 = 439;
/// update_quiet_histories用のpawnHistoryオフセット
pub const PAWN_HISTORY_OFFSET: i32 = 70;

/// ContinuationHistory近接ply(1,2手前)へのオフセット
/// YaneuraOu: update_continuation_histories で (bonus * weight / 1024) + 88 * (i < 2)
pub const CONTINUATION_HISTORY_NEAR_PLY_OFFSET: i32 = 88;

/// Prior Capture Countermove Bonus(fail low時の前の捕獲手へのボーナス)
/// YaneuraOu: yaneuraou-search.cpp:3976
pub const PRIOR_CAPTURE_COUNTERMOVE_BONUS: i32 = 964;

// =============================================================================
// StatsEntry
// =============================================================================

/// 履歴統計の1エントリ
///
/// 値の範囲を [-D, D] に制限しながら更新できる。
#[derive(Clone, Copy)]
pub struct StatsEntry<const D: i32> {
    value: i16,
}

impl<const D: i32> Default for StatsEntry<D> {
    fn default() -> Self {
        Self { value: 0 }
    }
}

impl<const D: i32> StatsEntry<D> {
    /// 値を取得
    #[inline]
    pub fn get(&self) -> i16 {
        self.value
    }

    /// 値を設定
    #[inline]
    pub fn set(&mut self, v: i16) {
        self.value = v;
    }

    /// ボーナス値を加算(範囲制限付き)
    ///
    /// 更新式: entry += clamp(bonus, -D, D) - entry * |clamp(bonus, -D, D)| / D
    ///
    /// この式の性質:
    /// - bonus == D のとき、entry が D に収束
    /// - bonus が小さいとき、ほぼそのまま加算
    /// - 値が D を超えないよう自動調整
    /// - 自然にゼロ方向に引っ張られる
    #[inline]
    pub fn update(&mut self, bonus: i32) {
        let clamped = bonus.clamp(-D, D);
        let delta = clamped - (self.value as i32) * clamped.abs() / D;
        self.value = (self.value as i32 + delta) as i16;
        debug_assert!(
            self.value.abs() <= D as i16,
            "StatsEntry out of range: {} (D={})",
            self.value,
            D
        );
    }
}

// =============================================================================
// ButterflyHistory
// =============================================================================

/// ButterflyHistory: [Color][from_to] -> score
///
/// 静かな手(quiet moves)の成功/失敗を記録。
/// 手の移動元と移動先でインデックス。
pub struct ButterflyHistory {
    table: [[StatsEntry<7183>; FROM_TO_SIZE]; Color::NUM],
}

impl ButterflyHistory {
    /// 新しいButterflyHistoryを作成
    pub fn new() -> Self {
        Self {
            table: [[StatsEntry::default(); FROM_TO_SIZE]; Color::NUM],
        }
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, color: Color, mv: Move) -> i16 {
        self.table[color.index()][mv.history_index()].get()
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, color: Color, mv: Move, bonus: i32) {
        self.table[color.index()][mv.history_index()].update(bonus);
    }

    /// クリア
    pub fn clear(&mut self) {
        for color_table in &mut self.table {
            for entry in color_table.iter_mut() {
                entry.set(0);
            }
        }
    }
}

impl Default for ButterflyHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// LowPlyHistory
// =============================================================================

/// LowPlyHistory: [ply][from_to] -> score
///
/// ルート付近での手の順序を改善するための履歴。
pub struct LowPlyHistory {
    table: [[StatsEntry<7183>; FROM_TO_SIZE]; LOW_PLY_HISTORY_SIZE],
}

impl LowPlyHistory {
    /// 新しいLowPlyHistoryを作成
    pub fn new() -> Self {
        Self {
            table: [[StatsEntry::default(); FROM_TO_SIZE]; LOW_PLY_HISTORY_SIZE],
        }
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, ply: usize, mv: Move) -> i16 {
        if ply < LOW_PLY_HISTORY_SIZE {
            self.table[ply][mv.history_index()].get()
        } else {
            0
        }
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, ply: usize, mv: Move, bonus: i32) {
        if ply < LOW_PLY_HISTORY_SIZE {
            self.table[ply][mv.history_index()].update(bonus);
        }
    }

    /// クリア
    pub fn clear(&mut self) {
        for ply_table in &mut self.table {
            for entry in ply_table.iter_mut() {
                entry.set(0);
            }
        }
    }
}

impl Default for LowPlyHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// CapturePieceToHistory
// =============================================================================

/// CapturePieceToHistory: [piece][to][captured_piece_type] -> score
///
/// 捕獲する手の履歴。
///
/// PERF: 約3.5MB。HistoryTables内の連続領域に配置するため配列で保持する。
pub struct CapturePieceToHistory {
    table: [[[StatsEntry<10692>; PIECE_TYPE_NUM]; Square::NUM]; PIECE_NUM],
}

impl CapturePieceToHistory {
    /// 新しいCapturePieceToHistoryを作成
    pub fn new() -> Self {
        Self {
            table: [[[StatsEntry::default(); PIECE_TYPE_NUM]; Square::NUM]; PIECE_NUM],
        }
    }

    /// 新しいCapturePieceToHistoryを作成(ヒープ確保)
    ///
    /// 大きな配列のスタック確保を避けるために `Box::new_zeroed` を使う。
    pub fn new_boxed() -> Box<Self> {
        // SAFETY: StatsEntryはi16のみで構成され、ゼロ初期化は常に有効。
        unsafe { Box::<Self>::new_zeroed().assume_init() }
    }

    /// YaneuraOu の `type_of(capturedPiece)` 相当。
    ///
    /// 捕獲された駒そのものを渡す版。下位4bit(0=NONE, 1..14=PieceType)を
    /// インデックスに使うため、呼び出し側で `piece_type()` を取らなくてよい。
    /// 捕獲がない場合(captured = NONE)は index=0 を使う。
    #[inline]
    pub fn get_with_captured_piece(&self, pc: Piece, to: Square, captured: Piece) -> i16 {
        let captured_idx = (captured.raw() & 0x0F) as usize;
        debug_assert!(
            captured_idx < PIECE_TYPE_NUM,
            "captured_idx {} out of bounds (PIECE_TYPE_NUM = {})",
            captured_idx,
            PIECE_TYPE_NUM
        );
        self.table[pc.index()][to.index()][captured_idx].get()
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, pc: Piece, to: Square, captured_pt: PieceType) -> i16 {
        self.table[pc.index()][to.index()][captured_pt as usize].get()
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, pc: Piece, to: Square, captured_pt: PieceType, bonus: i32) {
        self.table[pc.index()][to.index()][captured_pt as usize].update(bonus);
    }

    /// クリア
    pub fn clear(&mut self) {
        for pc_table in self.table.iter_mut() {
            for sq_table in pc_table.iter_mut() {
                for entry in sq_table.iter_mut() {
                    entry.set(0);
                }
            }
        }
    }
}

impl Default for CapturePieceToHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// PieceToHistory
// =============================================================================

/// PieceToHistory: [piece][to] -> score
///
/// 駒と移動先でインデックスする履歴。
#[derive(Clone)]
pub struct PieceToHistory {
    table: [[StatsEntry<30000>; Square::NUM]; PIECE_NUM],
}

impl PieceToHistory {
    /// 新しいPieceToHistoryを作成
    pub fn new() -> Self {
        Self {
            table: [[StatsEntry::default(); Square::NUM]; PIECE_NUM],
        }
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, pc: Piece, to: Square) -> i16 {
        self.table[pc.index()][to.index()].get()
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, pc: Piece, to: Square, bonus: i32) {
        self.table[pc.index()][to.index()].update(bonus);
    }

    /// クリア
    pub fn clear(&mut self) {
        for pc_table in &mut self.table {
            for entry in pc_table.iter_mut() {
                entry.set(0);
            }
        }
    }
}

impl Default for PieceToHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// ContinuationHistory
// =============================================================================

/// ContinuationHistory: [prev_piece][prev_to][piece][to] -> score
///
/// 連続する2手の組み合わせ履歴。
/// 1手前の駒と移動先から、現在の駒と移動先へのスコア。
///
/// PERF: 約1.3MBのサイズがあり、SearchWorkerでは[2][2]で4つ保持(計約5.2MB)。
/// HistoryTables内の連続領域に配置するため配列で保持する。
pub struct ContinuationHistory {
    table: [[PieceToHistory; Square::NUM]; PIECE_NUM],
}

impl ContinuationHistory {
    /// 新しいContinuationHistoryを作成
    pub fn new() -> Self {
        let table = std::array::from_fn(|_| std::array::from_fn(|_| PieceToHistory::new()));
        Self { table }
    }

    #[inline]
    pub fn get_table(&self, prev_pc: Piece, prev_to: Square) -> &PieceToHistory {
        &self.table[prev_pc.index()][prev_to.index()]
    }

    /// 内部テーブルへの可変参照を取得
    #[inline]
    pub fn get_table_mut(&mut self, prev_pc: Piece, prev_to: Square) -> &mut PieceToHistory {
        &mut self.table[prev_pc.index()][prev_to.index()]
    }

    /// 新しいContinuationHistoryを作成(ヒープ確保)
    ///
    /// 大きな配列のスタック確保を避けるために `Box::new_zeroed` を使う。
    pub fn new_boxed() -> Box<Self> {
        // SAFETY: PieceToHistoryはStatsEntry(i16)のみで構成され、ゼロ初期化は有効。
        unsafe { Box::<Self>::new_zeroed().assume_init() }
    }

    /// 1手前・2手前などの継続手を更新
    pub fn update_from_prev(
        &mut self,
        prev_pc: Piece,
        prev_to: Square,
        pc: Piece,
        to: Square,
        bonus: i32,
    ) {
        self.get_table_mut(prev_pc, prev_to).update(pc, to, bonus);
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, prev_pc: Piece, prev_to: Square, pc: Piece, to: Square) -> i16 {
        self.get_table(prev_pc, prev_to).get(pc, to)
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, prev_pc: Piece, prev_to: Square, pc: Piece, to: Square, bonus: i32) {
        self.get_table_mut(prev_pc, prev_to).update(pc, to, bonus);
    }

    /// クリア
    pub fn clear(&mut self) {
        for row in self.table.iter_mut() {
            for entry in row.iter_mut() {
                entry.clear();
            }
        }
    }
}

impl Default for ContinuationHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// PawnHistory
// =============================================================================

/// PawnHistory: [pawn_key_index][piece][to] -> score
///
/// 歩の陣形に対する履歴。
///
/// PERF: 約2.3MB。HistoryTables内の連続領域に配置するため配列で保持する。
pub struct PawnHistory {
    table: [[[StatsEntry<8192>; Square::NUM]; PIECE_NUM]; PAWN_HISTORY_SIZE],
}

impl PawnHistory {
    /// 新しいPawnHistoryを作成
    pub fn new() -> Self {
        let row = [[StatsEntry::default(); Square::NUM]; PIECE_NUM];
        Self {
            table: [row; PAWN_HISTORY_SIZE],
        }
    }

    /// 新しいPawnHistoryを作成(ヒープ確保)
    ///
    /// 大きな配列のスタック確保を避けるために `Box::new_zeroed` を使う。
    pub fn new_boxed() -> Box<Self> {
        // SAFETY: StatsEntryはi16のみで構成され、ゼロ初期化は常に有効。
        unsafe { Box::<Self>::new_zeroed().assume_init() }
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, pawn_key_index: usize, pc: Piece, to: Square) -> i16 {
        self.table[pawn_key_index][pc.index()][to.index()].get()
    }

    /// 値を更新
    #[inline]
    pub fn update(&mut self, pawn_key_index: usize, pc: Piece, to: Square, bonus: i32) {
        self.table[pawn_key_index][pc.index()][to.index()].update(bonus);
    }

    /// クリア
    pub fn clear(&mut self) {
        for pawn_table in self.table.iter_mut() {
            for pc_table in pawn_table.iter_mut() {
                for entry in pc_table.iter_mut() {
                    entry.set(0);
                }
            }
        }
    }
}

impl Default for PawnHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// CounterMoveHistory
// =============================================================================

/// CounterMoveHistory: [piece][square] -> Move
///
/// 直前の相手の手に対するカウンター手。
pub struct CounterMoveHistory {
    table: [[Move; Square::NUM]; PIECE_NUM],
}

impl CounterMoveHistory {
    /// 新しいCounterMoveHistoryを作成
    pub fn new() -> Self {
        Self {
            table: [[Move::NONE; Square::NUM]; PIECE_NUM],
        }
    }

    /// 値を取得
    #[inline]
    pub fn get(&self, pc: Piece, sq: Square) -> Move {
        self.table[pc.index()][sq.index()]
    }

    /// 値を設定
    #[inline]
    pub fn set(&mut self, pc: Piece, sq: Square, mv: Move) {
        self.table[pc.index()][sq.index()] = mv;
    }

    /// クリア
    pub fn clear(&mut self) {
        for pc_table in &mut self.table {
            for entry in pc_table.iter_mut() {
                *entry = Move::NONE;
            }
        }
    }
}

impl Default for CounterMoveHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// CorrectionHistory
// =============================================================================

/// CorrectionHistory (YaneuraOu準拠)
///
/// - Pawn/Minor: [key_index][color] -> correction
/// - NonPawn: [key_index][side_to_move][piece_color] -> correction
/// - Continuation: [prev_pc][prev_to][pc][to] -> correction
///
/// PERF: 約4.5MB。HistoryTables内の連続領域に配置するため配列で保持する。
pub struct CorrectionHistory {
    pawn: [[StatsEntry<CORRECTION_HISTORY_LIMIT>; Color::NUM]; CORRECTION_HISTORY_SIZE],
    minor: [[StatsEntry<CORRECTION_HISTORY_LIMIT>; Color::NUM]; CORRECTION_HISTORY_SIZE],
    non_pawn:
        [[[StatsEntry<CORRECTION_HISTORY_LIMIT>; Color::NUM]; Color::NUM]; CORRECTION_HISTORY_SIZE],
    continuation: [[[[StatsEntry<CORRECTION_HISTORY_LIMIT>; Square::NUM]; Piece::NUM]; Square::NUM];
        Piece::NUM],
}

impl CorrectionHistory {
    /// 新しいCorrectionHistoryを作成(初期値込み)
    pub fn new() -> Self {
        let mut history = Self {
            pawn: [[StatsEntry::default(); Color::NUM]; CORRECTION_HISTORY_SIZE],
            minor: [[StatsEntry::default(); Color::NUM]; CORRECTION_HISTORY_SIZE],
            non_pawn: [[[StatsEntry::default(); Color::NUM]; Color::NUM]; CORRECTION_HISTORY_SIZE],
            continuation: [[[[StatsEntry::default(); Square::NUM]; Piece::NUM]; Square::NUM];
                Piece::NUM],
        };
        history.fill_initial_values();
        history
    }

    /// 新しいCorrectionHistoryを作成(ヒープ確保)
    ///
    /// 大きな配列のスタック確保を避けるために `Box::new_zeroed` を使う。
    pub fn new_boxed() -> Box<Self> {
        // SAFETY: StatsEntryはi16のみで構成され、ゼロ初期化は常に有効。
        let mut history = unsafe { Box::<Self>::new_zeroed().assume_init() };
        history.fill_initial_values();
        history
    }

    /// 初期値で埋め直す
    pub fn clear(&mut self) {
        self.fill_initial_values();
    }

    fn fill_initial_values(&mut self) {
        for entry in self.pawn.iter_mut().flatten() {
            entry.set(5);
        }
        for entry in self.minor.iter_mut().flatten() {
            entry.set(0);
        }
        for entry in self.non_pawn.iter_mut().flatten().flatten() {
            entry.set(0);
        }
        for prev_pc in self.continuation.iter_mut() {
            for prev_to in prev_pc.iter_mut() {
                for entry in prev_to.iter_mut().flatten() {
                    entry.set(8);
                }
            }
        }
    }

    #[inline]
    pub fn pawn_value(&self, idx: usize, color: Color) -> i16 {
        self.pawn[idx % CORRECTION_HISTORY_SIZE][color.index()].get()
    }

    #[inline]
    pub fn update_pawn(&mut self, idx: usize, color: Color, bonus: i32) {
        self.pawn[idx % CORRECTION_HISTORY_SIZE][color.index()].update(bonus);
    }

    #[inline]
    pub fn minor_value(&self, idx: usize, color: Color) -> i16 {
        self.minor[idx % CORRECTION_HISTORY_SIZE][color.index()].get()
    }

    #[inline]
    pub fn update_minor(&mut self, idx: usize, color: Color, bonus: i32) {
        self.minor[idx % CORRECTION_HISTORY_SIZE][color.index()].update(bonus);
    }

    #[inline]
    pub fn non_pawn_value(&self, idx: usize, board_color: Color, stm: Color) -> i16 {
        self.non_pawn[idx % CORRECTION_HISTORY_SIZE][board_color.index()][stm.index()].get()
    }

    #[inline]
    pub fn update_non_pawn(&mut self, idx: usize, board_color: Color, stm: Color, bonus: i32) {
        self.non_pawn[idx % CORRECTION_HISTORY_SIZE][board_color.index()][stm.index()]
            .update(bonus);
    }

    #[inline]
    pub fn continuation_value(
        &self,
        prev_pc: Piece,
        prev_to: Square,
        pc: Piece,
        to: Square,
    ) -> i16 {
        self.continuation[prev_pc.index()][prev_to.index()][pc.index()][to.index()].get()
    }

    #[inline]
    pub fn update_continuation(
        &mut self,
        prev_pc: Piece,
        prev_to: Square,
        pc: Piece,
        to: Square,
        bonus: i32,
    ) {
        self.continuation[prev_pc.index()][prev_to.index()][pc.index()][to.index()].update(bonus);
    }
}

impl Default for CorrectionHistory {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// HistoryTables
// =============================================================================

/// 履歴/統計テーブルをまとめて保持するコンテナ
///
/// 大きな配列を単一のヒープ領域に配置し、MovePickerの参照経路を短縮する。
pub struct HistoryTables {
    pub main_history: ButterflyHistory,
    pub low_ply_history: LowPlyHistory,
    pub capture_history: CapturePieceToHistory,
    pub continuation_history: [[ContinuationHistory; 2]; 2],
    pub pawn_history: PawnHistory,
    pub correction_history: CorrectionHistory,
    pub tt_move_history: TTMoveHistory,
}

impl HistoryTables {
    /// 新しいHistoryTablesを作成(ヒープ確保)
    ///
    /// `Box::new_zeroed` で一括確保し、CorrectionHistoryのみ初期値を設定する。
    pub fn new_boxed() -> Box<Self> {
        // SAFETY: 各テーブルは数値型のみで構成され、ゼロ初期化は常に有効。
        let mut history = unsafe { Box::<Self>::new_zeroed().assume_init() };
        history.correction_history.fill_initial_values();
        history
    }

    /// すべての履歴テーブルをクリア
    pub fn clear(&mut self) {
        self.main_history.clear();
        self.low_ply_history.clear();
        self.capture_history.clear();
        for row in &mut self.continuation_history {
            for ch in row {
                ch.clear();
            }
        }
        self.pawn_history.clear();
        self.correction_history.clear();
        self.tt_move_history.clear();
    }
}

// =============================================================================
// ボーナス計算(YaneuraOu準拠)
// =============================================================================

/// History更新用のボーナスを計算
///
/// YaneuraOu準拠: `min(170*depth-87, 1598) + 332*(bestMove == ttMove)`
/// - `is_tt_move`: bestMoveがTT手と一致する場合はtrue
#[inline]
pub fn stat_bonus(depth: i32, is_tt_move: bool) -> i32 {
    let base = (170 * depth - 87).min(1598);
    if is_tt_move {
        base + 332
    } else {
        base
    }
}

/// Quiet手用のマイナスボーナス(ペナルティ)を計算
///
/// YaneuraOu準拠: `min(743*depth-180, 2287) - 33*quietsSearched.size()`
#[inline]
pub fn quiet_malus(depth: i32, quiets_count: usize) -> i32 {
    let base = (743 * depth - 180).min(2287);
    base - 33 * quiets_count as i32
}

/// 捕獲手用のマイナスボーナス(ペナルティ)を計算
///
/// YaneuraOu準拠: `min(708*depth-148, 2287) - 29*capturesSearched.size()`
#[inline]
pub fn capture_malus(depth: i32, captures_count: usize) -> i32 {
    let base = (708 * depth - 148).min(2287);
    base - 29 * captures_count as i32
}

// 後方互換性のため旧APIも残す(非推奨)
/// History更新用のボーナスを計算(旧API、非推奨)
#[deprecated(note = "Use stat_bonus(depth, is_tt_move) instead")]
#[inline]
pub fn stat_bonus_old(depth: i32) -> i32 {
    (130 * depth - 103).min(1652)
}

/// マイナスボーナス(ペナルティ)を計算(旧API、非推奨)
#[deprecated(note = "Use quiet_malus(depth, quiets_count) instead")]
#[inline]
pub fn stat_malus(depth: i32) -> i32 {
    (303 * depth - 273).min(1352)
}

// =============================================================================
// YaneuraOu準拠: 更新ヘルパー関数
// =============================================================================

/// LowPlyHistory用のボーナスを計算(YaneuraOu準拠)
#[inline]
pub fn low_ply_history_bonus(bonus: i32) -> i32 {
    bonus * LOW_PLY_HISTORY_MULTIPLIER / 1024 + LOW_PLY_HISTORY_OFFSET
}

/// ContinuationHistory用のボーナスを計算(YaneuraOu準拠)
///
/// 正のボーナスと負のボーナスで倍率が異なる。
#[inline]
pub fn continuation_history_bonus(bonus: i32) -> i32 {
    if bonus > 0 {
        bonus * CONTINUATION_HISTORY_POS_MULTIPLIER / 1024
    } else {
        bonus * CONTINUATION_HISTORY_NEG_MULTIPLIER / 1024
    }
}

/// ContinuationHistory近接ply(1,2手前)用のオフセット込みボーナスを計算
///
/// YaneuraOu準拠: オフセットは常に加算(負のボーナス時もペナルティを緩める)
/// `(bonus * weight / 1024) + 80 * (i < 2)`
#[inline]
pub fn continuation_history_bonus_with_offset(bonus: i32, ply_back: usize) -> i32 {
    let base = continuation_history_bonus(bonus);
    if ply_back <= 2 {
        // YaneuraOu: 負のボーナスでも+80を加算してペナルティを緩める
        base + CONTINUATION_HISTORY_NEAR_PLY_OFFSET
    } else {
        base
    }
}

/// PawnHistory用のボーナスを計算(YaneuraOu準拠)
///
/// YaneuraOu準拠: オフセットは常に加算(負のボーナス時もペナルティを緩める)
/// `(bonus * (bonus > 0 ? 704 : 439) / 1024) + 70`
#[inline]
pub fn pawn_history_bonus(bonus: i32) -> i32 {
    if bonus > 0 {
        bonus * PAWN_HISTORY_POS_MULTIPLIER / 1024 + PAWN_HISTORY_OFFSET
    } else {
        // YaneuraOu: 負のボーナスでも+70を加算してペナルティを緩める
        bonus * PAWN_HISTORY_NEG_MULTIPLIER / 1024 + PAWN_HISTORY_OFFSET
    }
}

// =============================================================================
// テスト
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_stats_entry_default() {
        let entry = StatsEntry::<1000>::default();
        assert_eq!(entry.get(), 0);
    }

    #[test]
    fn test_stats_entry_update_positive() {
        let mut entry = StatsEntry::<1000>::default();

        // ボーナスを加算
        entry.update(100);
        assert!(entry.get() > 0);
        assert!(entry.get() <= 1000);
    }

    #[test]
    fn test_stats_entry_update_convergence() {
        let mut entry = StatsEntry::<1000>::default();

        // 繰り返し更新してもDを超えない
        for _ in 0..100 {
            entry.update(1000);
        }
        assert!(entry.get() <= 1000);
        assert!(entry.get() > 900); // 収束に近づく
    }

    #[test]
    fn test_stats_entry_update_negative() {
        let mut entry = StatsEntry::<1000>::default();

        // マイナス方向
        for _ in 0..100 {
            entry.update(-1000);
        }
        assert!(entry.get() >= -1000);
        assert!(entry.get() < -900); // 収束に近づく
    }

    #[test]
    fn test_stats_entry_decay() {
        let mut entry = StatsEntry::<1000>::default();

        // 大きなボーナスで値を上げる
        for _ in 0..50 {
            entry.update(1000);
        }
        let high_value = entry.get();
        assert!(high_value > 0, "値が上がっているべき");

        // マイナスボーナスで徐々に減衰
        for _ in 0..5 {
            entry.update(-100);
        }
        let decayed_value = entry.get();

        // 減衰していることを確認(完全に0になる可能性もある)
        assert!(decayed_value < high_value, "減衰しているべき");
    }

    #[test]
    fn test_butterfly_history() {
        let mut history = ButterflyHistory::new();
        let mv = Move::from_usi("7g7f").unwrap();

        assert_eq!(history.get(Color::Black, mv), 0);

        history.update(Color::Black, mv, 100);
        assert!(history.get(Color::Black, mv) > 0);
        assert_eq!(history.get(Color::White, mv), 0); // 別の色は影響なし
    }

    #[test]
    fn test_low_ply_history() {
        let mut history = LowPlyHistory::new();
        let mv = Move::from_usi("7g7f").unwrap();

        history.update(0, mv, 100);
        assert!(history.get(0, mv) > 0);
        assert_eq!(history.get(1, mv), 0);

        // 範囲外のplyは0を返す
        assert_eq!(history.get(LOW_PLY_HISTORY_SIZE, mv), 0);
    }

    #[test]
    fn test_counter_move_history() {
        let mut history = CounterMoveHistory::new();
        let mv = Move::from_usi("7g7f").unwrap();
        let pc = Piece::B_PAWN;
        let sq = Square::SQ_55;

        assert!(history.get(pc, sq).is_none());

        history.set(pc, sq, mv);
        assert_eq!(history.get(pc, sq), mv);
    }

    #[test]
    fn test_stat_bonus() {
        // YaneuraOu準拠: min(170*depth-87, 1598) + 332*(is_tt_move)
        // depth=1, is_tt_move=false: 170*1-87 = 83
        assert_eq!(stat_bonus(1, false), 83);
        // depth=1, is_tt_move=true: 83 + 332 = 415
        assert_eq!(stat_bonus(1, true), 415);
        // depth=20: min(170*20-87, 1598) = min(3313, 1598) = 1598
        assert_eq!(stat_bonus(20, false), 1598);
        assert_eq!(stat_bonus(20, true), 1598 + 332);
    }

    #[test]
    fn test_quiet_malus() {
        // YaneuraOu準拠: min(743*depth-180, 2287) - 33*quiets_count
        // depth=1, quiets_count=0: 743*1-180 = 563
        assert_eq!(quiet_malus(1, 0), 563);
        // depth=1, quiets_count=10: 563 - 33*10 = 563 - 330 = 233
        assert_eq!(quiet_malus(1, 10), 233);
        // depth=10: min(743*10-180, 2287) = min(7250, 2287) = 2287
        assert_eq!(quiet_malus(10, 0), 2287);
    }

    #[test]
    fn test_capture_malus() {
        // YaneuraOu準拠: min(708*depth-148, 2287) - 29*captures_count
        // depth=1, captures_count=0: 708*1-148 = 560
        assert_eq!(capture_malus(1, 0), 560);
        // depth=1, captures_count=5: 560 - 29*5 = 560 - 145 = 415
        assert_eq!(capture_malus(1, 5), 415);
        // depth=10: min(708*10-148, 2287) = min(6932, 2287) = 2287
        assert_eq!(capture_malus(10, 0), 2287);
    }

    #[test]
    fn test_capture_piece_to_history_with_captured_piece() {
        let mut history = CapturePieceToHistory::new_boxed();
        let pc = Piece::B_GOLD;
        let to = Square::SQ_55;
        let captured = Piece::W_SILVER;

        // 初期値は0
        assert_eq!(history.get_with_captured_piece(pc, to, captured), 0);

        // 更新後、get/with_captured_pieceが同じ値を返す
        history.update(pc, to, captured.piece_type(), 100);
        assert_eq!(
            history.get_with_captured_piece(pc, to, captured),
            history.get(pc, to, captured.piece_type())
        );

        // NONEの場合はindex=0を使う
        let captured_none = Piece::NONE;
        assert_eq!(history.get_with_captured_piece(pc, to, captured_none), 0);
    }
}