Skip to content

Commit

Permalink
Impl raw slice methods for blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gunvirranu committed May 7, 2021
1 parent 9afcfb4 commit f9cfe1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

- Cache `col_blocks` value in struct ([#66])
- Add optional [Serde][serde] framework support ([#67])
- Memory slice access for blocks ([#68])

[#66]: https://github.com/gunvirranu/block-grid/pull/66
[serde]: https://crates.io/crates/serde
[#67]: https://github.com/gunvirranu/block-grid/pull/67
[#68]: https://github.com/gunvirranu/block-grid/pull/68

## 0.1.1 - 2021-02-01

Expand Down
22 changes: 20 additions & 2 deletions src/block_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ impl<T, B: BlockDim> BlockGrid<T, B> {
self.buf.get_unchecked_mut(ind)
}

/// Borrow `BlockGrid<T, B>` as a slice in memory order.
/// Returns all elements as a slice in memory order.
#[inline]
pub fn raw(&self) -> &[T] {
&self.buf
}

/// Mutably borrow `BlockGrid<T, B>` as a mutable slice in memory order.
/// Returns all elements as a mutable slice in memory order.
#[inline]
pub fn raw_mut(&mut self) -> &mut [T] {
&mut self.buf
Expand Down Expand Up @@ -438,6 +438,12 @@ impl<'a, T, B: BlockDim> Block<'a, T, B> {
self.arr.get_unchecked(self.calc_index(coords))
}

/// Returns all elements in block as a slice in memory order.
#[inline]
pub fn raw(&self) -> &[T] {
self.arr
}

/// Returns the 1D memory index calculated from 2D coordinates.
fn calc_index(&self, (row, col): Coords) -> usize {
B::WIDTH * row + col
Expand Down Expand Up @@ -538,6 +544,18 @@ impl<'a, T, B: BlockDim> BlockMut<'a, T, B> {
self.arr.get_unchecked_mut(self.calc_index(coords))
}

/// Returns all elements in block as a slice in memory order.
#[inline]
pub fn raw(&self) -> &[T] {
self.arr
}

/// Returns all elements in block as a mutable slice in memory order.
#[inline]
pub fn raw_mut(&mut self) -> &mut [T] {
self.arr
}

/// Returns the 1D memory index calculated from 2D coordinates.
fn calc_index(&self, (row, col): Coords) -> usize {
B::WIDTH * row + col
Expand Down

0 comments on commit f9cfe1a

Please sign in to comment.