MultiNode.pm

filter: Statementstmt 100% 25 / 25 Branchbran n/a Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 89.4% 34 / 38 CC 2 SCAR 2.3
CC
2
Cov
93%
new
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.3
« MultiNode/Handle.pm ↑ Summary
1  
2 =head1 NAME
3  
4 =for markdown [![testsuite](https://github.com/cpan-authors/Tree-MultiNode/actions/workflows/testsuite.yml/badge.svg)](https://github.com/cpan-authors/Tree-MultiNode/actions/workflows/testsuite.yml)
5  
6 Tree::MultiNode -- a multi-node tree object. Most useful for
7 modeling hierarchical data structures.
8  
9 =head1 SYNOPSIS
10  
11 use Tree::MultiNode;
12 use strict;
13 use warnings;
14 my $tree = Tree::MultiNode->new;
15 my $handle = Tree::MultiNode::Handle->new($tree);
16  
17 $handle->set_key("top");
18 $handle->set_value("level");
19  
20 $handle->add_child("child","1");
21 $handle->add_child("child","2");
22  
23 $handle->first();
24 $handle->down();
25  
26 $handle->add_child("grandchild","1-1");
27 $handle->up();
28  
29 $handle->last();
30 $handle->down();
31  
32 $handle->add_child("grandchild","2-1");
33 $handle->up();
34  
35 $handle->top();
36 &dump_tree($handle);
37  
38 my $depth = 0;
39 sub dump_tree
40 {
41 ++$depth;
42 my $handle = shift;
43 my $lead = ' ' x ($depth*2);
44 my($key,$val);
45  
46 ($key,$val) = $handle->get_data();
47  
48 print $lead, "key: $key\n";
49 print $lead, "val: $val\n";
50 print $lead, "depth: $depth\n";
51  
52 my $i;
53 for( $i = 0; $i < scalar($handle->children); ++$i ) {
54 $handle->down($i);
55 &dump_tree($handle);
56 $handle->up();
57 }
58 --$depth;
59 }
60  
61 =head1 DESCRIPTION
62  
63 Tree::MultiNode, Tree::MultiNode::Node, and MultiNode::Handle are objects
64 modeled after C++ classes that I had written to help me model hierarchical
65 information as data structures (such as the relationships between records in
66 an RDBMS). The tree is basically a list of lists type data structure, where
67 each node has a key, a value, and a list of children. The tree has no
68 internal sorting, though all operations preserve the order of the child
69 nodes.
70  
71 =head2 Creating a Tree
72  
73 The concept of creating a handle based on a tree lets you have multiple handles
74 into a single tree without having to copy the tree. You have to use a handle
75 for all operations on the tree (other than construction).
76  
77 When you first construct a tree, it will have a single empty node. When you
78 construct a handle into that tree, it will set the top node in the tree as
79 it's current node.
80  
81 my $tree = Tree::MultiNode->new;
82 my $handle = Tree::MultiNode::Handle->new($tree);
83  
84 =head2 Using a Handle to Manipulate the Tree
85  
86 At this point, you can set the key/value in the top node, or start adding
87 child nodes.
88  
89 $handle->set_key("blah");
90 $handle->set_value("foo");
91  
92 $handle->add_child("quz","baz");
93 # or
94 $handle->add_child();
95  
96 add_child can take 3 parameters -- a key, a value, and a position. The key
97 and value will set the key/value of the child on construction. If pos is
98 passed, the new child will be inserted into the list of children.
99  
100 To move the handle so it points at a child (so you can start manipulating that
101 child), there are a series of methods to call:
102  
103 $handle->first(); # sets the current child to the first in the list
104 $handle->next(); # sets the next, or first if there was no next
105 $handle->prev(); # sets the previous, or last if there was no next
106 $handle->last(); # sets to the last child
107 $handle->down(); # positions the handle's current node to the
108 # current child
109  
110 To move back up, you can call the method up:
111  
112 $handle->up(); # moves to this node's parent
113  
114 up() will fail if the current node has no parent node. Most of the member
115 functions return either undef to indicate failure, or some other value to
116 indicate success.
117  
118 =head2 $Tree::MultiNode::debug
119  
120 If set to a true value, it enables debugging output in the code. This will
121 likely be removed in future versions as the code becomes more stable.
122  
123 =head1 API REFERENCE
124  
125 =cut
126  
127 ################################################################################
128  
129 =head2 Tree::MultiNode
130  
131 The tree object.
132  
133 =cut
134  
135 package Tree::MultiNode;
136 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
137 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
138 17 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
139  
140 17 our $VERSION = '2.02';
141 17 our $debug;
142  
143 17 ▶use Tree::MultiNode::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
144 17 ▶use Tree::MultiNode::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 =head2 Tree::MultiNode::new
147  
148 @param package name or tree object [scalar]
149 @returns new tree object
150  
151 Creates a new Tree. The tree will have a single top level node when created.
152 The first node will have no value (undef) in either it's key or it's value.
153  
154 my $tree = Tree::MultiNode->new;
155  
156 =cut
157  
158 88 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
159 88 my $this = shift;
160 88 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
0880
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
161 88 my $self = {};
162 88 bless $self, $class;
163  
164 88 $self->{'top'} = Tree::MultiNode::Node->new();
165 88 return $self;
166 }
167  
168 #
169 # With weak parent references, there are no circular references to break.
170 # Perl's reference counting handles cleanup naturally.
171 #
172 89 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
173 89 my $self = shift;
174 89 delete $self->{'top'};
175 }
176  
177 =head1 SEE ALSO
178  
179 Algorithms in C++
180 Robert Sedgwick
181 Addison Wesley 1992
182 ISBN 0201510596
183  
184 The Art of Computer Programming, Volume 1: Fundamental Algorithms,
185 third edition, Donald E. Knuth
186  
187 =head1 AUTHORS
188  
189 Kyle R. Burton <mortis@voicenet.com> (initial version, and maintenence)
190  
191 Daniel X. Pape <dpape@canis.uiuc.edu> (see Changes file from the source archive)
192  
193 Eric Joanis <joanis@cs.toronto.edu>
194  
195 Todd Rinaldo <toddr@cpan.org>
196  
197 =head1 BUGS
198  
199 Please report bugs via the issue tracker at
200 L<https://github.com/cpan-authors/Tree-MultiNode/issues>.
201  
202 =head1 LICENSE
203  
204 This module is free software; you can redistribute it and/or modify it
205 under the same terms as Perl itself. See L<perlartistic> and L<perlgpl>.
206  
207 =cut
208  
209 17 1;
« MultiNode/Handle.pm ↑ Summary