| 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 | use strict; | ||
| 23 | use warnings; | ||
| 24 | use base 'Template::Plugin'; | ||
| 25 | use Scalar::Util 'blessed'; | ||
| 26 | |||
| 27 | our $VERSION = '3.100'; | ||
| 28 | 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 | sub new { | ||
| 43 | my ($class, $context, $data, $params) = @_; | ||
| 44 | 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 | if (blessed($data) && $data->isa('Template::Iterator')) { | ||
| 50 | ($data, $error) = $data->get_all(); | ||
| 51 | return $class->error( | ||
| 52 | "iterator failed to provide data for table: ", | ||
| 53 | $error | ||
| 54 | ) if $error; | ||
| 55 | } | ||
| 56 | |||
| 57 | return $class->error('invalid table data, expecting a list') | ||
| 58 | unless ref $data eq 'ARRAY'; | ||
| 59 | |||
| 60 | $params ||= { }; | ||
| 61 | return $class->error('invalid table parameters, expecting a hash') | ||
| 62 | unless ref $params eq 'HASH'; | ||
| 63 | |||
| 64 | # ensure keys are folded to upper case | ||
| 65 | @$params{ map { uc } keys %$params } = values %$params; | ||
| 66 | |||
| 67 | $size = scalar @$data; | ||
| 68 | $overlap = $params->{ OVERLAP } || 0; | ||
| 69 | |||
| 70 | # calculate number of columns based on a specified number of rows | ||
| 71 | if ($rows = $params->{ ROWS }) { | ||
| 72 | if ($size < $rows) { | ||
| 73 | $rows = $size; # pad? | ||
| 74 | $cols = 1; | ||
| 75 | $coloff = 0; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | $coloff = $rows - $overlap; | ||
| 79 | $cols = int ($size / $coloff) | ||
| 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 | if ($size < $cols) { | ||
| 86 | $cols = $size; | ||
| 87 | $rows = 1; | ||
| 88 | $coloff = 1; | ||
| 89 | } | ||
| 90 | else { | ||
| 91 | $coloff = int ($size / $cols) | ||
| 92 | + ($size % $cols > $overlap ? 1 : 0); | ||
| 93 | $rows = $coloff + $overlap; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | else { | ||
| 97 | $rows = $size; | ||
| 98 | $cols = 1; | ||
| 99 | $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 | _PAD => defined $params->{ PAD } ? $params->{ PAD } : 1, | ||
| 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 | sub row { | ||
| 123 | my ($self, $row) = @_; | ||
| 124 | my ($data, $cols, $offset, $size, $pad) | ||
| 125 | = @$self{ qw( _DATA _NCOLS _COLOFF _SIZE _PAD) }; | ||
| 126 | my @set; | ||
| 127 | |||
| 128 | # return all rows if row number not specified | ||
| 129 | return $self->rows() | ||
| 130 | unless defined $row; | ||
| 131 | |||
| 132 | return () if $row >= $self->{ _NROWS } || $row < 0; | ||
| 133 | |||
| 134 | my $index = $row; | ||
| 135 | |||
| 136 | for (my $c = 0; $c < $cols; $c++) { | ||
| 137 | push( | ||
| 138 | @set, | ||
| 139 | $index < $size | ||
| 140 | ? $data->[$index] | ||
| 141 | : ($pad ? undef : ()) | ||
| 142 | ); | ||
| 143 | $index += $offset; | ||
| 144 | } | ||
| 145 | 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 | sub col { | ||
| 158 | my ($self, $col) = @_; | ||
| 159 | my ($data, $size) = @$self{ qw( _DATA _SIZE ) }; | ||
| 160 | my ($start, $end); | ||
| 161 | my $blanks = 0; | ||
| 162 | |||
| 163 | # return all cols if row number not specified | ||
| 164 | return $self->cols() | ||
| 165 | unless defined $col; | ||
| 166 | |||
| 167 | return () if $col >= $self->{ _NCOLS } || $col < 0; | ||
| 168 | |||
| 169 | $start = $self->{ _COLOFF } * $col; | ||
| 170 | $end = $start + $self->{ _NROWS } - 1; | ||
| 171 | $end = $start if $end < $start; | ||
| 172 | if ($end >= $size) { | ||
| 173 | $blanks = ($end - $size) + 1; | ||
| 174 | $end = $size - 1; | ||
| 175 | } | ||
| 176 | return () if $start >= $size; | ||
| 177 | return [ | ||
| 178 | @$data[$start..$end], | ||
| 179 | $self->{ _PAD } | ||
| 180 | ? ((undef) x $blanks) | ||
| 181 | : () | ||
| 182 | ]; | ||
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | #------------------------------------------------------------------------ | ||
| 187 | # rows() | ||
| 188 | # | ||
| 189 | # Returns all rows as a reference to a list of rows. | ||
| 190 | #------------------------------------------------------------------------ | ||
| 191 | |||
| 192 | sub rows { | ||
| 193 | my $self = shift; | ||
| 194 | 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 | sub cols { | ||
| 205 | my $self = shift; | ||
| 206 | 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 | sub AUTOLOAD { | ||
| 217 | my $self = shift; | ||
| 218 | my $item = $AUTOLOAD; | ||
| 219 | $item =~ s/.*:://; | ||
| 220 | return if $item eq 'DESTROY'; | ||
| 221 | |||
| 222 | if ($item =~ /^(?:data|size|nrows|ncols|overlap|pad)$/) { | ||
| 223 | return $self->{ $item }; | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | return (undef, "no such table method: $item"); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | |||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | __END__ |