1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use std::sync::Arc;

use anyhow::{format_err, Result};

use crate::fst_impls::ConstFst;
use crate::fst_properties::FstProperties;
use crate::fst_traits::{CoreFst, Fst};
use crate::semirings::Semiring;
use crate::{SymbolTable, TrsConst};

impl<W: Semiring> Fst<W> for ConstFst<W> {
    fn input_symbols(&self) -> Option<&Arc<SymbolTable>> {
        self.isymt.as_ref()
    }

    fn output_symbols(&self) -> Option<&Arc<SymbolTable>> {
        self.osymt.as_ref()
    }

    fn set_input_symbols(&mut self, symt: Arc<SymbolTable>) {
        self.isymt = Some(symt)
    }

    fn set_output_symbols(&mut self, symt: Arc<SymbolTable>) {
        self.osymt = Some(symt);
    }

    fn take_input_symbols(&mut self) -> Option<Arc<SymbolTable>> {
        self.isymt.take()
    }

    fn take_output_symbols(&mut self) -> Option<Arc<SymbolTable>> {
        self.osymt.take()
    }
}

impl<W: Semiring> CoreFst<W> for ConstFst<W> {
    type TRS = TrsConst<W>;

    fn start(&self) -> Option<usize> {
        self.start
    }

    fn final_weight(&self, state_id: usize) -> Result<Option<W>> {
        let s = self
            .states
            .get(state_id)
            .ok_or_else(|| format_err!("State {:?} doesn't exist", state_id))?;
        Ok(s.final_weight.clone())
    }

    unsafe fn final_weight_unchecked(&self, state_id: usize) -> Option<W> {
        self.states.get_unchecked(state_id).final_weight.clone()
    }

    fn num_trs(&self, s: usize) -> Result<usize> {
        Ok(self
            .states
            .get(s)
            .ok_or_else(|| format_err!("State {:?} doesn't exist", s))?
            .ntrs)
    }

    unsafe fn num_trs_unchecked(&self, s: usize) -> usize {
        self.states.get_unchecked(s).ntrs
    }

    fn get_trs(&self, state_id: usize) -> Result<Self::TRS> {
        let state = self
            .states
            .get(state_id)
            .ok_or_else(|| format_err!("State {:?} doesn't exist", state_id))?;
        Ok(TrsConst {
            trs: Arc::clone(&self.trs),
            pos: state.pos,
            n: state.ntrs,
        })
    }

    unsafe fn get_trs_unchecked(&self, state_id: usize) -> Self::TRS {
        let state = self.states.get_unchecked(state_id);
        TrsConst {
            trs: Arc::clone(&self.trs),
            pos: state.pos,
            n: state.ntrs,
        }
    }

    fn properties(&self) -> FstProperties {
        self.properties
    }

    fn num_input_epsilons(&self, state: usize) -> Result<usize> {
        Ok(self
            .states
            .get(state)
            .ok_or_else(|| format_err!("State {:?} doesn't exist", state))?
            .niepsilons)
    }

    fn num_output_epsilons(&self, state: usize) -> Result<usize> {
        Ok(self
            .states
            .get(state)
            .ok_or_else(|| format_err!("State {:?} doesn't exist", state))?
            .noepsilons)
    }
}