@@ -41,32 +41,26 @@ use super::*; use std::marker::PhantomData as PD;
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 ) ;
44
- ti ! ( i31_for_3x3, arr3x3, 3 , 1 , 6 ) ; ti ! ( i33_for_3x3, arr3x3, 3 , 3 , 8 ) ;
45
- }
44
+ ti ! ( i31_for_3x3, arr3x3, 3 , 1 , 6 ) ; ti ! ( i33_for_3x3, arr3x3, 3 , 3 , 8 ) ; }
46
45
47
46
/**array allocation*/ mod alloc{ use { super :: * , std:: alloc:: { alloc, alloc_zeroed, dealloc} } ;
48
47
/**sealed trait, memory markers*/ pub trait MX { } impl MX for MU { } impl MX for MI { }
49
48
/**marker: memory uninitialized*/ #[ derive( CL , DBG ) ] pub struct MU ;
50
49
/**marker: memory initialized*/ #[ derive( CL , DBG ) ] pub struct MI ;
51
50
impl A < MU > {
52
51
pub fn new ( m : U , n : U ) ->R < Self > { Self :: alloc ( m, n) . map ( |( l, d) |A { m, n, d, l, i : PD } ) }
53
- // TODO: use `A::iter`
54
- // TODO: use `set_unchecked` here.
55
52
pub fn init_with < F : FnMut ( U , U ) ->R < I > > ( mut self , mut f : F ) ->R < A < MI > > { let ( A { m, n, ..} ) =self ;
56
53
for ( i) in ( 1 ..=m) { for ( j) in ( 1 ..=n) { let ( v) =f ( i, j) ?; self . set ( i, j, v) ?; } } Ok ( unsafe { self . finish ( ) } ) }
57
- pub unsafe fn finish ( self ) ->A < MI > { std:: mem:: transmute ( self ) }
58
- }
54
+ pub unsafe fn finish ( self ) ->A < MI > { std:: mem:: transmute ( self ) } }
59
55
impl A < MI > {
60
56
pub fn zeroed ( m : U , n : U ) ->R < Self > { let ( l, d) =Self :: allocz ( m, n) ?; Ok ( A { m, n, d, l, i : PD } ) }
61
57
pub fn from_i ( i : I ) ->R < Self > { let mut a=A :: new ( 1 , 1 ) ?; a. set ( 1 , 1 , i) ?; Ok ( unsafe { a. finish ( ) } ) } }
62
58
impl TF < I > for A < MI > { type Error =E ; fn try_from ( i : I ) ->R < Self > { A :: from_i ( i) } }
63
59
impl < X : MX > A < X > {
64
- fn alloc ( m : U , n : U ) ->R < ( L , * mut u8 ) > { let ( l) =Self :: l ( m, n) ?; let d=unsafe { alloc ( l) } ; Ok ( ( l, d) ) }
60
+ fn alloc ( m : U , n : U ) ->R < ( L , * mut u8 ) > { let ( l) =Self :: l ( m, n) ?; let d=unsafe { alloc ( l) } ; Ok ( ( l, d) ) }
65
61
fn allocz ( m : U , n : U ) ->R < ( L , * mut u8 ) > { let ( l) =Self :: l ( m, n) ?; let d=unsafe { alloc_zeroed ( l) } ; Ok ( ( l, d) ) }
66
- fn l ( m : U , n : U ) ->R < L > { L :: array :: < I > ( m* n) . map_err ( E :: from) }
67
- }
62
+ fn l ( m : U , n : U ) ->R < L > { L :: array :: < I > ( m* n) . map_err ( E :: from) } }
68
63
impl < M > Drop for A < M > { fn drop ( & mut self ) { let ( A { d, l, ..} ) =self ; unsafe { dealloc ( * d, * l) } } }
69
- // TODO: add compile_fail checks to ensure that e.g. `get` cannot be used on an uninitialized array
70
64
} pub use self :: alloc:: { MI , MU , MX } ;
71
65
72
66
/**array access*/ mod access{ use { super :: * , std:: mem:: size_of} ;
@@ -86,8 +80,7 @@ use super::*; use std::marker::PhantomData as PD;
86
80
pub ( crate ) fn ptr_at_uc ( & self , i : U , j : U ) ->R < * mut I > { self . ptr_at_impl ( i, j, Self :: index_uc) }
87
81
fn ptr_at_impl < F : Fn ( & Self , U , U ) ->R < U > > ( & self , i : U , j : U , f : F ) ->R < * mut I > {
88
82
let ( o) : isize =f ( self , i, j) . map ( |x|x* size_of :: < I > ( ) ) ?. try_into ( ) ?; let ( d) =unsafe { ( self . d . offset ( o) as * mut I ) } ;
89
- Ok ( d) }
90
- } }
83
+ Ok ( d) } } }
91
84
92
85
/**scalar conversion/comparison*/ mod scalars{ use super :: * ; /*todo...*/
93
86
impl A < MI > { pub fn as_i ( & self ) ->R < I > { let a@A { m : 1 , n : 1 , ..} =self else { bail ! ( "not a scalar" ) } ; a. get ( 1 , 1 ) } }
@@ -123,8 +116,7 @@ use super::*; use std::marker::PhantomData as PD;
123
116
if ( r. len ( ) !=self . m ) { r ! ( false ) } for ( i, r_i) in ( r. into_iter ( ) . enumerate ( ) ) {
124
117
if ( r_i. len ( ) !=self . n ) { r ! ( false ) } for ( j, r_ij) in ( r_i. into_iter ( ) . enumerate ( ) ) {
125
118
let ( i, j) =( i+1 , j+1 ) ; let ( a_ij) =match ( self . get ( i, j) ) { Ok ( v) =>v, Err ( _) =>r ! ( false ) } ;
126
- if ( a_ij) !=( * r_ij) { r ! ( false ) } } } true } }
127
- }
119
+ if ( a_ij) !=( * r_ij) { r ! ( false ) } } } true } } }
128
120
129
121
/**monadic verbs*/ impl A {
130
122
pub fn m_same ( self ) ->R < A > { Ok ( self ) }
@@ -138,8 +130,7 @@ use super::*; use std::marker::PhantomData as PD;
138
130
pub fn m_tally ( self ) ->R < A > { let A { m, n, ..} =self ; let ( i) =I :: try_from ( m* n) ?; A :: from_i ( i) }
139
131
pub fn m_trans ( self ) ->R < A > { let ( i@A { m : m_i, n : n_i, ..} ) =self ; let ( m_o, n_o) =( n_i, m_i) ;
140
132
let ( f) =|i_o, j_o|{ i. get ( j_o, i_o) } ; A :: new ( m_o, n_o) ?. init_with ( f) }
141
- pub fn m_inc ( self ) ->R < A > { let ( a@A { m, n, ..} ) =self ; A :: new ( m, n) ?. init_with ( |i, j|a. get ( i, j) . map ( |x|x+1 ) ) }
142
- }
133
+ pub fn m_inc ( self ) ->R < A > { let ( a@A { m, n, ..} ) =self ; A :: new ( m, n) ?. init_with ( |i, j|a. get ( i, j) . map ( |x|x+1 ) ) } }
143
134
144
135
/**dyadic verbs*/ impl D {
145
136
/*return dyad function**/ pub fn f ( & self ) ->fn ( I , I ) ->I { use D :: * ;
@@ -163,8 +154,7 @@ use super::*; use std::marker::PhantomData as PD;
163
154
else if ( ml==nr) &&( nl==mr) /*NB: inherit the dimensions of the right-hand operand.*/ // rotation
164
155
{ let ( f) =|i, j|{ let ( x) =l. get ( j, i) ?; let ( y) =r. get ( i, j) ?; Ok ( f ( x, y) ) } ; r ! ( A :: new( mr, nr) ?. init_with( f) ) }
165
156
bail ! ( "length error" ) ;
166
- }
167
- }
157
+ } }
168
158
169
159
/**monadic adverbs*/ mod adverbs_m{ use super :: * ;
170
160
impl Ym {
@@ -177,17 +167,15 @@ use super::*; use std::marker::PhantomData as PD;
177
167
else if let Ok ( s) =a. as_slice ( ) { let ( m, n) =( s. len ( ) , s. len ( ) ) ;
178
168
let p=|i, j|if ( j>i) { Ok ( 0 ) } else { a. get ( 1 , j) . map ( d_) } ;
179
169
A :: new ( m, n) ?. init_with ( p) }
180
- else { bail ! ( "monadic `\\ ` is not implemented for matrices" ) } }
181
- }
170
+ else { bail ! ( "monadic `\\ ` is not implemented for matrices" ) } } }
182
171
// === monadic `/`, `Ym::Insert` tests
183
172
macro_rules! test_insert{ ( $f: ident, $d: expr, $a: expr, $o: expr) =>
184
173
{ #[ test] fn $f( ) ->R <( ) >{ let ( a) : R <A >={ $a} ; let ( d) : D ={ $d} ; // typecheck macro arguments.
185
174
let i: I =a. and_then( |a: A |Ym :: insert( $d, a) ) . and_then( |a|a. as_i( ) ) ?;
186
175
eq!( i, $o) ; ok!( ) } } }
187
176
test_insert ! ( add_a_scalar, D :: Plus , A :: from_i( 42 ) , 42 ) ;
188
177
test_insert ! ( add_a_sequence, D :: Plus , <A as TF <& [ I ] >>:: try_from( & [ 1 , 2 , 3 , 4 , 5 ] ) , ( 1 +2 +3 +4 +5 ) ) ;
189
- test_insert ! ( mul_a_sequence, D :: Mul , <A as TF <& [ I ] >>:: try_from( & [ 1 , 2 , 3 , 4 , 5 ] ) , ( 1 * 2 * 3 * 4 * 5 ) ) ;
190
- }
178
+ test_insert ! ( mul_a_sequence, D :: Mul , <A as TF <& [ I ] >>:: try_from( & [ 1 , 2 , 3 , 4 , 5 ] ) , ( 1 * 2 * 3 * 4 * 5 ) ) ; }
191
179
192
180
/**dyadic adverbs*/ mod adverbs_d{ use super :: * ;
193
181
impl Yd {
@@ -203,17 +191,12 @@ use super::*; use std::marker::PhantomData as PD;
203
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" ) ) ?;
204
192
let ( il) =l. as_i ( ) . map_err ( |_|err ! ( "infix lhs must be a scalar" ) ) ?. try_into ( ) ?;
205
193
let ( ic) =( s. len ( ) -il) +1 ;
206
- A :: new ( ic, il) ?. init_with ( |i, j|Ok ( s[ ( i-1 ) +( j-1 ) ] ) ) }
207
- }
208
- }
194
+ A :: new ( ic, il) ?. init_with ( |i, j|Ok ( s[ ( i-1 ) +( j-1 ) ] ) ) } } }
209
195
210
196
/**deep-copy*/ impl A < MI > {
211
- 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) } ) }
212
- }
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) } ) } }
213
198
214
199
/**display*/ mod fmt{ use super :: * ;
215
200
impl DS for A < MI > {
216
- // TODO: buffer stdout, flush after loops
217
- // TODO: use `unchecked` to elide bounds checks in printing
218
201
fn fmt ( & self , fmt : & mut FMT ) ->FR { let A { m, n, ..} =* self ; for ( i, j) in ( self . iter ( ) )
219
202
{ let ( x) =self . get_uc ( i, j) . map_err ( |_|std:: fmt:: Error ) ?; write ! ( fmt, "{x}{}" , if ( j==n) { '\n' } else{ ' ' } ) ?; } ok ! ( ) } } }
0 commit comments