Faster matrix construction
Also fix checks
This commit is contained in:
parent
0466546981
commit
70fddcd6d4
1 changed files with 11 additions and 4 deletions
|
|
@ -267,24 +267,31 @@ fn co_occurrence_matrix(num_colors: usize, png: &PngImage) -> Vec<Vec<u32>> {
|
||||||
for line in png.scan_lines(false) {
|
for line in png.scan_lines(false) {
|
||||||
for i in 0..line.data.len() {
|
for i in 0..line.data.len() {
|
||||||
let val = line.data[i] as usize;
|
let val = line.data[i] as usize;
|
||||||
if val > num_colors {
|
if val >= num_colors {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(prev_val) = prev_val.replace(val) {
|
if let Some(prev_val) = prev_val.replace(val) {
|
||||||
matrix[prev_val][val] += 1;
|
matrix[prev_val][val] += 1;
|
||||||
matrix[val][prev_val] += 1;
|
|
||||||
}
|
}
|
||||||
if let Some(prev) = &prev {
|
if let Some(prev) = &prev {
|
||||||
let prev_val = prev.data[i] as usize;
|
let prev_val = prev.data[i] as usize;
|
||||||
if prev_val > num_colors {
|
if prev_val >= num_colors {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
matrix[prev_val][val] += 1;
|
matrix[prev_val][val] += 1;
|
||||||
matrix[val][prev_val] += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
prev = Some(line);
|
prev = Some(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make the matrix symmetrical - this is faster to do afterward than maintaining symmetry during counting
|
||||||
|
#[allow(clippy::needless_range_loop)]
|
||||||
|
for i in 0..num_colors {
|
||||||
|
for j in 0..num_colors {
|
||||||
|
matrix[j][i] += matrix[i][j];
|
||||||
|
matrix[i][j] = matrix[j][i];
|
||||||
|
}
|
||||||
|
}
|
||||||
matrix
|
matrix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue