File Coverage

blib/lib/DataStructure/LinkedList/Node.pm
Criterion Covered Total %
statement 46 54 85.1
branch n/a
condition n/a
subroutine 11 12 91.6
pod 3 4 75.0
total 60 70 85.7


line stmt bran cond sub pod time code
1             # A node of a linked list.
2              
3             package DataStructure::LinkedList::Node;
4              
5 1     1   7 use strict;
  1         2  
  1         28  
6 1     1   5 use warnings;
  1         2  
  1         22  
7 1     1   4 use utf8;
  1         2  
  1         4  
8 1     1   38 use feature ':5.24';
  1         2  
  1         89  
9 1     1   6 use feature 'signatures';
  1         2  
  1         45  
10 1     1   7 no warnings 'experimental::signatures';
  1         2  
  1         53  
11              
12 1     1   7 use Scalar::Util qw(weaken);
  1         2  
  1         387  
13              
14             =pod
15              
16             =head1 NAME
17              
18             DataStructure::LinkedList::Node
19              
20             =head1 SYNOPSIS
21              
22             A single node (element) in a L.
23              
24             =head1 DESCRIPTION
25              
26             =head2 CONSTRUCTOR
27              
28             You can’t build a node directly. Instead you can call one of the accessors of a
29             L or some of the methods below.
30              
31             Note that a B does not hold a reference on its
32             parent list. So a node becomes invalid when the last reference to its list is
33             deleted as the list itself will be destroyed. But you should also not depend on
34             this behavior as it might be fixed in the future.
35              
36             =cut
37              
38             # The constructor is private and should be called only by this package and its
39             # parent.
40 4     4 0 5 sub new ($class, $list, $next, $value) {
  4         8  
  4         5  
  4         7  
  4         6  
  4         5  
41 4         15 my $self = bless {
42             list => $list,
43             next => $next,
44             value => $value,
45             }, $class;
46 4         17 weaken($self->{list});
47 4         9 return $self;
48             }
49              
50             =pod
51              
52             =head2 METHODS
53              
54             All the functions below are class methods that should be called on a
55             B object.
56              
57             =head3 I
58              
59             Returns the value held by this node.
60              
61             =cut
62              
63 8     8 1 1251 sub value ($self) {
  8         9  
  8         13  
64 8         34 return $self->{value};
65             }
66              
67             =pod
68              
69             =head3 I
70              
71             Returns the next B in this list or B
72             if the current object is the last node in its list.
73              
74             The current node can still be used after that call.
75              
76             =cut
77              
78 8     8 1 459 sub next ($self) {
  8         13  
  8         10  
79 8         23 return $self->{next};
80             }
81              
82             =pod
83              
84             =head3 I
85              
86             Inserts a new node in the list after the current one, with the given value and
87             returns that new node.
88              
89             The current node can still be used after that call.
90              
91             =cut
92              
93 0     0 1 0 sub insert_after ($self, $value) {
  0         0  
  0         0  
  0         0  
94 0         0 my $new_node = new(ref $self, $self->{list}, $self->{next}, $value);
95 0         0 $self->{next} = $new_node;
96 0         0 $self->{list}{size}++;
97 0         0 return $new_node;
98             }
99              
100             # Delete the node, assuming that it is the first node of the list.
101 2     2   3 sub _delete_first ($self) {
  2         3  
  2         4  
102 2         4 my $value = $self->{value};
103 2         4 $self->{list}{size}--;
104 2         3 undef %{$self};
  2         6  
105 2         9 return $value;
106             }
107              
108             1;