sum calculation on demand and cached
[barcat.git] / barcat
diff --git a/barcat b/barcat
index efb631f248964c7714c4852ae6883c1668681a25..1fd0dd3e8815c8879569fc3bf0e4fa5c679524d5 100755 (executable)
--- a/barcat
+++ b/barcat
@@ -289,20 +289,7 @@ if ($opt{markers} and $size > 0) {
                my ($char, $func) = split //, $markspec, 2;
                my $increment = $func =~ s/[+]\z//;
                my @pos = eval {
-                       if ($func eq 'avg') {
-                               return sum(@order) / @order;
-                       }
-                       elsif ($func =~ /\A([0-9.]+)v\z/) {
-                               $1 <= 100 or die(
-                                       "Invalid marker $char: percentile $1 out of bounds\n"
-                               );
-                               my $index = $#order * $1 / 100;
-                               return ($order[$index] + $order[$index + .5]) / 2;
-                       }
-                       elsif ($func =~ /\A-?[0-9.]+\z/) {
-                               return $func;
-                       }
-                       elsif ($func =~ /\A\/($float)\z/) {
+                       if ($func =~ /\A\/($float)\z/) {
                                my @range = my $multiple = my $next = $1;
                                while ($next < $maxval) {
                                        $multiple *= 10 if $opt{log};
@@ -310,12 +297,9 @@ if ($opt{markers} and $size > 0) {
                                }
                                return @range;
                        }
-                       else {
-                               die "Unknown marker $char: $func\n";
-                       }
-               };
-               @pos or do {
-                       warn $@ if $@;
+                       return calc($func);
+               } or do {
+                       warn "Invalid marker $char: $@" if $@;
                        next;
                };
                for my $pos (@pos) {
@@ -323,7 +307,7 @@ if ($opt{markers} and $size > 0) {
                        $pos &&= log $pos if $opt{log};
                        $pos >= 0 or next;
                        $increment ||= $minval && !$pos;
-                       color(36) for $barmark[$pos / $range * $size + $increment] = $char;
+                       color(36) for $barmark[$pos / $range * $size + $increment + .5] = $char;
                }
        }
 
@@ -404,6 +388,7 @@ say $opt{palette} ? color(0) : '' if $opt{spark};
 
 sub show_stat {
        my %vars = (
+               partsum => undef,
                count => int @order,
                lines => int @lines,
        );
@@ -415,26 +400,59 @@ sub show_stat {
                $vars{partsum} = sum(0, grep {length} @values[$linemin .. $linemax])
                        if $linemin <= $linemax and ($opt{hidemin} or $opt{hidemax});
                %vars = (%vars,
-                       sum => sum(@order),
                        min => $order[-1],
                        max => $order[0],
                );
-               $vars{avg} = $vars{sum} / @order;
        }
        say varfmt($opt{report}, \%vars);
        return 1;
 }
 
+sub calc {
+       my ($func) = @_;
+       if ($func eq 'avg') {
+               return calc('sum') / @order;
+       }
+       elsif ($func eq 'sum') {
+               state $cache;         # avoid recount
+               state $cachednr = 0;  # if unchanged
+               unless (@order == $cachednr) {
+                       $cache = sum(@order);
+                       $cachednr = @order;
+               }
+               return $cache;
+       }
+       elsif ($func =~ /\A([0-9.]+)v\z/) {
+               $1 <= 100 or die(
+                       "percentile $1 out of bounds\n"
+               );
+               my $index = $#order * $1 / 100;
+               my $f = $index - int $index;
+               my $val = $order[$index];
+               if ($f) {
+                       my $next = $order[$index + 1];
+                       $val -= $f * ($val - $next);
+               }
+               return $val;
+       }
+       elsif ($func =~ /\A-?[0-9.]+\z/) {
+               return $func;
+       }
+       else {
+               die "$func unknown\n";
+       }
+}
+
 sub varfmt {
        my ($fmt, $vars) = @_;
        $fmt =~ s[\$\{ \h*+ ((?: [^{}]++ | \{(?1)\} )+) \}]{
                my ($name, $op, $cmd) = split /\s*([;:])/, $1, 2;
                my $format = $name =~ s/\+// || $name !~ s/\#// && $opt{reformat};
-               local $_ = $vars->{$name};
+               local $_ = exists $vars->{$name} ? $vars->{$name} : calc($name);
                defined && do {
                        $_ = $opt{'value-format'}->($_) if $format;
                        if ($cmd and $op eq ':') {
-                               $_ = varfmt($cmd, $vars);
+                               $_ = !!$_ && varfmt($cmd, $vars);
                        }
                        elsif ($cmd) {
                                eval $cmd;
@@ -630,16 +648,19 @@ For example C<:/1> for a grid at every integer.
 
 =item I<percentage>B<v>
 
-Ranked value at the given percentile.
-The default shows C<+> at C<50v> for the mean or median;
-the middle value or average between middle values.
-One standard deviation right of the mean is at about C<68.3v>.
+Ranked value at the given percentile,
+or score at or below which a percentage falls
+in its frequency distribution (inclusive).
+
+The default shows C<+> at C<50v> for the mean or median:
+the middle value or interpolation between two values.
+One standard deviation below the median is at about C<68v>.
 The default includes C<< >31.73v <68.27v >>
 to encompass all I<normal> results, or 68% of all entries, by I<< <--> >>.
 
 =item B<avg>
 
-Matches the average;
+Matches the average (arithmetic mean);
 the sum of all values divided by the number of counted lines.
 Indicated by default as C<=>.