MaskedCellBuffer

Struct MaskedCellBuffer 

Source
pub struct MaskedCellBuffer(/* private fields */);
Expand description

A CellBuffer with a companion Mask.

The Mask tracks which cells are valid across operations, and which should be treated as “no-data” values.

§Example

use erased_cells::{BufferOps, Mask, MaskedCellBuffer};
// Fill a buffer with the `u16` numbers `0..=3` and mask [true, false, true, false].
let buf = MaskedCellBuffer::fill_with_mask_via(4, |i| (i as f64, i % 2 == 0));
assert_eq!(buf.mask(), &Mask::new(vec![true, false, true, false]));
// We can count the data/no-data values
assert_eq!(buf.counts(), (2, 2));

// Mask values are propagated across math operations.
let ones = MaskedCellBuffer::from_vec(vec![1.0; 4]);
let r = (buf + ones) * 2.0;

let expected = MaskedCellBuffer::new(
    vec![
        (0.0 + 1.0) * 2.0,
        (1.0 + 1.0) * 2.0,
        (2.0 + 1.0) * 2.0,
        (3.0 + 1.0) * 2.0,
    ]
    .into(),
    Mask::new(vec![true, false, true, false]),
);
assert_eq!(r, expected);

Implementations§

Source§

impl MaskedCellBuffer

Source

pub fn new(buffer: CellBuffer, mask: Mask) -> Self

Create a new combined CellBuffer and Mask.

§Panics

Will panics if buffer and mask are not the same length.

Examples found in repository?
examples/masked.rs (lines 13-22)
2fn main() {
3    use erased_cells::{BufferOps, Mask, MaskedCellBuffer};
4    // Fill a buffer with the `u16` numbers `0..=3` and mask [true, false, true, false].
5    let buf = MaskedCellBuffer::fill_with_mask_via(4, |i| (i as f64, i % 2 == 0));
6    assert_eq!(buf.mask(), &Mask::new(vec![true, false, true, false]));
7    // We can count the data/no-data values
8    assert_eq!(buf.counts(), (2, 2));
9
10    // Mask values are propagated across math operations.
11    let ones = MaskedCellBuffer::from_vec(vec![1.0; 4]);
12    let r = (buf + ones) * 2.0;
13    let expected = MaskedCellBuffer::new(
14        vec![
15            (0.0 + 1.0) * 2.0,
16            (1.0 + 1.0) * 2.0,
17            (2.0 + 1.0) * 2.0,
18            (3.0 + 1.0) * 2.0,
19        ]
20        .into(),
21        Mask::new(vec![true, false, true, false]),
22    );
23    assert_eq!(r, expected);
24}
Source

pub fn from_vec_with_nodata<T: CellEncoding>( data: Vec<T>, nodata: NoData<T>, ) -> Self

Constructs a MaskedCellBuffer from a Vec<CellEncoding>, specifying a NoData<T> value.

Mask value will be false when associated cell matches nodata.

Use Self::from_vec

Source

pub fn fill_with_mask_via<T, F>(len: usize, mv: F) -> Self
where T: CellEncoding, F: Fn(usize) -> (T, bool),

Examples found in repository?
examples/masked.rs (line 5)
2fn main() {
3    use erased_cells::{BufferOps, Mask, MaskedCellBuffer};
4    // Fill a buffer with the `u16` numbers `0..=3` and mask [true, false, true, false].
5    let buf = MaskedCellBuffer::fill_with_mask_via(4, |i| (i as f64, i % 2 == 0));
6    assert_eq!(buf.mask(), &Mask::new(vec![true, false, true, false]));
7    // We can count the data/no-data values
8    assert_eq!(buf.counts(), (2, 2));
9
10    // Mask values are propagated across math operations.
11    let ones = MaskedCellBuffer::from_vec(vec![1.0; 4]);
12    let r = (buf + ones) * 2.0;
13    let expected = MaskedCellBuffer::new(
14        vec![
15            (0.0 + 1.0) * 2.0,
16            (1.0 + 1.0) * 2.0,
17            (2.0 + 1.0) * 2.0,
18            (3.0 + 1.0) * 2.0,
19        ]
20        .into(),
21        Mask::new(vec![true, false, true, false]),
22    );
23    assert_eq!(r, expected);
24}
Source

pub fn buffer(&self) -> &CellBuffer

Source

pub fn buffer_mut(&mut self) -> &mut CellBuffer

Source

pub fn mask(&self) -> &Mask

Examples found in repository?
examples/masked.rs (line 6)
2fn main() {
3    use erased_cells::{BufferOps, Mask, MaskedCellBuffer};
4    // Fill a buffer with the `u16` numbers `0..=3` and mask [true, false, true, false].
5    let buf = MaskedCellBuffer::fill_with_mask_via(4, |i| (i as f64, i % 2 == 0));
6    assert_eq!(buf.mask(), &Mask::new(vec![true, false, true, false]));
7    // We can count the data/no-data values
8    assert_eq!(buf.counts(), (2, 2));
9
10    // Mask values are propagated across math operations.
11    let ones = MaskedCellBuffer::from_vec(vec![1.0; 4]);
12    let r = (buf + ones) * 2.0;
13    let expected = MaskedCellBuffer::new(
14        vec![
15            (0.0 + 1.0) * 2.0,
16            (1.0 + 1.0) * 2.0,
17            (2.0 + 1.0) * 2.0,
18            (3.0 + 1.0) * 2.0,
19        ]
20        .into(),
21        Mask::new(vec![true, false, true, false]),
22    );
23    assert_eq!(r, expected);
24}
Source

pub fn mask_mut(&mut self) -> &mut Mask

Source

pub fn get_masked(&self, index: usize) -> Option<CellValue>

Get a buffer value at position index with mask evaluated.

Returns Some(CellValue) if mask at index is true, None otherwise.

Source

pub fn get_with_mask(&self, index: usize) -> (CellValue, bool)

Get the cell value and mask value at position index.

Returns (CellValue, bool). If bool is false, associated CellValue should be considered invalid.

Source

pub fn put_with_mask( &mut self, index: usize, value: CellValue, mask: bool, ) -> Result<()>

Set the value and mask at position index.

Returns Err(NarrowingError) if value cannot be converted to self.cell_type() without data loss (e.g. overflow).

Source

pub fn counts(&self) -> (usize, usize)

Returns a tuple of representing counts of (data, nodata).

Examples found in repository?
examples/masked.rs (line 8)
2fn main() {
3    use erased_cells::{BufferOps, Mask, MaskedCellBuffer};
4    // Fill a buffer with the `u16` numbers `0..=3` and mask [true, false, true, false].
5    let buf = MaskedCellBuffer::fill_with_mask_via(4, |i| (i as f64, i % 2 == 0));
6    assert_eq!(buf.mask(), &Mask::new(vec![true, false, true, false]));
7    // We can count the data/no-data values
8    assert_eq!(buf.counts(), (2, 2));
9
10    // Mask values are propagated across math operations.
11    let ones = MaskedCellBuffer::from_vec(vec![1.0; 4]);
12    let r = (buf + ones) * 2.0;
13    let expected = MaskedCellBuffer::new(
14        vec![
15            (0.0 + 1.0) * 2.0,
16            (1.0 + 1.0) * 2.0,
17            (2.0 + 1.0) * 2.0,
18            (3.0 + 1.0) * 2.0,
19        ]
20        .into(),
21        Mask::new(vec![true, false, true, false]),
22    );
23    assert_eq!(r, expected);
24}
Source

pub fn to_vec_with_nodata<T: CellEncoding>( self, no_data: NoData<T>, ) -> Result<Vec<T>>

Convert self into a Vec<T>, replacing values where the mask is 0 to no_data.value()

Trait Implementations§

Source§

impl Add<&MaskedCellBuffer> for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &MaskedCellBuffer) -> Self::Output

Performs the + operation. Read more
Source§

impl<R> Add<R> for MaskedCellBuffer
where R: Into<CellValue>,

Source§

type Output = MaskedCellBuffer

The resulting type after applying the + operator.
Source§

fn add(self, rhs: R) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for &MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl BufferOps for MaskedCellBuffer

Source§

fn to_vec<T: CellEncoding>(self) -> Result<Vec<T>>

Converts self to Vec<T>, ignoring the mask values.

