File Coverage

lib/Parse/Gnaw/LinkedListDimensions2.pm
Criterion Covered Total %
statement 62 72 86.1
branch 7 12 58.3
condition n/a
subroutine 12 13 92.3
pod 4 4 100.0
total 85 101 84.1


line stmt bran cond sub pod time code
1              
2              
3             package Parse::Gnaw::LinkedListDimensions2;
4              
5             our $VERSION = '0.001';
6              
7 3     3   63724 use warnings;
  3         6  
  3         77  
8 3     3   14 use strict;
  3         4  
  3         67  
9 3     3   14 use Data::Dumper;
  3         4  
  3         141  
10 3     3   14 use Carp;
  3         5  
  3         140  
11              
12 3     3   1103 use Parse::Gnaw::Blocks::LetterConstants;
  3         6  
  3         252  
13              
14 3     3   1404 use Parse::Gnaw::LinkedList;
  3         10  
  3         101  
15 3     3   25 use base "Parse::Gnaw::LinkedList";
  3         6  
  3         275  
16 3     3   19 use Parse::Gnaw::LinkedListConstants;
  3         7  
  3         320  
17              
18              
19 3     3   1574 use Parse::Gnaw::Blocks::LetterDimensions2;
  3         6  
  3         1651  
20              
21             =head1 NAME
22              
23             Parse::Gnaw::LinkedListLetterDimensions2 - Create a Parsable linked list of Parse::Gnaw::Letter objects,
24             with 4 axis of dimension. This would be equivalent to a 2-dimensional block of text, equivalent to somthing like:
25              
26             a-b-c
27             |X|X|
28             d-e-f
29             |X|X|
30             g-h-i
31              
32              
33              
34             =head1 SUBROUTINES/METHODS
35              
36             =cut
37              
38             =head2 constructor_defaults
39              
40             return a hash containing the default values for constructor arguments.
41             this gets overloaded by derived classes so base constructor always does the right thing.
42              
43             =cut
44             sub constructor_defaults{
45             # derived classes always override the defaults for constructor
46 6     6 1 34 my %defaults=(
47             # you don't have to pass in a string to convert into a linked list.
48             # can create bare linked list now, and then append string later.
49             string=>'',
50              
51             # 0 => horizontally
52             # 1 => vertically
53             # 2 => diagonal upper left to lower right
54             # 3 => diagonal upper right ot lower left
55             max_connections=>4,
56              
57             # linked list of something. this says of what.
58             # can change this to make linked list of some other, new class.
59             letterpkg=>'Parse::Gnaw::Blocks::LetterDimensions2',
60             );
61              
62 6         37 return (%defaults);
63             }
64              
65              
66             =head2 append
67             this gets overloaded by derived classes so base constructor always does the right thing.
68             =cut
69             sub append{
70 6     6 1 11 my $obj=shift(@_);
71 6         76 $obj->append_block_d2(@_);
72             }
73              
74              
75              
76             =head2 append_block_d2
77             This method appends a two-dimensional block of text
78             =cut
79             sub append_block_d2{
80 6     6 1 13 my($llist, $lettertoappendto, $blocktoappend, $location)=@_;
81              
82 6 50       19 if(not(defined($location))){
83 6         38 $location = $llist->get_location_of_caller($location);
84             }
85              
86 6         26 my @strings=split("\n", $blocktoappend);
87 6         11 my $linenum=0;
88 6         8 my @two_most_recent_lines_created;
89 6         12 foreach my $string (@strings){
90 23         27 $linenum++;
91 23         48 my $stringlocation = "$location, textline $linenum";
92              
93 23         94 my $newletter = $llist->append_string($lettertoappendto, $string, $stringlocation);
94              
95 23         35 my $packletter = $lettertoappendto->[LETTER__NEXT_START];
96              
97             # follow the letter to the end of the line
98 23         24 my @letters_in_this_line;
99 23         32 push(@two_most_recent_lines_created, \@letters_in_this_line);
100 23 100       49 if(scalar(@two_most_recent_lines_created)>2){
101 11         18 shift(@two_most_recent_lines_created);
102             }
103              
104 23         52 while($packletter){
105 89         101 push(@letters_in_this_line, $packletter);
106 89         206 $packletter=$packletter->[LETTER__CONNECTIONS]->[0]->[LETTER__CONNECTION_NEXT];
107             }
108              
109              
110              
111              
112              
113 23 100       64 if(scalar(@two_most_recent_lines_created)==2){
114 17         22 my $aboveline =$two_most_recent_lines_created[0];
115 17         22 my $bottomline=$two_most_recent_lines_created[1];
116            
117 17         38 for(my $x=0; $x
118 66         77 my $aboveletter=$aboveline->[$x];
119 66         94 my $bottomletter=$bottomline->[$x];
120              
121 66         140 $aboveletter->link_two_letters_via_interconnection($bottomletter,2);
122            
123 66 100       152 if($x>0){
124 49         57 my $aboveleftletter=$aboveline->[$x-1];
125 49         65 my $bottomleftletter=$bottomline->[$x-1];
126              
127 49         100 $aboveleftletter->link_two_letters_via_interconnection($bottomletter,1);
128 49         101 $aboveletter->link_two_letters_via_interconnection($bottomleftletter,3);
129            
130              
131             }
132             }
133              
134             }
135              
136 23         77 $lettertoappendto = $newletter;
137             }
138              
139              
140              
141              
142              
143             }
144              
145             my $blank_obj=[];
146             #print "blank_obj is '$blank_obj'\n"; die;
147             my $blank_str=$blank_obj.'';
148             my $blank_len=length($blank_str);
149             my $BLANK = '.'x($blank_len-5);
150              
151              
152              
153             =head2 create_interconnections_for_newly_appended_character
154              
155             The variable names reflect the physical position of hte letters
156              
157             $upleft $above
158              
159             $left $newletter
160              
161              
162             The axis interconnect the letters as follows:
163              
164             0 => horizontally
165             1 => vertically
166             2 => diagonal upper left to lower right
167             3 => diagonal upper right ot lower left
168              
169             Assume that axis [0] is already connected.
170             We need to connect axis 1,2,3
171              
172             =cut
173              
174             sub create_interconnections_for_newly_appended_character{
175 0     0 1   my($llist,$newletter)=@_;
176              
177 0           my $above = $llist->[LIST__PREVIOUS_LINE_LETTER]->[0];
178              
179             # if we just added first line, then previous line will be undefined.
180 0 0         return unless($above);
181              
182 0           $llist->[LIST__PREVIOUS_LINE_LETTER]->[0]=$above->[LETTER__CONNECTIONS]->[0]->[LETTER__CONNECTION_NEXT];
183              
184             # letter adn letter above it are defined, connect them on vertical axis.
185 0           $above->link_two_letters_via_interconnection($newletter,1);
186              
187             # assume [0] is already connected, so we can do what we're about to do:
188 0           my $left = $newletter->[LETTER__CONNECTIONS]->[0]->[LETTER__CONNECTION_PREV];
189 0           my $upleft = $above->[LETTER__CONNECTIONS]->[0]->[LETTER__CONNECTION_PREV];
190              
191             # if we are adding first letter of line, then previous column will be undefined.
192             # if not first letter of line, then column to left will be defined, connect it.
193 0 0         if($upleft){
194              
195 0           $upleft->link_two_letters_via_interconnection($newletter,2);
196 0           $above->link_two_letters_via_interconnection($left,3);
197              
198             }
199             }
200              
201              
202            
203             1;
204              
205