| 1 | #============================================================= -*-Perl-*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Template::Plugin::Table | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # DESCRIPTION | ||||||||||||||||||||||||||||
| 6 | # Plugin to order a linear data set into a virtual 2-dimensional table | ||||||||||||||||||||||||||||
| 7 | # from which row and column permutations can be fetched. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | # AUTHOR | ||||||||||||||||||||||||||||
| 10 | # Andy Wardley <abw@wardley.org> | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # COPYRIGHT | ||||||||||||||||||||||||||||
| 13 | # Copyright (C) 2000-2022 Andy Wardley. All Rights Reserved. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # This module is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||
| 16 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 17 | # | ||||||||||||||||||||||||||||
| 18 | #============================================================================ | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | package Template::Plugin::Table; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 5 | ▶ | use base 'Template::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 5 | our $VERSION = '3.106'; | |||||||||||||||||||||||||||
| 28 | 5 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 32 | # new($context, \@data, \%args) | ||||||||||||||||||||||||||||
| 33 | # | ||||||||||||||||||||||||||||
| 34 | # This constructor method initialises the object to iterate through | ||||||||||||||||||||||||||||
| 35 | # the data set passed by reference to a list as the first parameter. | ||||||||||||||||||||||||||||
| 36 | # It calculates the shape of the permutation table based on the ROWS | ||||||||||||||||||||||||||||
| 37 | # or COLS parameters specified in the $args hash reference. The | ||||||||||||||||||||||||||||
| 38 | # OVERLAP parameter may be provided to specify the number of common | ||||||||||||||||||||||||||||
| 39 | # items that should be shared between subsequent columns. | ||||||||||||||||||||||||||||
| 40 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 18 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=15 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||
| 43 | 18 | my ($class, $context, $data, $params) = @_; | |||||||||||||||||||||||||||
| 44 | 18 | my ($size, $rows, $cols, $coloff, $overlap, $error); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # if the data item is a reference to a Template::Iterator object, | ||||||||||||||||||||||||||||
| 47 | # or subclass thereof, we call its get_all() method to extract all | ||||||||||||||||||||||||||||
| 48 | # the data it contains | ||||||||||||||||||||||||||||
| 49 | 18 | ▶ | if (blessed($data) && $data->isa('Template::Iterator')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($data) and $data->isa("Template::Iterator")
MC/DC0%0/2
builtin::blessed($data) and $data->isa("Template::Iterator")
builtin::blessed($data)
$data->isa("Template::Iterator")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->isa("Template::Iterator")
| |||||||||||||||||||||||||||||
| 50 | 0 | ($data, $error) = $data->get_all(); | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | return $class->error( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | "iterator failed to provide data for table: ", | ||||||||||||||||||||||||||||
| 53 | $error | ||||||||||||||||||||||||||||
| 54 | ) if $error; | ||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 18 | ▶ | return $class->error('invalid table data, expecting a list') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | unless ref $data eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 18 | ▶ | $params ||= { }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$params ||= {}
MC/DC0%0/1
$params ||= {}
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||
| 61 | 18 | ▶ | return $class->error('invalid table parameters, expecting a hash') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | unless ref $params eq 'HASH'; | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # ensure keys are folded to upper case | ||||||||||||||||||||||||||||
| 65 | 18 | @$params{ map { uc } keys %$params } = values %$params; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 18 | $size = scalar @$data; | |||||||||||||||||||||||||||
| 68 | 18 | ▶ | $overlap = $params->{ OVERLAP } || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$params->{'OVERLAP'} || 0
MC/DC100%1/1
$params->{'OVERLAP'} || 0
$params->{'OVERLAP'}
Truth table100%2/2
A
A= $params->{'OVERLAP'}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # calculate number of columns based on a specified number of rows | ||||||||||||||||||||||||||||
| 71 | 18 | ▶ | if ($rows = $params->{ ROWS }) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 72 | 16 | ▶ | if ($size < $rows) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | $rows = $size; # pad? | |||||||||||||||||||||||||||
| 74 | 1 | $cols = 1; | |||||||||||||||||||||||||||
| 75 | 1 | $coloff = 0; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else { | ||||||||||||||||||||||||||||
| 78 | 15 | $coloff = $rows - $overlap; | |||||||||||||||||||||||||||
| 79 | 15 | ▶ | $cols = int ($size / $coloff) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | + ($size % $coloff > $overlap ? 1 : 0) | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | # calculate number of rows based on a specified number of columns | ||||||||||||||||||||||||||||
| 84 | elsif ($cols = $params->{ COLS }) { | ||||||||||||||||||||||||||||
| 85 | 2 | ▶ | if ($size < $cols) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 1 | $cols = $size; | |||||||||||||||||||||||||||
| 87 | 1 | $rows = 1; | |||||||||||||||||||||||||||
| 88 | 1 | $coloff = 1; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | else { | ||||||||||||||||||||||||||||
| 91 | 1 | ▶ | $coloff = int ($size / $cols) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | + ($size % $cols > $overlap ? 1 : 0); | ||||||||||||||||||||||||||||
| 93 | 1 | $rows = $coloff + $overlap; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | else { | ||||||||||||||||||||||||||||
| 97 | 0 | $rows = $size; | |||||||||||||||||||||||||||
| 98 | 0 | $cols = 1; | |||||||||||||||||||||||||||
| 99 | 0 | $coloff = 0; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | bless { | ||||||||||||||||||||||||||||
| 103 | _DATA => $data, | ||||||||||||||||||||||||||||
| 104 | _SIZE => $size, | ||||||||||||||||||||||||||||
| 105 | _NROWS => $rows, | ||||||||||||||||||||||||||||
| 106 | _NCOLS => $cols, | ||||||||||||||||||||||||||||
| 107 | _COLOFF => $coloff, | ||||||||||||||||||||||||||||
| 108 | _OVERLAP => $overlap, | ||||||||||||||||||||||||||||
| 109 | 18 | ▶ | _PAD => $params->{ PAD } // 1, | ||||||||||||||||||||||||||
|
Condition100%2/2
$params->{"PAD"} // 1
MC/DC100%1/1
$params->{"PAD"} // 1
$params->{"PAD"}
Truth table100%2/2
A
A= $params->{"PAD"}
| |||||||||||||||||||||||||||||
| 110 | }, $class; | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 115 | # row($n) | ||||||||||||||||||||||||||||
| 116 | # | ||||||||||||||||||||||||||||
| 117 | # Returns a reference to a list containing the items in the row whose | ||||||||||||||||||||||||||||
| 118 | # number is specified by parameter. If the row number is undefined, | ||||||||||||||||||||||||||||
| 119 | # it calls rows() to return a list of all rows. | ||||||||||||||||||||||||||||
| 120 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 11 | ▶ | sub row { | ||||||||||||||||||||||||||
|
Subroutine
row: called CC=7 SCAR=20.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 123 | 11 | my ($self, $row) = @_; | |||||||||||||||||||||||||||
| 124 | my ($data, $cols, $offset, $size, $pad) | ||||||||||||||||||||||||||||
| 125 | 11 | = @$self{ qw( _DATA _NCOLS _COLOFF _SIZE _PAD) }; | |||||||||||||||||||||||||||
| 126 | 11 | my @set; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # return all rows if row number not specified | ||||||||||||||||||||||||||||
| 129 | 11 | ▶ | return $self->rows() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | unless defined $row; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 11 | ▶ | return () if $row >= $self->{ _NROWS } || $row < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$row >= $self->{"_NROWS"} or $row < 0
MC/DC0%0/2
$row >= $self->{"_NROWS"} or $row < 0
$row >= $self->{"_NROWS"}
$row < 0
Truth table33%1/3
A or B
A= $row >= $self->{"_NROWS"}B= $row < 0
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 11 | my $index = $row; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 11 | ▶ | for (my $c = 0; $c < $cols; $c++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 37 | ▶ | push( | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 138 | @set, | ||||||||||||||||||||||||||||
| 139 | $index < $size | ||||||||||||||||||||||||||||
| 140 | ? $data->[$index] | ||||||||||||||||||||||||||||
| 141 | : ($pad ? undef : ()) | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | 37 | $index += $offset; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 11 | return \@set; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 150 | # col($n) | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | # Returns a reference to a list containing the items in the column whose | ||||||||||||||||||||||||||||
| 153 | # number is specified by parameter. If the column number is undefined, | ||||||||||||||||||||||||||||
| 154 | # it calls cols() to return a list of all columns. | ||||||||||||||||||||||||||||
| 155 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 34 | ▶ | sub col { | ||||||||||||||||||||||||||
|
Subroutine
col: called CC=8 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 158 | 34 | my ($self, $col) = @_; | |||||||||||||||||||||||||||
| 159 | 34 | my ($data, $size) = @$self{ qw( _DATA _SIZE ) }; | |||||||||||||||||||||||||||
| 160 | 34 | my ($start, $end); | |||||||||||||||||||||||||||
| 161 | 34 | my $blanks = 0; | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | # return all cols if row number not specified | ||||||||||||||||||||||||||||
| 164 | 34 | ▶ | return $self->cols() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | unless defined $col; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 32 | ▶ | return () if $col >= $self->{ _NCOLS } || $col < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$col >= $self->{"_NCOLS"} or $col < 0
MC/DC0%0/2
$col >= $self->{"_NCOLS"} or $col < 0
$col >= $self->{"_NCOLS"}
$col < 0
Truth table33%1/3
A or B
A= $col >= $self->{"_NCOLS"}B= $col < 0
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 32 | $start = $self->{ _COLOFF } * $col; | |||||||||||||||||||||||||||
| 170 | 32 | $end = $start + $self->{ _NROWS } - 1; | |||||||||||||||||||||||||||
| 171 | 32 | ▶ | $end = $start if $end < $start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 32 | ▶ | if ($end >= $size) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 6 | $blanks = ($end - $size) + 1; | |||||||||||||||||||||||||||
| 174 | 6 | $end = $size - 1; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 32 | ▶ | return () if $start >= $size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | return [ | ||||||||||||||||||||||||||||
| 178 | @$data[$start..$end], | ||||||||||||||||||||||||||||
| 179 | $self->{ _PAD } | ||||||||||||||||||||||||||||
| 180 | 31 | ▶ | ? ((undef) x $blanks) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | : () | ||||||||||||||||||||||||||||
| 182 | ]; | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 187 | # rows() | ||||||||||||||||||||||||||||
| 188 | # | ||||||||||||||||||||||||||||
| 189 | # Returns all rows as a reference to a list of rows. | ||||||||||||||||||||||||||||
| 190 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 1 | ▶ | sub rows { | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 194 | 1 | return [ map { $self->row($_) } (0..$self->{ _NROWS }-1) ]; | |||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 199 | # cols() | ||||||||||||||||||||||||||||
| 200 | # | ||||||||||||||||||||||||||||
| 201 | # Returns all rows as a reference to a list of rows. | ||||||||||||||||||||||||||||
| 202 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 6 | ▶ | sub cols { | ||||||||||||||||||||||||||
|
Subroutine
cols: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 205 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 206 | 6 | return [ map { $self->col($_) } (0..$self->{ _NCOLS }-1) ]; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 211 | # AUTOLOAD | ||||||||||||||||||||||||||||
| 212 | # | ||||||||||||||||||||||||||||
| 213 | # Provides read access to various internal data members. | ||||||||||||||||||||||||||||
| 214 | #------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 217 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 218 | 0 | my $item = $AUTOLOAD; | |||||||||||||||||||||||||||
| 219 | 0 | $item =~ s/.*:://; | |||||||||||||||||||||||||||
| 220 | 0 | ▶ | return if $item eq 'DESTROY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if ($item =~ /^(?:data|size|nrows|ncols|overlap|pad)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 0 | return $self->{ $item }; | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | else { | ||||||||||||||||||||||||||||
| 226 | 0 | return (undef, "no such table method: $item"); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 5 | 1; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||