Generic implementation of summed area tables with Rust
You can find more information about summed area tables on Wikipedia.
http://en.wikipedia.org/wiki/Summedareatable
rust
[dependencies]
summed-area-table = "*"
nalgebra = "0.3.0" // check cargo.toml for dependency for newer version
rust
use nalgebra::{DMat};
use summed_area_table::{SummedAreaTableSource, SummedAreaTable};
rust
let src: DMat<usize> = DMat::new_ones(10,10);
let table = src.calculate_full_summed_area_table();
rust
assert_eq!(100, table.get_sum((0,0),(9,9)));
assert_eq!(50, table.get_sum((0,0),(9,4)));
assert_eq!(25, table.get_sum((0,0),(4,4)));
rust
assert_eq!(10, table.get_average((0,0),(9,9)));
assert_eq!(10, table.get_average((0,0),(9,4)));
assert_eq!(10, table.get_average((0,0),(4,4)));
You can implement the SummedAreaTableSource
trait for your own types if you want them to have the calculate_summed_area_table()
method. All you need, is to implement get_values
and return a DMat
This is how the library does it for DMat<usize>
type.
rust
impl SummedAreaTableSource for DMat<usize>{
fn get_values(&self) -> &DMat<usize> {
self
}
}