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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#![allow(unused)]

use std::cmp::Ordering;
use std::fmt;
use std::fmt::Debug;
use std::hash::Hash;
use std::hash::Hasher;

use anyhow::Result;

use generic_array::ArrayLength;
use generic_array::GenericArray;

use crate::semirings::{
    DivideType, Semiring, SemiringProperties, WeaklyDivisibleSemiring, WeightQuantize,
};

/// Cartesian power semiring: W ^ n.
pub struct PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    weights: GenericArray<W, N>,
}

impl<W, N> fmt::Display for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.weights.as_slice().fmt(f)
    }
}

impl<W, N> fmt::Debug for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.weights.as_slice().fmt(f)
    }
}

impl<W, N> Hash for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.weights.as_slice().hash(state);
    }
}

impl<W, N> Clone for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn clone(&self) -> Self {
        PowerWeight {
            weights: self.weights.clone(),
        }
    }
}

impl<W, N> PartialOrd for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.weights.partial_cmp(&other.weights)
    }
}

impl<W, N> PartialEq for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn eq(&self, other: &Self) -> bool {
        self.weights.eq(&other.weights)
    }
}

impl<W, N> AsRef<Self> for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
    fn as_ref(&self) -> &PowerWeight<W, N> {
        self
    }
}

impl<W, N> Eq for PowerWeight<W, N>
where
    W: Semiring,
    N: ArrayLength<W>,
{
}

//impl<W, N> Semiring for PowerWeight<W, N>
//where
//    W: Semiring,
//    N: ArrayLength<W>,
//{
//    type Type = GenericArray<W, N>;
//    type ReverseSemiring<P> = PowerWeight<W::ReverseSemiring, P>;
//
//    fn zero() -> Self {
//        Self {
//            weights: GenericArray::clone_from_slice(&[W::zero()]),
//        }
//    }
//
//    fn one() -> Self {
//        Self {
//            weights: GenericArray::clone_from_slice(&[W::one()]),
//        }
//    }
//
//    fn new(value: <Self as Semiring>::Type) -> Self {
//        Self { weights: value }
//    }
//
//    fn plus_assign<P: AsRef<Self>>(&mut self, rhs: P) -> Result<()> {
//        for i in 0..self.weights.len() {
//            self.weights[i].plus_assign(&rhs.as_ref().weights[i])?;
//        }
//        Ok(())
//    }
//
//    fn times_assign<P: AsRef<Self>>(&mut self, rhs: P) -> Result<()> {
//        for i in 0..self.weights.len() {
//            self.weights[i].times_assign(&rhs.as_ref().weights[i])?;
//        }
//        Ok(())
//    }
//
//    fn value(&self) -> <Self as Semiring>::Type {
//        self.weights.clone()
//    }
//
//    fn set_value(&mut self, value: <Self as Semiring>::Type) {
//        self.weights = value;
//    }
//
//    fn reverse(&self) -> Self::ReverseSemiring {
//        let mut rw = Vec::with_capacity(self.weights.len());
//        for i in 0..self.weights.len() {
//            rw.push(self.weights[i].reverse());
//        }
//        PowerWeight::new(GenericArray::clone_from(rw))
//    }
//
//    fn properties() -> SemiringProperties {
//        W::properties()
//            & (SemiringProperties::LEFT_SEMIRING
//                | SemiringProperties::RIGHT_SEMIRING
//                | SemiringProperties::COMMUTATIVE
//                | SemiringProperties::IDEMPOTENT)
//    }
//}
//
//impl<W, N> WeaklyDivisibleSemiring for PowerWeight<W, N>
//where
//    W: WeaklyDivisibleSemiring,
//    N: ArrayLength<W>,
//{
//    fn divide(&self, rhs: &Self, divide_type: DivideType) -> Result<Self> {
//        let mut mul = self.clone();
//        for i in 0..self.weights.len() {
//            mul.weights[i] = self.weights[i].divide(&rhs.weights[i], divide_type)?;
//        }
//        Ok(mul)
//    }
//}
//
//impl<W, N> WeightQuantize for PowerWeight<W, N>
//where
//    W: WeightQuantize,
//    N: ArrayLength<W>,
//{
//    fn quantize_assign(&mut self, delta: f32) -> Result<()> {
//        for i in 0..self.weights.len() {
//            unsafe { self.weights.get_unchecked_mut(i).quantize_assign(delta)? };
//        }
//        Ok(())
//    }
//}