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
#[allow(clippy::len_without_is_empty)]
pub trait ArrayAdapter<N> {
fn len(&self) -> usize;
fn is_square(&self) -> bool;
fn get(&self, x: usize, y: usize) -> N;
}
#[cfg(feature = "ndarray")]
impl<A, N> ArrayAdapter<N> for ndarray::ArrayBase<A, ndarray::Ix2>
where
A: ndarray::Data<Elem = N>,
N: Copy,
{
#[inline]
fn len(&self) -> usize {
self.shape()[0]
}
#[inline]
fn is_square(&self) -> bool {
self.shape()[0] == self.shape()[1]
}
#[inline]
fn get(&self, x: usize, y: usize) -> N {
self[[x, y]]
}
}
#[derive(Debug, Clone)]
pub struct LowerTriangle<N> {
pub n: usize,
pub data: Vec<N>,
}
impl<N: Copy + num_traits::Zero> ArrayAdapter<N> for LowerTriangle<N> {
#[inline]
fn len(&self) -> usize {
self.n
}
#[inline]
fn is_square(&self) -> bool {
self.data.len() == (self.n * (self.n - 1)) >> 1
}
#[inline]
fn get(&self, x: usize, y: usize) -> N {
match x.cmp(&y) {
std::cmp::Ordering::Less => self.data[((y * (y - 1)) >> 1) + x],
std::cmp::Ordering::Greater => self.data[((x * (x - 1)) >> 1) + y],
std::cmp::Ordering::Equal => N::zero(),
}
}
}