1 |
## -------------------------------------------------------------------------------- |
2 |
## $Id: Struct.pm,v 1.2 2002/12/05 07:56:34 joko Exp $ |
3 |
## -------------------------------------------------------------------------------- |
4 |
## $Log: Struct.pm,v $ |
5 |
## Revision 1.2 2002/12/05 07:56:34 joko |
6 |
## + updated/enhanced isEmpty (hacky?) |
7 |
## |
8 |
## Revision 1.1 2002/11/29 04:49:30 joko |
9 |
## + initial check-in |
10 |
## |
11 |
## -------------------------------------------------------------------------------- |
12 |
|
13 |
|
14 |
package Data::Compare::Struct; |
15 |
|
16 |
use strict; |
17 |
use warnings; |
18 |
|
19 |
require Exporter; |
20 |
our @ISA = qw( Exporter ); |
21 |
our @EXPORT_OK = qw( |
22 |
&getRelations |
23 |
&getDifference |
24 |
&isEmpty |
25 |
); |
26 |
|
27 |
use Data::Dumper; |
28 |
|
29 |
sub getRelations { |
30 |
my $a_ref = shift; |
31 |
my $b_ref = shift; |
32 |
my @a = @{$a_ref}; |
33 |
my @b = @{$b_ref}; |
34 |
|
35 |
my @isect = my @diff = my @union = (); |
36 |
my $e; |
37 |
my %count; |
38 |
|
39 |
foreach $e (@a, @b) { $count{$e}++ } |
40 |
|
41 |
foreach $e (keys %count) { |
42 |
push(@union, $e); |
43 |
push @{ $count{$e} == 2 ? \@isect : \@diff }, $e; |
44 |
} |
45 |
|
46 |
# bugfix: a diff is no diff if we don't even have an isect |
47 |
@diff = undef if !@isect; |
48 |
|
49 |
my $result = { |
50 |
union => \@union, |
51 |
isect => \@isect, |
52 |
diff => \@diff, |
53 |
}; |
54 |
|
55 |
return $result; |
56 |
|
57 |
} |
58 |
|
59 |
sub getDifference { |
60 |
my $res = getRelations(shift, shift); |
61 |
return $res->{diff}; |
62 |
} |
63 |
|
64 |
sub isEmpty { |
65 |
|
66 |
my $var = shift; |
67 |
my $ref = ref($var); |
68 |
my $reftype = ''; |
69 |
$reftype = attributes::reftype($var) if $ref; |
70 |
|
71 |
#print "isEmpty is parsing variable ( ref $ref reftype $reftype )", "\n"; |
72 |
|
73 |
#print Dumper($var); |
74 |
|
75 |
my $found; |
76 |
|
77 |
if ($ref eq 'Set::Object') { |
78 |
$found = 1; |
79 |
#print "members:", "\n"; |
80 |
#print Dumper($var->members()); |
81 |
#print "\n"; |
82 |
my $tc = { ok => 0, count => 0, empty => 0 }; |
83 |
foreach ($var->members()) { |
84 |
#print "member!!", "\n"; |
85 |
#print "empty: ", isEmpty($_), "\n"; |
86 |
#$tc->{ok} = 1; |
87 |
$tc->{count}++; |
88 |
$tc->{empty}++ if isEmpty($_); |
89 |
#return $result if $result; |
90 |
} |
91 |
|
92 |
#print Dumper($tc); |
93 |
|
94 |
return 1 if ($tc->{count} == $tc->{empty}); |
95 |
return 0; |
96 |
|
97 |
} |
98 |
|
99 |
if ($reftype eq 'ARRAY') { |
100 |
my @arr = @$var; |
101 |
my $isReallyEmpty = ($#arr == -1 ? 1 : 0); |
102 |
return 1 if $isReallyEmpty; |
103 |
$found = 1; |
104 |
foreach (@$var) { |
105 |
return 0 if defined $_; |
106 |
} |
107 |
|
108 |
} elsif ($reftype eq 'HASH') { |
109 |
my $empty = 1; |
110 |
$found = 1; |
111 |
foreach (keys %$var) { |
112 |
# TODO: review and fix this! |
113 |
=pod |
114 |
# recursion |
115 |
if (ref $var->{$_} eq 'Set::Object') { |
116 |
print "==========set", "\n"; |
117 |
if (isEmpty($var->{$_})) { |
118 |
return 0; |
119 |
#next; |
120 |
} |
121 |
} |
122 |
=cut |
123 |
next if ref $var->{$_} eq 'Set::Object'; |
124 |
#return 0 if defined $var->{$_}; |
125 |
#return 1 if isEmpty($var->{$_}); |
126 |
my $elem_empty = isEmpty($var->{$_}); |
127 |
|
128 |
#print "elem: $elem_empty\n"; |
129 |
$empty &&= $elem_empty; |
130 |
} |
131 |
return $empty; |
132 |
#return 0; |
133 |
|
134 |
} else { |
135 |
$found = 1; |
136 |
return 0 if defined $var; |
137 |
} |
138 |
|
139 |
if (!$found) { |
140 |
print "isEmpty could not parse variable ( ref $ref reftype $reftype )", "\n"; |
141 |
} |
142 |
|
143 |
return 1; |
144 |
|
145 |
} |
146 |
|
147 |
1; |