sudachi/analysis/
inner.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::analysis::node::{LatticeNode, RightId};
18use crate::dic::word_id::WordId;
19
20#[derive(Copy, Clone, Eq, PartialEq, Debug)]
21pub struct NodeIdx {
22    end: u16,
23    index: u16,
24}
25
26impl NodeIdx {
27    pub fn empty() -> NodeIdx {
28        NodeIdx {
29            end: u16::MAX,
30            index: u16::MAX,
31        }
32    }
33
34    pub fn new(end: u16, index: u16) -> NodeIdx {
35        NodeIdx { end, index }
36    }
37
38    pub fn end(&self) -> u16 {
39        self.end
40    }
41
42    pub fn index(&self) -> u16 {
43        self.index
44    }
45}
46
47#[derive(Clone, Debug)]
48pub struct Node {
49    begin: u16,
50    end: u16,
51    left_id: u16,
52    right_id: u16,
53    cost: i16,
54    word_id: WordId,
55}
56
57impl Node {
58    pub fn new(
59        begin: u16,
60        end: u16,
61        left_id: u16,
62        right_id: u16,
63        cost: i16,
64        word_id: WordId,
65    ) -> Node {
66        Node {
67            begin,
68            end,
69            left_id,
70            right_id,
71            cost,
72            word_id,
73        }
74    }
75
76    pub fn set_range(&mut self, begin: u16, end: u16) {
77        self.begin = begin;
78        self.end = end;
79    }
80}
81
82impl RightId for Node {
83    #[inline(always)]
84    fn right_id(&self) -> u16 {
85        self.right_id
86    }
87}
88
89impl LatticeNode for Node {
90    fn begin(&self) -> usize {
91        self.begin as usize
92    }
93
94    fn end(&self) -> usize {
95        self.end as usize
96    }
97
98    fn cost(&self) -> i16 {
99        self.cost
100    }
101
102    fn word_id(&self) -> WordId {
103        self.word_id
104    }
105
106    fn left_id(&self) -> u16 {
107        self.left_id
108    }
109}
110
111#[cfg(test)]
112mod test {
113    use super::*;
114    use claim::*;
115
116    #[test]
117    fn lesser_than_32b() {
118        assert_le!(core::mem::size_of::<Node>(), 32);
119    }
120}