rustfst/fst_impls/vector_fst/
allocable_fst.rs

1use crate::fst_impls::vector_fst::VectorFst;
2use crate::fst_traits::AllocableFst;
3use crate::semirings::Semiring;
4use crate::StateId;
5use anyhow::Result;
6use std::sync::Arc;
7
8impl<W: 'static + Semiring> AllocableFst<W> for VectorFst<W> {
9    fn reserve_trs(&mut self, source: StateId, additional: usize) -> Result<()> {
10        let trs = &mut self
11            .states
12            .get_mut(source as usize)
13            .ok_or_else(|| format_err!("State {:?} doesn't exist", source))?
14            .trs;
15
16        Arc::make_mut(&mut trs.0).reserve(additional);
17        Ok(())
18    }
19
20    #[inline]
21    unsafe fn reserve_trs_unchecked(&mut self, source: StateId, additional: usize) {
22        let trs = &mut self.states.get_unchecked_mut(source as usize).trs;
23        Arc::make_mut(&mut trs.0).reserve(additional)
24    }
25
26    #[inline]
27    fn reserve_states(&mut self, additional: usize) {
28        self.states.reserve(additional);
29    }
30
31    fn shrink_to_fit(&mut self) {
32        self.states.shrink_to_fit();
33        for state in self.states.iter_mut() {
34            Arc::make_mut(&mut state.trs.0).shrink_to_fit();
35        }
36    }
37
38    #[inline]
39    fn shrink_to_fit_states(&mut self) {
40        self.states.shrink_to_fit()
41    }
42
43    fn shrink_to_fit_trs(&mut self, source: StateId) -> Result<()> {
44        let trs = &mut self
45            .states
46            .get_mut(source as usize)
47            .ok_or_else(|| format_err!("State {:?} doesn't exist", source))?
48            .trs;
49        Arc::make_mut(&mut trs.0).shrink_to_fit();
50        Ok(())
51    }
52
53    #[inline]
54    unsafe fn shrink_to_fit_trs_unchecked(&mut self, source: StateId) {
55        Arc::make_mut(&mut self.states.get_unchecked_mut(source as usize).trs.0).shrink_to_fit()
56    }
57
58    #[inline]
59    fn states_capacity(&self) -> usize {
60        self.states.capacity()
61    }
62
63    fn trs_capacity(&self, source: StateId) -> Result<usize> {
64        Ok(self
65            .states
66            .get(source as usize)
67            .ok_or_else(|| format_err!("State {:?} doesn't exist", source))?
68            .trs
69            .0
70            .capacity())
71    }
72
73    #[inline]
74    unsafe fn trs_capacity_unchecked(&self, source: StateId) -> usize {
75        self.states.get_unchecked(source as usize).trs.0.capacity()
76    }
77}