Skip to content

Commit 1f87b64

Browse files
authored
Merge pull request #38 from lf-lang/rust-childref-benches
Rust benchmarks that depend on childport references
2 parents be68640 + cdc2338 commit 1f87b64

File tree

4 files changed

+283
-271
lines changed

4 files changed

+283
-271
lines changed

Rust/Savina/src/lib/matrix.rs

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@
2323
*
2424
* @author Johannes Hayeß
2525
*/
26+
#![allow(dead_code)]
2627

2728
use std::ops::Add;
29+
use std::fmt;
2830

29-
#[derive(Default, Debug, Clone)]
31+
#[derive(Debug, Clone)]
3032
pub struct Matrix<T> {
3133
data: Vec<T>,
34+
size_x: usize,
3235
size_y: usize,
3336
}
3437

35-
#[derive(Default)]
36-
pub struct TransposedMatrix<T> {
37-
data: Vec<T>,
38-
size_x: usize,
39-
}
38+
pub struct TransposedMatrix<T>(Matrix<T>);
4039

41-
impl<T: Default + Clone + Copy> Matrix<T> {
42-
pub fn new(size_x: usize, size_y: usize) -> Self {
40+
impl<T> Matrix<T> {
41+
pub fn new(size_x: usize, size_y: usize) -> Self where T: Default + Clone {
4342
Matrix::<T> {
4443
data: vec![T::default(); size_x * size_y],
44+
size_x,
4545
size_y,
4646
}
4747
}
@@ -53,39 +53,58 @@ impl<T: Default + Clone + Copy> Matrix<T> {
5353
pub fn set(&mut self, x: usize, y: usize, value: T) {
5454
self.data[x * self.size_y + y] = value;
5555
}
56+
57+
pub fn transpose(self) -> TransposedMatrix<T> {
58+
TransposedMatrix(self)
59+
}
5660
}
5761

5862
pub fn matrix_sum<T>(matrices: &[Matrix<T>]) -> Matrix<T>
59-
where
60-
T: Default + Clone + Copy + Add<Output = T>,
63+
where
64+
T: Default + Clone + Copy + Add<Output=T>,
6165
{
62-
let size_x = matrices[0].data.len() / matrices[0].size_y;
66+
let size_x = matrices[0].size_x;
6367
let size_y = matrices[0].size_y;
6468
let mut result = Matrix::<T>::new(size_x, size_y);
6569
for x in 0..size_x {
6670
for y in 0..size_y {
67-
result.data[y * size_x + x] = matrices
68-
.iter()
69-
.fold(T::default(), |acc, m| acc + m.data[y * size_x + x])
71+
for m in matrices {
72+
result.set(x, y, *result.get(x, y) + *m.get(x, y))
73+
}
7074
}
7175
}
7276

7377
result
7478
}
7579

76-
impl<T: Default + Clone + Copy> TransposedMatrix<T> {
77-
pub fn new(size_x: usize, size_y: usize) -> Self {
78-
TransposedMatrix::<T> {
79-
data: vec![T::default(); size_x * size_y],
80-
size_x,
81-
}
80+
impl<T> TransposedMatrix<T> {
81+
pub fn new(size_x: usize, size_y: usize) -> Self where T: Default + Clone {
82+
Self(Matrix::new(size_y, size_x))
8283
}
8384

8485
pub fn get(&self, x: usize, y: usize) -> &T {
85-
&self.data[y * self.size_x + x]
86+
self.0.get(y, x)
8687
}
8788

8889
pub fn set(&mut self, x: usize, y: usize, value: T) {
89-
self.data[y * self.size_x + x] = value;
90+
self.0.set(y, x, value)
91+
}
92+
}
93+
94+
impl<T: fmt::Display> fmt::Display for Matrix<T> {
95+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96+
for i in 0..self.size_x {
97+
for j in 0..self.size_y {
98+
write!(f, "{} ", self.get(i, j))?;
99+
}
100+
write!(f, "\n")?;
101+
}
102+
Ok(())
103+
}
104+
}
105+
106+
impl<T: fmt::Display> fmt::Display for TransposedMatrix<T> {
107+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
108+
write!(f, "{}", self.0)
90109
}
91110
}

0 commit comments

Comments
 (0)