$cache = proximityCache(\%hash, $range, $step); $value = getCached($cache, $key); sub proximityCache { my ($mi,$span,$step) = @_; my ($min, $max); while (my ($m) = each %$mi) { $min = $m if not defined($min) or $min > $m; $max = $m if not defined($max) or $max < $m; } my $latest = $min - $span; my %cache = (); for (my $m = $min - $span; $m <= $max + $span; $m += $step) { $latest = $m - $span if $m - $span > $latest; for my $d ($latest .. ($m + $span)) { $cache{$m} = [ $mi->{$d}, $latest = $d ] if exists $mi->{$d} and ( not($cache{$m}) or $cache{$m}[0] < $mi->{$d} ); } } return \%cache; } sub getCached { my ($cache, $key) = @_; return $cache->{$key}[0]; }
In reply to Proximity Caching by japhy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |