Skip to content

Commit

Permalink
Add ChunkShape::num_elements
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Feb 12, 2024
1 parent f7ac30d commit ceb1018
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Additional sharding tests
- Add store lock tests
- Added `contiguous_elements` method to `ContiguousIndicesIterator` and `ContiguousLinearisedIndicesIterator`
- Added `ChunkShape::num_elements`

### Changed
- Dependency bumps
Expand Down
13 changes: 13 additions & 0 deletions src/array/chunk_shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@ use serde::{Deserialize, Serialize};
use super::{ArrayShape, NonZeroError};

/// The shape of a chunk. All dimensions must be non-zero.
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug)]
pub struct ChunkShape(Vec<NonZeroU64>);

impl ChunkShape {
/// Return the number of elements.
///
/// Equal to the product of the components of its shape.
pub fn num_elements(&self) -> NonZeroU64 {
unsafe {
// Multiplying NonZeroU64 must result in NonZeroU64
NonZeroU64::new_unchecked(self.0.iter().copied().map(NonZeroU64::get).product::<u64>())
}
}
}

impl From<ChunkShape> for Vec<NonZeroU64> {
fn from(val: ChunkShape) -> Self {
val.0.clone()
Expand Down

0 comments on commit ceb1018

Please sign in to comment.