TPath/Index.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« TPath/Selector/Test.pm ↑ Summary TPath/AttributeTest.pm »
1 # ABSTRACT: tree-specific database
2  
3 package TPath::Index;
4 $TPath::Index::VERSION = '1.007';
5  
6 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 0 ▶use TPath::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=2.6
11  
12 with 'TPath::TypeCheck';
13  
14  
15 has f => ( is => 'ro', does => 'TPath::Forester', required => 1 );
16  
17  
18 has indexed => (
19 is => 'ro',
20 isa => 'HashRef',
21 default => sub { {} },
22 );
23  
24 has _is_indexed => ( is => 'rw', isa => 'Bool', default => 0 );
25  
26 # Map from children to their parents.
27 has cp_index => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
28  
29 # descendant index
30 has d_index => ( is => 'ro', isa => 'HashRef', default => sub {{}});
31  
32 # kid index
33 has k_index => ( is => 'ro', isa => 'HashRef', default => sub {{}});
34  
35  
36 has root => ( is => 'ro', required => 1 );
37  
38 # micro-optimization
39 has _root_ref => (
40 is => 'ro',
41 isa => 'Str',
42 lazy => 1,
43 default => sub { refaddr $_[0]->root }
44 );
45  
46  
47 sub is_root {
48 my ( $self, $n ) = @_;
49 return refaddr $n == $self->_root_ref;
50 }
51  
52 sub BUILD {
53 my $self = shift;
54 confess 'forester node type is '
55 . $self->f->node_type
56 . ' while index node type is '
57 . $self->node_type
58 unless ( $self->f->node_type // '' ) eq ( $self->node_type // '' );
59 }
60  
61  
62 sub index {
63 my $self = shift;
64 return if $self->_is_indexed;
65 $self->walk( $self->root );
66 $self->_is_indexed(1);
67 }
68  
69 sub walk {
70 my ( $self, $n ) = @_;
71 my $children = $self->f->_decontextualized_kids( $n, $self );
72 $self->n_index($n);
73 for my $c (@$children) {
74 $self->pc_index( $n, $c );
75 $self->walk($c);
76 }
77 }
78  
79  
80 sub parent {
81 my ( $self, $n ) = @_;
82 return $self->cp_index->{ refaddr $n };
83 }
84  
85 sub n_index {
86 my ( $self, $n ) = @_;
87 my $id = $self->id($n);
88 if ( defined $id ) {
89 $self->indexed->{$id} = $n;
90 }
91 }
92  
93  
94 sub pc_index {
95 my ( $self, $n, $c ) = @_;
96 confess "$c must be a reference" unless ref $c;
97 my $ref = $n;
98 weaken $ref;
99 $self->cp_index->{ refaddr $c} = $ref;
100 }
101  
102  
103 sub id {
104 my ( $self, $n ) = @_;
105 $self->_typecheck($n);
106 return $self->f->id($n);
107 }
108  
109 __PACKAGE__->meta->make_immutable;
110  
111 1;
112  
113 __END__
« TPath/Selector/Test.pm ↑ Summary TPath/AttributeTest.pm »