18
18
//!
19
19
//! For [`Shape`]s, reshaping also expands dimension labels using a
20
20
//! `label/N` naming convention, preserving the semantics of the
21
- //! original shape in the reshaped view_limit .
21
+ //! original shape in the reshaped reshape_with_limit .
22
22
//!
23
- //! See [`view_limit`] and [`reshape_shape`] for entry points.
24
-
23
+ //! See [`reshape_with_limit`] and [`reshape_shape`] for entry points.
25
24
use std:: fmt;
26
25
27
26
use crate :: shape:: Shape ;
@@ -34,7 +33,6 @@ pub type Coord = Vec<usize>;
34
33
/// A reshaped version of a `Shape`, with factored dimensions and
35
34
/// updated labels.
36
35
///
37
- ///
38
36
/// This type preserves coordinate bijections with the original shape
39
37
/// and provides access to the transformed layout and label mappings.
40
38
pub struct ReshapedShape {
@@ -151,7 +149,7 @@ pub fn to_original_coord<'a>(
151
149
///
152
150
/// This limit controls how a given dimension is factored during
153
151
/// reshaping. Values larger than `limit` are recursively decomposed
154
- /// into smaller factors (e.g., `view_limit ([1024],
152
+ /// into smaller factors (e.g., `reshape_with_limit ([1024],
155
153
/// Limit::new(32))` → `[32, 32]`).
156
154
///
157
155
/// The default limit is `32`, which balances fanout depth and layout
@@ -195,7 +193,7 @@ impl From<usize> for Limit {
195
193
/// view by factoring large extents into smaller ones.
196
194
///
197
195
/// This is implemented for [`Slice`], enabling ergonomic access to
198
- /// [`view_limit `] as a method.
196
+ /// [`reshape_with_limit `] as a method.
199
197
///
200
198
/// # Example
201
199
/// ```
@@ -204,7 +202,7 @@ impl From<usize> for Limit {
204
202
/// use ndslice::reshape::ReshapeSliceExt;
205
203
///
206
204
/// let slice = Slice::new_row_major(vec![1024]);
207
- /// let reshaped = slice.view_limit (Limit::new(32));
205
+ /// let reshaped = slice.reshape_with_limit (Limit::new(32));
208
206
/// assert_eq!(reshaped.sizes(), &[32, 32]);
209
207
/// ```
210
208
/// # Returns
@@ -214,20 +212,20 @@ pub trait ReshapeSliceExt {
214
212
/// Returns a reshaped version of this structure by factoring each
215
213
/// dimension into smaller extents no greater than `limit`,
216
214
/// preserving memory layout and flat index semantics. See
217
- /// [`view_limit `] for full behavior and rationale.
215
+ /// [`reshape_with_limit `] for full behavior and rationale.
218
216
///
219
217
/// # Arguments
220
218
/// - `limit`: maximum size allowed in any reshaped dimension
221
219
///
222
220
/// # Returns
223
221
/// A reshaped [`Slice`] with increased dimensionality and a
224
222
/// bijective mapping to the original.
225
- fn view_limit ( & self , limit : Limit ) -> Slice ;
223
+ fn reshape_with_limit ( & self , limit : Limit ) -> Slice ;
226
224
}
227
225
228
226
impl ReshapeSliceExt for Slice {
229
- fn view_limit ( & self , limit : Limit ) -> Slice {
230
- view_limit ( self , limit)
227
+ fn reshape_with_limit ( & self , limit : Limit ) -> Slice {
228
+ reshape_with_limit ( self , limit)
231
229
}
232
230
}
233
231
@@ -244,7 +242,7 @@ impl ReshapeShapeExt for Shape {
244
242
}
245
243
}
246
244
247
- /// For convenient `slice.view_limit ()`, `shape.reshape()`
245
+ /// For convenient `slice.reshape_with_limit ()`, `shape.reshape()`
248
246
/// syntax, `use reshape::prelude::*`.
249
247
pub mod prelude {
250
248
pub use super :: ReshapeShapeExt ;
@@ -274,13 +272,13 @@ pub mod prelude {
274
272
/// ```
275
273
/// use ndslice::Slice;
276
274
/// use ndslice::reshape::Limit;
277
- /// use ndslice::reshape::view_limit ;
275
+ /// use ndslice::reshape::reshape_with_limit ;
278
276
///
279
277
/// let slice = Slice::new_row_major(vec![1024]);
280
- /// let reshaped = view_limit (&slice, Limit::new(32));
278
+ /// let reshaped = reshape_with_limit (&slice, Limit::new(32));
281
279
/// assert_eq!(reshaped.sizes(), &[32, 32]);
282
280
/// ```
283
- pub fn view_limit ( slice : & Slice , limit : Limit ) -> Slice {
281
+ pub fn reshape_with_limit ( slice : & Slice , limit : Limit ) -> Slice {
284
282
let orig_sizes = slice. sizes ( ) ;
285
283
let orig_strides = slice. strides ( ) ;
286
284
@@ -309,7 +307,7 @@ pub fn view_limit(slice: &Slice, limit: Limit) -> Slice {
309
307
/// smaller ones, producing a new shape with expanded dimensionality
310
308
/// and updated labels.
311
309
///
312
- /// This uses [`view_limit `] on the underlying slice and [`expand_labels`]
310
+ /// This uses [`reshape_with_limit `] on the underlying slice and [`expand_labels`]
313
311
/// to generate labels for each factored dimension.
314
312
///
315
313
/// # Arguments
@@ -325,7 +323,7 @@ pub fn view_limit(slice: &Slice, limit: Limit) -> Slice {
325
323
/// occur unless the reshaped slice and labels are inconsistent (a
326
324
/// programming logic error).
327
325
pub fn reshape_shape ( shape : & Shape , limit : Limit ) -> ReshapedShape {
328
- let reshaped_slice = shape. slice ( ) . view_limit ( limit) ;
326
+ let reshaped_slice = shape. slice ( ) . reshape_with_limit ( limit) ;
329
327
let original_labels = shape. labels ( ) ;
330
328
let original_sizes = shape. slice ( ) . sizes ( ) ;
331
329
@@ -446,7 +444,7 @@ mod tests {
446
444
#[ test]
447
445
fn test_reshape_split_1d_row_major ( ) {
448
446
let s = Slice :: new_row_major ( vec ! [ 1024 ] ) ;
449
- let reshaped = s. view_limit ( Limit :: from ( 8 ) ) ;
447
+ let reshaped = s. reshape_with_limit ( Limit :: from ( 8 ) ) ;
450
448
451
449
assert_eq ! ( reshaped. offset( ) , 0 ) ;
452
450
assert_eq ! ( reshaped. sizes( ) , & vec![ 8 , 8 , 8 , 2 ] ) ;
@@ -462,7 +460,7 @@ mod tests {
462
460
#[ test]
463
461
fn test_reshape_6_with_limit_2 ( ) {
464
462
let s = Slice :: new_row_major ( vec ! [ 6 ] ) ;
465
- let reshaped = view_limit ( & s, Limit :: from ( 2 ) ) ;
463
+ let reshaped = reshape_with_limit ( & s, Limit :: from ( 2 ) ) ;
466
464
assert_eq ! ( factor_dims( s. sizes( ) , Limit :: from( 2 ) ) , vec![ vec![ 2 , 3 ] ] ) ;
467
465
assert_layout_preserved ! ( & s, & reshaped) ;
468
466
}
@@ -471,7 +469,7 @@ mod tests {
471
469
fn test_reshape_identity_noop_2d ( ) {
472
470
// All dimensions ≤ limit.
473
471
let original = Slice :: new_row_major ( vec ! [ 4 , 8 ] ) ;
474
- let reshaped = original. view_limit ( Limit :: from ( 8 ) ) ;
472
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 8 ) ) ;
475
473
476
474
assert_eq ! ( reshaped. sizes( ) , original. sizes( ) ) ;
477
475
assert_eq ! ( reshaped. strides( ) , original. strides( ) ) ;
@@ -491,7 +489,7 @@ mod tests {
491
489
fn test_reshape_empty_slice ( ) {
492
490
// 0-dimensional slice.
493
491
let original = Slice :: new_row_major ( vec ! [ ] ) ;
494
- let reshaped = view_limit ( & original, Limit :: from ( 8 ) ) ;
492
+ let reshaped = reshape_with_limit ( & original, Limit :: from ( 8 ) ) ;
495
493
496
494
assert_eq ! ( reshaped. sizes( ) , original. sizes( ) ) ;
497
495
assert_eq ! ( reshaped. strides( ) , original. strides( ) ) ;
@@ -504,7 +502,7 @@ mod tests {
504
502
fn test_reshape_mixed_dims_3d ( ) {
505
503
// 3D slice with one dimension exceeding the limit.
506
504
let original = Slice :: new_row_major ( vec ! [ 6 , 8 , 10 ] ) ;
507
- let reshaped = original. view_limit ( Limit :: from ( 4 ) ) ;
505
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 4 ) ) ;
508
506
509
507
assert_eq ! (
510
508
factor_dims( original. sizes( ) , Limit :: from( 4 ) ) ,
@@ -519,7 +517,7 @@ mod tests {
519
517
fn test_reshape_all_large_dims ( ) {
520
518
// 3D slice with all dimensions exceeding the limit.
521
519
let original = Slice :: new_row_major ( vec ! [ 12 , 18 , 20 ] ) ;
522
- let reshaped = original. view_limit ( Limit :: from ( 4 ) ) ;
520
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 4 ) ) ;
523
521
524
522
assert_eq ! (
525
523
factor_dims( original. sizes( ) , Limit :: from( 4 ) ) ,
@@ -534,7 +532,7 @@ mod tests {
534
532
fn test_reshape_split_1d_factors_3_3_2_2 ( ) {
535
533
// 36 = 3 × 3 × 2 × 2.
536
534
let original = Slice :: new_row_major ( vec ! [ 36 ] ) ;
537
- let reshaped = view_limit ( & original, Limit :: from ( 3 ) ) ;
535
+ let reshaped = reshape_with_limit ( & original, Limit :: from ( 3 ) ) ;
538
536
539
537
assert_eq ! (
540
538
factor_dims( original. sizes( ) , Limit :: from( 3 ) ) ,
@@ -548,7 +546,7 @@ mod tests {
548
546
fn test_reshape_large_prime_dimension ( ) {
549
547
// Prime larger than limit, cannot be factored.
550
548
let original = Slice :: new_row_major ( vec ! [ 7 ] ) ;
551
- let reshaped = view_limit ( & original, Limit :: from ( 4 ) ) ;
549
+ let reshaped = reshape_with_limit ( & original, Limit :: from ( 4 ) ) ;
552
550
553
551
// Should remain as-is since 7 is prime > 4
554
552
assert_eq ! ( factor_dims( original. sizes( ) , Limit :: from( 4 ) ) , vec![ vec![ 7 ] ] ) ;
@@ -561,7 +559,7 @@ mod tests {
561
559
fn test_reshape_split_1d_factors_5_3_2 ( ) {
562
560
// 30 = 5 × 3 × 2, all ≤ limit.
563
561
let original = Slice :: new_row_major ( vec ! [ 30 ] ) ;
564
- let reshaped = view_limit ( & original, Limit :: from ( 5 ) ) ;
562
+ let reshaped = reshape_with_limit ( & original, Limit :: from ( 5 ) ) ;
565
563
566
564
assert_eq ! (
567
565
factor_dims( original. sizes( ) , Limit :: from( 5 ) ) ,
@@ -577,7 +575,7 @@ mod tests {
577
575
fn test_reshape_factors_2_6_2_8_8 ( ) {
578
576
// 12 = 6 × 2, 64 = 8 × 8 — all ≤ 8
579
577
let original = Slice :: new_row_major ( vec ! [ 2 , 12 , 64 ] ) ;
580
- let reshaped = original. view_limit ( Limit :: from ( 8 ) ) ;
578
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 8 ) ) ;
581
579
582
580
assert_eq ! (
583
581
factor_dims( original. sizes( ) , Limit :: from( 8 ) ) ,
@@ -593,7 +591,7 @@ mod tests {
593
591
fn test_reshape_all_dims_within_limit ( ) {
594
592
// Original shape: [2, 3, 4] — all ≤ limit (4).
595
593
let original = Slice :: new_row_major ( vec ! [ 2 , 3 , 4 ] ) ;
596
- let reshaped = original. view_limit ( Limit :: from ( 4 ) ) ;
594
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 4 ) ) ;
597
595
598
596
assert_eq ! (
599
597
factor_dims( original. sizes( ) , Limit :: from( 4 ) ) ,
@@ -610,7 +608,7 @@ mod tests {
610
608
fn test_reshape_degenerate_dimension ( ) {
611
609
// Degenerate dimension should remain unchanged.
612
610
let original = Slice :: new_row_major ( vec ! [ 1 , 12 ] ) ;
613
- let reshaped = original. view_limit ( Limit :: from ( 4 ) ) ;
611
+ let reshaped = original. reshape_with_limit ( Limit :: from ( 4 ) ) ;
614
612
615
613
assert_eq ! (
616
614
factor_dims( original. sizes( ) , Limit :: from( 4 ) ) ,
@@ -633,7 +631,7 @@ mod tests {
633
631
634
632
// Reshape the selected slice using limit=2 in row-major
635
633
// layout.
636
- let reshaped = selected. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
634
+ let reshaped = selected. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
637
635
638
636
assert_eq ! (
639
637
factor_dims( selected. slice( ) . sizes( ) , Limit :: from( 2 ) ) ,
@@ -654,7 +652,7 @@ mod tests {
654
652
let selected = original. select ( "host" , 2 ) . unwrap ( ) ;
655
653
// Reshape the selected slice using limit=2 in row-major
656
654
// layout.
657
- let reshaped = selected. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
655
+ let reshaped = selected. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
658
656
659
657
assert_layout_preserved ! ( selected. slice( ) , & reshaped) ;
660
658
}
@@ -670,7 +668,7 @@ mod tests {
670
668
let selected_host = selected_zone. select ( "host" , 2 ) . unwrap ( ) ;
671
669
assert_eq ! ( selected_host. slice( ) . sizes( ) , & [ 1 , 1 , 5 ] ) ;
672
670
// Reshape with limit = 2.
673
- let reshaped = selected_host. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
671
+ let reshaped = selected_host. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
674
672
675
673
assert_eq ! (
676
674
factor_dims( selected_host. slice( ) . sizes( ) , Limit :: from( 2 ) ) ,
@@ -694,7 +692,7 @@ mod tests {
694
692
assert_eq ! ( selected_host. slice( ) . sizes( ) , & [ 1 , 1 , 8 ] ) ;
695
693
696
694
// Reshape with limit = 2 → gpu=8 should factor
697
- let reshaped = selected_host. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
695
+ let reshaped = selected_host. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
698
696
699
697
assert_eq ! (
700
698
factor_dims( selected_host. slice( ) . sizes( ) , Limit :: from( 2 ) ) ,
@@ -748,7 +746,7 @@ mod tests {
748
746
assert_eq ! ( reshaped. shape. labels( ) , & [ "gpu/0" , "gpu/1" , "gpu/2" ] ) ;
749
747
assert_eq ! ( reshaped. shape. slice( ) . sizes( ) , & [ 2 , 2 , 2 ] ) ;
750
748
751
- let expected = shape. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
749
+ let expected = shape. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
752
750
assert_eq ! ( reshaped. shape. slice( ) , & expected) ;
753
751
}
754
752
@@ -778,7 +776,7 @@ mod tests {
778
776
) ;
779
777
assert_eq ! ( reshaped. shape. slice( ) . sizes( ) , & [ 2 , 2 , 2 , 2 ] ) ;
780
778
781
- let expected = shape. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
779
+ let expected = shape. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
782
780
assert_eq ! ( reshaped. shape. slice( ) , & expected) ;
783
781
}
784
782
@@ -808,7 +806,7 @@ mod tests {
808
806
assert_eq ! ( reshaped. shape. slice( ) . sizes( ) , & [ 1 , 1 , 2 , 2 , 2 ] ) ;
809
807
810
808
// Check against low-level equivalent reshaped slice
811
- let expected = selected_host. slice ( ) . view_limit ( Limit :: from ( 2 ) ) ;
809
+ let expected = selected_host. slice ( ) . reshape_with_limit ( Limit :: from ( 2 ) ) ;
812
810
assert_eq ! ( reshaped. shape. slice( ) , & expected) ;
813
811
}
814
812
}
0 commit comments