File Coverage

blib/lib/Algorithm/SetSimilarity/Join/Datum.pm
Criterion Covered Total %
statement 136 136 100.0
branch 58 70 82.8
condition 23 33 69.7
subroutine 16 16 100.0
pod 0 12 0.0
total 233 267 87.2


line stmt bran cond sub pod time code
1             package Algorithm::SetSimilarity::Join::Datum;
2              
3 3     3   25878 use 5.008005;
  3         11  
  3         114  
4 3     3   15 use strict;
  3         4  
  3         89  
5 3     3   15 use warnings;
  3         13  
  3         118  
6              
7             our $VERSION = "0.0.0_03";
8              
9 3     3   15 use Scalar::Util;
  3         15  
  3         5134  
10              
11             sub new {
12 36     36 0 61938 my ($class, $param) = @_;
13 36         68 my @array = ();
14 36         122 my %hash = (
15             "datum" => \@array,
16             );
17 36 100       124 $hash{"data_type"} = $param->{"data_type"} if (exists $param->{"data_type"});
18 36         176 bless \%hash, $class;
19             }
20              
21             sub get_num {
22 62     62 0 1129 my ($self) = @_;
23 62         74 my $num = 0;
24 62 50       155 if (exists $self->{datum}) {
25 62 100 66     404 if ((defined $self->{datum}->[0]) && (ref $self->{datum}->[0] eq "ARRAY")) {
26 60 100       155 if (ref $self->{datum}->[0]->[0] eq "ARRAY") {
27 30 50       70 $num = ($#{$self->{datum}} + 1) if ($self->check_pushability($self->{datum}->[0]->[0]));
  30         61  
28             } else {
29 30 50       71 $num = ($#{$self->{datum}} + 1) if ($self->check_pushability($self->{datum}->[0]));
  30         65  
30             }
31             }
32             }
33 62         194 return $num;
34             }
35              
36             sub check_pushability {
37 178     178 0 6846 my ($self, $set) = @_;
38 178         193 my $is_pushable = 0;
39 178 100 66     1303 $is_pushable = 1 if ((defined $set) && (ref $set eq "ARRAY") && (defined $set->[0]) && (ref $set->[0] eq ''));
      100        
      100        
40 178 100 66     984 $is_pushable = 1 if ((defined $set) && (ref $set eq "HASH") && ((scalar(keys %$set)) > 0));
      100        
41 178         467 return $is_pushable;
42             }
43              
44             sub update_multi {
45 2     2 0 1744 my ($self, $sets) = @_;
46 2         3 my $is_update = 0;
47 2 50 33     17 if ((defined $sets) && (ref $sets eq "HASH")) {
48 2         3 foreach my $key (keys %{$sets}) {
  2         6  
49 6 50       18 if ($key >= 0) {
50 6         14 $is_update += $self->update($key, $sets->{$key});
51             }
52             }
53             }
54 2         7 return $is_update;
55             }
56              
57             sub estimate_data_type {
58 41     41 0 6262 my ($self, $set) = @_;
59 41         79 my $is_estimate = -1;
60 41         72 my $type = "";
61 41         49 my $max_check_elements = 5;
62 41 100       128 if (ref $set eq "ARRAY") {
    50          
63 19   100     96 for (my $i = 0; ($i <= $#$set) && ($i < $max_check_elements); $i++) {
64 73         71 my $tmp_type = "";
65 73 100       200 if (Scalar::Util::looks_like_number($set->[$i])) {
66 11         13 $tmp_type = "number";
67             } else {
68 62         68 $tmp_type = "string";
69             }
70 73 100       289 if ($type eq "") {
    100          
71 19         23 $type = $tmp_type;
72 19         81 $is_estimate = 1;
73             } elsif ($type ne $tmp_type) {
74 4         5 $is_estimate = 0;
75 4         6 last;
76             }
77             }
78             } elsif (ref $set eq "HASH") {
79 22         26 my $i = 0;
80 22         59 foreach my $key (keys %$set) {
81 79         85 $i++;
82 79         701 my $tmp_type = "";
83 79 100       193 if (Scalar::Util::looks_like_number($key)) {
84 21         25 $tmp_type = "number";
85             } else {
86 58         72 $tmp_type = "string";
87             }
88 79 100       181 if ($type eq "") {
    100          
89 22         25 $type = $tmp_type;
90 22         24 $is_estimate = 1;
91             } elsif ($type ne $tmp_type) {
92 4         5 $is_estimate = 0;
93 4         9 last;
94             }
95 75 100       194 last if ($i >= $max_check_elements);
96             }
97             }
98 41 100       128 $self->{data_type} = $type if ($is_estimate);
99 41         74 return $is_estimate;
100             }
101              
102             sub sort_set {
103 54     54 0 61 my ($self, $set) = @_;
104 54         89 my @array = ();
105 54 50 33     208 if ((defined $set) && (ref $set eq "ARRAY")) {
106 54         65 @array = @{$set};
  54         131  
107 54 100       126 if ($#$set > 0) {
108 53 100       136 $self->estimate_data_type($set) unless (exists $self->{data_type});
109 53 100       119 if ($self->{data_type} eq "number") {
110 6         6 @array = sort { $a <=> $b } @{$set};
  114         673  
  6         21  
111             }
112             else {
113 47         50 @array = sort { $a cmp $b } @{$set};
  427         594  
  47         131  
114             }
115             }
116             }
117 54         131 return \@array;
118             }
119              
120             sub update {
121 12     12 0 8913 my ($self, $id, $set) = @_;
122 12         24 my $is_update = 0;
123 12 100 66     29 if (($self->check_pushability($set)) && ($id < ($self->get_num()))) {
124 10 100       28 if (ref $set eq "ARRAY") {
    50          
125 5         13 $set = $self->sort_set($set);
126             } elsif (ref $set eq "HASH") {
127 5         9 $set = $self->make_pair_from_hash($set);
128             }
129 10         17 $self->{datum}->[$id] = $set;
130 10         19 $is_update = 1;
131             }
132 12         30 return $is_update;
133             }
134              
135             sub push_multi {
136 24     24 0 6513 my ($self, $sets) = @_;
137 24         34 my $is_push = 0;
138 24 50 33     241 if ((defined $sets) && (ref $sets eq "ARRAY")) {
139 24         32 foreach my $set (@{$sets}) {
  24         56  
140 88         181 $is_push += $self->push($set);
141             }
142             }
143 24         63 return $is_push;
144             }
145              
146             sub make_pair_from_hash {
147 55     55 0 79 my ($self, $set) = @_;
148 55         66 my @pair = ();
149 55         139 foreach my $key (keys %$set) {
150 335         869 my $entry = [$key, $set->{$key}];
151 335         576 push @pair, $entry;
152             }
153 55 100       196 $self->estimate_data_type($set) unless (exists $self->{data_type});
154 55 100       114 if ($self->{data_type} eq "number") {
155 6         18 @pair = sort { $a->[0] <=> $b->[0] } @pair;
  137         178  
156             } else {
157 49         117 @pair = sort { $a->[0] cmp $b->[0] } @pair;
  483         684  
158             }
159 55         114 return \@pair;
160             }
161              
162             sub push {
163 101     101 0 14878 my ($self, $set) = @_;
164 101         105 my $is_push = 0;
165 101 100       197 if ($self->check_pushability($set)) {
166 98 100       266 if (ref $set eq "ARRAY") {
    50          
167 49         101 $set = $self->sort_set($set);
168             } elsif (ref $set eq "HASH") {
169 49         103 $set = $self->make_pair_from_hash($set);
170             }
171 98         135 push @{$self->{datum}}, $set;
  98         179  
172 98         120 $is_push = 1;
173             }
174 101         215 return $is_push;
175             }
176              
177             sub get {
178 150     150 0 3069 my ($self, $id) = @_;
179 150         215 my $set = [];
180 150 50       450 $set = $self->{datum}->[$id] if (defined $self->{datum}->[$id]);
181 150         1060 return $set;
182             }
183              
184             sub sort {
185 24     24 0 911 my ($self) = @_;
186 24         35 my $is_sort = 0;
187 24 50       61 if ($self->get_num() > 1) {
188 24         37 my @array = sort { $#$b <=> $#$a } @{$self->{datum}};
  66         146  
  24         78  
189 24         50 $self->{datum} = \@array;
190 24         48 $is_sort = 1;
191             }
192 24         61 return $is_sort;
193             }
194              
195             1;
196              
197             __END__
198              
199             =encoding utf-8
200              
201             =head1 NAME
202              
203             Algorithm::SetSimilarity::Join::Datum - It's new $module
204              
205             =head1 SYNOPSIS
206              
207             use Algorithm::SetSimilarity::Join::Datum;
208              
209             =head1 DESCRIPTION
210              
211             Algorithm::SetSimilarity::Join::Datum is ...
212              
213             =head1 LICENSE
214              
215             Copyright (C) Toshinori Sato (@overlast).
216              
217             This library is free software; you can redistribute it and/or modify
218             it under the same terms as Perl itself.
219              
220             =head1 AUTHOR
221              
222             Toshinori Sato (@overlast) Eoverlasting@gmail.comE
223              
224             =cut