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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
Check whether the given indices are valid. This is the case if the length of `indices`
is equal to the dimensionality of the data `N`, and if all individual axes indices are in bounds.
*/
/**
Convert a cartesian index into a linear index (row-major).
This function takes two arguments -- cartesian indices and the size of each dimension as slices -- and uses them
to calculate the corresponding linear index in row-major order. If the length of the cartesian index is not
equal to the number of dimensions (= length of `dim_size`), or if any of the cartesian indices are out of bounds,
this function returns `None`.
```
use cart_lin::cart_to_lin;
// 2 x 5 matrix with five columns and two rows
let dim_size = [2, 5];
assert_eq!(cart_to_lin(&[0, 0], &dim_size).unwrap(), 0);
assert_eq!(cart_to_lin(&[0, 1], &dim_size).unwrap(), 1);
assert_eq!(cart_to_lin(&[0, 2], &dim_size).unwrap(), 2);
// ...
assert_eq!(cart_to_lin(&[1, 3], &dim_size).unwrap(), 8);
assert_eq!(cart_to_lin(&[1, 4], &dim_size).unwrap(), 9);
// Out-of-bounds cartesian indices:
assert!(cart_to_lin(&[1, 5], &dim_size).is_none()); // matrix has five columns, hence the maximum column index is 4.
// 2 x 3 x 4 matrix
let dim_size = [2, 3, 4];
assert_eq!(cart_to_lin(&[0, 0, 0], &dim_size).unwrap(), 0);
assert_eq!(cart_to_lin(&[0, 0, 1], &dim_size).unwrap(), 1);
assert_eq!(cart_to_lin(&[0, 0, 2], &dim_size).unwrap(), 2);
// ...
assert_eq!(cart_to_lin(&[0, 1, 0], &dim_size).unwrap(), 4);
assert_eq!(cart_to_lin(&[0, 1, 1], &dim_size).unwrap(), 5);
assert_eq!(cart_to_lin(&[0, 1, 2], &dim_size).unwrap(), 6);
// ...
assert_eq!(cart_to_lin(&[1, 0, 0], &dim_size).unwrap(), 12);
assert_eq!(cart_to_lin(&[1, 0, 1], &dim_size).unwrap(), 13);
assert_eq!(cart_to_lin(&[1, 0, 2], &dim_size).unwrap(), 14);
```
*/
/**
Like [`cart_to_lin`], but without the checks.
Despite the name, this function itself is safe. However, the index received from this function might be invalid. Using
such an invalid index may cause an out-of-bounds read.
```
use cart_lin::{cart_to_lin, cart_to_lin_unchecked};
// 2 x 5 matrix with five columns and two rows
let dim_size = [2, 5];
// Valid cartesian indices
assert_eq!(cart_to_lin(&[1, 4], &dim_size).unwrap(), 9);
assert_eq!(cart_to_lin_unchecked(&[1, 4], &dim_size), 9);
// Out-of-bounds cartesian indices:
assert!(cart_to_lin(&[1, 5], &dim_size).is_none());
assert_eq!(cart_to_lin_unchecked(&[1, 5], &dim_size), 10); // Nonsensical value - matrix only has 10 entries (linear index 0 to 9)!
```
*/
/**
Convert a linear index to a cartesian index (row-major).
This function takes the linear index and the size of each dimension as a slice and uses them to
calculate the corresponding cartesian index. If the linear index is out of bounds (= equal to
or larger than the cartiter of all values in the `dim_size` vector), this function returns `None`.
```
use cart_lin::lin_to_cart;
let dim_size = [2, 3];
assert_eq!([0, 2], lin_to_cart(2, &dim_size).unwrap());
assert_eq!([1, 1], lin_to_cart(4, &dim_size).unwrap());
assert!(lin_to_cart(6, &dim_size).is_none()); // Out of bounds
```
*/
/**
Like [`lin_to_cart`], but without the checks.
Despite the name, this function itself is safe. However, the index received from this function might be invalid. Using
such an invalid index may cause an out-of-bounds read.
```
use cart_lin::{lin_to_cart, lin_to_cart_unchecked};
let dim_size = [2, 3];
// Valid linear indices
assert_eq!([0, 2], lin_to_cart(2, &dim_size).unwrap());
assert_eq!([0, 2], lin_to_cart_unchecked(2, &dim_size));
// Out-of-bounds linear indices:
assert!(lin_to_cart(6, &dim_size).is_none()); // Out of bounds
assert_eq!([0, 0], lin_to_cart_unchecked(6, &dim_size)); // Nonsensical value (wrapping around)
```
*/
/**
Like [`lin_to_cart`], but mutates `cart_indices` in place instead of returning a new array.
While [`lin_to_cart`] expects arrays (size known at compile time), this function works with slices
(whose length is not known until runtime and may dynamically change).
If the length of `bounds` and `cart_indices` is not identical or the linear index is out of bounds, this function
returns an error (and does not change `cart_indices`).
```
use cart_lin::lin_to_cart_dyn;
let dim_size = vec![2, 3];
let mut indices = vec![0, 0];
assert!(lin_to_cart_dyn(2, dim_size.as_slice(), indices.as_mut_slice()).is_ok());
assert_eq!(&[0, 2], indices.as_slice());
assert!(lin_to_cart_dyn(4, dim_size.as_slice(), indices.as_mut_slice()).is_ok());
assert_eq!(&[1, 1], indices.as_slice());
// Incorrect usage: indices vector is too short -> Error
let mut indices = vec![0];
assert!(lin_to_cart_dyn(4, dim_size.as_slice(), indices.as_mut_slice()).is_err());
```
*/
/**
Like [`lin_to_cart_dyn`], but without the checks.
```
use cart_lin::lin_to_cart_dyn_unchecked;
let dim_size = vec![2, 3];
let mut indices = vec![0, 0];
// Correct usage
lin_to_cart_dyn_unchecked(4, dim_size.as_slice(), indices.as_mut_slice());
assert_eq!(&[1, 1], indices.as_slice());
// Incorrect usage: indices vector is too short, hence only the first value is populated
let mut indices = vec![0];
lin_to_cart_dyn_unchecked(4, dim_size.as_slice(), indices.as_mut_slice());
assert_eq!(&[1], indices.as_slice());
```
*/
/**
An iterator over all cartesian indices within the input dimension sizes.
*/