Expand description
This module is used for storing 2-dimensional data arrays, and internally uses Z-index arrays to improve data localization and alignment to the CPU cache-line fetches. In other words, use this to improve performance for 2D data that is randomly accessed rather than raster scanned or if your data processing makes heavy use of neighbor look-up in both the X and Y directions.
How It Works
When you initialize a zarray::z2d::ZArray2D struct, it creates an array of 8x8 data patches, using Z-curve indexing within that patch. When you call a getter or setter method, it finds the corresponding data patch and then looks up (or sets) the data from within the patch. Since the cache-line size on most CPUs is 64 bytes (and up to only 128 bytes on more exotic chips), the 8x8 patch is sufficient localization for the majority of applications.
Example Usage
An example of a simple blurring operation
use zarray::z2d::ZArray2D;
let w = 800;
let h = 600;
let mut input = ZArray2D::new(w, h, 0i32);
let mut blurred = ZArray2D::new(w, h, 0i32);
for y in 0..h {
for x in 0..w {
let random_number = (((x*1009+1031)*y*1013+1051) % 10) as i32;
input.set(x, y, random_number).unwrap();
}
}
let radius: i32 = 2;
for y in radius..h as i32-radius {
for x in radius..w as i32-radius {
let mut sum = 0;
for dy in -radius..radius+1 {
for dx in -radius..radius+1 {
sum += *input.bounded_get((x+dx) as isize, (y+dy) as isize).unwrap_or(&0);
}
}
blurred.set(x as usize, y as usize, sum/((2*radius+1).pow(2))).unwrap();
}
}Structs
This is primary struct for z-indexed 2D arrays. Create new instances with ZArray2D::new(x_size, y_size, initial_value)
Functions
General purpose Z-index function to convert a two-dimensional coordinate into a localized one-dimensional coordinate
General purpose Z-index function to convert a two-dimensional coordinate into a localized one-dimensional coordinate
General purpose Z-index function to convert a two-dimensional coordinate into a localized one-dimensional coordinate