Skip to content

Commit

Permalink
add unchecked getters
Browse files Browse the repository at this point in the history
  • Loading branch information
TudbuT committed May 10, 2023
1 parent 3634720 commit 0791864
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "micro_ndarray"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
repository = "https://github.com/tudbut/micro_ndarray"
license = "MIT"
Expand Down
37 changes: 36 additions & 1 deletion src/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{ops::{Index, IndexMut}, slice};
use std::{
ops::{Index, IndexMut},
slice,
};

use crate::iterator::Iter;

Expand Down Expand Up @@ -105,6 +108,22 @@ impl<'a, T, const D: usize> Array<T, D> {
}
}

pub unsafe fn get_unchecked(&'a self, loc: [usize; D]) -> &'a T {
let mut real_loc = 0;
for (i, dim) in loc.iter().enumerate() {
let mut dim = *dim;
if i == 0 {
real_loc += dim;
continue;
}
for s in &self.size[0..i] {
dim *= s;
}
real_loc += dim;
}
self.data.get_unchecked(real_loc)
}

pub fn get_mut(&'a mut self, loc: [usize; D]) -> Option<&'a mut T> {
self.internal_get_mut(loc, false)
}
Expand Down Expand Up @@ -141,6 +160,22 @@ impl<'a, T, const D: usize> Array<T, D> {
}
}

pub unsafe fn get_unchecked_mut(&'a mut self, loc: [usize; D]) -> &'a mut T {
let mut real_loc = 0;
for (i, dim) in loc.iter().enumerate() {
let mut dim = *dim;
if i == 0 {
real_loc += dim;
continue;
}
for s in &self.size[0..i] {
dim *= s;
}
real_loc += dim;
}
self.data.get_unchecked_mut(real_loc)
}

pub fn iter(&mut self) -> Iter<slice::Iter<T>, D> {
Iter::new(self)
}
Expand Down

0 comments on commit 0791864

Please sign in to comment.