rustfst/semirings/
power_weight.rs

1#![allow(unused)]
2
3use std::cmp::Ordering;
4use std::fmt;
5use std::fmt::Debug;
6use std::hash::Hash;
7use std::hash::Hasher;
8
9use anyhow::Result;
10
11use generic_array::ArrayLength;
12use generic_array::GenericArray;
13
14use crate::semirings::{
15    DivideType, Semiring, SemiringProperties, WeaklyDivisibleSemiring, WeightQuantize,
16};
17
18/// Cartesian power semiring: W ^ n.
19pub struct PowerWeight<W, N>
20where
21    W: Semiring,
22    N: ArrayLength<W>,
23{
24    weights: GenericArray<W, N>,
25}
26
27impl<W, N> fmt::Display for PowerWeight<W, N>
28where
29    W: Semiring,
30    N: ArrayLength<W>,
31{
32    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33        self.weights.as_slice().fmt(f)
34    }
35}
36
37impl<W, N> fmt::Debug for PowerWeight<W, N>
38where
39    W: Semiring,
40    N: ArrayLength<W>,
41{
42    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43        self.weights.as_slice().fmt(f)
44    }
45}
46
47impl<W, N> Hash for PowerWeight<W, N>
48where
49    W: Semiring,
50    N: ArrayLength<W>,
51{
52    fn hash<H: Hasher>(&self, state: &mut H) {
53        self.weights.as_slice().hash(state);
54    }
55}
56
57impl<W, N> Clone for PowerWeight<W, N>
58where
59    W: Semiring,
60    N: ArrayLength<W>,
61{
62    fn clone(&self) -> Self {
63        PowerWeight {
64            weights: self.weights.clone(),
65        }
66    }
67}
68
69impl<W, N> PartialOrd for PowerWeight<W, N>
70where
71    W: Semiring,
72    N: ArrayLength<W>,
73{
74    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
75        self.weights.partial_cmp(&other.weights)
76    }
77}
78
79impl<W, N> PartialEq for PowerWeight<W, N>
80where
81    W: Semiring,
82    N: ArrayLength<W>,
83{
84    fn eq(&self, other: &Self) -> bool {
85        self.weights.eq(&other.weights)
86    }
87}
88
89impl<W, N> AsRef<Self> for PowerWeight<W, N>
90where
91    W: Semiring,
92    N: ArrayLength<W>,
93{
94    fn as_ref(&self) -> &PowerWeight<W, N> {
95        self
96    }
97}
98
99impl<W, N> Eq for PowerWeight<W, N>
100where
101    W: Semiring,
102    N: ArrayLength<W>,
103{
104}
105
106//impl<W, N> Semiring for PowerWeight<W, N>
107//where
108//    W: Semiring,
109//    N: ArrayLength<W>,
110//{
111//    type Type = GenericArray<W, N>;
112//    type ReverseSemiring<P> = PowerWeight<W::ReverseSemiring, P>;
113//
114//    fn zero() -> Self {
115//        Self {
116//            weights: GenericArray::clone_from_slice(&[W::zero()]),
117//        }
118//    }
119//
120//    fn one() -> Self {
121//        Self {
122//            weights: GenericArray::clone_from_slice(&[W::one()]),
123//        }
124//    }
125//
126//    fn new(value: <Self as Semiring>::Type) -> Self {
127//        Self { weights: value }
128//    }
129//
130//    fn plus_assign<P: AsRef<Self>>(&mut self, rhs: P) -> Result<()> {
131//        for i in 0..self.weights.len() {
132//            self.weights[i].plus_assign(&rhs.as_ref().weights[i])?;
133//        }
134//        Ok(())
135//    }
136//
137//    fn times_assign<P: AsRef<Self>>(&mut self, rhs: P) -> Result<()> {
138//        for i in 0..self.weights.len() {
139//            self.weights[i].times_assign(&rhs.as_ref().weights[i])?;
140//        }
141//        Ok(())
142//    }
143//
144//    fn value(&self) -> <Self as Semiring>::Type {
145//        self.weights.clone()
146//    }
147//
148//    fn set_value(&mut self, value: <Self as Semiring>::Type) {
149//        self.weights = value;
150//    }
151//
152//    fn reverse(&self) -> Self::ReverseSemiring {
153//        let mut rw = Vec::with_capacity(self.weights.len());
154//        for i in 0..self.weights.len() {
155//            rw.push(self.weights[i].reverse());
156//        }
157//        PowerWeight::new(GenericArray::clone_from(rw))
158//    }
159//
160//    fn properties() -> SemiringProperties {
161//        W::properties()
162//            & (SemiringProperties::LEFT_SEMIRING
163//                | SemiringProperties::RIGHT_SEMIRING
164//                | SemiringProperties::COMMUTATIVE
165//                | SemiringProperties::IDEMPOTENT)
166//    }
167//}
168//
169//impl<W, N> WeaklyDivisibleSemiring for PowerWeight<W, N>
170//where
171//    W: WeaklyDivisibleSemiring,
172//    N: ArrayLength<W>,
173//{
174//    fn divide(&self, rhs: &Self, divide_type: DivideType) -> Result<Self> {
175//        let mut mul = self.clone();
176//        for i in 0..self.weights.len() {
177//            mul.weights[i] = self.weights[i].divide(&rhs.weights[i], divide_type)?;
178//        }
179//        Ok(mul)
180//    }
181//}
182//
183//impl<W, N> WeightQuantize for PowerWeight<W, N>
184//where
185//    W: WeightQuantize,
186//    N: ArrayLength<W>,
187//{
188//    fn quantize_assign(&mut self, delta: f32) -> Result<()> {
189//        for i in 0..self.weights.len() {
190//            unsafe { self.weights.get_unchecked_mut(i).quantize_assign(delta)? };
191//        }
192//        Ok(())
193//    }
194//}