See also: Self::to_vec_with_nodata and NoData.

Source§

fn from_vec<T: CellEncoding>(data: Vec<T>) -> Self

Construct a CellBuffer from a Vec<T>.
Source§

fn with_defaults(len: usize, ct: CellType) -> Self

Construct a CellBuffer of given len length and ct CellType Read more
Source§

fn fill(len: usize, value: CellValue) -> Self

Create a buffer of size len with all values value.
Source§

fn fill_via<T, F>(len: usize, f: F) -> Self
where T: CellEncoding, F: Fn(usize) -> T,

Fill a buffer of size len with values from a closure. Read more
Source§

fn len(&self) -> usize

Get the length of the buffer.
Source§

fn cell_type(&self) -> CellType

Get the cell type of the encoded value.
Source§

fn get(&self, index: usize) -> CellValue

Get the CellValue at index idx. Read more
Source§

fn put(&mut self, idx: usize, value: CellValue) -> Result<()>

Store value at position idx. Read more
Source§

fn convert(&self, cell_type: CellType) -> Result<Self>
where Self: Sized,

Create a new CellBuffer whereby all CellValues are converted to cell_type. Read more
Source§

fn min_max(&self) -> (CellValue, CellValue)

Compute the minimum and maximum values the buffer.
Source§

fn is_empty(&self) -> bool

Determine if the buffer has zero values in it.
Source§

impl Clone for MaskedCellBuffer

Source§

fn clone(&self) -> MaskedCellBuffer

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for MaskedCellBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for MaskedCellBuffer

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div<&MaskedCellBuffer> for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &MaskedCellBuffer) -> Self::Output

Performs the / operation. Read more
Source§

impl<R> Div<R> for MaskedCellBuffer
where R: Into<CellValue>,

Source§

type Output = MaskedCellBuffer

The resulting type after applying the / operator.
Source§

fn div(self, rhs: R) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for &MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl Div for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<C: CellEncoding> Extend<(C, bool)> for MaskedCellBuffer

Source§

fn extend<T: IntoIterator<Item = (C, bool)>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<'a> From<&'a MaskedCellBuffer> for (&'a CellBuffer, &'a Mask)

Source§

fn from(value: &'a MaskedCellBuffer) -> Self

Converts to this type from the input type.
Source§

impl From<CellBuffer> for MaskedCellBuffer

Converts a CellBuffer into a MaskedCellBuffer with an all-true mask.

Source§

fn from(value: CellBuffer) -> Self

Converts to this type from the input type.
Source§

impl From<MaskedCellBuffer> for (CellBuffer, Mask)

Source§

fn from(value: MaskedCellBuffer) -> Self

Converts to this type from the input type.
Source§

impl<C: CellEncoding> FromIterator<(C, bool)> for MaskedCellBuffer

Source§

fn from_iter<T: IntoIterator<Item = (C, bool)>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<C: CellEncoding> FromIterator<C> for MaskedCellBuffer

Source§

fn from_iter<T: IntoIterator<Item = C>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'buf> IntoIterator for &'buf MaskedCellBuffer

Source§

type Item = (CellValue, bool)

The type of the elements being iterated over.
Source§

type IntoIter = MaskedCellBufferIterator<'buf>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl Mul<&MaskedCellBuffer> for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &MaskedCellBuffer) -> Self::Output

Performs the * operation. Read more
Source§

impl<R> Mul<R> for MaskedCellBuffer
where R: Into<CellValue>,

Source§

type Output = MaskedCellBuffer

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: R) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for &MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl Neg for &MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl PartialEq for MaskedCellBuffer

Source§

fn eq(&self, other: &MaskedCellBuffer) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for MaskedCellBuffer

Source§

fn partial_cmp(&self, other: &MaskedCellBuffer) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for MaskedCellBuffer

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub<&MaskedCellBuffer> for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &MaskedCellBuffer) -> Self::Output

Performs the - operation. Read more
Source§

impl<R> Sub<R> for MaskedCellBuffer
where R: Into<CellValue>,

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: R) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for &MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for MaskedCellBuffer

Source§

type Output = MaskedCellBuffer

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl StructuralPartialEq for MaskedCellBuffer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,