lindera_dictionary/dictionary/
connection_cost_matrix.rs

1use crate::util::Data;
2
3use byteorder::{ByteOrder, LittleEndian};
4use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
5
6#[derive(Clone, Archive, RkyvSerialize, RkyvDeserialize)]
7
8pub struct ConnectionCostMatrix {
9    pub costs_data: Data,
10    pub backward_size: u32,
11}
12
13impl ConnectionCostMatrix {
14    pub fn load(conn_data: impl Into<Data>) -> ConnectionCostMatrix {
15        let conn_data = conn_data.into();
16        let backward_size = LittleEndian::read_i16(&conn_data[2..4]);
17        ConnectionCostMatrix {
18            costs_data: conn_data,
19            backward_size: backward_size as u32,
20        }
21    }
22
23    pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32 {
24        let cost_id = (backward_id + forward_id * self.backward_size) as usize;
25        LittleEndian::read_i16(&self.costs_data[4 + cost_id * 2..]) as i32
26    }
27}
28
29impl ArchivedConnectionCostMatrix {
30    pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32 {
31        let cost_id = (backward_id + forward_id * self.backward_size) as usize;
32        LittleEndian::read_i16(&self.costs_data.as_slice()[4 + cost_id * 2..]) as i32
33    }
34}