| 1 | # /=====================================================================\ # | ||
| 2 | # | LaTeXML::Core::Pair | # | ||
| 3 | # | Representation of pairs of numbers or dimensions | # | ||
| 4 | # |=====================================================================| # | ||
| 5 | # | Part of LaTeXML: | # | ||
| 6 | # | Public domain software, produced as part of work done by the | # | ||
| 7 | # | United States Government & not subject to copyright in the US. | # | ||
| 8 | # |---------------------------------------------------------------------| # | ||
| 9 | # | Bruce Miller <bruce.miller@nist.gov> #_# | # | ||
| 10 | # | http://dlmf.nist.gov/LaTeXML/ (o o) | # | ||
| 11 | # \=========================================================ooo==U==ooo=/ # | ||
| 12 | package LaTeXML::Core::Pair; | ||
| 13 | use strict; | ||
| 14 | use warnings; | ||
| 15 | use LaTeXML::Global; | ||
| 16 | use LaTeXML::Common::Object; | ||
| 17 | use LaTeXML::Core::Token; | ||
| 18 | use base qw(LaTeXML::Common::Object); | ||
| 19 | use base qw(Exporter); | ||
| 20 | our @EXPORT = (qw(&Pair)); | ||
| 21 | |||
| 22 | #====================================================================== | ||
| 23 | # Exported constructor. | ||
| 24 | |||
| 25 | sub Pair { | ||
| 26 | my ($x, $y) = @_; | ||
| 27 | return LaTeXML::Core::Pair->new($x, $y); } | ||
| 28 | |||
| 29 | #====================================================================== | ||
| 30 | |||
| 31 | # NOTE: This is candiate to be absorbed into Array (perhaps) | ||
| 32 | |||
| 33 | sub new { | ||
| 34 | my ($class, $x, $y) = @_; | ||
| 35 | return bless [$x, $y], $class; } | ||
| 36 | |||
| 37 | sub getX { | ||
| 38 | my ($self) = @_; | ||
| 39 | return $$self[0]; } | ||
| 40 | |||
| 41 | sub getY { | ||
| 42 | my ($self) = @_; | ||
| 43 | return $$self[1]; } | ||
| 44 | |||
| 45 | # multiply by anything; this keeps the same type of elements in the pair | ||
| 46 | sub multiplyN { | ||
| 47 | my ($self, $other, $other2) = @_; | ||
| 48 | return (ref $self)->new($$self[0]->multiply($other), $$self[1]->multiply($other2 || $other)); } | ||
| 49 | |||
| 50 | # multiply by a dimension or such; this upgrades the elements in the pair to | ||
| 51 | # the type used in multiplication | ||
| 52 | sub multiply { | ||
| 53 | my ($self, $other, $other2) = @_; | ||
| 54 | return $self->multiplyN($other, $other2) if !(ref $other) || ($other2 && !ref $other2); | ||
| 55 | return (ref $self)->new($other->multiply($$self[0]), ($other2 || $other)->multiply($$self[1])); } | ||
| 56 | |||
| 57 | sub swap { | ||
| 58 | my ($self) = @_; | ||
| 59 | return (ref $self)->new($$self[1], $$self[0]); } | ||
| 60 | |||
| 61 | sub ptValue { | ||
| 62 | my ($self, $prec) = @_; | ||
| 63 | return $$self[0]->ptValue($prec) . ',' . $$self[1]->ptValue($prec); } | ||
| 64 | |||
| 65 | sub pxValue { | ||
| 66 | my ($self, $prec) = @_; | ||
| 67 | return $$self[0]->pxValue($prec) . ',' . $$self[1]->pxValue($prec); } | ||
| 68 | |||
| 69 | sub toString { | ||
| 70 | my ($self) = @_; | ||
| 71 | return $$self[0]->toString() . ',' . $$self[1]->toString(); } | ||
| 72 | |||
| 73 | sub toAttribute { | ||
| 74 | my ($self) = @_; | ||
| 75 | return $$self[0]->toAttribute() . ',' . $$self[1]->toAttribute(); } | ||
| 76 | |||
| 77 | sub stringify { | ||
| 78 | my ($self) = @_; | ||
| 79 | return "Pair[" . join(',', map { $_->stringify } @$self) . "]"; } | ||
| 80 | |||
| 81 | sub revert { | ||
| 82 | my ($self) = @_; | ||
| 83 | return (T_OTHER('('), Revert($$self[0]), T_OTHER(','), Revert($$self[1]), T_OTHER(')')); } | ||
| 84 | |||
| 85 | sub negate { | ||
| 86 | my ($self) = @_; | ||
| 87 | return $self->multiply(-1); } | ||
| 88 | |||
| 89 | #====================================================================== | ||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |