-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathdyn_repr.rs
More file actions
188 lines (163 loc) · 4.16 KB
/
Copy pathdyn_repr.rs
File metadata and controls
188 lines (163 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use alloc::boxed::Box;
use alloc::vec::Vec;
use core::iter::Cloned;
use core::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
use core::ops::{Deref, DerefMut, Index, IndexMut};
use core::slice::Iter;
use crate::layout::rank::DynRank;
use crate::layout::ranked::Ranked;
use crate::layout::shape::Shape;
use crate::Axis;
const CAP: usize = 4;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DynAxesRepr<T>
{
Inline(usize, [T; CAP]),
Alloc(Box<[T]>),
}
/// An array shape with a dynamic rank.
pub type DShape = DynAxesRepr<usize>;
impl<T> Deref for DynAxesRepr<T>
{
type Target = [T];
fn deref(&self) -> &Self::Target
{
match self {
DynAxesRepr::Inline(len, arr) => {
debug_assert!(*len <= arr.len());
unsafe { arr.get_unchecked(..*len) }
}
DynAxesRepr::Alloc(items) => items,
}
}
}
impl<T> DerefMut for DynAxesRepr<T>
{
fn deref_mut(&mut self) -> &mut Self::Target
{
match self {
DynAxesRepr::Inline(len, arr) => {
debug_assert!(*len <= arr.len());
unsafe { arr.get_unchecked_mut(..*len) }
}
DynAxesRepr::Alloc(items) => items,
}
}
}
impl<T> Index<usize> for DynAxesRepr<T>
{
type Output = T;
fn index(&self, index: usize) -> &Self::Output
{
&(**self)[index]
}
}
impl<T> IndexMut<usize> for DynAxesRepr<T>
{
fn index_mut(&mut self, index: usize) -> &mut Self::Output
{
&mut (**self)[index]
}
}
impl<T> Index<Axis> for DynAxesRepr<T>
{
type Output = T;
fn index(&self, index: Axis) -> &Self::Output
{
self.index(index.0)
}
}
impl<T> IndexMut<Axis> for DynAxesRepr<T>
{
fn index_mut(&mut self, index: Axis) -> &mut Self::Output
{
self.index_mut(index.0)
}
}
impl<Rhs, T> From<Rhs> for DynAxesRepr<T>
where
Rhs: AsRef<[T]>,
T: Default + Copy,
{
fn from(value: Rhs) -> Self
{
let value = value.as_ref();
let n = value.len();
if n <= CAP {
let mut inline = [T::default(); CAP];
inline.split_at_mut(n).0.copy_from_slice(value);
Self::Inline(n, inline)
} else {
Self::Alloc(value.into())
}
}
}
impl<T> Ranked for DynAxesRepr<T>
{
type NDim = DynRank;
fn ndim(&self) -> usize
{
match self {
DynAxesRepr::Inline(d, _) => d.clone(),
DynAxesRepr::Alloc(items) => items.len(),
}
}
}
macro_rules! impl_op {
($op_trait:ty, $op_fn:ident, $op_assign_trait:ty, $op_assign_fn:ident) => {
/// *Panics* if the two dimensionalities are different
impl<Rhs> $op_trait for DShape
where
Rhs: Into<Self>,
{
type Output = DShape;
fn $op_fn(self, rhs: Rhs) -> <Self as $op_trait>::Output {
let mut output = self.clone();
output.$op_assign_fn(rhs);
output
}
}
/// *Panics* if the two dimensionalities are different
impl<Rhs> $op_assign_trait for DShape
where
Rhs: Into<Self>,
{
fn $op_assign_fn(&mut self, rhs: Rhs) {
let other = rhs.into();
for i in 0..self.ndim().max(other.ndim()) {
self[i].$op_assign_fn(other[i]);
}
}
}
};
}
impl_op!(Add<Rhs>, add, AddAssign<Rhs>, add_assign);
impl_op!(Sub<Rhs>, sub, SubAssign<Rhs>, sub_assign);
impl_op!(Mul<Rhs>, mul, MulAssign<Rhs>, mul_assign);
impl<T> IntoIterator for DynAxesRepr<T>
where T: Clone
{
type Item = T;
type IntoIter = alloc::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter
{
match self {
DynAxesRepr::Inline(len, arr) => Vec::from(arr[..len].to_vec()).into_iter(),
DynAxesRepr::Alloc(b) => b.into_vec().into_iter(),
}
}
}
impl Shape for DynAxesRepr<usize>
{
type Iter<'a>
= Cloned<Iter<'a, usize>>
where Self: 'a;
fn axis_len(&self, axis: usize) -> usize
{
self[axis]
}
fn iter(&self) -> Self::Iter<'_>
{
(**self).iter().cloned()
}
}