File Coverage

blib/lib/Bloom/Faster.pm
Criterion Covered Total %
statement 41 71 57.7
branch 7 14 50.0
condition 1 6 16.6
subroutine 12 21 57.1
pod 0 12 0.0
total 61 124 49.1


line stmt bran cond sub pod time code
1             package Bloom::Faster;
2              
3 1     1   21972 use 5.008005;
  1         5  
  1         37  
4 1     1   6 use strict;
  1         2  
  1         32  
5 1     1   5 use warnings;
  1         6  
  1         43  
6 1     1   5 use Carp;
  1         1  
  1         111  
7              
8             require Exporter;
9 1     1   1003 use AutoLoader;
  1         1731  
  1         6  
10              
11             our @ISA = qw(Exporter);
12              
13             # Items to export into callers namespace by default. Note: do not export
14             # names by default without a very good reason. Use EXPORT_OK instead.
15             # Do not simply export all your public functions/methods/constants.
16              
17             # This allows declaration use Bloom ':all';
18             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19             # will save memory.
20             our %EXPORT_TAGS = ( 'all' => [ qw(
21             HASHCNT
22             SET
23             ) ] );
24              
25             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
26              
27             our @EXPORT = qw(
28             HASHCNT
29             SET
30             );
31              
32              
33             sub AUTOLOAD {
34             # This AUTOLOAD is used to 'autoload' constants from the constant()
35             # XS function.
36              
37 1     1   75 my $constname;
38 1         2 our $AUTOLOAD;
39 1         7 ($constname = $AUTOLOAD) =~ s/.*:://;
40 1 50       5 croak "&Bloom::constant not defined" if $constname eq 'constant';
41 1         9 my ($error, $val) = constant($constname);
42 1 50       4 if ($error) { croak $error; }
  1         217  
43             {
44 1     1   209 no strict 'refs';
  1         2  
  1         920  
  0         0  
45             # Fixed between 5.005_53 and 5.005_61
46             #XXX if ($] >= 5.00561) {
47             #XXX *$AUTOLOAD = sub () { $val };
48             #XXX }
49             #XXX else {
50 0     0   0 *$AUTOLOAD = sub { $val };
  0         0  
51             #XXX }
52             }
53 0         0 goto &$AUTOLOAD;
54             }
55              
56              
57             our $VERSION = '1.7';
58              
59              
60             require XSLoader;
61             XSLoader::load('Bloom::Faster', $VERSION);
62              
63              
64             sub new {
65 2     2 0 445 my ($package,$data) = @_;
66 2         5 my %struct;
67 2 100       7 if (! (ref $data)) {
68 1         1678 $struct{vector} = bdeserialize($data);
69             } else {
70 1 50 33     11 if (defined($data->{e}) && defined($data->{n})) {
    0 0        
71 1         120 $struct{vector} = binit_sugg($data->{n},$data->{e});
72             } elsif (defined($data->{m}) && defined($data->{k})) {
73 0         0 $data->{n} = $data->{e} = 0;
74 0         0 $struct{vector} = binit($data->{m},$data->{k},$data->{n},$data->{e});
75             } else {
76 0         0 $! = "invalid arguments to Bloom::Faster";
77 0         0 return undef;
78             }
79            
80 1 50       5 if (!defined($struct{vector})) {
81 0         0 $! = "bloom init failure\n";
82 0         0 return undef;
83             }
84             }
85             # binit will implicitly set m to a close prime
86             ###$struct{inserts} = 0;
87            
88 2         19 bless \%struct => $package;
89             }
90              
91             sub from_file {
92 0     0 0 0 my ($self, $filename) = @_;
93 0         0 bloom_destroyer($self->{vector});
94 0         0 $self->{vector} = bdeserialize($filename);
95             }
96              
97             sub to_file {
98 1     1 0 4 my ($self, $filename) = @_;
99 1         7468 return bserialize($self->{vector}, $filename);
100              
101             }
102              
103             sub vector {
104 0     0 0 0 my ($self) = @_;
105            
106 0         0 return get_vector($self->{vector});
107             }
108              
109             sub get_suggestion {
110 0     0 0 0 my ($n,$e) = @_;
111            
112 0         0 my ($m,$k);
113 0         0 $m = $k = 0;
114            
115 0         0 suggestion($n,$e,$m,$k);
116            
117 0         0 return ($m,$k);
118             }
119              
120             sub DESTROY {
121 2     2   5 my ($self) = @_;
122 2 50       706 defined($self->{vector}) and bloom_destroyer($self->{vector});
123             }
124              
125             sub get_inserts {
126 0     0 0 0 my ($self) = @_;
127             ##return $self->{inserts};
128 0         0 return binserts($self->{vector});
129             }
130              
131             sub key_count {
132 0     0 0 0 my ($self) = @_;
133 0         0 return $self->get_inserts();
134             }
135              
136             sub capacity {
137 2     2 0 4 my ($self) = @_;
138 2         21 return bcapacity($self->{vector});
139             }
140              
141             sub get_vectorsize {
142 0     0 0 0 my ($self) = @_;
143 0         0 return belements($self->{vector});
144             }
145              
146             sub test {
147 0     0 0 0 my ($self,$str) = @_;
148            
149 0         0 return $self->add($str);
150             }
151              
152             sub check {
153 0     0 0 0 my ($self,$str) = @_;
154            
155             ##$self->{inserts}++;
156 0         0 return test_bloom($self->{vector},$str,0);
157             }
158              
159             sub add {
160 100005     100005 0 920593 my ($self,$str) = @_;
161            
162             ##$self->{inserts}++;
163 100005         1959176 return test_bloom($self->{vector},$str,1);
164             }
165              
166              
167              
168             # Preloaded methods go here.
169              
170             # Autoload methods go after =cut, and are processed by the autosplit program.
171              
172             1;
173             __END__