@@ -165,3 +165,65 @@ macro_rules! seq {
165
165
$crate:: Seq :: <i32 >:: new( $start, $end, $step)
166
166
} ;
167
167
}
168
+
169
+ /// Indexing into an existing Array
170
+ ///
171
+ /// This macro call with return an Array that has a view of another Array. The Array returned due to
172
+ /// the indexing operation will follow copy-on-write semantics. The Array identifier taken by this
173
+ /// macro is passed to the relevant internal functions as a borrowed reference. Thus, this identifier
174
+ /// will be still available for futher use after the macro call.
175
+ ///
176
+ /// The following types of inputs are matched by this macro.
177
+ ///
178
+ /// - A simple Array identifier.
179
+ /// - An Array with slicing info for indexing.
180
+ /// - An Array with slicing info and other arrays used for indexing.
181
+ #[ macro_export]
182
+ macro_rules! view {
183
+ ( @af_max_dims) => {
184
+ 4
185
+ } ;
186
+ ( $array_ident: ident ) => {
187
+ $array_ident. clone( )
188
+ } ;
189
+ ( $array_ident: ident [ $( $start: literal : $end: literal : $step: literal) ,+ ] ) => {
190
+ {
191
+ let AF_MAX_DIMS : usize = view!( @af_max_dims) ;
192
+ let mut seq_vec = Vec :: <$crate:: Seq <i32 >>:: with_capacity( AF_MAX_DIMS ) ;
193
+ $(
194
+ seq_vec. push( $crate:: seq!( $start: $end: $step) ) ;
195
+ ) *
196
+ for span_place_holder in seq_vec. len( ) ..AF_MAX_DIMS {
197
+ seq_vec. push( $crate:: seq!( ) ) ;
198
+ }
199
+ $crate:: index( & $array_ident, & seq_vec)
200
+ }
201
+ } ;
202
+ ( @set_indexer $idim: expr, $idxr: ident, $lterm: expr) => {
203
+ {
204
+ $idxr. set_index( & $lterm, $idim, None ) ;
205
+ }
206
+ } ;
207
+ ( @set_indexer $idim: expr, $idxr: ident, $hterm: expr, $( $tterm: expr) ,* ) => {
208
+ {
209
+ $idxr. set_index( & $hterm, $idim, None ) ;
210
+ view!( @set_indexer $idim + 1 , $idxr, $( $tterm) ,* ) ;
211
+ }
212
+ } ;
213
+ ( $array_ident: ident [ $( $_e: expr) ,+ ] ) => {
214
+ {
215
+ let AF_MAX_DIMS : u32 = view!( @af_max_dims) ;
216
+ let span = $crate:: seq!( ) ;
217
+ let mut idxrs = $crate:: Indexer :: default ( ) ;
218
+
219
+ view!( @set_indexer 0 , idxrs, $( $_e) ,* ) ;
220
+
221
+ let mut dimIx = idxrs. len( ) as u32 ;
222
+ while dimIx < AF_MAX_DIMS {
223
+ idxrs. set_index( & span, dimIx, None ) ;
224
+ dimIx += 1 ;
225
+ }
226
+ $crate:: index_gen( & $array_ident, idxrs)
227
+ }
228
+ } ;
229
+ }
0 commit comments