File Coverage

blib/lib/Text/German/Cache.pm
Criterion Covered Total %
statement 24 48 50.0
branch 3 14 21.4
condition 5 12 41.6
subroutine 2 4 50.0
pod 0 3 0.0
total 34 81 41.9


line stmt bran cond sub pod time code
1             # -*- Mode: Perl -*-
2             # Cache.pm --
3             # Author : Ulrich Pfeifer
4             # Created On : Mon May 13 11:14:06 1996
5             # Last Modified By: Ulrich Pfeifer
6             # Last Modified On: Sun Apr 3 11:43:04 2005
7             # Language : CPerl
8             # Update Count : 17
9             # Status : Unknown, Use with caution!
10              
11             package Text::German::Cache;
12              
13             sub new {
14 1     1 0 3 my $type = shift;
15 1         3 my $self = {};
16 1         5 my %para = @_;
17              
18 1   50     6 $self->{Function} = $para{Function} || \&Text::German::reduce;
19 1   50     5 $self->{Hold} = $para{Hold} || 100;
20 1   33     5 $self->{Gc} = $para{Gc} || 2 * $self->{Hold};
21 1   50     25 $self->{Verbose} = $para{Verbose} || 0;
22 1         4 $self->{Entries} = 0;
23 1         2 $self->{Contents} = {};
24 1         3 $self->{Hit} = {};
25 1         4 $self->{Hits} = 0;
26 1         3 $self->{Misses} = 0;
27 1   33     11 bless $self, ref($type) || $type;
28             }
29              
30             sub get {
31 18     18 0 23 my $self = shift;
32 18         31 my $key = shift;
33              
34 18 100       51 if (defined $self->{Contents}->{$key}) {
35 4         7 $self->{Hits}++;
36 4         9 $self->{Hit}->{$key}++;
37             } else {
38 14         18 $self->{Misses}++;
39 14         18 $self->{Entries}++;
40 14 50       34 if ($self->{Entries} >= $self->{Gc}) {
41 0         0 $self->gc;
42             }
43 14         16 $self->{Contents}->{$key} = &{$self->{Function}}($key);
  14         42  
44             }
45 18         67 $self->{Contents}->{$key};
46             }
47              
48             sub gc {
49 0     0 0   my $self = shift;
50 0           my %rank;
51             my $rank;
52            
53 0 0         if ($self->{Verbose}) {
54 0           printf (STDERR "Cache: enter garbadge collect %d\n", $self->{Entries});
55             }
56 0           for (keys %{$self->{Contents}}) {
  0            
57 0           push @{$rank{$self->{Hit}->{$_}}}, $_;
  0            
58             }
59 0           for $rank (sort {$a <=> $b} keys %rank) {
  0            
60 0           for (@{$rank{$rank}}) {
  0            
61 0 0         if ($self->{Verbose}) {
62 0           printf (STDERR "Cache: deleting $_(%d)\n", $rank+1);
63             }
64 0           delete $self->{Contents}->{$_};
65 0           delete $self->{Hit}->{$_};
66 0           $self->{Entries}--;
67             }
68             # We delete a complete rank. this is more than we must do ..
69 0 0         last if $self->{Entries} <= $self->{Hold};
70             }
71 0 0         if ($self->{Verbose}) {
72 0           printf (STDERR "Cache: leave garbadge collect %d\n", $self->{Entries});
73             }
74             }
75              
76             sub DESTROY {
77 0     0     my $self = shift;
78              
79 0 0         if ($self->{Verbose}) {
80 0           printf (STDERR "\nCache Hits: %d\tMisses: %d\n", $self->{Hits}, $self->{Misses});
81             }
82             }
83              
84             1;