lindera_core/
connection.rs1use std::borrow::Cow;
2
3use byteorder::{ByteOrder, LittleEndian};
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Serialize, Deserialize)]
7pub struct ConnectionCostMatrix {
8 pub costs_data: Cow<'static, [u8]>,
9 pub backward_size: u32,
10}
11
12impl ConnectionCostMatrix {
13 pub fn load_static(conn_data: &'static [u8]) -> ConnectionCostMatrix {
14 let backward_size = LittleEndian::read_i16(&conn_data[2..4]);
15 ConnectionCostMatrix {
16 costs_data: Cow::Borrowed(&conn_data[4..]),
17 backward_size: backward_size as u32,
18 }
19 }
20
21 pub fn load(conn_data: &[u8]) -> ConnectionCostMatrix {
22 let backward_size = LittleEndian::read_i16(&conn_data[2..4]);
23 ConnectionCostMatrix {
24 costs_data: Cow::Owned(conn_data[4..].to_vec()),
25 backward_size: backward_size as u32,
26 }
27 }
28
29 pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32 {
30 let cost_id = (backward_id + forward_id * self.backward_size) as usize;
31 LittleEndian::read_i16(&self.costs_data[cost_id * 2..]) as i32
32 }
33}