/usr/share/perl5/Statistics/Basic/Vector.pm is in libstatistics-basic-perl 1.6607-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | package Statistics::Basic::Vector;
use strict;
use warnings;
use Carp;
use Scalar::Util qw(blessed weaken looks_like_number);
our $tag_number = 0;
use Statistics::Basic;
use overload
'0+' => sub { croak "attempt to use vector as scalar numerical value" },
'""' => sub {
my $this = $_[0];
local $" = ", ";
my @r = map { defined $_ ? $Statistics::Basic::fmt->format_number($_, $Statistics::Basic::IPRES) : "_" } $this->query;
$Statistics::Basic::DEBUG ? "vector-$this->{tag}:[@r]" : "[@r]";
},
'bool' => sub { 1 },
fallback => 1; # tries to do what it would have done if this wasn't present.
# new {{{
sub new {
my $class = shift;
my $vector = $_[0];
if( blessed($vector) and $vector->isa(__PACKAGE__) ) {
warn "vector->new called with blessed argument, returning $vector instead of making another\n" if $Statistics::Basic::DEBUG >= 3;
return $vector;
}
my $this = bless {tag=>(++$tag_number), s=>0, c=>{}, v=>[]}, $class;
$this->set_vector( @_ );
warn "created new vector $this\n" if $Statistics::Basic::DEBUG >= 3;
return $this;
}
# }}}
# copy {{{
sub copy {
my $this = shift;
my $that = __PACKAGE__->new( [@{$this->{v}}] );
warn "copied vector($this -> $that)\n" if $Statistics::Basic::DEBUG >= 3;
return $that;
}
# }}}
# _set_computer {{{
sub _set_computer {
my $this = shift;
while( my ($k,$v) = splice @_, 0, 2 ) {
warn "$this set_computer($k => " . overload::StrVal($v) . ")\n" if $Statistics::Basic::DEBUG;
weaken($this->{c}{$k} = $v);
$v->_recalc_needed;
}
return;
}
# }}}
# _set_linked_computer {{{
sub _set_linked_computer {
my $this = shift;
my $key = shift;
my $var = shift;
my $new_key = join("_", ($key, sort {$a<=>$b} map {$_->{tag}} @_));
$this->_set_computer( $new_key => $var );
return;
}
# }}}
# _get_computer {{{
sub _get_computer {
my $this = shift;
my $k = shift;
warn "$this get_computer($k): " . overload::StrVal($this->{c}{$k}||"<undef>") . "\n" if $Statistics::Basic::DEBUG;
return $this->{c}{$k};
}
# }}}
# _get_linked_computer {{{
sub _get_linked_computer {
my $this = shift;
my $key = shift;
my $new_key = join("_", ($key, sort {$a<=>$b} map {$_->{tag}} @_));
return $this->_get_computer( $new_key );
}
# }}}
# _inform_computers_of_change {{{
sub _inform_computers_of_change {
my $this = shift;
for my $k (keys %{ $this->{c} }) {
my $v = $this->{c}{$k};
if( defined($v) and blessed($v) ) {
$v->_recalc_needed;
} else {
delete $this->{c}{$k};
}
}
return;
}
# }}}
# _fix_size {{{
sub _fix_size {
my $this = shift;
my $fixed = 0;
my $d = @{$this->{v}} - $this->{s};
if( $d > 0 ) {
splice @{$this->{v}}, 0, $d;
$fixed = 1;
}
unless( $Statistics::Basic::NOFILL ) {
if( $d < 0 ) {
unshift @{$this->{v}}, # unshift so the 0s leave first
map {0} $d .. -1; # add $d of them
$fixed = 1;
}
}
warn "[fix_size $this] [@{ $this->{v} }]\n" if $Statistics::Basic::DEBUG >= 2;
return $fixed;
}
# }}}
# query {{{
sub query {
my $this = shift;
return (wantarray ? @{$this->{v}} : $this->{v});
}
# }}}
# query_filled {{{
sub query_filled {
my $this = shift;
warn "[query_filled $this $this->{s}]\n" if $Statistics::Basic::DEBUG >= 1;
return if @{$this->{v}} < $this->{s};
return 1;
}
# }}}
# insert {{{
sub insert {
my $this = shift;
croak "you must define a vector size before using insert()" unless defined $this->{s};
for my $e (@_) {
if( ref($e) and not blessed($e) ) {
if( ref($e) eq "ARRAY" ) {
push @{ $this->{v} }, @$e;
warn "[insert $this] @$e\n" if $Statistics::Basic::DEBUG >= 1;
} else {
croak "insert() elements do not make sense";
}
} else {
push @{ $this->{v} }, $e;
warn "[insert $this] $e\n" if $Statistics::Basic::DEBUG >= 1;
}
}
$this->_fix_size;
$this->_inform_computers_of_change;
return $this;
}
# }}}
# ginsert {{{
sub ginsert {
my $this = shift;
for my $e (@_) {
if( ref($e) and not blessed($e)) {
if( ref($e) eq "ARRAY" ) {
push @{ $this->{v} }, @$e;
warn "[ginsert $this] @$e\n" if $Statistics::Basic::DEBUG >= 1;
} else {
croak "insert() elements do not make sense";
}
} else {
push @{ $this->{v} }, $e;
warn "[ginsert $this] $e\n" if $Statistics::Basic::DEBUG >= 1;
}
}
$this->{s} = @{$this->{v}} if @{$this->{v}} > $this->{s};
$this->_inform_computers_of_change;
return $this;
}
*append = \&ginsert;
# }}}
# query_size {{{
sub query_size {
my $this = shift;
return scalar @{$this->{v}};
}
# maybe deprecate this later
*size = \&query_size unless $ENV{TEST_AUTHOR};
# }}}
# set_size {{{
sub set_size {
my $this = shift;
my $size = shift;
croak "invalid vector size ($size)" if $size < 0;
if( $this->{s} != $size ) {
$this->{s} = $size;
$this->_fix_size;
$this->_inform_computers_of_change;
}
return $this;
}
# }}}
# set_vector {{{
sub set_vector {
my $this = shift;
my $vector = $_[0];
if( ref($vector) eq "ARRAY" ) {
@{$this->{v}} = @$vector;
$this->{s} = int @$vector;
$this->_inform_computers_of_change;
} elsif( UNIVERSAL::isa($vector, "Statistics::Basic::ComputedVector") ) {
$this->set_vector($vector->{input_vector});
} elsif( UNIVERSAL::isa($vector, "Statistics::Basic::Vector") ) {
$this->{s} = $vector->{s};
@{$this->{v}} = @{$vector->{v}}; # copy the vector
# I don't think this is the behavior that we really want, since they
# stay separate objects, they shouldn't be linked like this.
# $this->{s} = $vector->{s};
# $this->{v} = $vector->{v}; # this links the vectors together
# $this->{c} = $vector->{c}; # so we should link their computers too
} elsif( @_ ) {
@{$this->{v}} = @_;
$this->{s} = int @_;
} elsif( defined $vector ) {
croak "argument to set_vector() too strange";
}
warn "[set_vector $this] [@{ $this->{v} }]\n" if $Statistics::Basic::DEBUG >= 2 and ref($this->{v});
return $this;
}
# }}}
1;
|