| 1 | |||
| 2 | ### Generated from Types.pm.PL automatically - do not modify! ### | ||
| 3 | |||
| 4 | package PDL::Types; | ||
| 5 | require Exporter; | ||
| 6 | use Carp; | ||
| 7 | |||
| 8 | |||
| 9 | @EXPORT = qw( $PDL_B $PDL_S $PDL_US $PDL_L $PDL_IND $PDL_LL $PDL_F $PDL_D | ||
| 10 | @pack %typehash ); | ||
| 11 | |||
| 12 | @EXPORT_OK = (@EXPORT, qw/types ppdefs typesrtkeys mapfld typefld/); | ||
| 13 | %EXPORT_TAGS = ( | ||
| 14 | All=>[@EXPORT,qw/types ppdefs typesrtkeys mapfld typefld/], | ||
| 15 | ); | ||
| 16 | |||
| 17 | @ISA = qw( Exporter ); | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | # Data types/sizes (bytes) [must be in order of complexity] | ||
| 22 | # Enum | ||
| 23 | ( $PDL_B, $PDL_S, $PDL_US, $PDL_L, $PDL_IND, $PDL_LL, $PDL_F, $PDL_D ) = (0..7); | ||
| 24 | # Corresponding pack types | ||
| 25 | @pack= qw/C* s* S* l* q* q* f* d*/; | ||
| 26 | @names= qw/PDL_B PDL_S PDL_US PDL_L PDL_IND PDL_LL PDL_F PDL_D/; | ||
| 27 | |||
| 28 | %PDL::Types::typehash = ( | ||
| 29 | PDL_B => | ||
| 30 | { | ||
| 31 | 'convertfunc' => 'byte', | ||
| 32 | 'ctype' => 'PDL_Byte', | ||
| 33 | 'defbval' => 'UCHAR_MAX', | ||
| 34 | 'ioname' => 'byte', | ||
| 35 | 'numval' => 0, | ||
| 36 | 'ppforcetype' => 'byte', | ||
| 37 | 'ppsym' => 'B', | ||
| 38 | 'realctype' => 'unsigned char', | ||
| 39 | 'sym' => 'PDL_B', | ||
| 40 | 'usenan' => 0 | ||
| 41 | } | ||
| 42 | , | ||
| 43 | PDL_S => | ||
| 44 | { | ||
| 45 | 'convertfunc' => 'short', | ||
| 46 | 'ctype' => 'PDL_Short', | ||
| 47 | 'defbval' => 'SHRT_MIN', | ||
| 48 | 'ioname' => 'short', | ||
| 49 | 'numval' => 1, | ||
| 50 | 'ppforcetype' => 'short', | ||
| 51 | 'ppsym' => 'S', | ||
| 52 | 'realctype' => 'short', | ||
| 53 | 'sym' => 'PDL_S', | ||
| 54 | 'usenan' => 0 | ||
| 55 | } | ||
| 56 | , | ||
| 57 | PDL_US => | ||
| 58 | { | ||
| 59 | 'convertfunc' => 'ushort', | ||
| 60 | 'ctype' => 'PDL_Ushort', | ||
| 61 | 'defbval' => 'USHRT_MAX', | ||
| 62 | 'ioname' => 'ushort', | ||
| 63 | 'numval' => 2, | ||
| 64 | 'ppforcetype' => 'ushort', | ||
| 65 | 'ppsym' => 'U', | ||
| 66 | 'realctype' => 'unsigned short', | ||
| 67 | 'sym' => 'PDL_US', | ||
| 68 | 'usenan' => 0 | ||
| 69 | } | ||
| 70 | , | ||
| 71 | PDL_L => | ||
| 72 | { | ||
| 73 | 'convertfunc' => 'long', | ||
| 74 | 'ctype' => 'PDL_Long', | ||
| 75 | 'defbval' => 'INT_MIN', | ||
| 76 | 'ioname' => 'long', | ||
| 77 | 'numval' => 3, | ||
| 78 | 'ppforcetype' => 'int', | ||
| 79 | 'ppsym' => 'L', | ||
| 80 | 'realctype' => 'int', | ||
| 81 | 'sym' => 'PDL_L', | ||
| 82 | 'usenan' => 0 | ||
| 83 | } | ||
| 84 | , | ||
| 85 | PDL_IND => | ||
| 86 | { | ||
| 87 | 'convertfunc' => 'indx', | ||
| 88 | 'ctype' => 'PDL_Indx', | ||
| 89 | 'defbval' => 'LONG_MIN', | ||
| 90 | 'ioname' => 'indx', | ||
| 91 | 'numval' => 4, | ||
| 92 | 'ppforcetype' => 'indx', | ||
| 93 | 'ppsym' => 'N', | ||
| 94 | 'realctype' => 'long', | ||
| 95 | 'sym' => 'PDL_IND', | ||
| 96 | 'usenan' => 0 | ||
| 97 | } | ||
| 98 | , | ||
| 99 | PDL_LL => | ||
| 100 | { | ||
| 101 | 'convertfunc' => 'longlong', | ||
| 102 | 'ctype' => 'PDL_LongLong', | ||
| 103 | 'defbval' => 'LONG_MIN', | ||
| 104 | 'ioname' => 'longlong', | ||
| 105 | 'numval' => 5, | ||
| 106 | 'ppforcetype' => 'longlong', | ||
| 107 | 'ppsym' => 'Q', | ||
| 108 | 'realctype' => 'long', | ||
| 109 | 'sym' => 'PDL_LL', | ||
| 110 | 'usenan' => 0 | ||
| 111 | } | ||
| 112 | , | ||
| 113 | PDL_F => | ||
| 114 | { | ||
| 115 | 'convertfunc' => 'float', | ||
| 116 | 'ctype' => 'PDL_Float', | ||
| 117 | 'defbval' => '-FLT_MAX', | ||
| 118 | 'ioname' => 'float', | ||
| 119 | 'numval' => 6, | ||
| 120 | 'ppforcetype' => 'float', | ||
| 121 | 'ppsym' => 'F', | ||
| 122 | 'realctype' => 'float', | ||
| 123 | 'sym' => 'PDL_F', | ||
| 124 | 'usenan' => 1 | ||
| 125 | } | ||
| 126 | , | ||
| 127 | PDL_D => | ||
| 128 | { | ||
| 129 | 'convertfunc' => 'double', | ||
| 130 | 'ctype' => 'PDL_Double', | ||
| 131 | 'defbval' => '-DBL_MAX', | ||
| 132 | 'ioname' => 'double', | ||
| 133 | 'numval' => 7, | ||
| 134 | 'ppforcetype' => 'double', | ||
| 135 | 'ppsym' => 'D', | ||
| 136 | 'realctype' => 'double', | ||
| 137 | 'sym' => 'PDL_D', | ||
| 138 | 'usenan' => 1 | ||
| 139 | } | ||
| 140 | , | ||
| 141 | ); # end typehash definition | ||
| 142 | |||
| 143 | # Cross-reference by common names | ||
| 144 | %PDL::Types::typenames = (); | ||
| 145 | for my $k(keys %PDL::Types::typehash) { | ||
| 146 | my $n = $PDL::Types::typehash{$k}->{'numval'}; | ||
| 147 | $PDL::Types::typenames{$k} = $n; | ||
| 148 | $PDL::Types::typenames{$n} = $n; | ||
| 149 | $PDL::Types::typenames{$PDL::Types::typehash{$k}->{ioname}} = $n; | ||
| 150 | $PDL::Types::typenames{$PDL::Types::typehash{$k}->{ctype}} = $n; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | =head1 NAME | ||
| 155 | |||
| 156 | PDL::Types - define fundamental PDL Datatypes | ||
| 157 | |||
| 158 | =head1 SYNOPSIS | ||
| 159 | |||
| 160 | use PDL::Types; | ||
| 161 | |||
| 162 | $pdl = ushort( 2.0, 3.0 ); | ||
| 163 | print "The actual c type used to store ushort's is '" . | ||
| 164 | $pdl->type->realctype() . "'\n"; | ||
| 165 | The actual c type used to store ushort's is 'unsigned short' | ||
| 166 | |||
| 167 | =head1 DESCRIPTION | ||
| 168 | |||
| 169 | Internal module - holds all the PDL Type info. The type info can be | ||
| 170 | accessed easily using the C<PDL::Type> object returned by | ||
| 171 | the L<type|PDL::Core/type> method. | ||
| 172 | |||
| 173 | Skip to the end of this document to find out how to change | ||
| 174 | the set of types supported by PDL. | ||
| 175 | |||
| 176 | =head1 Support functions | ||
| 177 | |||
| 178 | A number of functions are available for module writers | ||
| 179 | to get/process type information. These are used in various | ||
| 180 | places (e.g. C<PDL::PP>, C<PDL::Core>) to generate the | ||
| 181 | appropriate type loops, etc. | ||
| 182 | |||
| 183 | =head2 typesrtkeys | ||
| 184 | |||
| 185 | return array of keys of typehash sorted in order of type complexity | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub typesrtkeys { | ||
| 190 | return sort {$typehash{$a}->{numval} <=> $typehash{$b}->{numval}} | ||
| 191 | keys %typehash; | ||
| 192 | } | ||
| 193 | |||
| 194 | =head2 ppdefs | ||
| 195 | |||
| 196 | return array of pp symbols for all known types | ||
| 197 | |||
| 198 | =cut | ||
| 199 | |||
| 200 | sub ppdefs { | ||
| 201 | return map {$typehash{$_}->{ppsym}} typesrtkeys; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 typefld | ||
| 205 | |||
| 206 | return specified field (C<$fld>) for specified type (C<$type>) | ||
| 207 | by querying type hash | ||
| 208 | |||
| 209 | =cut | ||
| 210 | |||
| 211 | sub typefld { | ||
| 212 | my ($type,$fld) = @_; | ||
| 213 | croak "unknown type $type" unless exists $typehash{$type}; | ||
| 214 | croak "unknown field $fld in type $type" | ||
| 215 | unless exists $typehash{$type}->{$fld}; | ||
| 216 | return $typehash{$type}->{$fld}; | ||
| 217 | } | ||
| 218 | |||
| 219 | =head2 mapfld (in_value, in_key, out_key) | ||
| 220 | |||
| 221 | Map a given source field to the corresponding target field by | ||
| 222 | querying the type hash. This gives you a way to say, "Find the type | ||
| 223 | whose C<$in_key> is equal to C<$value>, and return that type's value | ||
| 224 | for C<$out_key>. For example: | ||
| 225 | |||
| 226 | # Does byte type use nan? | ||
| 227 | $uses_nan = PDL::Types::mapfld(byte => 'ppforcetype', 'usenan'); | ||
| 228 | # Equivalent: | ||
| 229 | $uses_nan = byte->usenan; | ||
| 230 | |||
| 231 | # What is the actual C type for the value that we call 'long'? | ||
| 232 | $type_name = PDL::Types::mapfld(long => 'convertfunc', 'realctype'); | ||
| 233 | # Equivalent: | ||
| 234 | $type_name = long->realctype; | ||
| 235 | |||
| 236 | As you can see, the equivalent examples are much shorter and legible, so you | ||
| 237 | should only use mapfld if you were given the type index (in which case the | ||
| 238 | actual type is not immediately obvious): | ||
| 239 | |||
| 240 | $type_index = 4; | ||
| 241 | $type_name = PDL::Types::mapfld($type_index => numval, 'realctype'); | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub mapfld { | ||
| 246 | my ($type,$src,$trg) = @_; | ||
| 247 | my @keys = grep {$typehash{$_}->{$src} eq $type} typesrtkeys; | ||
| 248 | return @keys > 0 ? $typehash{$keys[0]}->{$trg} : undef; | ||
| 249 | } | ||
| 250 | |||
| 251 | =head2 typesynonyms | ||
| 252 | |||
| 253 | =for ref | ||
| 254 | |||
| 255 | return type related synonym definitions to be included in pdl.h . | ||
| 256 | This routine must be updated to include new types as required. | ||
| 257 | Mostly the automatic updating should take care of the vital | ||
| 258 | things. | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | sub typesynonyms { | ||
| 263 | my $add = join "\n", | ||
| 264 | map {"#define PDL_".typefld($_,'ppsym')." ".typefld($_,'sym')} | ||
| 265 | grep {"PDL_".typefld($_,'ppsym') ne typefld($_,'sym')} typesrtkeys; | ||
| 266 | print "adding...\n$add\n"; | ||
| 267 | return "$add\n"; | ||
| 268 | } | ||
| 269 | |||
| 270 | =head2 datatypes_header | ||
| 271 | |||
| 272 | =for ref | ||
| 273 | |||
| 274 | return C header text for F<pdl.h> and F<pdlsimple.h>. | ||
| 275 | |||
| 276 | =cut | ||
| 277 | |||
| 278 | sub datatypes_header { | ||
| 279 | require Config; | ||
| 280 | $PDL_Indx_type = $Config::Config{'ivtype'}; | ||
| 281 | warn "Using new 64bit index support\n" if $Config::Config{'ivsize'}==8; | ||
| 282 | |||
| 283 | my $anyval_union = ''; | ||
| 284 | my $enum = 'PDL_INVALID=-1, '; | ||
| 285 | my $typedefs = ''; | ||
| 286 | for (sort { $typehash{$a}{'numval'}<=>$typehash{$b}{'numval'} } keys %typehash) { | ||
| 287 | $enum .= $typehash{$_}{'sym'}.", "; | ||
| 288 | $anyval_union .= " $typehash{$_}{'ctype'} $typehash{$_}{'ppsym'};\n"; | ||
| 289 | $typedefs .= "typedef $typehash{$_}{'realctype'} $typehash{$_}{'ctype'};\n"; | ||
| 290 | } | ||
| 291 | chop $enum; | ||
| 292 | chop $enum; | ||
| 293 | |||
| 294 | $typedefs .= "typedef struct {\n pdl_datatypes type;\n union {\n"; | ||
| 295 | $typedefs .= $anyval_union; | ||
| 296 | $typedefs .= " } value;\n} PDL_Anyval;\n"; | ||
| 297 | |||
| 298 | my $indx_type = typefld('PDL_IND','realctype'); | ||
| 299 | $typedefs .= '#define IND_FLAG '; | ||
| 300 | if ($indx_type eq 'long'){ | ||
| 301 | $typedefs .= qq|"ld"|; | ||
| 302 | } elsif ($indx_type eq 'long long'){ | ||
| 303 | $typedefs .= qq|"lld"|; | ||
| 304 | } else { | ||
| 305 | $typedefs .= qq|"d"|; | ||
| 306 | } | ||
| 307 | $typedefs .= "\n\n"; | ||
| 308 | |||
| 309 | my $PDL_DATATYPES = <<"EOD"; | ||
| 310 | |||
| 311 | /*****************************************************************************/ | ||
| 312 | /*** This section of .h file generated automatically by ***/ | ||
| 313 | /*** PDL::Types::datatypes_header() - don't edit manually ***/ | ||
| 314 | |||
| 315 | /* Data types/sizes [must be in order of complexity] */ | ||
| 316 | |||
| 317 | typedef enum { $enum } pdl_datatypes; | ||
| 318 | |||
| 319 | /* Define the pdl data types */ | ||
| 320 | |||
| 321 | $typedefs | ||
| 322 | |||
| 323 | /* typedef $PDL_Indx_type PDL_Indx; */ | ||
| 324 | |||
| 325 | /*****************************************************************************/ | ||
| 326 | |||
| 327 | EOD | ||
| 328 | |||
| 329 | $PDL_DATATYPES .= "\n".typesynonyms()."\n"; | ||
| 330 | $PDL_DATATYPES; | ||
| 331 | } | ||
| 332 | |||
| 333 | =head1 PDL::Type OBJECTS | ||
| 334 | |||
| 335 | This module declares one class - C<PDL::Type> - objects of this class | ||
| 336 | are returned by the L<type|PDL::Core/type> method of a piddle. It has | ||
| 337 | several methods, listed below, which provide an easy way to access | ||
| 338 | type information: | ||
| 339 | |||
| 340 | Additionally, comparison and stringification are overloaded so that | ||
| 341 | you can compare and print type objects, e.g. | ||
| 342 | |||
| 343 | $nofloat = 1 if $pdl->type < float; | ||
| 344 | die "must be double" if $type != double; | ||
| 345 | |||
| 346 | For further examples check again the | ||
| 347 | L<type|PDL::Core/type> method. | ||
| 348 | |||
| 349 | =over 4 | ||
| 350 | |||
| 351 | =item enum | ||
| 352 | |||
| 353 | Returns the number representing this datatype (see L<get_datatype|PDL::Core/PDL::get_datatype>). | ||
| 354 | |||
| 355 | =item symbol | ||
| 356 | |||
| 357 | Returns one of 'PDL_B', 'PDL_S', 'PDL_US', 'PDL_L', 'PDL_IND', 'PDL_LL', | ||
| 358 | 'PDL_F' or 'PDL_D'. | ||
| 359 | |||
| 360 | =item ctype | ||
| 361 | |||
| 362 | Returns the macro used to represent this type in C code (eg 'PDL_Long'). | ||
| 363 | |||
| 364 | =item ppsym | ||
| 365 | |||
| 366 | The letter used to represent this type in PP code code (eg 'U' for L<ushort|PDL::Core/ushort>). | ||
| 367 | |||
| 368 | =item realctype | ||
| 369 | |||
| 370 | The actual C type used to store this type. | ||
| 371 | |||
| 372 | =item shortctype | ||
| 373 | |||
| 374 | The value returned by C<ctype> without the 'PDL_' prefix. | ||
| 375 | |||
| 376 | =item badvalue | ||
| 377 | |||
| 378 | The special numerical value used to represent bad values for this type. | ||
| 379 | See L<badvalue routine in PDL::Bad|PDL::Bad/badvalue> for more details. | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | |||
| 384 | =item orig_badvalue | ||
| 385 | |||
| 386 | The default special numerical value used to represent bad values for this | ||
| 387 | type. (You can change the value that represents bad values for each type | ||
| 388 | during runtime.) See the | ||
| 389 | L<orig_badvalue routine in PDL::Bad|PDL::Bad/orig_badvalue> for more details. | ||
| 390 | |||
| 391 | =cut | ||
| 392 | |||
| 393 | |||
| 394 | =back | ||
| 395 | |||
| 396 | =cut | ||
| 397 | |||
| 398 | { | ||
| 399 | package PDL::Type; | ||
| 400 | sub new { | ||
| 401 | my($type,$val) = @_; | ||
| 402 | if("PDL::Type" eq ref $val) { return bless [@$val],$type; } | ||
| 403 | if(ref $val and $val->isa(PDL)) { | ||
| 404 | if($val->getndims != 0) { | ||
| 405 | PDL::Core::barf( | ||
| 406 | "Can't make a type out of non-scalar piddle $val!"); | ||
| 407 | } | ||
| 408 | $val = $val->at; | ||
| 409 | } | ||
| 410 | PDL::Core::barf("Can't make a type out of non-scalar $val!". | ||
| 411 | (ref $val)."!") if ref $val; | ||
| 412 | |||
| 413 | if(length($PDL::Types::typenames{$val})) { | ||
| 414 | $val =~ s/^\s*//o; | ||
| 415 | $val =~ s/\s*$//o; | ||
| 416 | return bless [$PDL::Types::typenames{$val}],$type; | ||
| 417 | } else { | ||
| 418 | die("Unknown type string '$val' (should be one of ". | ||
| 419 | join(",",map { $PDL::Types::typehash{$_}->{ioname} } @names). | ||
| 420 | ")\n"); | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | sub enum { return $_[0]->[0]; } | ||
| 425 | sub symbol { return $PDL::Types::names[ $_[0]->enum ]; } | ||
| 426 | sub PDL::Types::types { # return all known types as type objects | ||
| 427 | map { new PDL::Type PDL::Types::typefld($_,'numval') } | ||
| 428 | PDL::Types::typesrtkeys(); | ||
| 429 | } | ||
| 430 | |||
| 431 | sub ctype { | ||
| 432 | return $PDL::Types::typehash{$_[0]->symbol}->{ctype}; | ||
| 433 | } | ||
| 434 | sub ppsym { | ||
| 435 | return $PDL::Types::typehash{$_[0]->symbol}->{ppsym}; | ||
| 436 | } | ||
| 437 | sub realctype { | ||
| 438 | return $PDL::Types::typehash{$_[0]->symbol}->{realctype}; | ||
| 439 | } | ||
| 440 | sub ppforcetype { | ||
| 441 | return $PDL::Types::typehash{$_[0]->symbol}->{ppforcetype}; | ||
| 442 | } | ||
| 443 | sub convertfunc { | ||
| 444 | return $PDL::Types::typehash{$_[0]->symbol}->{convertfunc}; | ||
| 445 | } | ||
| 446 | sub sym { | ||
| 447 | return $PDL::Types::typehash{$_[0]->symbol}->{sym}; | ||
| 448 | } | ||
| 449 | sub numval { | ||
| 450 | return $PDL::Types::typehash{$_[0]->symbol}->{numval}; | ||
| 451 | } | ||
| 452 | sub usenan { | ||
| 453 | return $PDL::Types::typehash{$_[0]->symbol}->{usenan}; | ||
| 454 | } | ||
| 455 | sub ioname { | ||
| 456 | return $PDL::Types::typehash{$_[0]->symbol}->{ioname}; | ||
| 457 | } | ||
| 458 | sub defbval { | ||
| 459 | return $PDL::Types::typehash{$_[0]->symbol}->{defbval}; | ||
| 460 | } | ||
| 461 | |||
| 462 | no strict 'refs'; | ||
| 463 | sub badvalue { | ||
| 464 | my ( $self, $val ) = @_; | ||
| 465 | my $name = "PDL::_badvalue_int" . $self->enum(); | ||
| 466 | if ( defined $val ) { return &{$name}( $val )->sclr; } | ||
| 467 | else { return &{$name}( undef )->sclr; } | ||
| 468 | } | ||
| 469 | |||
| 470 | sub orig_badvalue { | ||
| 471 | my $self = shift; | ||
| 472 | my $name = "PDL::_default_badvalue_int" . $self->enum(); | ||
| 473 | return &{$name}()->sclr; | ||
| 474 | } | ||
| 475 | use strict 'refs'; | ||
| 476 | |||
| 477 | |||
| 478 | sub shortctype { my $txt = $_[0]->ctype; $txt =~ s/PDL_//; return $txt; } | ||
| 479 | |||
| 480 | # make life a bit easier | ||
| 481 | use overload ( | ||
| 482 | "\"\"" => sub { lc $_[0]->shortctype }, | ||
| 483 | "eq" => sub { my($self, $other, $swap) = @_; | ||
| 484 | return ("$self" eq $other); | ||
| 485 | }, | ||
| 486 | "cmp" => sub { my($self, $other, $swap) = @_; | ||
| 487 | return ($swap ? $other cmp "$self" : "$self" cmp $other); | ||
| 488 | }, | ||
| 489 | "<=>" => sub { $_[2] ? $_[1]->enum <=> $_[0]->enum : | ||
| 490 | $_[0]->enum <=> $_[1]->enum }, | ||
| 491 | ); | ||
| 492 | |||
| 493 | |||
| 494 | } # package: PDL::Type | ||
| 495 | # Return | ||
| 496 | 1; | ||
| 497 | |||
| 498 | __END__ |