sudachi/dic/lexicon/
word_params.rs

1/*
2 * Copyright (c) 2021 Works Applications Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use crate::util::cow_array::CowArray;
18
19pub struct WordParams<'a> {
20    data: CowArray<'a, i16>,
21    size: u32,
22}
23
24impl<'a> WordParams<'a> {
25    const PARAM_SIZE: usize = 3;
26    const ELEMENT_SIZE: usize = 2 * Self::PARAM_SIZE;
27
28    pub fn new(bytes: &'a [u8], size: u32, offset: usize) -> WordParams {
29        let n_entries = size as usize * Self::PARAM_SIZE;
30        Self {
31            data: CowArray::from_bytes(bytes, offset, n_entries),
32            size,
33        }
34    }
35
36    pub fn storage_size(&self) -> usize {
37        4 + WordParams::ELEMENT_SIZE * self.size as usize
38    }
39
40    pub fn size(&self) -> u32 {
41        self.size
42    }
43
44    #[inline]
45    pub fn get_params(&self, word_id: u32) -> (i16, i16, i16) {
46        let begin = word_id as usize * Self::PARAM_SIZE;
47        let end = begin + Self::PARAM_SIZE;
48        let slice = &self.data[begin..end];
49        (slice[0], slice[1], slice[2])
50    }
51
52    pub fn get_cost(&self, word_id: u32) -> i16 {
53        let cost_offset = word_id as usize * Self::PARAM_SIZE + 2;
54        self.data[cost_offset]
55    }
56
57    pub fn set_cost(&mut self, word_id: u32, cost: i16) {
58        let cost_offset = word_id as usize * Self::PARAM_SIZE + 2;
59        self.data.set(cost_offset, cost)
60    }
61}