File Coverage

blib/lib/Tree/MultiNode.pm
Criterion Covered Total %
statement 22 22 100.0
branch n/a
condition 1 3 33.3
subroutine 7 7 100.0
pod 1 1 100.0
total 31 33 93.9


line stmt bran cond sub pod time code
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     17   1382660 use strict;
  17         32  
  17         592  
137 17     17   64 use warnings;
  17         26  
  17         699  
138 17     17   295 use 5.008;
  17         52  
139              
140             our $VERSION = '2.02';
141             our $debug;
142              
143 17     17   7148 use Tree::MultiNode::Node;
  17         41  
  17         543  
144 17     17   7824 use Tree::MultiNode::Handle;
  17         41  
  17         2103  
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             sub new {
159 88     88 1 1768924 my $this = shift;
160 88   33     392 my $class = ref($this) || $this;
161 88         140 my $self = {};
162 88         138 bless $self, $class;
163              
164 88         301 $self->{'top'} = Tree::MultiNode::Node->new();
165 88         178 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             sub DESTROY {
173 89     89   77059 my $self = shift;
174 89         724 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 (initial version, and maintenence)
190              
191             Daniel X. Pape (see Changes file from the source archive)
192              
193             Eric Joanis
194              
195             Todd Rinaldo
196              
197             =head1 BUGS
198              
199             Please report bugs via the issue tracker at
200             L.
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 and L.
206              
207             =cut
208              
209             1;