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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
use core::marker::PhantomData;
use core::ops::Range;
use crate::Matrix;
/// A matrix wrapper that exposes a contiguous range of columns from an inner matrix.
///
/// This struct:
/// - wraps another matrix,
/// - restricts access to only the columns within the specified `column_range`.
#[derive(Clone)]
pub struct HorizontallyTruncated<T, Inner> {
/// The underlying full matrix being wrapped.
inner: Inner,
/// The range of columns to expose from the inner matrix.
column_range: Range<usize>,
/// Marker for the element type `T`, not used at runtime.
_phantom: PhantomData<T>,
}
impl<T, Inner: Matrix<T>> HorizontallyTruncated<T, Inner>
where
T: Send + Sync + Clone,
{
/// Construct a new horizontally truncated view of a matrix.
///
/// # Arguments
/// - `inner`: The full inner matrix to be wrapped.
/// - `truncated_width`: The number of columns to expose from the start (must be ≤ `inner.width()`).
///
/// This is equivalent to `new_with_range(inner, 0..truncated_width)`.
///
/// Returns `None` if `truncated_width` is greater than the width of the inner matrix.
pub fn new(inner: Inner, truncated_width: usize) -> Option<Self> {
Self::new_with_range(inner, 0..truncated_width)
}
/// Construct a new view exposing a specific column range of a matrix.
///
/// # Arguments
/// - `inner`: The full inner matrix to be wrapped.
/// - `column_range`: The range of columns to expose (must satisfy `column_range.end <= inner.width()`).
///
/// Returns `None` if the column range extends beyond the width of the inner matrix.
pub fn new_with_range(inner: Inner, column_range: Range<usize>) -> Option<Self> {
(column_range.end <= inner.width()).then(|| Self {
inner,
column_range,
_phantom: PhantomData,
})
}
}
impl<T, Inner> Matrix<T> for HorizontallyTruncated<T, Inner>
where
T: Send + Sync + Clone,
Inner: Matrix<T>,
{
/// Returns the number of columns exposed by the truncated matrix.
#[inline(always)]
fn width(&self) -> usize {
self.column_range.len()
}
/// Returns the number of rows in the matrix (same as the inner matrix).
#[inline(always)]
fn height(&self) -> usize {
self.inner.height()
}
#[inline(always)]
unsafe fn get_unchecked(&self, r: usize, c: usize) -> T {
unsafe {
// Safety: The caller must ensure that `c < self.width()` and `r < self.height()`.
//
// We translate the column index by adding `column_range.start`.
self.inner.get_unchecked(r, self.column_range.start + c)
}
}
unsafe fn row_unchecked(
&self,
r: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync> {
unsafe {
// Safety: The caller must ensure that `r < self.height()`.
self.inner
.row_subseq_unchecked(r, self.column_range.start, self.column_range.end)
}
}
unsafe fn row_subseq_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl IntoIterator<Item = T, IntoIter = impl Iterator<Item = T> + Send + Sync> {
unsafe {
// Safety: The caller must ensure that r < self.height() and start <= end <= self.width().
//
// We translate the column indices by adding `column_range.start`.
self.inner.row_subseq_unchecked(
r,
self.column_range.start + start,
self.column_range.start + end,
)
}
}
unsafe fn row_subslice_unchecked(
&self,
r: usize,
start: usize,
end: usize,
) -> impl core::ops::Deref<Target = [T]> {
unsafe {
// Safety: The caller must ensure that `r < self.height()` and `start <= end <= self.width()`.
//
// We translate the column indices by adding `column_range.start`.
self.inner.row_subslice_unchecked(
r,
self.column_range.start + start,
self.column_range.start + end,
)
}
}
}
#[cfg(test)]
mod tests {
use alloc::vec;
use alloc::vec::Vec;
use super::*;
use crate::dense::RowMajorMatrix;
#[test]
fn test_truncate_width_by_one() {
// Create a 3x4 matrix:
// [ 1 2 3 4]
// [ 5 6 7 8]
// [ 9 10 11 12]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 4);
// Truncate to width 3.
let truncated = HorizontallyTruncated::new(inner, 3).unwrap();
// Width should be 3.
assert_eq!(truncated.width(), 3);
// Height remains unchanged.
assert_eq!(truncated.height(), 3);
// Check individual elements.
assert_eq!(truncated.get(0, 0), Some(1)); // row 0, col 0
assert_eq!(truncated.get(1, 1), Some(6)); // row 1, col 1
unsafe {
assert_eq!(truncated.get_unchecked(0, 1), 2); // row 0, col 1
assert_eq!(truncated.get_unchecked(2, 2), 11); // row 1, col 0
}
// Row 0: should return [1, 2, 3]
let row0: Vec<_> = truncated.row(0).unwrap().into_iter().collect();
assert_eq!(row0, vec![1, 2, 3]);
unsafe {
// Row 2: should return [5, 6, 7]
let row1: Vec<_> = truncated.row_unchecked(1).into_iter().collect();
assert_eq!(row1, vec![5, 6, 7]);
// Row 3: is equal to return [9, 10, 11]
let row3_subset: Vec<_> = truncated
.row_subseq_unchecked(2, 1, 2)
.into_iter()
.collect();
assert_eq!(row3_subset, vec![10]);
}
unsafe {
let row1 = truncated.row_slice(1).unwrap();
assert_eq!(&*row1, &[5, 6, 7]);
let row2 = truncated.row_slice_unchecked(2);
assert_eq!(&*row2, &[9, 10, 11]);
let row0_subslice = truncated.row_subslice_unchecked(0, 0, 2);
assert_eq!(&*row0_subslice, &[1, 2]);
}
assert!(truncated.get(0, 3).is_none()); // Width out of bounds
assert!(truncated.get(3, 0).is_none()); // Height out of bounds
assert!(truncated.row(3).is_none()); // Height out of bounds
assert!(truncated.row_slice(3).is_none()); // Height out of bounds
// Convert the truncated view to a RowMajorMatrix and check contents.
let as_matrix = truncated.to_row_major_matrix();
// The expected matrix after truncation:
// [1 2 3]
// [5 6 7]
// [9 10 11]
let expected = RowMajorMatrix::new(vec![1, 2, 3, 5, 6, 7, 9, 10, 11], 3);
assert_eq!(as_matrix, expected);
}
#[test]
fn test_no_truncation() {
// 2x2 matrix:
// [ 7 8 ]
// [ 9 10 ]
let inner = RowMajorMatrix::new(vec![7, 8, 9, 10], 2);
// Truncate to full width (no change).
let truncated = HorizontallyTruncated::new(inner, 2).unwrap();
assert_eq!(truncated.width(), 2);
assert_eq!(truncated.height(), 2);
assert_eq!(truncated.get(0, 1).unwrap(), 8);
assert_eq!(truncated.get(1, 0).unwrap(), 9);
unsafe {
assert_eq!(truncated.get_unchecked(0, 0), 7);
assert_eq!(truncated.get_unchecked(1, 1), 10);
}
let row0: Vec<_> = truncated.row(0).unwrap().into_iter().collect();
assert_eq!(row0, vec![7, 8]);
let row1: Vec<_> = unsafe { truncated.row_unchecked(1).into_iter().collect() };
assert_eq!(row1, vec![9, 10]);
assert!(truncated.get(0, 2).is_none()); // Width out of bounds
assert!(truncated.get(2, 0).is_none()); // Height out of bounds
assert!(truncated.row(2).is_none()); // Height out of bounds
assert!(truncated.row_slice(2).is_none()); // Height out of bounds
}
#[test]
fn test_truncate_to_zero_width() {
// 1x3 matrix: [11 12 13]
let inner = RowMajorMatrix::new(vec![11, 12, 13], 3);
// Truncate to width 0.
let truncated = HorizontallyTruncated::new(inner, 0).unwrap();
assert_eq!(truncated.width(), 0);
assert_eq!(truncated.height(), 1);
// Row should be empty.
assert!(truncated.row(0).unwrap().into_iter().next().is_none());
assert!(truncated.get(0, 0).is_none()); // Width out of bounds
assert!(truncated.get(1, 0).is_none()); // Height out of bounds
assert!(truncated.row(1).is_none()); // Height out of bounds
assert!(truncated.row_slice(1).is_none()); // Height out of bounds
}
#[test]
fn test_invalid_truncation_width() {
// 2x2 matrix:
// [1 2]
// [3 4]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4], 2);
// Attempt to truncate beyond inner width (invalid).
assert!(HorizontallyTruncated::new(inner, 5).is_none());
}
#[test]
fn test_column_range_middle() {
// Create a 3x5 matrix:
// [ 1 2 3 4 5]
// [ 6 7 8 9 10]
// [11 12 13 14 15]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], 5);
// Select columns 1..4 (columns 1, 2, 3).
let view = HorizontallyTruncated::new_with_range(inner, 1..4).unwrap();
// Width should be 3 (columns 1, 2, 3).
assert_eq!(view.width(), 3);
// Height remains unchanged.
assert_eq!(view.height(), 3);
// Check individual elements (column indices are relative to the view).
assert_eq!(view.get(0, 0), Some(2)); // row 0, col 0 -> inner col 1
assert_eq!(view.get(0, 1), Some(3)); // row 0, col 1 -> inner col 2
assert_eq!(view.get(0, 2), Some(4)); // row 0, col 2 -> inner col 3
assert_eq!(view.get(1, 0), Some(7)); // row 1, col 0 -> inner col 1
assert_eq!(view.get(2, 2), Some(14)); // row 2, col 2 -> inner col 3
unsafe {
assert_eq!(view.get_unchecked(1, 1), 8); // row 1, col 1 -> inner col 2
assert_eq!(view.get_unchecked(2, 0), 12); // row 2, col 0 -> inner col 1
}
// Row 0: should return [2, 3, 4]
let row0: Vec<_> = view.row(0).unwrap().into_iter().collect();
assert_eq!(row0, vec![2, 3, 4]);
// Row 1: should return [7, 8, 9]
let row1: Vec<_> = view.row(1).unwrap().into_iter().collect();
assert_eq!(row1, vec![7, 8, 9]);
unsafe {
// Row 2: should return [12, 13, 14]
let row2: Vec<_> = view.row_unchecked(2).into_iter().collect();
assert_eq!(row2, vec![12, 13, 14]);
// Subsequence of row 1, cols 1..3 (view indices) -> [8, 9]
let row1_subseq: Vec<_> = view.row_subseq_unchecked(1, 1, 3).into_iter().collect();
assert_eq!(row1_subseq, vec![8, 9]);
}
// Out of bounds checks.
assert!(view.get(0, 3).is_none()); // Width out of bounds
assert!(view.get(3, 0).is_none()); // Height out of bounds
// Convert the view to a RowMajorMatrix and check contents.
let as_matrix = view.to_row_major_matrix();
// The expected matrix after selecting columns 1..4:
// [2 3 4]
// [7 8 9]
// [12 13 14]
let expected = RowMajorMatrix::new(vec![2, 3, 4, 7, 8, 9, 12, 13, 14], 3);
assert_eq!(as_matrix, expected);
}
#[test]
fn test_column_range_end() {
// Create a 2x4 matrix:
// [1 2 3 4]
// [5 6 7 8]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6, 7, 8], 4);
// Select columns 2..4 (columns 2, 3).
let view = HorizontallyTruncated::new_with_range(inner, 2..4).unwrap();
assert_eq!(view.width(), 2);
assert_eq!(view.height(), 2);
// Row 0: should return [3, 4]
let row0: Vec<_> = view.row(0).unwrap().into_iter().collect();
assert_eq!(row0, vec![3, 4]);
// Row 1: should return [7, 8]
let row1: Vec<_> = view.row(1).unwrap().into_iter().collect();
assert_eq!(row1, vec![7, 8]);
assert_eq!(view.get(0, 0), Some(3));
assert_eq!(view.get(1, 1), Some(8));
}
#[test]
fn test_column_range_single_column() {
// Create a 3x4 matrix:
// [1 2 3 4]
// [5 6 7 8]
// [9 10 11 12]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 4);
// Select only column 2.
let view = HorizontallyTruncated::new_with_range(inner, 2..3).unwrap();
assert_eq!(view.width(), 1);
assert_eq!(view.height(), 3);
assert_eq!(view.get(0, 0), Some(3));
assert_eq!(view.get(1, 0), Some(7));
assert_eq!(view.get(2, 0), Some(11));
// Row 0: should return [3]
let row0: Vec<_> = view.row(0).unwrap().into_iter().collect();
assert_eq!(row0, vec![3]);
}
#[test]
fn test_column_range_empty() {
// Create a 2x3 matrix:
// [1 2 3]
// [4 5 6]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6], 3);
// Select empty range (2..2).
let view = HorizontallyTruncated::new_with_range(inner, 2..2).unwrap();
assert_eq!(view.width(), 0);
assert_eq!(view.height(), 2);
// Row should be empty.
assert!(view.row(0).unwrap().into_iter().next().is_none());
}
#[test]
fn test_invalid_column_range() {
// Create a 2x3 matrix:
// [1 2 3]
// [4 5 6]
let inner = RowMajorMatrix::new(vec![1, 2, 3, 4, 5, 6], 3);
// Attempt to select columns 1..5 (extends beyond width).
assert!(HorizontallyTruncated::new_with_range(inner, 1..5).is_none());
}
}