1 |
joko |
1.1 |
## -------------------------------------------------------------------------------- |
2 |
jonen |
1.6 |
## $Id: Struct.pm,v 1.5 2003/03/27 15:17:05 joko Exp $ |
3 |
joko |
1.1 |
## -------------------------------------------------------------------------------- |
4 |
joko |
1.2 |
## $Log: Struct.pm,v $ |
5 |
jonen |
1.6 |
## Revision 1.5 2003/03/27 15:17:05 joko |
6 |
|
|
## namespace fixes for Data::Mungle::* |
7 |
|
|
## |
8 |
joko |
1.5 |
## Revision 1.4 2002/12/19 01:06:27 joko |
9 |
|
|
## + fixed isEmpty with ARRAYs |
10 |
|
|
## |
11 |
joko |
1.4 |
## Revision 1.3 2002/12/13 21:47:04 joko |
12 |
|
|
## + fixes to 'sub isEmpty' |
13 |
|
|
## |
14 |
joko |
1.3 |
## Revision 1.2 2002/12/05 07:56:34 joko |
15 |
|
|
## + updated/enhanced isEmpty (hacky?) |
16 |
|
|
## |
17 |
joko |
1.2 |
## Revision 1.1 2002/11/29 04:49:30 joko |
18 |
|
|
## + initial check-in |
19 |
|
|
## |
20 |
joko |
1.1 |
## -------------------------------------------------------------------------------- |
21 |
|
|
|
22 |
|
|
|
23 |
joko |
1.5 |
package Data::Mungle::Compare::Struct; |
24 |
joko |
1.1 |
|
25 |
|
|
use strict; |
26 |
|
|
use warnings; |
27 |
|
|
|
28 |
|
|
require Exporter; |
29 |
|
|
our @ISA = qw( Exporter ); |
30 |
|
|
our @EXPORT_OK = qw( |
31 |
|
|
&getRelations |
32 |
|
|
&getDifference |
33 |
|
|
&isEmpty |
34 |
|
|
); |
35 |
|
|
|
36 |
|
|
use Data::Dumper; |
37 |
|
|
|
38 |
|
|
sub getRelations { |
39 |
|
|
my $a_ref = shift; |
40 |
|
|
my $b_ref = shift; |
41 |
|
|
my @a = @{$a_ref}; |
42 |
|
|
my @b = @{$b_ref}; |
43 |
|
|
|
44 |
jonen |
1.6 |
# BUGFIX 2004-05-12: following declaration of arrays occures an strange perl error: |
45 |
|
|
# 'Bizarre copy of ARRAY in aassign at ..<PATH_TO>/<THIS_FILE> line <NEXT LINE>' |
46 |
|
|
# (at PERL 5.8.4, Debian/unstable/experimental) |
47 |
|
|
# |
48 |
|
|
#my @isect = my @diff = my @union = (); |
49 |
|
|
# |
50 |
|
|
# so use normal declaration instead!! |
51 |
|
|
my @isect = (); |
52 |
|
|
my @diff = (); |
53 |
|
|
my @union = (); |
54 |
|
|
|
55 |
joko |
1.1 |
my $e; |
56 |
|
|
my %count; |
57 |
|
|
|
58 |
|
|
foreach $e (@a, @b) { $count{$e}++ } |
59 |
|
|
|
60 |
|
|
foreach $e (keys %count) { |
61 |
|
|
push(@union, $e); |
62 |
|
|
push @{ $count{$e} == 2 ? \@isect : \@diff }, $e; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
# bugfix: a diff is no diff if we don't even have an isect |
66 |
|
|
@diff = undef if !@isect; |
67 |
|
|
|
68 |
|
|
my $result = { |
69 |
|
|
union => \@union, |
70 |
|
|
isect => \@isect, |
71 |
|
|
diff => \@diff, |
72 |
|
|
}; |
73 |
|
|
|
74 |
|
|
return $result; |
75 |
|
|
|
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
sub getDifference { |
79 |
|
|
my $res = getRelations(shift, shift); |
80 |
|
|
return $res->{diff}; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
sub isEmpty { |
84 |
joko |
1.2 |
|
85 |
joko |
1.1 |
my $var = shift; |
86 |
joko |
1.2 |
my $ref = ref($var); |
87 |
|
|
my $reftype = ''; |
88 |
|
|
$reftype = attributes::reftype($var) if $ref; |
89 |
|
|
|
90 |
|
|
#print "isEmpty is parsing variable ( ref $ref reftype $reftype )", "\n"; |
91 |
|
|
|
92 |
joko |
1.3 |
#print Dumper($var); |
93 |
|
|
|
94 |
joko |
1.2 |
my $found; |
95 |
|
|
|
96 |
|
|
if ($ref eq 'Set::Object') { |
97 |
|
|
$found = 1; |
98 |
joko |
1.3 |
#print "members:", "\n"; |
99 |
|
|
#print Dumper($var->members()); |
100 |
|
|
#print "\n"; |
101 |
joko |
1.2 |
my $tc = { ok => 0, count => 0, empty => 0 }; |
102 |
|
|
foreach ($var->members()) { |
103 |
joko |
1.3 |
#print "member!!", "\n"; |
104 |
joko |
1.2 |
#print "empty: ", isEmpty($_), "\n"; |
105 |
|
|
#$tc->{ok} = 1; |
106 |
|
|
$tc->{count}++; |
107 |
|
|
$tc->{empty}++ if isEmpty($_); |
108 |
|
|
#return $result if $result; |
109 |
|
|
} |
110 |
joko |
1.3 |
|
111 |
|
|
#print Dumper($tc); |
112 |
joko |
1.2 |
|
113 |
|
|
return 1 if ($tc->{count} == $tc->{empty}); |
114 |
|
|
return 0; |
115 |
|
|
|
116 |
|
|
} |
117 |
joko |
1.1 |
|
118 |
joko |
1.3 |
if ($reftype eq 'ARRAY') { |
119 |
|
|
my @arr = @$var; |
120 |
|
|
my $isReallyEmpty = ($#arr == -1 ? 1 : 0); |
121 |
|
|
return 1 if $isReallyEmpty; |
122 |
|
|
$found = 1; |
123 |
joko |
1.4 |
foreach (@arr) { |
124 |
|
|
#return 0 if defined $_; |
125 |
|
|
#return 1 if isEmpty($_); |
126 |
|
|
return 0 if !isEmpty($_); |
127 |
joko |
1.3 |
} |
128 |
|
|
|
129 |
|
|
} elsif ($reftype eq 'HASH') { |
130 |
|
|
my $empty = 1; |
131 |
joko |
1.2 |
$found = 1; |
132 |
joko |
1.1 |
foreach (keys %$var) { |
133 |
joko |
1.3 |
# TODO: review and fix this! |
134 |
joko |
1.2 |
=pod |
135 |
|
|
# recursion |
136 |
|
|
if (ref $var->{$_} eq 'Set::Object') { |
137 |
|
|
print "==========set", "\n"; |
138 |
|
|
if (isEmpty($var->{$_})) { |
139 |
|
|
return 0; |
140 |
|
|
#next; |
141 |
|
|
} |
142 |
|
|
} |
143 |
|
|
=cut |
144 |
|
|
next if ref $var->{$_} eq 'Set::Object'; |
145 |
|
|
#return 0 if defined $var->{$_}; |
146 |
joko |
1.3 |
#return 1 if isEmpty($var->{$_}); |
147 |
|
|
my $elem_empty = isEmpty($var->{$_}); |
148 |
|
|
|
149 |
|
|
#print "elem: $elem_empty\n"; |
150 |
|
|
$empty &&= $elem_empty; |
151 |
joko |
1.1 |
} |
152 |
joko |
1.3 |
return $empty; |
153 |
|
|
#return 0; |
154 |
joko |
1.2 |
|
155 |
|
|
} else { |
156 |
|
|
$found = 1; |
157 |
|
|
return 0 if defined $var; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
if (!$found) { |
161 |
|
|
print "isEmpty could not parse variable ( ref $ref reftype $reftype )", "\n"; |
162 |
joko |
1.1 |
} |
163 |
joko |
1.4 |
|
164 |
|
|
#print "EMPTY!!!", "\n"; |
165 |
joko |
1.1 |
|
166 |
|
|
return 1; |
167 |
joko |
1.2 |
|
168 |
joko |
1.1 |
} |
169 |
|
|
|
170 |
|
|
1; |