From 29b9a066773aa252929e713aae4d85079395441d Mon Sep 17 00:00:00 2001 From: Andrew Champion Date: Mon, 26 Oct 2020 16:06:01 +0000 Subject: [PATCH] Ndarray: add method for reading reusing block buffer --- src/ndarray.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ndarray.rs b/src/ndarray.rs index 491948d..49b33b4 100644 --- a/src/ndarray.rs +++ b/src/ndarray.rs @@ -172,11 +172,31 @@ pub trait N5NdarrayReader : N5Reader { /// Assumes blocks are column-major. The array can be any order, but column- /// major will be more efficient. fn read_ndarray_into<'a, T>( + &self, + path_name: &str, + data_attrs: &DatasetAttributes, + bbox: &BoundingBox, + arr: ndarray::ArrayViewMut<'a, T, ndarray::Dim>, + ) -> Result<(), Error> + where VecDataBlock: DataBlock + ReinitDataBlock + ReadableDataBlock, + T: ReflectedType + num_traits::identities::Zero { + + self.read_ndarray_into_with_buffer(path_name, data_attrs, bbox, arr, &mut None) + } + + /// Read an arbitrary bounding box from an N5 volume into an existing + /// ndarray view, reading blocks in serial as necessary into a provided + /// buffer. + /// + /// Assumes blocks are column-major. The array can be any order, but column- + /// major will be more efficient. + fn read_ndarray_into_with_buffer<'a, T>( &self, path_name: &str, data_attrs: &DatasetAttributes, bbox: &BoundingBox, mut arr: ndarray::ArrayViewMut<'a, T, ndarray::Dim>, + block_buff_opt: &mut Option>, ) -> Result<(), Error> where VecDataBlock: DataBlock + ReinitDataBlock + ReadableDataBlock, T: ReflectedType + num_traits::identities::Zero { @@ -189,14 +209,12 @@ pub trait N5NdarrayReader : N5Reader { return Err(Error::new(ErrorKind::InvalidData, "Bounding box and array have different shape")); } - let mut block_buff_opt: Option> = None; - for coord in data_attrs.bounded_coord_iter(bbox) { let grid_pos = GridCoord::from(&coord[..]); let is_block = match block_buff_opt { None => { - block_buff_opt = self.read_block(path_name, data_attrs, grid_pos)?; + *block_buff_opt = self.read_block(path_name, data_attrs, grid_pos)?; block_buff_opt.is_some() }, Some(ref mut block_buff) => {