Skip to main content

kiri_engine/
lib.rs

1pub mod dictionary;
2pub mod lattice;
3pub mod oov;
4pub mod shared;
5pub mod tokenizer;
6pub mod types;
7
8pub use crate::shared::DictData;
9use crate::types::ConnectionMatrix;
10
11/// Inhibit a connection by writing INHIBITED_CONNECTION (0x7fff) into the
12/// connection matrix region of the raw dictionary data.
13pub fn inhibit_connection_in_data(
14    data: &mut [u8],
15    connection: &ConnectionMatrix,
16    left_id: i16,
17    right_id: i16,
18) {
19    let index = right_id as u16 as usize * connection.left_size + left_id as u16 as usize;
20    let byte_offset = connection.data_offset + index * 2;
21    if byte_offset + 1 < data.len() {
22        let inhibited = 0x7fff_i16.to_le_bytes();
23        data[byte_offset] = inhibited[0];
24        data[byte_offset + 1] = inhibited[1];
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn test_inhibit_connection() {
34        // 2x2 connection matrix, all zeros initially
35        let mut data = vec![0u8; 8]; // 4 entries * 2 bytes each
36        let conn = ConnectionMatrix {
37            left_size: 2,
38            right_size: 2,
39            data_offset: 0,
40        };
41
42        // Inhibit connection (left=0, right=1) → index = 1*2+0 = 2, byte_offset = 4
43        inhibit_connection_in_data(&mut data, &conn, 0, 1);
44        let val = i16::from_le_bytes([data[4], data[5]]);
45        assert_eq!(val, 0x7fff);
46
47        // Verify other entries are untouched
48        let val0 = i16::from_le_bytes([data[0], data[1]]);
49        assert_eq!(val0, 0);
50    }
51}