scirs2_core/memory_efficient/
views.rs1use super::validation;
2use crate::error::{CoreError, ErrorContext, ErrorLocation};
3use ::ndarray::{
4 Array, ArrayBase, ArrayView as NdArrayView, ArrayViewMut as NdArrayViewMut, Data, Dimension,
5 Ix1, Ix2,
6};
7
8pub type ArrayView<'a, A, D> = NdArrayView<'a, A, D>;
10
11pub type ViewMut<'a, A, D> = NdArrayViewMut<'a, A, D>;
13
14pub unsafe fn view_as<A, B, S, D>(array: &ArrayBase<S, D>) -> Result<ArrayView<'_, B, D>, CoreError>
33where
34 A: Clone,
35 S: Data<Elem = A>,
36 D: Dimension,
37{
38 validation::check_not_empty(array)?;
39
40 let a_size = std::mem::size_of::<A>();
42 let b_size = std::mem::size_of::<B>();
43
44 if a_size == 0 || b_size == 0 {
45 return Err(CoreError::ValidationError(
46 ErrorContext::new("Cannot reinterpret view of zero-sized type".to_string())
47 .with_location(ErrorLocation::new(file!(), line!())),
48 ));
49 }
50
51 if a_size % b_size != 0 && b_size % a_size != 0 {
52 return Err(CoreError::ValidationError(
53 ErrorContext::new(format!(
54 "Type sizes are not compatible: {a_size} is not divisible by or a divisor of {b_size}"
55 ))
56 .with_location(ErrorLocation::new(file!(), line!())),
57 ));
58 }
59
60 Err(CoreError::ImplementationError(
63 ErrorContext::new("view_as is not yet implemented".to_string())
64 .with_location(ErrorLocation::new(file!(), line!())),
65 ))
66}
67
68pub unsafe fn view_mut_as<A, B, S, D>(
83 array: &mut ArrayBase<S, D>,
84) -> Result<ViewMut<'_, B, D>, CoreError>
85where
86 A: Clone,
87 S: Data<Elem = A>,
88 D: Dimension,
89{
90 validation::check_not_empty(array)?;
91
92 let a_size = std::mem::size_of::<A>();
94 let b_size = std::mem::size_of::<B>();
95
96 if a_size == 0 || b_size == 0 {
97 return Err(CoreError::ValidationError(
98 ErrorContext::new("Cannot reinterpret view of zero-sized type".to_string())
99 .with_location(ErrorLocation::new(file!(), line!())),
100 ));
101 }
102
103 if a_size % b_size != 0 && b_size % a_size != 0 {
104 return Err(CoreError::ValidationError(
105 ErrorContext::new(format!(
106 "Type sizes are not compatible: {a_size} is not divisible by or a divisor of {b_size}"
107 ))
108 .with_location(ErrorLocation::new(file!(), line!())),
109 ));
110 }
111
112 Err(CoreError::ImplementationError(
115 ErrorContext::new("view_mut_as is not yet implemented".to_string())
116 .with_location(ErrorLocation::new(file!(), line!())),
117 ))
118}
119
120#[allow(dead_code)]
130pub fn transpose_view<A, S>(array: &ArrayBase<S, Ix2>) -> Result<Array<A, Ix2>, CoreError>
131where
132 A: Clone,
133 S: Data<Elem = A>,
134{
135 validation::check_not_empty(array)?;
136
137 Ok(array.to_owned().t().to_owned())
139}
140
141#[allow(dead_code)]
151pub fn diagonal_view<A, S>(array: &ArrayBase<S, Ix2>) -> Result<Array<A, Ix1>, CoreError>
152where
153 A: Clone,
154 S: Data<Elem = A>,
155{
156 validation::check_not_empty(array)?;
157 validation::check_square(array)?;
158
159 Ok(array.diag().to_owned())
161}