lindera_dictionary/loader/connection_cost_matrix.rs
1use std::path::Path;
2
3use crate::LinderaResult;
4use crate::dictionary::connection_cost_matrix::ConnectionCostMatrix;
5#[cfg(feature = "mmap")]
6use crate::util::mmap_file;
7use crate::util::read_file;
8
9/// Loader for connection cost matrix data from disk files.
10pub struct ConnectionCostMatrixLoader {}
11
12impl ConnectionCostMatrixLoader {
13 /// Load connection cost matrix from a file in the specified directory.
14 ///
15 /// # Arguments
16 ///
17 /// * `input_dir` - Path to the directory containing matrix.mtx.
18 ///
19 /// # Returns
20 ///
21 /// A `ConnectionCostMatrix` loaded from the file.
22 pub fn load(input_dir: &Path) -> LinderaResult<ConnectionCostMatrix> {
23 let data = read_file(input_dir.join("matrix.mtx").as_path())?;
24
25 ConnectionCostMatrix::load(data)
26 }
27
28 /// Load connection cost matrix using memory-mapped file.
29 ///
30 /// Note: mmap only avoids the initial file-read syscall/allocation.
31 /// [`ConnectionCostMatrix::load`] always eagerly decodes the whole buffer
32 /// into an owned `Vec<i16>` regardless of source (by design, to make the
33 /// hot-path `cost()` lookup a plain array index), so this does not make
34 /// the matrix lazily memory-resident at runtime.
35 ///
36 /// # Arguments
37 ///
38 /// * `input_dir` - Path to the directory containing matrix.mtx.
39 ///
40 /// # Returns
41 ///
42 /// A `ConnectionCostMatrix` loaded via memory mapping.
43 #[cfg(feature = "mmap")]
44 pub fn load_mmap(input_dir: &Path) -> LinderaResult<ConnectionCostMatrix> {
45 let data = mmap_file(input_dir.join("matrix.mtx").as_path())?;
46
47 ConnectionCostMatrix::load(data)
48 }
49}