1
2use crate::{Dimension, Order, ShapeError, ErrorKind};
3use crate::dimension::sequence::{Sequence, SequenceMut, Forward, Reverse};
4
5#[inline]
6pub(crate) fn reshape_dim<D, E>(from: &D, strides: &D, to: &E, order: Order)
7 -> Result<E, ShapeError>
8where
9 D: Dimension,
10 E: Dimension,
11{
12 debug_assert_eq!(from.ndim(), strides.ndim());
13 let mut to_strides = E::zeros(to.ndim());
14 match order {
15 Order::RowMajor => {
16 reshape_dim_c(&Forward(from), &Forward(strides),
17 &Forward(to), Forward(&mut to_strides))?;
18 }
19 Order::ColumnMajor => {
20 reshape_dim_c(&Reverse(from), &Reverse(strides),
21 &Reverse(to), Reverse(&mut to_strides))?;
22 }
23 }
24 Ok(to_strides)
25}
26
27fn reshape_dim_c<D, E, E2>(from_dim: &D, from_strides: &D, to_dim: &E, mut to_strides: E2)
51 -> Result<(), ShapeError>
52where
53 D: Sequence<Output=usize>,
54 E: Sequence<Output=usize>,
55 E2: SequenceMut<Output=usize>,
56{
57 let mut fi = 0; let mut ti = 0; while fi < from_dim.len() && ti < to_dim.len() {
62 let mut fd = from_dim[fi];
63 let mut fs = from_strides[fi] as isize;
64 let mut td = to_dim[ti];
65
66 if fd == td {
67 to_strides[ti] = from_strides[fi];
68 fi += 1;
69 ti += 1;
70 continue
71 }
72
73 if fd == 1 {
74 fi += 1;
75 continue;
76 }
77
78 if td == 1 {
79 to_strides[ti] = 1;
80 ti += 1;
81 continue;
82 }
83
84 if fd == 0 || td == 0 {
85 debug_assert!(false, "zero dim not handled by this function");
86 return Err(ShapeError::from_kind(ErrorKind::IncompatibleShape));
87 }
88
89 let mut fstride_whole = fs * (fd as isize);
91 let mut fd_product = fd; let mut td_product = td; while fd_product != td_product {
97 if fd_product < td_product {
98 fi += 1;
100 if fi >= from_dim.len() {
101 return Err(ShapeError::from_kind(ErrorKind::IncompatibleShape));
102 }
103 fd = from_dim[fi];
104 fd_product *= fd;
105 if fd > 1 {
106 let fs_old = fs;
107 fs = from_strides[fi] as isize;
108 if fs_old != fd as isize * fs {
110 return Err(ShapeError::from_kind(ErrorKind::IncompatibleLayout));
111 }
112 }
113 } else {
114 fstride_whole /= td as isize;
117 to_strides[ti] = fstride_whole as usize;
118 ti += 1;
119 if ti >= to_dim.len() {
120 return Err(ShapeError::from_kind(ErrorKind::IncompatibleShape));
121 }
122
123 td = to_dim[ti];
124 td_product *= td;
125 }
126 }
127
128 fstride_whole /= td as isize;
129 to_strides[ti] = fstride_whole as usize;
130
131 fi += 1;
132 ti += 1;
133 }
134
135 while fi < from_dim.len() && from_dim[fi] == 1 {
137 fi += 1;
138 }
139
140 while ti < to_dim.len() && to_dim[ti] == 1 {
141 to_strides[ti] = 1;
142 ti += 1;
143 }
144
145 if fi < from_dim.len() || ti < to_dim.len() {
146 return Err(ShapeError::from_kind(ErrorKind::IncompatibleShape));
147 }
148
149 Ok(())
150}
151
152#[cfg(feature = "std")]
153#[test]
154fn test_reshape() {
155 use crate::Dim;
156
157 macro_rules! test_reshape {
158 (fail $order:ident from $from:expr, $stride:expr, to $to:expr) => {
159 let res = reshape_dim(&Dim($from), &Dim($stride), &Dim($to), Order::$order);
160 println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}",
161 $from, $stride, $to, Order::$order, res);
162 let _res = res.expect_err("Expected failed reshape");
163 };
164 (ok $order:ident from $from:expr, $stride:expr, to $to:expr, $to_stride:expr) => {{
165 let res = reshape_dim(&Dim($from), &Dim($stride), &Dim($to), Order::$order);
166 println!("Reshape {:?} {:?} to {:?}, order {:?}\n => {:?}",
167 $from, $stride, $to, Order::$order, res);
168 println!("default stride for from dim: {:?}", Dim($from).default_strides());
169 println!("default stride for to dim: {:?}", Dim($to).default_strides());
170 let res = res.expect("Expected successful reshape");
171 assert_eq!(res, Dim($to_stride), "mismatch in strides");
172 }};
173 }
174
175 test_reshape!(ok C from [1, 2, 3], [6, 3, 1], to [1, 2, 3], [6, 3, 1]);
176 test_reshape!(ok C from [1, 2, 3], [6, 3, 1], to [2, 3], [3, 1]);
177 test_reshape!(ok C from [1, 2, 3], [6, 3, 1], to [6], [1]);
178 test_reshape!(fail C from [1, 2, 3], [6, 3, 1], to [1]);
179 test_reshape!(fail F from [1, 2, 3], [6, 3, 1], to [1]);
180
181 test_reshape!(ok C from [6], [1], to [3, 2], [2, 1]);
182 test_reshape!(ok C from [3, 4, 5], [20, 5, 1], to [4, 15], [15, 1]);
183
184 test_reshape!(ok C from [4, 4, 4], [16, 4, 1], to [16, 4], [4, 1]);
185
186 test_reshape!(ok C from [4, 4], [4, 1], to [2, 2, 4, 1], [8, 4, 1, 1]);
187 test_reshape!(ok C from [4, 4], [4, 1], to [2, 2, 4], [8, 4, 1]);
188 test_reshape!(ok C from [4, 4], [4, 1], to [2, 2, 2, 2], [8, 4, 2, 1]);
189
190 test_reshape!(ok C from [4, 4], [4, 1], to [2, 2, 1, 4], [8, 4, 1, 1]);
191
192 test_reshape!(ok C from [4, 4, 4], [16, 4, 1], to [16, 4], [4, 1]);
193 test_reshape!(ok C from [3, 4, 4], [16, 4, 1], to [3, 16], [16, 1]);
194
195 test_reshape!(ok C from [4, 4], [8, 1], to [2, 2, 2, 2], [16, 8, 2, 1]);
196
197 test_reshape!(fail C from [4, 4], [8, 1], to [2, 1, 4, 2]);
198
199 test_reshape!(ok C from [16], [4], to [2, 2, 4], [32, 16, 4]);
200 test_reshape!(ok C from [16], [-4isize as usize], to [2, 2, 4],
201 [-32isize as usize, -16isize as usize, -4isize as usize]);
202 test_reshape!(ok F from [16], [4], to [2, 2, 4], [4, 8, 16]);
203 test_reshape!(ok F from [16], [-4isize as usize], to [2, 2, 4],
204 [-4isize as usize, -8isize as usize, -16isize as usize]);
205
206 test_reshape!(ok C from [3, 4, 5], [20, 5, 1], to [12, 5], [5, 1]);
207 test_reshape!(ok C from [3, 4, 5], [20, 5, 1], to [4, 15], [15, 1]);
208 test_reshape!(fail F from [3, 4, 5], [20, 5, 1], to [4, 15]);
209 test_reshape!(ok C from [3, 4, 5, 7], [140, 35, 7, 1], to [28, 15], [15, 1]);
210
211 test_reshape!(ok C from [10], [2], to [10], [2]);
213 test_reshape!(ok F from [10], [2], to [10], [2]);
214 test_reshape!(ok C from [2, 10], [1, 2], to [2, 10], [1, 2]);
215 test_reshape!(ok F from [2, 10], [1, 2], to [2, 10], [1, 2]);
216 test_reshape!(ok C from [3, 4, 5], [20, 5, 1], to [3, 4, 5], [20, 5, 1]);
217 test_reshape!(ok F from [3, 4, 5], [20, 5, 1], to [3, 4, 5], [20, 5, 1]);
218
219 test_reshape!(ok C from [3, 4, 5], [4, 1, 1], to [12, 5], [1, 1]);
220 test_reshape!(ok F from [3, 4, 5], [1, 3, 12], to [12, 5], [1, 12]);
221 test_reshape!(ok F from [3, 4, 5], [1, 3, 1], to [12, 5], [1, 1]);
222
223 test_reshape!(ok C from [3, 4, 5, 7], [0, 0, 7, 1], to [12, 35], [0, 1]);
225 test_reshape!(fail C from [3, 4, 5, 7], [0, 0, 7, 1], to [28, 15]);
226
227 test_reshape!(ok C from [10], [1], to [1, 10, 1, 1, 1], [1, 1, 1, 1, 1]);
229 test_reshape!(ok F from [10], [1], to [1, 10, 1, 1, 1], [1, 1, 1, 1, 1]);
230 test_reshape!(ok C from [1, 10], [10, 1], to [1, 10, 1, 1, 1], [10, 1, 1, 1, 1]);
231 test_reshape!(ok F from [1, 10], [10, 1], to [1, 10, 1, 1, 1], [10, 1, 1, 1, 1]);
232 test_reshape!(ok C from [1, 10], [1, 1], to [1, 5, 1, 1, 2], [1, 2, 2, 2, 1]);
233 test_reshape!(ok F from [1, 10], [1, 1], to [1, 5, 1, 1, 2], [1, 1, 5, 5, 5]);
234 test_reshape!(ok C from [10, 1, 1, 1, 1], [1, 1, 1, 1, 1], to [10], [1]);
235 test_reshape!(ok F from [10, 1, 1, 1, 1], [1, 1, 1, 1, 1], to [10], [1]);
236 test_reshape!(ok C from [1, 5, 1, 2, 1], [1, 2, 1, 1, 1], to [10], [1]);
237 test_reshape!(fail F from [1, 5, 1, 2, 1], [1, 2, 1, 1, 1], to [10]);
238 test_reshape!(ok F from [1, 5, 1, 2, 1], [1, 1, 1, 5, 1], to [10], [1]);
239 test_reshape!(fail C from [1, 5, 1, 2, 1], [1, 1, 1, 5, 1], to [10]);
240}
241