File Coverage

blib/lib/Data/Sheet.pm
Criterion Covered Total %
statement 74 155 47.7
branch 18 60 30.0
condition 1 21 4.7
subroutine 9 14 64.2
pod 11 11 100.0
total 113 261 43.3


line stmt bran cond sub pod time code
1             package Data::Sheet;
2              
3 1     1   25120 use 5.012001;
  1         4  
  1         40  
4 1     1   6 use strict;
  1         3  
  1         37  
5 1     1   5 use warnings;
  1         14  
  1         2295  
6              
7             require Exporter;
8              
9             our @ISA = qw(Exporter);
10              
11             # Items to export into callers namespace by default. Note: do not export
12             # names by default without a very good reason. Use EXPORT_OK instead.
13             # Do not simply export all your public functions/methods/constants.
14              
15             # This allows declaration use Data::Table ':all';
16             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
17             # will save memory.
18             our %EXPORT_TAGS = ( 'all' => [ qw(
19            
20             ) ] );
21              
22             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
23              
24             our @EXPORT = qw(
25            
26             );
27              
28             our $VERSION = '0.01';
29              
30             # Preloaded methods go here.
31              
32             sub new {
33 1     1 1 11 my $class = shift;
34 1         2 my ($w, $h, $init) = @_;
35 1 50       12 my $self = {
    50          
    50          
36             width => defined $w ? $w : 1,
37             height => defined $h ? $h : 1,
38             initVar => defined $init ? $init : 0,
39             table1 => [],
40             table2 => [],
41             flag => 0,
42             };
43             # Initialize
44 1         6 for(my $i = 0; $i < ($self->{width} * $self->{height}); $i++) {
45 12         13 push(@{ $self->{table1} }, $self->{initVar});
  12         17  
46 12         12 push(@{ $self->{table2} }, $self->{initVar});
  12         35  
47             }
48 1         4 return bless($self, $class);
49             }
50              
51             sub resize {
52 0     0 1 0 my $self = shift;
53 0         0 my ($w, $h) = @_;
54 0         0 my ($now_x, $now_y);
55             # initialize data
56 0 0       0 $self->{flag} == 1 ?
57             $self->{table2} = [] : #initialize table2
58             $self->{table1} = [];
59 0         0 for(my $i = 0; $i < ($w * $h); $i++) {
60 0 0       0 if($self->{flag} == 1) { #if using table1
61 0         0 push(@{$self->{table2}}, $self->{initVar});
  0         0  
62             }
63             else {
64 0         0 push(@{$self->{table1}}, $self->{initVar});
  0         0  
65             }
66             }
67             # copying data
68 0         0 my $i = 0;
69 0         0 while(1) {
70 0         0 $now_x = $i % $self->{width};
71 0         0 $now_y = int($i / $self->{width});
72 0 0 0     0 if($now_x >= $self->{width} ||
73             $now_y >= $self->{height}) {
74 0         0 last;
75             }
76 0 0       0 if($self->{flag} == 1) {
77 0         0 $self->{table2}[$now_x + $now_y * $w]
78             = $self->{table1}[$i];
79             } else {
80 0         0 $self->{table1}[$now_x + $now_y * $w]
81             = $self->{table2}[$i];
82             }
83 0         0 $i++;
84             };
85             # change flag
86 0 0       0 if($self->{flag} == 1) {
    0          
87 0         0 $self->{flag} = 0;
88             }
89             elsif($self->{flag} == 0) {
90 0         0 $self->{flag} = 1;
91             }
92             # Resize
93 0         0 $self->{width} = $w;
94 0         0 $self->{height} = $h;
95             }
96              
97             sub setCell {
98 1     1 1 358 my $self = shift;
99 1         2 my ($x, $y, $var) = @_;
100 1 50       9 if($self->{width} * $self->{height} < $x * $y) {
101             # bad status
102 0         0 return undef;
103             }
104             else {
105 1 50       5 $self->{flag} == 1 ?
106             $self->{table1}[$y * $self->{width} + $x] = $var :
107             $self->{table2}[$y * $self->{width} + $x] = $var;
108             # good status
109 1         2 return 0;
110             }
111             }
112              
113             sub getCell {
114 1     1 1 6 my $self = shift;
115 1         2 my ($x, $y) = @_;
116 1 50       4 if($self->{width} * $self->{height} < $x * $y) {
117 0         0 return undef;
118             }
119             else {
120 1 50       8 $self->{flag} == 1 ?
121             return $self->{table1}[$y * $self->{width} + $x] :
122             return $self->{table2}[$y * $self->{width} + $x];
123             }
124             }
125              
126             sub printSheet {
127 0     0 1 0 my $self = shift;
128 0         0 for(my $j = 0; $j < $self->{height}; $j++) {
129 0         0 for(my $i = 0; $i < $self->{width}; $i++) {
130 0 0       0 $self->{flag} == 1 ?
131 0         0 print "[@{ $self->{table1} }[$j
132             * $self->{width} + $i]]" :
133 0         0 print "[@{ $self->{table2} }[$j
134             * $self->{width} + $i]]";
135             }
136 0         0 print "\n";
137             }
138             }
139              
140             sub turnRight {
141 1     1 1 2 my $self = shift;
142 1         2 my ($now_x, $now_y);
143 0         0 my ($new_x, $new_y);
144             # initialize data
145 1 50       5 $self->{flag} == 1 ?
146             $self->{table2} = [] :
147             $self->{table1} = [];
148 1         7 for(my $i = 0; $i < ($self->{width} * $self->{height}); $i++) {
149 12 50       19 if($self->{flag} == 1) { #if using table1
150 0         0 push(@{$self->{table2}}, $self->{initVar});
  0         0  
151             }
152             else {
153 12         13 push(@{$self->{table1}}, $self->{initVar});
  12         37  
154             }
155             }
156             # copy Data
157 1         4 my $i = 0;
158 1         5 while($i < ($self->{width} * $self->{height})) {
159             # Calc current position
160 12         15 $now_x = $i % $self->{width};
161 12         17 $now_y = int($i / $self->{width});
162             # Calc new position
163 12         15 $new_x = ($self->{height} - 1 - $now_y);
164 12         13 $new_y = $now_x;
165 12 50       21 if($self->{flag} == 1) {
166 0         0 $self->{table2}[$new_x + $new_y * $self->{height}]
167             = $self->{table1}[$i];
168             } else {
169 12         23 $self->{table1}[$new_x + $new_y * $self->{height}]
170             = $self->{table2}[$i];
171             }
172 12         26 $i++;
173             };
174 1         3 ($self->{width}, $self->{height}) =
175             ($self->{height}, $self->{width});
176             #change Flag
177 1 50       5 if($self->{flag} == 1) {
178 0         0 $self->{flag} = 0;
179             }
180             else {
181 1         3 $self->{flag} = 1;
182             }
183             }
184              
185             sub turnLeft {
186 1     1 1 540 my $self = shift;
187 1         2 my ($now_x, $now_y);
188 0         0 my ($new_x, $new_y);
189             # initialize data
190 1 50       5 $self->{flag} == 1 ?
191             $self->{table2} = [] :
192             $self->{table1} = [];
193 1         6 for(my $i = 0; $i < ($self->{width} * $self->{height}); $i++) {
194 12 50       22 if($self->{flag} == 1) { #if using table1
195 12         10 push(@{$self->{table2}}, $self->{initVar});
  12         56  
196             }
197             else {
198 0         0 push(@{$self->{table1}}, $self->{initVar});
  0         0  
199             }
200             }
201             # copy Data
202 1         2 my $i = 0;
203 1         6 while($i < ($self->{width} * $self->{height})) {
204             # Calc current position
205 12         16 $now_x = $i % $self->{width};
206 12         15 $now_y = int($i / $self->{width});
207             # Calc new position
208 12         13 $new_x = $now_y;
209 12         13 $new_y = ($self->{width} - 1 - $now_x);
210 12 50       21 if($self->{flag} == 1) {
211 12         25 $self->{table2}[$new_x + $new_y * $self->{height}]
212             = $self->{table1}[$i];
213             } else {
214 0         0 $self->{table1}[$new_x + $new_y * $self->{height}]
215             = $self->{table2}[$i];
216             }
217 12         24 $i++;
218             };
219 1         3 ($self->{width}, $self->{height}) =
220             ($self->{height}, $self->{width});
221             #change Flag
222 1 50       5 if($self->{flag} == 1) {
223 1         4 $self->{flag} = 0;
224             }
225             else {
226 0         0 $self->{flag} = 1;
227             }
228             }
229              
230             sub getRow {
231 0     0 1 0 my $self = shift;
232 0         0 my $row = shift;
233 0 0 0     0 if($row > ($self->{height} - 1) ||
234             $row < 0) {
235 0         0 return undef;
236             }
237 0         0 my @rowArray = ();
238 0         0 for(my $i = 0; $i < $self->{width}; $i++) {
239 0         0 $self->{flag} == 1 ?
240 0         0 push(@rowArray, @{$self->{table1}}[$row * $self->{width} + $i]) :
241 0 0       0 push(@rowArray, @{$self->{table2}}[$row * $self->{width} + $i]);
242             }
243 0         0 return @rowArray;
244             }
245              
246             sub getCol {
247 2     2 1 5 my $self = shift;
248 2         3 my $col = shift;
249 2 50 33     14 if($col > ($self->{width} - 1) ||
250             $col < 0) {
251 0         0 return undef;
252             }
253 2         3 my @colArray = ();
254 2         6 for(my $i = 0; $i < $self->{height}; $i++) {
255 3         10 $self->{flag} == 1 ?
256 4         12 push(@colArray, @{$self->{table1}}[$col + $self->{width} * $i]) :
257 7 100       16 push(@colArray, @{$self->{table2}}[$col + $self->{width} * $i]);
258             }
259 2         17 return @colArray;
260             }
261              
262             sub setRow {
263 0     0 1   my ($self,$row,$data) = @_;
264 0 0 0       if($row > ($self->{height} - 1) ||
265             $row < 0) {
266 0           return 1;
267             }
268 0   0       for(my $i = 0; $i < $self->{width} && $i < ($#{$data} + 1); $i++) {
  0            
269 0 0         if($self->{flag} == 1) {
270 0           @{$self->{table1}}[$row * $self->{width} + $i] = ${$data}[$i];
  0            
  0            
271             }
272             else {
273 0           @{$self->{table2}}[$row * $self->{width} + $i] = ${$data}[$i];
  0            
  0            
274             }
275             }
276 0           return 0;
277             }
278              
279             sub setCol {
280 0     0 1   my ($self, $col, $data) = @_;
281 0 0 0       if($col > ($self->{width} - 1) ||
282             $col < 0) {
283 0           return 1;
284             }
285 0   0       for(my $i = 0; $i < $self->{height} && $i < ($#{$data} + 1); $i++) {
  0            
286 0 0         if($self->{flag} == 1) {
287 0           @{$self->{table1}}[$col + $self->{width} * $i] = ${$data}[$i];
  0            
  0            
288             }
289             else {
290 0           @{$self->{table2}}[$col + $self->{width} * $i] = ${$data}[$i];
  0            
  0            
291             }
292             }
293 0           return 0;
294             }
295              
296             1;
297             __END__