@@ -15,9 +15,9 @@ use super::*; use std::marker::PhantomData as PD;
15
15
fn oob ( & self , i : U , j : U ) ->R < ( ) > { let A { m, n, ..} =* self ;
16
16
if ( i==0 ||j==0 ||i>m||j>n) { bail ! ( "({i},{j}) is out-of-bounds of ({m},{n})" ) } ok ! ( ) }
17
17
/// returns the scalar `A_ij` within this array. returns an error if position is out-of-bounds.
18
- pub fn index ( & self , i : U , j : U ) ->R < U > { self . oob ( i, j) ?; let A { m , n , .. } = * self ; let ( i, j) =( i-1 , j-1 ) ; Ok ( ( i* n) +j) }
18
+ pub fn index ( & self , i : U , j : U ) ->R < U > { self . oob ( i, j) ?; let ( i, j) =( i-1 , j-1 ) ; Ok ( ( i* self . n ) +j) }
19
19
/// returns the scalar `A_ij` within this array. does not check if the position is in bounds.
20
- pub fn index_uc ( & self , i : U , j : U ) ->R < U > { let A { m , n , .. } = * self ; let ( i, j) =( i-1 , j-1 ) ; Ok ( ( i* n) +j) }
20
+ pub fn index_uc ( & self , i : U , j : U ) ->R < U > { let ( i, j) =( i-1 , j-1 ) ; Ok ( ( i* self . n ) +j) }
21
21
}
22
22
// === test helpers ===
23
23
/// `A::index` test case generator. check that indexing at a position returns the expected value.
@@ -27,17 +27,17 @@ use super::*; use std::marker::PhantomData as PD;
27
27
#[ macro_export] macro_rules! toob{ ( $f: ident, $a: ident, $i: literal, $j: literal) =>
28
28
{ #[ test] fn $f( ) ->R <( ) >{ is!( $a( ) ?. index( $i, $j) . is_err( ) ) ; ok!( ) } } }
29
29
// === 1x1 array indexing ===
30
- fn sca ( ) ->R < A > { let Ok ( a@A { m : 1 , n : 1 , ..} ) =A :: from_i ( 42 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
30
+ # [ cfg ( test ) ] fn sca ( ) ->R < A > { let Ok ( a@A { m : 1 , n : 1 , ..} ) =A :: from_i ( 42 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
31
31
toob ! ( i00_for_scalar, sca, 0 , 0 ) ; toob ! ( i10_for_scalar, sca, 1 , 0 ) ; toob ! ( i01_for_scalar, sca, 0 , 1 ) ;
32
32
toob ! ( i21_for_scalar, sca, 2 , 1 ) ; toob ! ( i12_for_scalar, sca, 1 , 2 ) ; toob ! ( i22_for_scalar, sca, 2 , 2 ) ;
33
33
ti ! ( i11_for_scalar, sca, 1 , 1 , 0 ) ;
34
34
// === 2x3 array indexing ===
35
- fn arr2x3 ( ) ->R < A > { let Ok ( a@A { m : 2 , n : 3 , ..} ) =A :: zeroed ( 2 , 3 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
35
+ # [ cfg ( test ) ] fn arr2x3 ( ) ->R < A > { let Ok ( a@A { m : 2 , n : 3 , ..} ) =A :: zeroed ( 2 , 3 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
36
36
toob ! ( i00_for_2x3, arr2x3, 0 , 0 ) ; toob ! ( i01_for_2x3, arr2x3, 0 , 1 ) ; toob ! ( i10_for_2x3, arr2x3, 1 , 0 ) ;
37
37
ti ! ( i11_for_2x3, arr2x3, 1 , 1 , 0 ) ; ti ! ( i12_for_2x3, arr2x3, 1 , 2 , 1 ) ; ti ! ( i13_for_2x3, arr2x3, 1 , 3 , 2 ) ;
38
38
ti ! ( i21_for_2x3, arr2x3, 2 , 1 , 3 ) ; ti ! ( i22_for_2x3, arr2x3, 2 , 2 , 4 ) ; ti ! ( i23_for_2x3, arr2x3, 2 , 3 , 5 ) ;
39
39
// === 3x3 array indexing ===
40
- fn arr3x3 ( ) ->R < A > { let Ok ( a@A { m : 3 , n : 3 , ..} ) =A :: zeroed ( 3 , 3 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
40
+ # [ cfg ( test ) ] fn arr3x3 ( ) ->R < A > { let Ok ( a@A { m : 3 , n : 3 , ..} ) =A :: zeroed ( 3 , 3 ) else { bail ! ( "bad dims" ) } ; Ok ( a) }
41
41
toob ! ( i00_for_3x3, arr3x3, 0 , 0 ) ; toob ! ( i01_for_3x3, arr3x3, 0 , 1 ) ; toob ! ( i14_for_3x3, arr3x3, 1 , 4 ) ;
42
42
toob ! ( i41_for_3x3, arr3x3, 4 , 1 ) ; toob ! ( i44_for_3x3, arr3x3, 4 , 4 ) ;
43
43
ti ! ( i11_for_3x3, arr3x3, 1 , 1 , 0 ) ; ti ! ( i21_for_3x3, arr3x3, 2 , 1 , 3 ) ; ti ! ( i22_for_3x3, arr3x3, 2 , 2 , 4 ) ;
@@ -111,8 +111,7 @@ use super::*; use std::marker::PhantomData as PD;
111
111
impl A < MI > { // only allow matrix access for initialized arrays
112
112
pub fn into_matrix ( & self ) ->R < Vec < & [ I ] > > { let ( A { m, n, ..} ) =* self ;
113
113
( 0 ..m) . map ( |m|{ self . ptr_at ( m+1 , 1 ) } ) . map ( |r|r. map ( |d|unsafe { from_raw_parts ( d as * mut I , n as U ) } ) ) . collect ( ) } }
114
- impl TF < & [ & [ I ] ] > for A { type Error =E ; fn try_from ( s : & [ & [ I ] ] ) ->R < A > { todo ! ( "TF<&[I]> for A" ) } }
115
- impl PE < & [ & [ I ] ] > for A { fn eq ( & self , r : & & [ & [ I ] ] ) ->bool { let ( A { m, n, ..} ) =self ;
114
+ impl PE < & [ & [ I ] ] > for A { fn eq ( & self , r : & & [ & [ I ] ] ) ->bool {
116
115
if ( r. len ( ) !=self . m ) { r ! ( false ) } for ( i, r_i) in ( r. into_iter ( ) . enumerate ( ) ) {
117
116
if ( r_i. len ( ) !=self . n ) { r ! ( false ) } for ( j, r_ij) in ( r_i. into_iter ( ) . enumerate ( ) ) {
118
117
let ( i, j) =( i+1 , j+1 ) ; let ( a_ij) =match ( self . get ( i, j) ) { Ok ( v) =>v, Err ( _) =>r ! ( false ) } ;
@@ -121,7 +120,7 @@ use super::*; use std::marker::PhantomData as PD;
121
120
/**monadic verbs*/ impl A {
122
121
pub fn m_same ( self ) ->R < A > { Ok ( self ) }
123
122
pub fn m_idot ( self ) ->R < A > { let ( a@A { m, n, ..} ) =self ; let gi=|i, j|a. get ( i, j) ?. try_into ( ) . map_err ( E :: from) ;
124
- if let ( 1 , 1 ) =( m, n) { let ( m , n) =( 1 , gi ( 1 , 1 ) ?) ; let ( mut o) =A :: new ( 1 , n) ?;
123
+ if let ( 1 , 1 ) =( m, n) { let ( n) =( gi ( 1 , 1 ) ?) ; let ( mut o) =A :: new ( 1 , n) ?;
125
124
for ( j) in ( 1 ..=n) { o. set ( 1 , j, ( j-1 ) . try_into ( ) ?) ?; } Ok ( unsafe { o. finish ( ) } ) }
126
125
else if let ( 1 , 2 ) =( m, n) { let ( m, n) =( gi ( 1 , 1 ) ?, gi ( 1 , 2 ) ?) ;
127
126
let ( mut v) =0_u32 ; let ( f) =move |_, _|{ let ( v_o) =v; v+=1 ; Ok ( v_o) } ; A :: new ( m, n) ?. init_with ( f) }
@@ -136,9 +135,9 @@ use super::*; use std::marker::PhantomData as PD;
136
135
/*return dyad function**/ pub fn f ( & self ) ->fn ( I , I ) ->I { use D :: * ;
137
136
match ( self ) { Plus =>D :: add, Mul =>D :: mul, Left =>D :: left, Right =>D :: right} }
138
137
/*add two numbers*/ fn add ( x : I , y : I ) ->I { x+y} /*multiply two numbers*/ fn mul ( x : I , y : I ) ->I { x* y}
139
- /*left */ fn left ( x : I , y : I ) ->I { x } /*right */ fn right ( x : I , y : I ) ->I { y}
138
+ /*left */ fn left ( x : I , _ : I ) ->I { x } /*right */ fn right ( _ : I , y : I ) ->I { y}
140
139
} impl A {
141
- pub fn d_left ( self , r : A ) ->R < A > { Ok ( self ) }
140
+ pub fn d_left ( self , _ : A ) ->R < A > { Ok ( self ) }
142
141
pub fn d_right ( self , r : A ) ->R < A > { Ok ( r) }
143
142
pub fn d_plus ( self , r : A ) ->R < A > { A :: d_do ( self , r, D :: add) }
144
143
pub fn d_mul ( self , r : A ) ->R < A > { A :: d_do ( self , r, D :: mul) }
@@ -171,7 +170,7 @@ use super::*; use std::marker::PhantomData as PD;
171
170
// === monadic `/`, `Ym::Insert` tests
172
171
macro_rules! test_insert{ ( $f: ident, $d: expr, $a: expr, $o: expr) =>
173
172
{ #[ test] fn $f( ) ->R <( ) >{ let ( a) : R <A >={ $a} ; let ( d) : D ={ $d} ; // typecheck macro arguments.
174
- let i: I =a. and_then( |a: A |Ym :: insert( $ d, a) ) . and_then( |a|a. as_i( ) ) ?;
173
+ let i: I =a. and_then( |a: A |Ym :: insert( d, a) ) . and_then( |a|a. as_i( ) ) ?;
175
174
eq!( i, $o) ; ok!( ) } } }
176
175
test_insert ! ( add_a_scalar, D :: Plus , A :: from_i( 42 ) , 42 ) ;
177
176
test_insert ! ( add_a_sequence, D :: Plus , <A as TF <& [ I ] >>:: try_from( & [ 1 , 2 , 3 , 4 , 5 ] ) , ( 1 +2 +3 +4 +5 ) ) ;
@@ -188,15 +187,14 @@ use super::*; use std::marker::PhantomData as PD;
188
187
let f=|i, j|->R < I > { let ( x, y) =( l[ i-1 ] , r[ j-1 ] ) ; Ok ( d ( x, y) ) } ;
189
188
A :: new ( m, n) ?. init_with ( f) }
190
189
else { bail ! ( "unexpected fallthrough in Yd::table" ) } }
191
- fn infix ( d : D , l : A , r : A ) ->R < A > { let ( s) =r. as_slice ( ) . map_err ( |_|err ! ( "infix rhs must be a slice" ) ) ?;
190
+ fn infix ( _ : D , l : A , r : A ) ->R < A > { let ( s) =r. as_slice ( ) . map_err ( |_|err ! ( "infix rhs must be a slice" ) ) ?;
192
191
let ( il) =l. as_i ( ) . map_err ( |_|err ! ( "infix lhs must be a scalar" ) ) ?. try_into ( ) ?;
193
192
let ( ic) =( s. len ( ) -il) +1 ;
194
193
A :: new ( ic, il) ?. init_with ( |i, j|Ok ( s[ ( i-1 ) +( j-1 ) ] ) ) } } }
195
194
196
- /**deep-copy*/ impl A < MI > {
197
- pub fn deep_copy ( & self ) ->R < A > { let A { m, n, l : li, d : di, i : _} =* self ; A :: new ( m, n) ?. init_with ( |i, j|{ self . get ( i, j) } ) } }
195
+ /**deep-copy*/ impl A < MI > { pub fn deep_copy ( & self ) ->R < A > { A :: new ( self . m , self . n ) ?. init_with ( |i, j|{ self . get ( i, j) } ) } }
198
196
199
197
/**display*/ mod fmt{ use super :: * ;
200
198
impl DS for A < MI > {
201
- fn fmt ( & self , fmt : & mut FMT ) ->FR { let A { m , n, ..} =* self ; for ( i, j) in ( self . iter ( ) )
199
+ fn fmt ( & self , fmt : & mut FMT ) ->FR { let A { n, ..} =* self ; for ( i, j) in ( self . iter ( ) )
202
200
{ let ( x) =self . get_uc ( i, j) . map_err ( |_|std:: fmt:: Error ) ?; write ! ( fmt, "{x}{}" , if ( j==n) { '\n' } else{ ' ' } ) ?; } ok ! ( ) } } }
0 commit comments