text
stringlengths
0
834k
sub cache_set {
my $o = shift;
my $type = shift; # 'query' or 'cache'
my $set = shift; # defaults to currently selected set
my $entry = shift; # defaults to 'results';
$entry = $o->conf('sort-field') unless defined $entry;
return undef unless grep { lc($entry) eq $_ } (sort_fields(), 'query');
$set = $o->{SHELL}{CACHE}{$type}{current} unless defined $set;
my $src = $o->{SHELL}{CACHE}{$type}{sets};
return undef unless defined $set;
return undef unless bounded(0, $set, $#$src);
# We've changed sort-field at some point -- make sure the sorted data
# exists, or else build it:
unless (defined $src->[$set]{$entry}) {
my $raw = $src->[$set]{raw};
my @sorted = $o->sort_pkgs($entry, @$raw);
$src->[$set]{$entry} = \@sorted;
}
return wantarray ? @{$src->[$set]{$entry}} : $src->[$set]{$entry};
}
sub cache_clear {
my $o = shift;
my $type = shift; # 'query' or 'cache'
$o->{SHELL}{CACHE}{$type}{sets} = [];
$o->{SHELL}{CACHE}{$type}{current} = -1;
$o->{SHELL}{CACHE}{$type}{index} = -1;
}
sub cache_sets {
my $o = shift;
my $type = shift;
@{$o->{SHELL}{CACHE}{$type}{sets}};
}
# This sub searches for an entry in the cache whose name matches that thing
# passed in. It searches in the current cache first. If the name isn't found,
# it searches in all caches. If the name still isn't found, it returns undef.
sub cache_find {
my $o = shift;
my $type = shift;
my $name = shift;
my $ncaches = $o->cache_sets($type);
my $current = $o->cache_set_current($type);
# First, search the current set:
my @pkgs = map { $_ ? $_->name : '' } $o->cache_set($type);
my $ind = find_index($name, 0, @pkgs);
return ($current, $ind) if $ind >= 0;
# Now try to find in all the sets:
for my $s (0 .. $ncaches - 1) {
next if $s == $current;
@pkgs = map { $_ ? $_->name : '' } $o->cache_set($type, $s);
$ind = find_index($name, 0, @pkgs);
return ($s, $ind) if $ind >= 0;
}
return (-1, -1);
}
# A pretty separator to print between logically separate items:
my $SEP;
BEGIN {
$SEP = '=' x 20;
}
# Useful functions:
sub max (&@) {
my $code = shift;
my $max;
local $_;
for (@_) {
my $res = $code->($_);
$max = $res if not defined $max or $max < $res;
}
$max || 0;
}
sub min (&@) {
my $code = shift;
my $min;
local $_;
for (@_) {
my $res = $code->($_);
$min = $res if not defined $min or $min > $res;
}
$min || 0;
}
sub sum (&@) {
my $code = shift;
my $sum = 0;
local $_;