VectorValued/Dev.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 163 CC 15 SCAR 29.3
CC
15
Cov
0%
vvpp_lb_code
56.0
vvpp_cmpvec_code
20.0
vvpp_def
12.0
CRAP 18.8
1 ## -*- Mode: CPerl -*-
2 ## + CPerl pukes on '/esg'-modifiers.... bummer
3 ##
4 ## $Id$
5 ##
6 ## File: PDL::VectorValued::Dev.pm
7 ## Author: Bryan Jurish <moocow@cpan.org>
8 ## Description: Vector utilities for PDL: development
9 ##======================================================================
10  
11 package PDL::VectorValued::Dev;
12 use PDL::Types;
13 use strict;
14  
15 ##======================================================================
16 ## Export hacks
17 #use PDL::PP; ##-- do NOT do this!
18 use Exporter;
19  
20 our $VERSION = '1.0.23'; ##-- v1.0.4: use perl-reversion from Perl::Version instead
21 our @ISA = qw(Exporter);
22 our @EXPORT_OK =
23 (
24 ##
25 ##-- High-level macro expansion
26 qw(vvpp_def vvpp_expand),
27 ##
28 ##-- Type utilities
29 qw(vv_indx_sig vv_indx_typedef),
30 ##
31 ##-- Macro expansion subs
32 qw(vvpp_pdlvar_basename),
33 qw(vvpp_expand_cmpvec vvpp_cmpvec_code),
34 qw(vvpp_expand_cmpval vvpp_cmpval_code),
35 );
36 our %EXPORT_TAGS =
37 (
38 all => [@EXPORT_OK],
39 default => [@EXPORT_OK],
40 );
41 our @EXPORT = @{$EXPORT_TAGS{default}};
42  
43 ##======================================================================
44 ## pod: header
45 =pod
46  
47 =head1 NAME
48  
49 PDL::VectorValued::Dev - development utilities for vector-valued PDLs
50  
51 =head1 SYNOPSIS
52  
53 use PDL;
54 use PDL::VectorValued::Dev;
55  
56 ##---------------------------------------------------------------------
57 ## ... stuff happens
58  
59 =cut
60  
61 ##======================================================================
62 ## Description
63 =pod
64  
65 =head1 DESCRIPTION
66  
67 PDL::VectorValued::Dev provides some developer utilities for
68 vector-valued PDLs. It produces code for processing with PDL::PP.
69  
70 =cut
71  
72  
73 ##======================================================================
74 ## PP Utiltiies
75 =pod
76  
77 =head1 PDL::PP Utilities
78  
79 =cut
80  
81 ##--------------------------------------------------------------
82 ## undef = vvpp_def($name,%args)
83 =pod
84  
85 =head2 vvpp_def($funcName,%args)
86  
87 Wrapper for pp_def() which calls vvpp_expand() on 'Code' and 'BadCode'
88 values in %args.
89  
90 =cut
91 our @_REAL_TYPES =
92 map { $_->{ppsym} }
93 # Older PDLs:
94 # - no native complex types, did not have real key
95 # Newer PDLs:
96 # - native complex types, have real key
97 grep { ! exists $_->{real} || $_->{real} }
98 @PDL::Types::typehash{PDL::Types::typesrtkeys()};
99 sub vvpp_def {
100 my ($name,%args) = @_;
101 foreach (qw(Code BadCode)) {
102 $args{$_} = vvpp_expand($args{$_}) if (defined($args{$_}));
103 }
104 $args{GenericTypes} = \@_REAL_TYPES unless exists $args{GenericTypes};
105 PDL::PP::pp_def($name,%args);
106 }
107  
108  
109  
110 ##--------------------------------------------------------------
111 ## $pp_code = vvpp_expand($vvpp_code)
112 =pod
113  
114 =head2 $pp_code = vvpp_expand($vvpp_code)
115  
116 Expand PDL::VectorValued macros in $vvpp_code.
117 Currently known PDL::VectorValued macros include:
118  
119 MACRO_NAME EXPANSION_SUBROUTINE
120 ----------------------------------------------------------------------
121 $CMPVEC(...) vvpp_expand_cmpvec(...)
122 $CMPVAL(...) vvpp_expand_cmpval(...)
123 $LB(...) vvpp_expand_lb(...)
124  
125 See the documentation of the individual expansion subroutines
126 for details on calling conventions.
127  
128 You can add your own expansion macros by pushing an expansion
129 manipulating the array
130  
131 @PDL::VectorValued::Dev::MACROS
132  
133 which is just a list of expansion subroutines which take a single
134 argument (string for Code or BadCode) and should return the expanded
135 string.
136  
137 =cut
138  
139 our @MACROS =
140 (
141 \&vvpp_expand_cmpvec,
142 \&vvpp_expand_cmpval,
143 \&vvpp_expand_lb,
144 ##
145 ## ... more macros here
146 );
147 sub vvpp_expand {
148 my $str = shift;
149 my ($macro_sub);
150 foreach $macro_sub (@MACROS) {
151 $str = $macro_sub->($str);
152 }
153 $str;
154 }
155  
156  
157 ##--------------------------------------------------------------
158 ## $pp_code = vvpp_expand_cmpvec($vvpp_code)
159 sub vvpp_expand_cmpvec {
160 my $str = shift;
161 #$str =~ s{\$CMPVEC\s*\(([^\)]*)\)}{vvpp_cmpvec_code(eval($1))}esg; ##-- nope
162 $str =~ s{\$CMPVEC\s*\((.*)\)}{vvpp_cmpvec_code(eval($1))}emg; ##-- single-line macros ONLY
163 return $str;
164 }
165  
166 ##--------------------------------------------------------------
167 ## $pp_code = vvpp_expand_cmpval($vvpp_code)
168 sub vvpp_expand_cmpval {
169 my $str = shift;
170 $str =~ s{\$CMPVAL\s*\((.*)\)}{vvpp_cmpval_code(eval($1))}emg; ##-- single-line macros ONLY
171 return $str;
172 }
173  
174 ##--------------------------------------------------------------
175 ## $pp_code = vvpp_expand_lb($vvpp_code)
176 sub vvpp_expand_lb {
177 my $str = shift;
178 $str =~ s{\$LB\s*\((.*)\)}{vvpp_lb_code(eval($1))}emg; ##-- single-line macros ONLY
179 return $str;
180 }
181  
182 ##======================================================================
183 ## PP Utilities: Types
184 =pod
185  
186 =head1 Type Utilities
187  
188 =cut
189  
190 ##--------------------------------------------------------------
191 ## $sigtype = vv_indx_sig()
192 =pod
193  
194 =head2 vv_indx_sig()
195  
196 Returns a signature type for representing PDL indices.
197 For PDL E<gt>= v2.007 this should be C<PDL_Indx>, otherwise it will be C<int>.
198  
199 =cut
200  
201 sub vv_indx_sig {
202 require PDL::Core;
203 return defined(&PDL::indx) ? 'indx' : 'int';
204 }
205  
206 ##--------------------------------------------------------------
207 ## $sigtype = vv_indx_typedef()
208 =pod
209  
210 =head2 vv_indx_typedef()
211  
212 Returns a C typedef for the C<PDL_Indx> type if running under
213 PDL E<lt>= v2.007, otherwise just a comment. You can call this
214 from client PDL::PP modules as
215  
216 pp_addhdr(PDL::VectorValued::Dev::vv_indx_typedef);
217  
218 =cut
219  
220 sub vv_indx_typedef {
221 require PDL::Core;
222 if (defined(&PDL::indx)) {
223 return "/*-- PDL_Indx built-in for PDL >= v2.007 --*/\n";
224 }
225 return "typedef int PDL_Indx; /*-- PDL_Indx typedef for PDL <= v2.007 --*/\n";
226 }
227  
228  
229 ##======================================================================
230 ## PP Utilities: Macro Expansion
231 =pod
232  
233 =head1 Macro Expansion Utilities
234  
235 =cut
236  
237 ##--------------------------------------------------------------
238 ## vvpp_pdlvar_basename()
239 =pod
240  
241 =head2 vvpp_pdlvar_basename($pdlVarString)
242  
243 Gets basename of a PDL::PP variable by removing leading '$'
244 and anything at or following the first open parenthesis:
245  
246 $base = vvpp_pdlvar_basename('$a(n=>0)'); ##-- $base is now 'a'
247  
248 =cut
249  
250 sub vvpp_pdlvar_basename {
251 my $varname = shift;
252 $varname =~ s/^\s*\$\s*//;
253 $varname =~ s/\s*\(.*//;
254 return $varname;
255 }
256  
257 ##--------------------------------------------------------------
258 ## vvpp_cmpvec_code()
259 =pod
260  
261 =head2 vvpp_cmpvec_code($vec1,$vec2,$dimName,$retvar,%options)
262  
263 Returns PDL::PP code for lexicographically comparing two vectors
264 C<$vec1> and C<$vec2> along the dimension named C<$dim>, storing the
265 comparison result in the C variable C<$retvar>,
266 similar to what:
267  
268 $retvar = ($vec1 <=> $vec2);
269  
270 "ought to" do.
271  
272 Parameters:
273  
274 =over 4
275  
276 =item $vec1
277  
278 =item $vec2
279  
280 PDL::PP string forms of vector PDLs to be compared.
281 Need not be physical.
282  
283 =item $dimName
284  
285 Name of the dimension along which vectors should be compared.
286  
287 =item $retvar
288  
289 Name of a C variable to store the comparison result.
290  
291 =item $options{cvar1}
292  
293 =item $options{cvar2}
294  
295 If specified, temporary values for C<$vec1> (rsp. C<$vec2>)
296 will be stored in the C variable $options{cvar1} (rsp. C<$options{cvar2}>).
297 If unspecified, a new locally scoped C variable
298 C<_vvpp_cmpvec_val1> (rsp. C<_vvpp_cmpvec_val2>) will be declared and used.
299  
300 =back
301  
302 =for example
303  
304 The PDL::PP code for cmpvec() looks something like this:
305  
306 use PDL::VectorValued::Dev;
307 pp_def('cmpvec',
308 Pars => 'a(n); b(n); int [o]cmp()',
309 Code => (
310 'int cmpval;'
311 .vvpp_cmpvec_code( '$a()', '$b()', 'n', 'cmpval' )
312 .$cmp() = cmpval'
313 );
314 );
315  
316 =cut
317  
318 sub vvpp_cmpvec_code {
319 my ($vec1,$vec2,$dimName,$retvar,%opts) = @_;
320 ##
321 ##-- sanity checks
322 my $USAGE = 'vvpp_cmpvec_code($vec1,$vec2,$dimName,$retvar,%opts)';
323 die ("Usage: $USAGE") if (grep {!defined($_)} @_[0,1,2,3]);
324 ##
325 ##-- get PDL variable basenames
326 my $vec1Name = vvpp_pdlvar_basename($vec1);
327 my $vec2Name = vvpp_pdlvar_basename($vec2);
328 my $ppcode = "\n{ /*-- BEGIN vvpp_cmpvec_code --*/\n";
329 ##
330 ##-- get C variables
331 my ($cvar1,$cvar2);
332 if (!defined($cvar1=$opts{var1})) {
333 $cvar1 = '_vvpp_cmpvec_val1';
334 $ppcode .= " \$GENERIC(${vec1Name}) ${cvar1};\n";
335 }
336 if (!defined($cvar2=$opts{var2})) {
337 $cvar2 = '_vvpp_cmpvec_val2';
338 $ppcode .= " \$GENERIC(${vec2Name}) ${cvar2};\n";
339 }
340 ##
341 ##-- generate comparison code
342 $ppcode .= (''
343 ." ${retvar}=0;\n"
344 ." loop (${dimName}) %{\n"
345 ." ${cvar1}=$vec1;\n"
346 ." ${cvar2}=$vec2;\n"
347 ." if (${cvar1} < ${cvar2}) { ${retvar}=-1; break; }\n"
348 ." else if (${cvar1} > ${cvar2}) { ${retvar}= 1; break; }\n"
349 ." %}\n"
350 ."} /*-- END vvpp_cmpvec_code --*/\n"
351 );
352 ##
353 ##-- ... and return
354 return $ppcode;
355 }
356  
357 ##--------------------------------------------------------------
358 ## vvpp_cmpval_code()
359 =pod
360  
361 =head2 vvpp_cmpval_code($val1,$val2)
362  
363 Returns PDL::PP expression code for lexicographically comparing two values
364 C<$val1> and C<$val2>, storing the
365 comparison result in the C variable C<$retvar>,
366 similar to what:
367  
368 ($vec1 <=> $vec2);
369  
370 "ought to" do.
371  
372 Parameters:
373  
374 =over 4
375  
376 =item $val1
377  
378 =item $val2
379  
380 PDL::PP string forms of values to be compared.
381 Need not be physical.
382  
383 =back
384  
385 =cut
386  
387 sub vvpp_cmpval_code {
388 my ($val1,$val2) = @_;
389 ##
390 ##-- sanity checks
391 my $USAGE = 'vvpp_cmpval_code($val1,$val2)';
392 die ("Usage: $USAGE") if (grep {!defined($_)} @_[0,1]);
393 ##
394 ##-- generate comparison code
395 my $ppcode = (''
396 ."/*-- BEGIN vvpp_cmpval_code --*/ "
397 ." (($val1) < ($val2) ? -1 : (($val1) > ($val2) ? 1 : 0)) "
398 ." /*-- END vvpp_cmpvec_code --*/"
399 );
400 ##
401 ##-- ... and return
402 return $ppcode;
403 }
404  
405 ##--------------------------------------------------------------
406 ## vvpp_lb_code()
407 =pod
408  
409 =head2 vvpp_lb_code($find,$vals, $imin,$imax, $retvar, %options)
410  
411 Returns PDL::PP code for binary lower-bound search for the value $find() in the sorted pdl $vals($imin:$imax-1).
412 Parameters:
413  
414 =over 4
415  
416 =item $find
417  
418 Value to search for or PDL::PP string form of such a value.
419  
420 =item $vals
421  
422 PDL::PP string form of PDL to be searched. $vals should contain a placeholder C<$_>
423 representing the dimension to be searched.
424  
425 =item $retvar
426  
427 Name of a C variable to store the result.
428 On return, C<$retvar> holds the maximum value for C<$_> in C<$vals($imin:$imax-1)> such that
429 C<$vals($_=$retvar) E<lt>= $find> and C<$vals($_=$j) E<lt> $find> for all
430 C<$j> with C<$imin E<lt>= $j E<lt> $retvar>, or C<$imin> if no such value for C<$retvar> exists,
431 C<$imin E<lt>= $retvar E<lt> $imax>.
432 In other words,
433 returns the least index $_ of a match for $find in $vals($imin:$imax-1) whenever a match exists,
434 otherwise the greatest index whose value in $vals($imin:$imax-1) is strictly less than $find if that exists,
435 and $imin if all values in $vals($imin:$imax-1) are strictly greater than $find.
436  
437 =item $options{lovar}
438  
439 =item $options{hivar}
440  
441 =item $options{midvar}
442  
443 =item $options{cmpvar}
444  
445 If specified, temporary indices and comparison values will be stored in
446 the C variables $options{lovar}, $options{hivar}, $options{midvar}, and $options{cmpvar}.
447 If unspecified, new locally scoped C variables
448 C<_vvpp_lb_loval> etc. will be declared and used.
449  
450 =item $options{ubmaxvar}
451  
452 If specified, should be a C variable to hold the index of the last inspected value for $_
453 in $vals($imin:$imax-1) strictly greater than $find.
454  
455 =back
456  
457 =cut
458  
459 sub vvpp_lb_code {
460 my ($find,$vals,$imin,$imax,$retvar,%opts) = @_;
461 ##
462 ##-- sanity checks
463 my $USAGE = 'vvpp_lb_code($find,$vals,$imin,$imax,$retvar,%opts)';
464 die ("Usage: $USAGE") if (grep {!defined($_)} @_[0..4]);
465 ##
466 ##-- get PDL variable basenames
467 my $ppcode = "\n{ /*-- BEGIN vvpp_lb_code --*/\n";
468 ##
469 ##-- get C variables
470 my ($lovar,$hivar,$midvar,$cmpvar);
471 if (!defined($lovar=$opts{lovar})) {
472 $lovar = '_vvpp_lb_loval';
473 $ppcode .= " long $lovar;";
474 }
475 if (!defined($hivar=$opts{hivar})) {
476 $hivar = '_vvpp_lb_hival';
477 $ppcode .= " long $hivar;";
478 }
479 if (!defined($midvar=$opts{midvar})) {
480 $midvar = '_vvpp_lb_midval';
481 $ppcode .= " long $midvar;";
482 }
483 if (!defined($cmpvar=$opts{cmpvar})) {
484 $cmpvar = '_vvpp_lb_cmpval';
485 $ppcode .= " int $cmpvar;";
486 }
487 my $ubmaxvar = $opts{ubmaxvar};
488 ##
489 ##-- generate search code
490 (my $val_mid = $vals) =~ s/\$_/${midvar}/;
491 (my $val_lo = $vals) =~ s/\$_/${lovar}/;
492 (my $val_hi = $vals) =~ s/\$_/${hivar}/;
493 $ppcode .= join("\n",
494 " $lovar = $imin;",
495 " $hivar = $imax;",
496 #($ubmaxvar ? " $ubmaxvar = -1;" : qw()),
497 " while ($hivar - $lovar > 1) {",
498 " $midvar = ($hivar + $lovar) >> 1;",
499 " $cmpvar = ".vvpp_cmpval_code($find, $val_mid).";",
500 " if ($cmpvar > 0) { $lovar = $midvar; }",
501 ($ubmaxvar
502 ? " else if ($cmpvar < 0) { $hivar = $midvar; $ubmaxvar = $midvar; }"
503 : qw()),
504 " else { $hivar = $midvar; }",
505 " }",
506 " if ( $val_lo == $find) $retvar = $lovar;",
507 " else if ($hivar < $imax && $val_hi == $find) $retvar = $hivar;",
508 " else if ($lovar >= $imin && $val_lo < $find) $retvar = $lovar;",
509 " else $retvar = $imin;",
510 "} /*-- END vvpp_lb_code --*/\n",
511 );
512 ##
513 ##-- ... and return
514 return $ppcode;
515 }
516  
517  
518 1; ##-- make perl happy
519  
520  
521 ##======================================================================
522 ## pod: Functions: low-level
523 =pod
524  
525 =head2 Low-Level Functions
526  
527 Some additional low-level functions are provided in the
528 PDL::Ngrams::Utils
529 package.
530 See L<PDL::Ngrams::Utils> for details.
531  
532 =cut
533  
534 ##======================================================================
535 ## pod: Bugs
536 =pod
537  
538 =head1 KNOWN BUGS
539  
540 =head2 Why not PDL::PP macros?
541  
542 All of these functions would be more intuitive if implemented directly
543 as PDL::PP macros, and thus expanded directly by pp_def() rather
544 than requiring vvpp_def().
545  
546 At the time of this module's writing, I was unable to figure out how to
547 use the (then undocumented) PDL::PP macro expansion mechanism.
548 As of 2022, PDL::PP offers support for user-defined macros, and
549 this module should be refactored to take advantage of that... but that
550 hasn't happened yet.
551  
552 =cut
553  
554 ##======================================================================
555 ## pod: Footer
556 =pod
557  
558 =head1 ACKNOWLEDGEMENTS
559  
560 perl by Larry Wall.
561  
562 =head1 AUTHOR
563  
564 Bryan Jurish E<lt>moocow@cpan.orgE<gt>
565  
566 PDL by Karl Glazebrook, Tuomas J. Lukka, Christian Soeller, and others.
567  
568 =head1 COPYRIGHT
569  
570 Copyright (c) 2007-2022, Bryan Jurish. All rights reserved.
571  
572 This package is free software. You may redistribute it
573 and/or modify it under the same terms as Perl itself.
574  
575 =head1 SEE ALSO
576  
577 perl(1), PDL::PP(3perl).
578  
579 =cut