| 1 | package jsFind; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use 5.005; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 8 | ▶ | use HTML::Entities; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 8 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 8 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||
| 15 | 8 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 16 | 8 | import 'jsFind::Node'; | |||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | jsFind - generate index for full text search engine in JavaScript | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||
| 25 | use jsFind; | ||||||||||||||||||||||||||||||||||
| 26 | my $t = new jsFind(B => 4); | ||||||||||||||||||||||||||||||||||
| 27 | my $f = 1; | ||||||||||||||||||||||||||||||||||
| 28 | foreach my $k (qw{minima ut dolorem sapiente voluptatem}) { | ||||||||||||||||||||||||||||||||||
| 29 | $t->B_search(Key => $k, | ||||||||||||||||||||||||||||||||||
| 30 | Data => { | ||||||||||||||||||||||||||||||||||
| 31 | "path" => { | ||||||||||||||||||||||||||||||||||
| 32 | t => "word $k", | ||||||||||||||||||||||||||||||||||
| 33 | f => $f }, | ||||||||||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||||||||
| 35 | Insert => 1, | ||||||||||||||||||||||||||||||||||
| 36 | Append => 1, | ||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||
| 42 | This module can be used to create index files for jsFind, powerful tool for | ||||||||||||||||||||||||||||||||||
| 43 | adding a search engine to a CDROM archive or catalog without requiring the | ||||||||||||||||||||||||||||||||||
| 44 | user to install anything. | ||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||
| 46 | Main difference between this module and scripts delivered with jsFind are: | ||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||
| 48 | =over 5 | ||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||
| 50 | =item * | ||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||
| 52 | You don't need to use swish-e to create index | ||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||
| 54 | =item * | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | you can programatically (and incrementaly) create index for jsFind | ||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||
| 58 | =item * | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | you can create more than one index and search them using same C<search.html> | ||||||||||||||||||||||||||||||||||
| 61 | page | ||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | =back | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | You can also examine examples which come as tests with this module, | ||||||||||||||||||||||||||||||||||
| 66 | for example C<t/04words.t> or C<t/10homer.t>. | ||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||
| 68 | =head2 jsFind | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | jsFind search engine was written by Shawn Garbett from eLucid Software. | ||||||||||||||||||||||||||||||||||
| 71 | The search engine itself is a small piece of JavaScript (1.2 with level 2 | ||||||||||||||||||||||||||||||||||
| 72 | DOM). It is easily customizable to fit into a current set of HTML. This | ||||||||||||||||||||||||||||||||||
| 73 | JavaScript searches an XML index dataset for the appropriate links, and can | ||||||||||||||||||||||||||||||||||
| 74 | filter and sort the results. | ||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||
| 76 | JavaScript code distributed with this module is based on version 0.0.3 which | ||||||||||||||||||||||||||||||||||
| 77 | was current when this module development started. Various changes where done | ||||||||||||||||||||||||||||||||||
| 78 | on JavaScript code to fix bugs, add features and remove warnings. For | ||||||||||||||||||||||||||||||||||
| 79 | complete list see C<Changes> file which comes with distribution. | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | This module has been tested using C<html/test.html> with following browsers: | ||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||
| 83 | =over 5 | ||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||
| 85 | =item Mozilla FireFox 0.8 to 1.0 | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | using DOM 2 C<document.implementation.createDocument> | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | =item Internet Explorer 5.5 and 6.0 | ||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||
| 91 | using ActiveX C<Microsoft.XMLDOM> or C<MSXML2.DOMDocument> | ||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||
| 93 | =item Konqueror 3.3 | ||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||
| 95 | using DOM 2 C<document.implementation.createDocument> | ||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||
| 97 | =item Opera 7.54 (without Java) | ||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||
| 99 | using experimental iframe implementation which is much slower than other methods. | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | =back | ||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||
| 103 | If searching doesn't work for your combination of operating system and | ||||||||||||||||||||||||||||||||||
| 104 | browser, please open C<html/test.html> file and wait a while. It will search sample | ||||||||||||||||||||||||||||||||||
| 105 | file included with distribution and report results. Reports with included | ||||||||||||||||||||||||||||||||||
| 106 | test debugging are welcomed. | ||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | =head1 jsFind methods | ||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | C<jsFind> is mode implementing methods which you, the user, are going to | ||||||||||||||||||||||||||||||||||
| 111 | use to create indexes. | ||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||
| 113 | =head2 new | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | Create new tree. Arguments are C<B> which is maximum numbers of keys in | ||||||||||||||||||||||||||||||||||
| 116 | each node and optional C<Root> node. Each root node may have child nodes. | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | All nodes are objects from C<jsFind::Node>. | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | my $t = new jsFind(B => 4); | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | 8 | my $DEBUG = 1; | |||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=15.8
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 127 | 4 | my $package = shift; | |||||||||||||||||||||||||||||||||
| 128 | 4 | my %ARGV = @_; | |||||||||||||||||||||||||||||||||
| 129 | croak "Usage: {$package}::new(B => number [, Root => root node ])" | ||||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | unless exists $ARGV{B}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 131 | 4 | ▶ | if ($ARGV{B} % 2) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 132 | 0 | my $B = $ARGV{B} + 1; | |||||||||||||||||||||||||||||||||
| 133 | 0 | carp "B must be an even number. Using $B instead."; | |||||||||||||||||||||||||||||||||
| 134 | 0 | $ARGV{B} = $B; | |||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||
| 137 | 4 | my $B = $ARGV{B}; | |||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | my $Root = exists($ARGV{Root}) ? $ARGV{Root} : jsFind::Node->emptynode; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | 4 | bless { B => $B, Root => $Root } => $package; | |||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | =head2 B_search | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | Search, insert, append or replace data in B-Tree | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | $t->B_search( | ||||||||||||||||||||||||||||||||||
| 147 | Key => 'key value', | ||||||||||||||||||||||||||||||||||
| 148 | Data => { "path" => { | ||||||||||||||||||||||||||||||||||
| 149 | "t" => "title of document", | ||||||||||||||||||||||||||||||||||
| 150 | "f" => 99, | ||||||||||||||||||||||||||||||||||
| 151 | }, | ||||||||||||||||||||||||||||||||||
| 152 | }, | ||||||||||||||||||||||||||||||||||
| 153 | Insert => 1, | ||||||||||||||||||||||||||||||||||
| 154 | Append => 1, | ||||||||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | Semantics: | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | If key not found, insert it iff C<Insert> argument is present. | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | If key B<is> found, replace existing data iff C<Replace> argument | ||||||||||||||||||||||||||||||||||
| 162 | is present or add new datum to existing iff C<Append> argument is present. | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 5927 | ▶ | sub B_search { | ||||||||||||||||||||||||||||||||
|
Subroutine
B_search: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 167 | 5927 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 168 | 5927 | my %args = @_; | |||||||||||||||||||||||||||||||||
| 169 | 5927 | my $cur_node = $self->root; | |||||||||||||||||||||||||||||||||
| 170 | 5927 | my $k = $args{Key}; | |||||||||||||||||||||||||||||||||
| 171 | 5927 | my $d = $args{Data}; | |||||||||||||||||||||||||||||||||
| 172 | 5927 | my @path; | |||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | 5927 | ▶ | if ($cur_node->is_empty) { # Special case for empty root | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 175 | 3 | ▶ | if ($args{Insert}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 176 | 3 | $cur_node->kdp_insert($k => $d); | |||||||||||||||||||||||||||||||||
| 177 | 3 | return $d; | |||||||||||||||||||||||||||||||||
| 178 | } else { | ||||||||||||||||||||||||||||||||||
| 179 | 0 | return undef; | |||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||
| 183 | # Descend tree to leaf | ||||||||||||||||||||||||||||||||||
| 184 | 5924 | for (;;) { | |||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | # Didn't hit bottom yet. | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | 11524 | my($there, $where) = $cur_node->locate_key($k); | |||||||||||||||||||||||||||||||||
| 189 | 11524 | ▶ | if ($there) { # Found it! | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 190 | 3871 | ▶ | if ($args{Replace}) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 191 | 1 | $cur_node->kdp_replace($where, $k => $d); | |||||||||||||||||||||||||||||||||
| 192 | } elsif ($args{Append}) { | ||||||||||||||||||||||||||||||||||
| 193 | 3870 | $cur_node->kdp_append($where, $k => $d); | |||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||
| 195 | 3871 | return $cur_node->data($where); | |||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||
| 198 | # Not here---must be in a subtree. | ||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||
| 200 | 7653 | ▶ | if ($cur_node->is_leaf) { # But there are no subtrees | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 201 | 2053 | ▶ | return undef unless $args{Insert}; # Search failed | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 202 | # Stuff it in | ||||||||||||||||||||||||||||||||||
| 203 | 2053 | $cur_node->kdp_insert($k => $d); | |||||||||||||||||||||||||||||||||
| 204 | 2053 | ▶ | if ($self->node_overfull($cur_node)) { # Oops--there was no room. | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 205 | 40 | $self->split_and_promote($cur_node, @path); | |||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||
| 207 | 2053 | return $d; | |||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||
| 210 | # There are subtrees, and the key is in one of them. | ||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||
| 212 | 5600 | push @path, [$cur_node, $where]; # Record path from root. | |||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||
| 214 | # Move down to search the subtree | ||||||||||||||||||||||||||||||||||
| 215 | 5600 | $cur_node = $cur_node->subnode($where); | |||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||
| 217 | # and start over. | ||||||||||||||||||||||||||||||||||
| 218 | } # for (;;) ... | ||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||
| 220 | 0 | croak ("How did I get here?"); | |||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | sub split_and_promote_old { | ||||||||||||||||||||||||||||||||
|
Subroutine
split_and_promote_old: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 227 | 0 | my ($cur_node, @path) = @_; | |||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||
| 229 | 0 | for (;;) { | |||||||||||||||||||||||||||||||||
| 230 | 0 | my ($newleft, $newright, $kdp) = $cur_node->halves($self->B / 2); | |||||||||||||||||||||||||||||||||
| 231 | 0 | my ($up, $where) = @{pop @path}; | |||||||||||||||||||||||||||||||||
| 232 | 0 | ▶ | if ($up) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 233 | 0 | $up->kdp_insert(@$kdp); | |||||||||||||||||||||||||||||||||
| 234 | 0 | my ($tthere, $twhere) = $up->locate_key($kdp->[0]); | |||||||||||||||||||||||||||||||||
| 235 | 0 | ▶ | croak "Couldn't find key `$kdp->[0]' in node after just inserting it!" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 236 | unless $tthere; | ||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | croak "`$kdp->[0]' went into node at `$twhere' instead of expected `$where'!" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | unless $twhere == $where; | ||||||||||||||||||||||||||||||||||
| 239 | 0 | $up->subnode($where, $newleft); | |||||||||||||||||||||||||||||||||
| 240 | 0 | $up->subnode($where+1, $newright); | |||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | return unless $self->node_overfull($up); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 242 | 0 | $cur_node = $up; | |||||||||||||||||||||||||||||||||
| 243 | } else { # We're at the top; make a new root. | ||||||||||||||||||||||||||||||||||
| 244 | 0 | my $newroot = new jsFind::Node ([$kdp->[0]], | |||||||||||||||||||||||||||||||||
| 245 | [$kdp->[1]], | ||||||||||||||||||||||||||||||||||
| 246 | [$newleft, $newright]); | ||||||||||||||||||||||||||||||||||
| 247 | 0 | $self->root($newroot); | |||||||||||||||||||||||||||||||||
| 248 | 0 | return; | |||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||
| 254 | 40 | ▶ | sub split_and_promote { | ||||||||||||||||||||||||||||||||
|
Subroutine
split_and_promote: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 255 | 40 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 256 | 40 | my ($cur_node, @path) = @_; | |||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||
| 258 | 40 | for (;;) { | |||||||||||||||||||||||||||||||||
| 259 | 47 | my ($newleft, $newright, $kdp) = $cur_node->halves($self->B / 2); | |||||||||||||||||||||||||||||||||
| 260 | 47 | ▶ | my ($up, $where) = @{pop @path} if (@path); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 261 | 47 | ▶ | if ($up) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 262 | 42 | $up->kdp_insert(@$kdp); | |||||||||||||||||||||||||||||||||
| 263 | 42 | ▶ | if ($DEBUG) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 264 | 42 | my ($tthere, $twhere) = $up->locate_key($kdp->[0]); | |||||||||||||||||||||||||||||||||
| 265 | 42 | ▶ | croak "Couldn't find key `$kdp->[0]' in node after just inserting it!" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | unless $tthere; | ||||||||||||||||||||||||||||||||||
| 267 | 42 | ▶ | croak "`$kdp->[0]' went into node at `$twhere' instead of expected `$where'!" | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 268 | unless $twhere == $where; | ||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||
| 270 | 42 | $up->subnode($where, $newleft); | |||||||||||||||||||||||||||||||||
| 271 | 42 | $up->subnode($where+1, $newright); | |||||||||||||||||||||||||||||||||
| 272 | 42 | ▶ | return unless $self->node_overfull($up); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 273 | 7 | $cur_node = $up; | |||||||||||||||||||||||||||||||||
| 274 | } else { # We're at the top; make a new root. | ||||||||||||||||||||||||||||||||||
| 275 | 5 | my $newroot = new jsFind::Node([$kdp->[0]], | |||||||||||||||||||||||||||||||||
| 276 | [$kdp->[1]], | ||||||||||||||||||||||||||||||||||
| 277 | [$newleft, $newright]); | ||||||||||||||||||||||||||||||||||
| 278 | 5 | $self->root($newroot); | |||||||||||||||||||||||||||||||||
| 279 | 5 | return; | |||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||
| 284 | =head2 B | ||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||
| 286 | Return B (maximum number of keys) | ||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||
| 288 | my $max_size = $t->B; | ||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||
| 290 | =cut | ||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||
| 292 | 2143 | ▶ | sub B { | ||||||||||||||||||||||||||||||||
|
Subroutine
B: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 293 | 2143 | $_[0]{B}; | |||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||
| 296 | =head2 root | ||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||
| 298 | Returns root node | ||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | my $root = $t->root; | ||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||
| 302 | =cut | ||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||
| 304 | 5941 | ▶ | sub root { | ||||||||||||||||||||||||||||||||
|
Subroutine
root: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 305 | 5941 | my ($self, $newroot) = @_; | |||||||||||||||||||||||||||||||||
| 306 | 5941 | ▶ | $self->{Root} = $newroot if defined $newroot; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 307 | 5941 | $self->{Root}; | |||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | =head2 node_overfull | ||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||
| 312 | Returns if node is overfull | ||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||
| 314 | if ($node->node_overfull) { something } | ||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||
| 316 | =cut | ||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||
| 318 | 2095 | ▶ | sub node_overfull { | ||||||||||||||||||||||||||||||||
|
Subroutine
node_overfull: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 319 | 2095 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 320 | 2095 | my $node = shift; | |||||||||||||||||||||||||||||||||
| 321 | 2095 | $node->size > $self->B; | |||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||
| 324 | =head2 to_string | ||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||
| 326 | Returns your tree as formatted string. | ||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||
| 328 | my $text = $root->to_string; | ||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||
| 330 | Mostly usefull for debugging as output leaves much to be desired. | ||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||
| 332 | =cut | ||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||
| 334 | 3 | ▶ | sub to_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 335 | 3 | $_[0]->root->to_string; | |||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||
| 338 | =head2 to_dot | ||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||
| 340 | Create Graphviz graph of your tree | ||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||
| 342 | my $dot_graph = $root->to_dot; | ||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||
| 344 | =cut | ||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||
| 346 | 1 | ▶ | sub to_dot { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_dot: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 347 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||
| 349 | 1 | my $dot = qq/digraph dns {\nrankdir=LR;\n/; | |||||||||||||||||||||||||||||||||
| 350 | 1 | $dot .= $self->root->to_dot; | |||||||||||||||||||||||||||||||||
| 351 | 1 | $dot .= qq/\n}\n/; | |||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||
| 353 | 1 | return $dot; | |||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||
| 356 | =head2 to_jsfind | ||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||
| 358 | Create xml index files for jsFind. This should be called after | ||||||||||||||||||||||||||||||||||
| 359 | your B-Tree has been filled with data. | ||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||
| 361 | $root->to_jsfind( | ||||||||||||||||||||||||||||||||||
| 362 | dir => '/full/path/to/index/dir/', | ||||||||||||||||||||||||||||||||||
| 363 | data_codepage => 'ISO-8859-2', | ||||||||||||||||||||||||||||||||||
| 364 | index_codepage => 'UTF-8', | ||||||||||||||||||||||||||||||||||
| 365 | output_filter => sub { | ||||||||||||||||||||||||||||||||||
| 366 | my $t = shift || return; | ||||||||||||||||||||||||||||||||||
| 367 | $t =~ s/è/e/; | ||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||
| 369 | ); | ||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||
| 371 | All options except C<dir> are optional. | ||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||
| 373 | Returns number of nodes in created tree. | ||||||||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||||||||
| 375 | Options: | ||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||
| 377 | =over 4 | ||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||
| 379 | =item dir | ||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||
| 381 | Full path to directory for index (which will be created if needed). | ||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||
| 383 | =item data_codepage | ||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||
| 385 | If your imput data isn't in C<ISO-8859-1> encoding, you will have to specify | ||||||||||||||||||||||||||||||||||
| 386 | this option. | ||||||||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||||||||
| 388 | =item index_codepage | ||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||
| 390 | If your index encoding is not C<UTF-8> use this option. | ||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||
| 392 | If you are not using supplied JavaScript search code, or your browser is | ||||||||||||||||||||||||||||||||||
| 393 | terribly broken and thinks that index shouldn't be in UTF-8 encoding, use | ||||||||||||||||||||||||||||||||||
| 394 | this option to specify encoding for created XML index. | ||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||
| 396 | =item output_filter | ||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||
| 398 | B<this is just draft of documentation for option which is not implemented!> | ||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||
| 400 | Code ref to sub which can do modifications on resulting XML file for node. | ||||||||||||||||||||||||||||||||||
| 401 | Encoding of this data will be in L<index_codepage> and you have to take care | ||||||||||||||||||||||||||||||||||
| 402 | not to break XML structure. Calling L<xmllint> on your result index | ||||||||||||||||||||||||||||||||||
| 403 | (like C<t/90xmllint.t> does in this distribution) is a good idea after using | ||||||||||||||||||||||||||||||||||
| 404 | this option. | ||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||
| 406 | This option is also right place to plug in unaccenting function using | ||||||||||||||||||||||||||||||||||
| 407 | L<Text::Unaccent>. | ||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||
| 409 | =back | ||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||
| 411 | =cut | ||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||
| 413 | 8 | my $iconv; | |||||||||||||||||||||||||||||||||
| 414 | my $iconv_l1; | ||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||
| 416 | 4 | ▶ | sub to_jsfind { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_jsfind: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 417 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||
| 419 | 4 | my %arg = @_; | |||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||
| 421 | 4 | ▶ | confess "to_jsfind need path to your index directory !" unless ($arg{'dir'}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||
| 423 | 4 | my $data_codepage = $arg{'data_codepage'}; | |||||||||||||||||||||||||||||||||
| 424 | 4 | ▶ | my $index_codepage = $arg{'index_codepage'} || 'UTF-8'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{'index_codepage'} || 'UTF-8'
MC/DC100%1/1
$arg{'index_codepage'} || 'UTF-8'
$arg{'index_codepage'}
Truth table100%2/2
A
A= $arg{'index_codepage'}
| |||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||
| 426 | # create ISO-8859-1 iconv for HTML::Entities decode | ||||||||||||||||||||||||||||||||||
| 427 | 4 | $iconv_l1 = Text::Iconv->new('ISO-8859-1',$index_codepage); | |||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||
| 429 | # create another iconv for data | ||||||||||||||||||||||||||||||||||
| 430 | 4 | ▶ | if ($data_codepage && $index_codepage) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data_codepage and $index_codepage
MC/DC50%1/2
$data_codepage and $index_codepage
$data_codepage
$index_codepage
Truth table66%2/3
A and B
A= $data_codepageB= $index_codepage
| |||||||||||||||||||||||||||||||||||
| 431 | 2 | $iconv = Text::Iconv->new($data_codepage,$index_codepage); | |||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||
| 434 | 4 | return $self->root->to_jsfind($arg{'dir'},"0"); | |||||||||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||
| 438 | # private, default cmd function | ||||||||||||||||||||||||||||||||||
| 439 | 609161 | ▶ | sub default_cmp { | ||||||||||||||||||||||||||||||||
|
Subroutine
default_cmp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 440 | 609161 | $_[0] cmp $_[1]; | |||||||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||
| 443 | =head2 _recode | ||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||
| 445 | This is internal function to recode charset. | ||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||
| 447 | It will also try to decode entities in data using L<HTML::Entities>. | ||||||||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||||||||
| 449 | =cut | ||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||
| 451 | 13239 | ▶ | sub _recode { | ||||||||||||||||||||||||||||||||
|
Subroutine
_recode: called CC=5 SCAR=18.0
| |||||||||||||||||||||||||||||||||||
| 452 | 13239 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 453 | 13239 | ▶ | my $text = shift || return; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||
| 455 | 36 | ▶ | sub _decode_html_entities { | ||||||||||||||||||||||||||||||||
|
Subroutine
_decode_html_entities: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||
| 456 | 36 | ▶ | my $data = shift || return; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 457 | 36 | ▶ | $data = $iconv_l1->convert(decode_entities($data)) || croak "entity decode problem: $data"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$iconv_l1->convert(decode_entities($data)) || croak("entity decode problem: $data")
MC/DC0%0/1
$iconv_l1->convert(decode_entities($data)) || croak("entity decode problem: $data")
$iconv_l1->convert(decode_entities($data))
Truth table50%1/2
A
A= $iconv_l1->convert(decode_entities($data))
| |||||||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||
| 460 | 13239 | ▶ | if ($iconv) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 461 | 66 | ▶ | $text = $iconv->convert($text) || $text && carp "convert problem: $text"; | ||||||||||||||||||||||||||||||||
|
Condition16%1/6
$iconv->convert($text) || $text && carp("convert problem: $text")
$text and carp("convert problem: $text")
MC/DC0%0/3
$iconv->convert($text) || $text && carp("convert problem: $text")
$iconv->convert($text)
$text
carp("convert problem: $text")
Truth table25%1/4
A || B and C
A= $iconv->convert($text)B= $textC= carp("convert problem: $text")
| |||||||||||||||||||||||||||||||||||
| 462 | 66 | $text =~ s/(\&\w+;)/_decode_html_entities($1)/ges; | |||||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||
| 465 | 13239 | return $text; | |||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||
| 468 | ##################################################################### | ||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||
| 470 | =head1 jsFind::Node methods | ||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||
| 472 | Each node has C<k> key-data pairs, with C<B> <= C<k> <= C<2B>, and | ||||||||||||||||||||||||||||||||||
| 473 | each has C<k+1> subnodes, which might be null. | ||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||
| 475 | The node is a blessed reference to a list with three elements: | ||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||
| 477 | ($keylist, $datalist, $subnodelist) | ||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||
| 479 | each is a reference to a list list. | ||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||
| 481 | The null node is represented by a blessed reference to an empty list. | ||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||
| 483 | =cut | ||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||
| 485 | package jsFind::Node; | ||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||
| 487 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 488 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||||||||
| 490 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 491 | 8 | ▶ | use File::Path; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 492 | 8 | ▶ | use Text::Iconv; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 493 | 8 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||
| 495 | 8 | ▶ | use base 'jsFind'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||
| 497 | 8 | my $KEYS = 0; | |||||||||||||||||||||||||||||||||
| 498 | 8 | my $DATA = 1; | |||||||||||||||||||||||||||||||||
| 499 | 8 | my $SUBNODES = 2; | |||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||
| 501 | =head2 new | ||||||||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||||||||
| 503 | Create New node | ||||||||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||||||||
| 505 | my $node = new jsFind::Node ($keylist, $datalist, $subnodelist); | ||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||
| 507 | You can also mit argument list to create empty node. | ||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||
| 509 | my $empty_node = new jsFind::Node; | ||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||
| 511 | =cut | ||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||
| 513 | 103 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||
| 514 | 103 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 515 | 103 | ▶ | my $package = ref $self || $self; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table66%2/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||||||||||||||||
| 516 | 103 | ▶ | croak "Internal error: jsFind::Node::new called with wrong number of arguments." | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 3 or @_ == 0
MC/DC0%0/2
@_ == 3 or @_ == 0
@_ == 3
@_ == 0
Truth table66%2/3
A or B
A= @_ == 3B= @_ == 0
| |||||||||||||||||||||||||||||||||||
| 517 | unless @_ == 3 || @_ == 0; | ||||||||||||||||||||||||||||||||||
| 518 | 103 | bless [@_] => $package; | |||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||
| 521 | =head2 locate_key | ||||||||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||||||||
| 523 | Locate key in node using linear search. This should probably be replaced | ||||||||||||||||||||||||||||||||||
| 524 | by binary search for better performance. | ||||||||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||||||||
| 526 | my ($found, $index) = $node->locate_key($key, $cmp_coderef); | ||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||
| 528 | Argument C<$cmp_coderef> is optional reference to custom comparison | ||||||||||||||||||||||||||||||||||
| 529 | operator. | ||||||||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||||||||
| 531 | Returns (1, $index) if $key[$index] eq $key. | ||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||
| 533 | Returns (0, $index) if key could be found in $subnode[$index]. | ||||||||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||||||||
| 535 | In scalar context, just returns 1 or 0. | ||||||||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||||||||
| 537 | =cut | ||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||
| 539 | 13661 | ▶ | sub locate_key { | ||||||||||||||||||||||||||||||||
|
Subroutine
locate_key: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 540 | # Use linear search for testing, replace with binary search. | ||||||||||||||||||||||||||||||||||
| 541 | 13661 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 542 | 13661 | my $key = shift; | |||||||||||||||||||||||||||||||||
| 543 | 13661 | ▶ | my $cmp = shift || \&jsFind::default_cmp; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || \&jsFind::default_cmp
MC/DC0%0/1
shift() || \&jsFind::default_cmp
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 544 | 13661 | my $i; | |||||||||||||||||||||||||||||||||
| 545 | my $cmp_result; | ||||||||||||||||||||||||||||||||||
| 546 | 13661 | my $N = $self->size; | |||||||||||||||||||||||||||||||||
| 547 | 13661 | ▶ | for ($i = 0; $i < $N; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 548 | 609161 | $cmp_result = &$cmp($key, $self->key($i)); | |||||||||||||||||||||||||||||||||
| 549 | 609161 | ▶ | last if $cmp_result <= 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 550 | } | ||||||||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||||||||
| 552 | # $i is now the index of the first node-key greater than $key | ||||||||||||||||||||||||||||||||||
| 553 | # or $N if there is no such. $cmp_result is 0 iff the key was found. | ||||||||||||||||||||||||||||||||||
| 554 | 13661 | (!$cmp_result, $i); | |||||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||
| 558 | =head2 emptynode | ||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||
| 560 | Creates new empty node | ||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||
| 562 | $node = $root->emptynode; | ||||||||||||||||||||||||||||||||||
| 563 | $new_node = $node->emptynode; | ||||||||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||||||||
| 565 | =cut | ||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||
| 567 | 4 | ▶ | sub emptynode { | ||||||||||||||||||||||||||||||||
|
Subroutine
emptynode: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 568 | 4 | new($_[0]); # Pass package name, but not anything else. | |||||||||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||||||||
| 571 | =head2 is_empty | ||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||
| 573 | Test if node is empty | ||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||
| 575 | if ($node->is_empty) { something } | ||||||||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||||||||
| 577 | =cut | ||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||
| 579 | # undef is empty; so is a blessed empty list. | ||||||||||||||||||||||||||||||||||
| 580 | 8173 | ▶ | sub is_empty { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_empty: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||
| 581 | 8173 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 582 | 8173 | ▶ | !defined($self) || $#$self < 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 583 | } | ||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||
| 585 | =head2 key | ||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||
| 587 | Return C<$i>th key from node | ||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||
| 589 | my $key = $node->key($i); | ||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||
| 591 | =cut | ||||||||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||||||||
| 593 | 609161 | ▶ | sub key { | ||||||||||||||||||||||||||||||||
|
Subroutine
key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 594 | # my ($self, $n) = @_; | ||||||||||||||||||||||||||||||||||
| 595 | # $self->[$KEYS][$n]; | ||||||||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||||||||
| 597 | # speedup | ||||||||||||||||||||||||||||||||||
| 598 | 609161 | $_[0]->[$KEYS][$_[1]]; | |||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||
| 601 | =head2 data | ||||||||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||||||||
| 603 | Return C<$i>th data from node | ||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||
| 605 | my $data = $node->data($i); | ||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||
| 607 | =cut | ||||||||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||||||||
| 609 | 3871 | ▶ | sub data { | ||||||||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 610 | 3871 | my ($self, $n) = @_; | |||||||||||||||||||||||||||||||||
| 611 | 3871 | $self->[$DATA][$n]; | |||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||
| 614 | =head2 kdp_replace | ||||||||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||||||||
| 616 | Set key data pair for C<$i>th element in node | ||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||
| 618 | $node->kdp_replace($i, "key value" => { | ||||||||||||||||||||||||||||||||||
| 619 | "data key 1" => "data value 1", | ||||||||||||||||||||||||||||||||||
| 620 | "data key 2" => "data value 2", | ||||||||||||||||||||||||||||||||||
| 621 | }; | ||||||||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||||||||
| 623 | =cut | ||||||||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||||||||
| 625 | 1 | ▶ | sub kdp_replace { | ||||||||||||||||||||||||||||||||
|
Subroutine
kdp_replace: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 626 | 1 | my ($self, $n, $k => $d) = @_; | |||||||||||||||||||||||||||||||||
| 627 | 1 | ▶ | if (defined $k) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 628 | 1 | $self->[$KEYS][$n] = $k; | |||||||||||||||||||||||||||||||||
| 629 | 1 | $self->[$DATA][$n] = $d; | |||||||||||||||||||||||||||||||||
| 630 | } | ||||||||||||||||||||||||||||||||||
| 631 | 1 | [$self->[$KEYS][$n], | |||||||||||||||||||||||||||||||||
| 632 | $self->[$DATA][$n]]; | ||||||||||||||||||||||||||||||||||
| 633 | } | ||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||
| 635 | =head2 kdp_insert | ||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||
| 637 | Insert key/data pair in tree | ||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||
| 639 | $node->kdp_insert("key value" => "data value"); | ||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||
| 641 | No return value. | ||||||||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||||||||
| 643 | =cut | ||||||||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||||||||
| 645 | 2098 | ▶ | sub kdp_insert { | ||||||||||||||||||||||||||||||||
|
Subroutine
kdp_insert: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||
| 646 | 2098 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 647 | 2098 | my ($k => $d) = @_; | |||||||||||||||||||||||||||||||||
| 648 | 2098 | ▶ | my ($there, $where) = $self->locate_key($k) unless $self->is_empty; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||||||||
| 650 | 0 | ▶ | if ($there) { croak("Tried to insert `$k => $d' into node where `$k' was already present."); } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||
| 652 | # undef fix | ||||||||||||||||||||||||||||||||||
| 653 | 2098 | ▶ | $where ||= 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$where ||= 0
MC/DC100%1/1
$where ||= 0
$where
Truth table100%2/2
A
A= $where
| |||||||||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||||||||
| 655 | 2098 | splice(@{$self->[$KEYS]}, $where, 0, $k); | |||||||||||||||||||||||||||||||||
| 656 | 2098 | splice(@{$self->[$DATA]}, $where, 0, $d); | |||||||||||||||||||||||||||||||||
| 657 | 2098 | splice(@{$self->[$SUBNODES]}, $where, 0, undef); | |||||||||||||||||||||||||||||||||
| 658 | } | ||||||||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||||||||
| 660 | =head2 kdp_append | ||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||
| 662 | Adds new data keys and values to C<$i>th element in node | ||||||||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||||||||
| 664 | $node->kdp_append($i, "key value" => { | ||||||||||||||||||||||||||||||||||
| 665 | "added data key" => "added data value", | ||||||||||||||||||||||||||||||||||
| 666 | }; | ||||||||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||||||||
| 668 | =cut | ||||||||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||||||||
| 670 | 3870 | ▶ | sub kdp_append { | ||||||||||||||||||||||||||||||||
|
Subroutine
kdp_append: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||
| 671 | 3870 | my ($self, $n, $k => $d) = @_; | |||||||||||||||||||||||||||||||||
| 672 | 3870 | ▶ | if (defined $k) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 673 | 3870 | $self->[$KEYS][$n] = $k; | |||||||||||||||||||||||||||||||||
| 674 | 3870 | my ($kv,$dv) = %{$d}; | |||||||||||||||||||||||||||||||||
| 675 | 3870 | $self->[$DATA][$n]->{$kv} = $dv; | |||||||||||||||||||||||||||||||||
| 676 | } | ||||||||||||||||||||||||||||||||||
| 677 | 3870 | [$self->[$KEYS][$n], | |||||||||||||||||||||||||||||||||
| 678 | $self->[$DATA][$n]]; | ||||||||||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||||||||
| 681 | =head2 subnode | ||||||||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||||||||
| 683 | Set new or return existing subnode | ||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||
| 685 | # return 4th subnode | ||||||||||||||||||||||||||||||||||
| 686 | my $my_node = $node->subnode(4); | ||||||||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||||||||
| 688 | # create new subnode 5 from $my_node | ||||||||||||||||||||||||||||||||||
| 689 | $node->subnode(5, $my_node); | ||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||
| 691 | =cut | ||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||
| 693 | 5684 | ▶ | sub subnode { | ||||||||||||||||||||||||||||||||
|
Subroutine
subnode: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 694 | 5684 | my ($self, $n, $newnode) = @_; | |||||||||||||||||||||||||||||||||
| 695 | 5684 | ▶ | $self->[$SUBNODES][$n] = $newnode if defined $newnode; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 696 | 5684 | $self->[$SUBNODES][$n]; | |||||||||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||||||||
| 699 | =head2 is_leaf | ||||||||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||||||||
| 701 | Test if node is leaf | ||||||||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||||||||
| 703 | if ($node->is_leaf) { something } | ||||||||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||||||||
| 705 | =cut | ||||||||||||||||||||||||||||||||||
| 706 | |||||||||||||||||||||||||||||||||||
| 707 | 7686 | ▶ | sub is_leaf { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_leaf: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 708 | 7686 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 709 | 7686 | ! defined $self->[$SUBNODES][0]; # undefined subnode means leaf node. | |||||||||||||||||||||||||||||||||
| 710 | } | ||||||||||||||||||||||||||||||||||
| 711 | |||||||||||||||||||||||||||||||||||
| 712 | =head2 size | ||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||
| 714 | Return number of keys in the node | ||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||
| 716 | my $nr = $node->size; | ||||||||||||||||||||||||||||||||||
| 717 | |||||||||||||||||||||||||||||||||||
| 718 | =cut | ||||||||||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||||||||
| 720 | 15951 | ▶ | sub size { | ||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 721 | 15951 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 722 | 15951 | return scalar(@{$self->[$KEYS]}); | |||||||||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||
| 725 | =head2 halves | ||||||||||||||||||||||||||||||||||
| 726 | |||||||||||||||||||||||||||||||||||
| 727 | Split node into two halves so that keys C<0 .. $n-1> are in one node | ||||||||||||||||||||||||||||||||||
| 728 | and keys C<$n+1 ... $size> are in the other. | ||||||||||||||||||||||||||||||||||
| 729 | |||||||||||||||||||||||||||||||||||
| 730 | my ($left_node, $right_node, $kdp) = $node->halves($n); | ||||||||||||||||||||||||||||||||||
| 731 | |||||||||||||||||||||||||||||||||||
| 732 | =cut | ||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||
| 734 | 47 | ▶ | sub halves { | ||||||||||||||||||||||||||||||||
|
Subroutine
halves: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 735 | 47 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 736 | 47 | my $n = shift; | |||||||||||||||||||||||||||||||||
| 737 | 47 | my $s = $self->size; | |||||||||||||||||||||||||||||||||
| 738 | 47 | my @right; | |||||||||||||||||||||||||||||||||
| 739 | my @left; | ||||||||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||||||||
| 741 | 47 | $left[$KEYS] = [@{$self->[$KEYS]}[0 .. $n-1]]; | |||||||||||||||||||||||||||||||||
| 742 | 47 | $left[$DATA] = [@{$self->[$DATA]}[0 .. $n-1]]; | |||||||||||||||||||||||||||||||||
| 743 | 47 | $left[$SUBNODES] = [@{$self->[$SUBNODES]}[0 .. $n]]; | |||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||
| 745 | 47 | $right[$KEYS] = [@{$self->[$KEYS]}[$n+1 .. $s-1]]; | |||||||||||||||||||||||||||||||||
| 746 | 47 | $right[$DATA] = [@{$self->[$DATA]}[$n+1 .. $s-1]]; | |||||||||||||||||||||||||||||||||
| 747 | 47 | $right[$SUBNODES] = [@{$self->[$SUBNODES]}[$n+1 .. $s]]; | |||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||
| 749 | 47 | my @middle = ($self->[$KEYS][$n], $self->[$DATA][$n]); | |||||||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||||||||
| 751 | 47 | ($self->new(@left), $self->new(@right), \@middle); | |||||||||||||||||||||||||||||||||
| 752 | } | ||||||||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||||||||
| 754 | =head2 to_string | ||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||
| 756 | Dumps tree as string | ||||||||||||||||||||||||||||||||||
| 757 | |||||||||||||||||||||||||||||||||||
| 758 | my $str = $root->to_string; | ||||||||||||||||||||||||||||||||||
| 759 | |||||||||||||||||||||||||||||||||||
| 760 | =cut | ||||||||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||||||||
| 762 | 55 | ▶ | sub to_string { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_string: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||
| 763 | 55 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 764 | 55 | ▶ | my $indent = shift || 0; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || 0
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 765 | 55 | my $I = ' ' x $indent; | |||||||||||||||||||||||||||||||||
| 766 | 55 | ▶ | return '' if $self->is_empty; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 767 | 55 | my ($k, $d, $s) = @$self; | |||||||||||||||||||||||||||||||||
| 768 | 55 | my $result = ''; | |||||||||||||||||||||||||||||||||
| 769 | 55 | ▶ | $result .= defined($s->[0]) ? $s->[0]->to_string($indent+2) : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 770 | 55 | my $N = $self->size; | |||||||||||||||||||||||||||||||||
| 771 | 55 | my $i; | |||||||||||||||||||||||||||||||||
| 772 | 55 | ▶ | for ($i = 0; $i < $N; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 773 | # $result .= $I . "$k->[$i] => $d->[$i]\n"; | ||||||||||||||||||||||||||||||||||
| 774 | 2056 | $result .= $I . "$k->[$i]\n"; | |||||||||||||||||||||||||||||||||
| 775 | 2056 | ▶ | $result .= defined($s->[$i+1]) ? $s->[$i+1]->to_string($indent+2) : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 776 | } | ||||||||||||||||||||||||||||||||||
| 777 | 55 | $result; | |||||||||||||||||||||||||||||||||
| 778 | } | ||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||
| 780 | =begin comment | ||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||
| 782 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||
| 784 | sub to_string { | ||||||||||||||||||||||||||||||||||
| 785 | my $self = shift; | ||||||||||||||||||||||||||||||||||
| 786 | my $indent = shift || 0; | ||||||||||||||||||||||||||||||||||
| 787 | my $path = shift || '0'; | ||||||||||||||||||||||||||||||||||
| 788 | return '' if $self->is_empty; | ||||||||||||||||||||||||||||||||||
| 789 | my ($k, $d, $s) = @$self; | ||||||||||||||||||||||||||||||||||
| 790 | my $result = ''; | ||||||||||||||||||||||||||||||||||
| 791 | $result .= defined($s->[0]) ? $s->[0]->to_string($indent+1,"$path/0") : ''; | ||||||||||||||||||||||||||||||||||
| 792 | my $N = $self->size; | ||||||||||||||||||||||||||||||||||
| 793 | for (my $i = 0; $i < $N; $i++) { | ||||||||||||||||||||||||||||||||||
| 794 | my $dump = Dumper($d->[$i]); | ||||||||||||||||||||||||||||||||||
| 795 | $dump =~ s/[\n\r\s]+/ /gs; | ||||||||||||||||||||||||||||||||||
| 796 | $dump =~ s/\$VAR1\s*=\s*//; | ||||||||||||||||||||||||||||||||||
| 797 | $result .= sprintf("%-5s [%2d] %2s: %s => %s\n", $path, $i, $indent, $k->[$i], $dump); | ||||||||||||||||||||||||||||||||||
| 798 | $result .= defined($s->[$i+1]) ? $s->[$i+1]->to_string($indent+1,"$path/$i") : ''; | ||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||
| 800 | $result; | ||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||
| 803 | =end comment | ||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||
| 805 | =head2 to_dot | ||||||||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||||||||
| 807 | Recursivly walk nodes of tree | ||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||
| 809 | =cut | ||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||
| 811 | 33 | ▶ | sub to_dot { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_dot: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||
| 812 | 33 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 813 | 33 | my $parent = shift; | |||||||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||||||||
| 815 | 33 | ▶ | return '' if $self->is_empty; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||
| 817 | 33 | my $dot = ''; | |||||||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||||||||
| 819 | 33 | my ($k, $d, $s) = @$self; | |||||||||||||||||||||||||||||||||
| 820 | 33 | my $N = $self->size; | |||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||
| 822 | 33 | my @dot_keys; | |||||||||||||||||||||||||||||||||
| 823 | |||||||||||||||||||||||||||||||||||
| 824 | 33 | ▶ | my $node_name = $parent || '_'; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$parent || '_'
MC/DC100%1/1
$parent || '_'
$parent
Truth table100%2/2
A
A= $parent
| |||||||||||||||||||||||||||||||||||
| 825 | 33 | $node_name =~ s/\W+//g; | |||||||||||||||||||||||||||||||||
| 826 | 33 | $node_name .= " [$N]"; | |||||||||||||||||||||||||||||||||
| 827 | |||||||||||||||||||||||||||||||||||
| 828 | 33 | ▶ | for (my $i = 0; $i <= $N; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 829 | 118 | ▶ | if (my $key = $k->[$i]) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 830 | 85 | push @dot_keys, qq{<$i>$key}; | |||||||||||||||||||||||||||||||||
| 831 | } | ||||||||||||||||||||||||||||||||||
| 832 | 118 | ▶ | $dot .= $s->[$i]->to_dot(qq{"$node_name":$i}) if ($s->[$i]); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 833 | } | ||||||||||||||||||||||||||||||||||
| 834 | 33 | ▶ | push @dot_keys, qq{<$N>...} if (! $self->is_leaf); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 835 | |||||||||||||||||||||||||||||||||||
| 836 | 33 | my $label = join("|",@dot_keys); | |||||||||||||||||||||||||||||||||
| 837 | 33 | $dot .= qq{"$node_name" [ shape=record, label="$label" ];\n}; | |||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||
| 839 | 33 | ▶ | $dot .= qq{$parent -> "$node_name";\n} if ($parent); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 840 | |||||||||||||||||||||||||||||||||||
| 841 | 33 | $dot; | |||||||||||||||||||||||||||||||||
| 842 | } | ||||||||||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||||||||
| 844 | =head2 to_xml | ||||||||||||||||||||||||||||||||||
| 845 | |||||||||||||||||||||||||||||||||||
| 846 | Escape <, >, & and ", and to produce valid XML | ||||||||||||||||||||||||||||||||||
| 847 | |||||||||||||||||||||||||||||||||||
| 848 | =cut | ||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||
| 850 | 8 | my %escape = ('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"'); | |||||||||||||||||||||||||||||||||
| 851 | 8 | my $escape_re = join '|' => keys %escape; | |||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||
| 853 | 13239 | ▶ | sub to_xml { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_xml: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||
| 854 | 13239 | ▶ | my $self = shift || confess "you should call to_xml as object!"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || confess('you should call to_xml as object!')
MC/DC0%0/1
shift() || confess('you should call to_xml as object!')
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 855 | |||||||||||||||||||||||||||||||||||
| 856 | 13239 | ▶ | my $d = shift || return; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return)
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 857 | 13239 | $d = $self->SUPER::_recode($d); | |||||||||||||||||||||||||||||||||
| 858 | 13239 | ▶ | confess "escape_re undefined!" unless ($escape_re); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 859 | 13239 | $d =~ s/($escape_re)/$escape{$1}/g; | |||||||||||||||||||||||||||||||||
| 860 | 13239 | return $d; | |||||||||||||||||||||||||||||||||
| 861 | } | ||||||||||||||||||||||||||||||||||
| 862 | |||||||||||||||||||||||||||||||||||
| 863 | =head2 base_x | ||||||||||||||||||||||||||||||||||
| 864 | |||||||||||||||||||||||||||||||||||
| 865 | Convert number to base x (used for jsFind index filenames). | ||||||||||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||||||||
| 867 | my $n = $tree->base_x(50); | ||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||
| 869 | =cut | ||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||
| 871 | 1356 | ▶ | sub base_x { | ||||||||||||||||||||||||||||||||
|
Subroutine
base_x: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||
| 872 | 1356 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 873 | |||||||||||||||||||||||||||||||||||
| 874 | 1356 | my $value = shift; | |||||||||||||||||||||||||||||||||
| 875 | |||||||||||||||||||||||||||||||||||
| 876 | 1356 | ▶ | confess("need non-negative number") if (! defined($value) || $value < 0); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $value or $value < 0
MC/DC0%0/2
not defined $value or $value < 0
not defined $value
$value < 0
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value < 0
| |||||||||||||||||||||||||||||||||||
| 877 | |||||||||||||||||||||||||||||||||||
| 878 | 1356 | my @digits = qw( | |||||||||||||||||||||||||||||||||
| 879 | 0 1 2 3 4 5 6 7 8 9 | ||||||||||||||||||||||||||||||||||
| 880 | a b c d e f g h i j k l m n o p q r s t u v w x y z | ||||||||||||||||||||||||||||||||||
| 881 | ); | ||||||||||||||||||||||||||||||||||
| 882 | |||||||||||||||||||||||||||||||||||
| 883 | 1356 | my $base = scalar(@digits); | |||||||||||||||||||||||||||||||||
| 884 | 1356 | my $out = ""; | |||||||||||||||||||||||||||||||||
| 885 | 1356 | my $pow = 1; | |||||||||||||||||||||||||||||||||
| 886 | 1356 | my $pos = 0; | |||||||||||||||||||||||||||||||||
| 887 | |||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||
| 889 | 1356 | ▶ | if($value == 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 890 | 18 | return "0"; | |||||||||||||||||||||||||||||||||
| 891 | } | ||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||
| 893 | 1338 | ▶ | while($value > 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 894 | 2598 | $pos = $value % $base; | |||||||||||||||||||||||||||||||||
| 895 | 2598 | $out = $digits[$pos] . $out; | |||||||||||||||||||||||||||||||||
| 896 | 2598 | $value = floor($value/$base); | |||||||||||||||||||||||||||||||||
| 897 | 2598 | $pow *= $base; | |||||||||||||||||||||||||||||||||
| 898 | } | ||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||
| 900 | 1338 | return $out; | |||||||||||||||||||||||||||||||||
| 901 | } | ||||||||||||||||||||||||||||||||||
| 902 | |||||||||||||||||||||||||||||||||||
| 903 | =head2 to_jsfind | ||||||||||||||||||||||||||||||||||
| 904 | |||||||||||||||||||||||||||||||||||
| 905 | Create jsFind xml files | ||||||||||||||||||||||||||||||||||
| 906 | |||||||||||||||||||||||||||||||||||
| 907 | my $nr=$tree->to_jsfind('/path/to/index','0'); | ||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||
| 909 | Returns number of elements created | ||||||||||||||||||||||||||||||||||
| 910 | |||||||||||||||||||||||||||||||||||
| 911 | =cut | ||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||
| 913 | 60 | ▶ | sub to_jsfind { | ||||||||||||||||||||||||||||||||
|
Subroutine
to_jsfind: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||||||||
| 914 | 60 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 915 | 60 | my ($path,$file) = @_; | |||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||
| 917 | 60 | ▶ | return 0 if $self->is_empty; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||
| 919 | 60 | ▶ | confess("path is undefined.") unless ($path); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 920 | 60 | ▶ | confess("file is undefined. Did you call \$t->root->to_jsfind(..) instead of \$t->to_jsfind(..) ?") unless (defined($file)); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 921 | |||||||||||||||||||||||||||||||||||
| 922 | 60 | $file = $self->base_x($file); | |||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||
| 924 | 60 | my $nr_keys = 0; | |||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||
| 926 | 60 | my ($k, $d, $s) = @$self; | |||||||||||||||||||||||||||||||||
| 927 | 60 | my $N = $self->size; | |||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||
| 929 | 60 | my ($key_xml, $data_xml) = ("<n>","<d>"); | |||||||||||||||||||||||||||||||||
| 930 | |||||||||||||||||||||||||||||||||||
| 931 | 60 | ▶ | for (my $i = 0; $i <= $N; $i++) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 932 | 2127 | my $key = lc($k->[$i]); | |||||||||||||||||||||||||||||||||
| 933 | |||||||||||||||||||||||||||||||||||
| 934 | 2127 | ▶ | if ($key) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 935 | 2067 | $key_xml .= '<k>'.$self->to_xml($key).'</k>'; | |||||||||||||||||||||||||||||||||
| 936 | 2067 | $data_xml .= '<e>'; | |||||||||||||||||||||||||||||||||
| 937 | #use Data::Dumper; | ||||||||||||||||||||||||||||||||||
| 938 | #print Dumper($d->[$i]); | ||||||||||||||||||||||||||||||||||
| 939 | 2067 | foreach my $path (keys %{$d->[$i]}) { | |||||||||||||||||||||||||||||||||
| 940 | 5586 | ▶ | $data_xml .= '<l f="'.($d->[$i]->{$path}->{'f'} || 1).'" t="'.$self->to_xml($d->[$i]->{$path}->{'t'} || 'no title').'">'.$self->to_xml($path).'</l>'; | ||||||||||||||||||||||||||||||||
|
Condition75%3/4
$d->[$i]{$path}{'f'} || 1
$d->[$i]{$path}{'t'} || 'no title'
MC/DC100%1/1
$d->[$i]{$path}{'f'} || 1
$d->[$i]{$path}{'f'}
MC/DC0%0/1
$d->[$i]{$path}{'t'} || 'no title'
$d->[$i]{$path}{'t'}
Truth table100%2/2
A
A= $d->[$i]{$path}{'f'}
Truth table50%1/2
A
A= $d->[$i]{$path}{'t'}
| |||||||||||||||||||||||||||||||||||
| 941 | 5586 | $nr_keys++; | |||||||||||||||||||||||||||||||||
| 942 | } | ||||||||||||||||||||||||||||||||||
| 943 | 2067 | $data_xml .= '</e>'; | |||||||||||||||||||||||||||||||||
| 944 | } | ||||||||||||||||||||||||||||||||||
| 945 | |||||||||||||||||||||||||||||||||||
| 946 | 2127 | ▶ | $nr_keys += $s->[$i]->to_jsfind("$path/$file","$i") if ($s->[$i]); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 947 | } | ||||||||||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||||||||
| 949 | 60 | $key_xml .= '</n>'; | |||||||||||||||||||||||||||||||||
| 950 | 60 | $data_xml .= '</d>'; | |||||||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||||||||
| 952 | 60 | ▶ | if (! -e $path) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 953 | 10 | ▶ | mkpath($path) || croak "can't create dir '$path': $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 954 | } | ||||||||||||||||||||||||||||||||||
| 955 | |||||||||||||||||||||||||||||||||||
| 956 | 60 | ▶ | open(K, "> ${path}/${file}.xml") || croak "can't open '$path/$file.xml': $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 957 | 60 | ▶ | open(D, "> ${path}/_${file}.xml") || croak "can't open '$path/_$file.xml': $!"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 958 | |||||||||||||||||||||||||||||||||||
| 959 | 60 | print K $key_xml; | |||||||||||||||||||||||||||||||||
| 960 | 60 | print D $data_xml; | |||||||||||||||||||||||||||||||||
| 961 | |||||||||||||||||||||||||||||||||||
| 962 | 60 | close(K); | |||||||||||||||||||||||||||||||||
| 963 | 60 | close(D); | |||||||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||||||||
| 965 | 60 | return $nr_keys; | |||||||||||||||||||||||||||||||||
| 966 | } | ||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||
| 968 | 8 | 1; | |||||||||||||||||||||||||||||||||
| 969 | __END__ | ||||||||||||||||||||||||||||||||||