Skip to content

Commit af0bc74

Browse files
committed
Add utlities for Layout
1 parent 22b3a4b commit af0bc74

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

src/layout.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,37 @@ impl Layout {
5050
Layout::F(_) => c::Layout::ColumnMajor,
5151
}
5252
}
53+
54+
pub fn same_order(&self, other: &Layout) -> bool {
55+
match *self {
56+
Layout::C(_) => {
57+
match *other {
58+
Layout::C(_) => true,
59+
Layout::F(_) => false,
60+
}
61+
}
62+
Layout::F(_) => {
63+
match *other {
64+
Layout::C(_) => false,
65+
Layout::F(_) => true,
66+
}
67+
}
68+
}
69+
}
70+
71+
pub fn as_shape(&self) -> Shape<Ix2> {
72+
match *self {
73+
Layout::C((row, col)) => (row as usize, col as usize).into_shape(),
74+
Layout::F((col, row)) => (row as usize, col as usize).f().into_shape(),
75+
}
76+
}
77+
78+
pub fn t(&self) -> Self {
79+
match *self {
80+
Layout::C((row, col)) => Layout::F((col, row)),
81+
Layout::F((col, row)) => Layout::C((row, col)),
82+
}
83+
}
5384
}
5485

5586
pub trait AllocatedArray {
@@ -133,10 +164,14 @@ impl<A, S> AllocatedArrayMut for ArrayBase<S, Ix1>
133164
pub fn reconstruct<A, S>(l: Layout, a: Vec<A>) -> Result<ArrayBase<S, Ix2>>
134165
where S: DataOwned<Elem = A>
135166
{
136-
Ok(match l {
137-
Layout::C((row, col)) => ArrayBase::from_shape_vec((row as usize, col as usize), a)?,
138-
Layout::F((col, row)) => ArrayBase::from_shape_vec((row as usize, col as usize).f(), a)?,
139-
})
167+
Ok(ArrayBase::from_shape_vec(l.as_shape(), a)?)
168+
}
169+
170+
pub fn uninitialized<A, S>(l: Layout) -> ArrayBase<S, Ix2>
171+
where A: Copy,
172+
S: DataOwned<Elem = A>
173+
{
174+
unsafe { ArrayBase::uninitialized(l.as_shape()) }
140175
}
141176

142177
pub fn replicate<A, Sv, So, D>(a: &ArrayBase<Sv, D>) -> ArrayBase<So, D>
@@ -149,3 +184,13 @@ pub fn replicate<A, Sv, So, D>(a: &ArrayBase<Sv, D>) -> ArrayBase<So, D>
149184
b.assign(a);
150185
b
151186
}
187+
188+
pub fn clone_with_layout<A, Si, So>(l: Layout, a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
189+
where A: Copy,
190+
Si: Data<Elem = A>,
191+
So: DataOwned<Elem = A> + DataMut
192+
{
193+
let mut b = uninitialized(l);
194+
b.assign(a);
195+
b
196+
}

0 commit comments

Comments
 (0)