File Coverage

blib/lib/WordListRole/BinarySearch.pm
Criterion Covered Total %
statement 27 29 93.1
branch 6 10 60.0
condition 3 8 37.5
subroutine 5 5 100.0
pod 1 1 100.0
total 42 53 79.2


line stmt bran cond sub pod time code
1             package WordListRole::BinarySearch;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2021-04-25'; # DATE
5             our $DIST = 'WordListRole-BinarySearch'; # DIST
6             our $VERSION = '0.006'; # VERSION
7              
8 1     1   14048 use strict 'subs', 'vars';
  1         3  
  1         40  
9 1     1   7 use warnings;
  1         3  
  1         28  
10 1     1   5 use Role::Tiny;
  1         3  
  1         7  
11              
12             sub word_exists {
13 1     1   194 no strict 'refs'; # this is required because Role::Tiny forces full stricture
  1         3  
  1         339  
14              
15 28     28 1 9743 require File::SortedSeek::PERLANCAR;
16              
17 28         5371 my ($self, $word) = @_;
18              
19 28   33     112 my $class = $self->{orig_class} // ref($self);
20              
21 28         45 my $dyn = ${"$class\::DYNAMIC"};
  28         106  
22 28 50       86 die "Can't binary search on a dynamic wordlist" if $dyn;
23              
24 28         40 my $fh = \*{"$class\::DATA"};
  28         78  
25 28   50     45 my $sort = ${"$class\::SORT"} || "";
26              
27 28         46 my $tell;
28 28 50 33     125 if ($sort && $sort =~ /num/) {
    50          
29 0         0 $tell = File::SortedSeek::PERLANCAR::numeric ($fh, $word, undef, $self->{fh_orig_pos});
30             } elsif (!$sort) {
31 28         80 $tell = File::SortedSeek::PERLANCAR::alphabetic($fh, $word, undef, $self->{fh_orig_pos});
32             } else {
33 0         0 die "Wordlist is not ascibetically/numerically sort (sort=$sort)";
34             }
35              
36 28 100       9648 return 0 unless File::SortedSeek::PERLANCAR::was_exact();
37 26 50       131 return 0 unless defined $tell;
38 26         90 1;
39             }
40              
41             1;
42             # ABSTRACT: Provide word_exists() that uses binary search
43              
44             __END__