TT/Enum.pm

filter: Statementstmt 15.5% 12 / 77 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 30.7% 4 / 13 Podpod 0.0% 0 / 9 totaltotal 11.0% 16 / 145 CC 19 SCAR 30.8
CC
19
Cov
10%
loadNativeFh
56.0
getId
20.0
saveNativeFh
20.0
CRAP 21.8
« TT/DBx/Document.pm ↑ Summary TT/Sentence.pm »
1 ## -*- Mode: CPerl -*-
2 ## File: Lingua::TT::Enum.pm
3 ## Author: Bryan Jurish <TT/IO.pm>
4 ## Descript: TT Utils: in-memory Enum
5  
6  
7 package Lingua::TT::Enum;
8 1 ▶use Lingua::TT::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 ##==============================================================================
13 ## Globals & Constants
14  
15 1 our @ISA = qw(Lingua::TT::Persistent);
16  
17 ##==============================================================================
18 ## Constructors etc.
19  
20 ## $enum = CLASS_OR_OBJECT->new(%opts)
21 ## + %opts, %$enum:
22 ## sym2id => \%sym2id, ##-- $sym=>$id, ...
23 ## id2sym => \@id2sym, ##-- $id=>$sym, ...
24 ## size => $n_ids, ##-- index of first free id
25 ## maxid => $max, ##-- maximum allowable id, e.g. 2**16-1, 2**32-1, (default=undef: no max)
26 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod undocumented
27 0 my $that = shift;
28 0 ▶ my $enum = bless({
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
29 sym2id => {},
30 id2sym => [],
31 size => 0,
32 maxid => undef,
33 @_
34 }, ref($that)||$that);
35 0 return $enum;
36 }
37  
38 ## undef = $enum->clear()
39 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod undocumented
40 0 my $enum = shift;
41 0 %{$enum->{sym2id}} = qw();
42 0 @{$enum->{id2sym}} = qw();
43 0 $enum->{size} = 0;
44 0 return $enum;
45 }
46  
47 ##==============================================================================
48 ## Methods: Access and Manipulation
49  
50 ## $id = $enum->getId($sym)
51 ## + gets (possibly new) id for $sym
52 0 ▶sub getId {
Subroutine getId: not called CC=4 SCAR=30.0
Pod undocumented
53 0 ▶ return $_[0]{sym2id}{$_[1]} if (exists($_[0]{sym2id}{$_[1]}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[0]{"sym2id"}{$_[1]} 0 0 0
54 confess(ref($_[0])."::getId(): maxid=$_[0]{maxid} exceeded!") ##-- check for overflow
55 0 ▶ if (defined($_[0]{maxid}) && $_[0]{size}==$_[0]{maxid});
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"maxid"} and $_[0]{"size"} == $_[0]{"maxid"} 0 0 0
Condition0%0/3
defined $_[0]{"maxid"} and $_[0]{"size"} == $_[0]{"maxid"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[0]{"maxid"} and $_[0]{"size"} == $_[0]{"maxid"}
defined $_[0]{"maxid"} $_[0]{"size"} == $_[0]{"maxid"}
Truth table0%0/3
A and B
A= defined $_[0]{"maxid"}B= $_[0]{"size"} == $_[0]{"maxid"}
ABresult
0X0
100
111
56 0 $_[0]{id2sym}[$_[0]{size}] = $_[1];
57 0 return $_[0]{sym2id}{$_[1]} = $_[0]{size}++;
58 }
59  
60 ## $id = $enum->getSym($id)
61 ## + gets (possibly new (and if so, "SYM${i}")) symbol for $id
62 1 ▶BEGIN { *getSym = \&getSymbol; };
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 ▶sub getSymbol {
Subroutine getSymbol: not called CC=2 SCAR=17.9
Pod undocumented
64 0 ▶ return $_[0]{id2sym}[$_[1]] if ($_[1] < $_[0]{size});
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] < $_[0]{"size"} 0 0 0
65 0 $_[0]{sym2id}{"SYM$_[1]"} = $_[1];
66 0 return $_[0]{id2sym}[$_[1]] = "SYM$_[1]";
67 }
68  
69 ## $id = $enum->setId($sym=>$id)
70 0 ▶sub setId {
Subroutine setId: not called CC=1 SCAR=6.9
Pod undocumented
71 0 $_[0]{id2sym}{$_[1]}=$_[2];
72 0 $_[0]{sym2id}[$_[2]]=$_[1];
73 0 return $_[2];
74 }
75  
76 ##==============================================================================
77 ## Methods: I/O
78  
79 ##--------------------------------------------------------------
80 ## Methods: I/O: Native
81  
82 ## $bool = $enum->saveNativeFh($fh,%opts)
83 ## + saves to filehandle
84 ## + implicitly sets $fh ':utf8' flag unless $opts{raw} is set
85 ## + %opts
86 ## noids => $bool, ##-- suppress printing of ids?
87 0 ▶sub saveNativeFh {
Subroutine saveNativeFh: not called CC=4 SCAR=30.0
Pod undocumented
88 0 my ($enum,$fh,%opts) = @_;
89 0 my ($sym,$id);
90 0 ▶ if ($opts{noids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'noids'}) { } 0 0 0
91 0 my $id2sym = $enum->{id2sym};
92 0 ▶ $fh->print(map {(defined($_) ? $_ : '')."\n"} @$id2sym);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
93 } else {
94 0 my $sym2id = $enum->{sym2id};
95 0 foreach $sym (sort {$sym2id->{$a} <=> $sym2id->{$b}} keys (%$sym2id)) {
96 0 $fh->print($sym2id->{$sym}, "\t", $sym, "\n");
97 }
98 }
99 0 return $enum;
100 }
101  
102 ## $bool = $enum->loadNativeFh($fh)
103 ## + loads from handle
104 ## + %opts
105 ## encoding => $enc, ##-- use encoding (default='utf8', unless 'raw' is true)
106 ## noids => $bool, ##-- don't expect to load ids?
107 0 ▶sub loadNativeFh {
Subroutine loadNativeFh: not called CC=7 SCAR=40.3
Pod undocumented
108 0 my ($enum,$fh,%opts) = @_;
109 0 ▶ $enum = $enum->new() if (!ref($enum));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $enum 0 0 0
110 0 my $id2sym = $enum->{id2sym};
111 0 my $sym2id = $enum->{sym2id};
112 0 my ($line,$sym);
113 0 my $id=0;
114 0 ▶ if ($opts{noids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'noids'}) { } 0 0 0
115 0 ▶ while (defined($sym=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($sym = readline $fh)) 0 0 0
116 0 chomp($sym);
117 0 $id2sym->[$id] = $sym;
118 0 $sym2id->{$sym} = $id;
119 0 ++$id;
120 }
121 } else {
122 0 ▶ while (defined($line=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline $fh)) 0 0 0
123 0 chomp($line);
124 0 ▶ next if ($line =~ /^\s*$/ || $line =~ /^%%/);
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ or $line =~ /^%%/ 0 0 0
Condition0%0/3
$line =~ /^\s*$/ or $line =~ /^%%/
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\s*$/ or $line =~ /^%%/
$line =~ /^\s*$/ $line =~ /^%%/
Truth table0%0/3
A or B
A= $line =~ /^\s*$/B= $line =~ /^%%/
ABresult
1X1
011
000
125 0 ($id,$sym) = split(/\t/,$line,2);
126 0 $id2sym->[$id] = $sym;
127 0 $sym2id->{$sym} = $id;
128 }
129 }
130 0 $enum->{size} = scalar(@$id2sym);
131 0 return $enum;
132 }
133  
134 ##--------------------------------------------------------------
135 ## Methods: I/O: Bin
136  
137  
138 ## ($serialized_string,\@other_refs) = STORABLE_freeze($obj, $cloning_flag)
139 0 ▶sub STORABLE_freeze {
Subroutine STORABLE_freeze: not called CC=2 SCAR=17.9
Pod undocumented
140 0 my ($obj,$cloning) = @_;
141 0 ▶ return ('',[map { $_ eq 'sym2id' ? qw() : ($_=>$obj->{$_}) } keys(%$obj)]);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'sym2id' ? : 0 0 0
142 }
143  
144 ## $obj = STORABLE_thaw($obj, $cloning_flag, $serialized_string, @other_refs)
145 0 ▶sub STORABLE_thaw {
Subroutine STORABLE_thaw: not called CC=4 SCAR=30.0
Pod undocumented
146 0 my ($obj,$cloning,$str,$ar) = @_;
147 0 ▶ if (!defined($str) || $str eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $str or $str eq "") 0 0 0
Condition0%0/3
not defined $str or $str eq ""
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $str or $str eq ""
not defined $str $str eq ""
Truth table0%0/3
not(A) or B
A= not defined $strB= $str eq ""
ABresult
1X1
011
000
148 ##-- backwards-compatibility
149 0 %$obj = @$ar;
150 #return $obj if (ref($obj) ne __PACKAGE__); ##-- hack
151 0 ▶ $obj->{sym2id} = {} if (!defined($obj->{sym2id}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj->{"sym2id"} 0 0 0
152 0 @{$obj->{sym2id}}{grep {defined($_)} @{$obj->{id2sym}}}
153 0 = grep {defined($obj->{id2sym}[$_])} (0..$#{$obj->{id2sym}});
154 }
155 0 return $obj;
156 }
157  
158  
159  
160 ##==============================================================================
161 ## Footer
162 1 1;
163  
164 __END__
« TT/DBx/Document.pm ↑ Summary TT/Sentence.pm »