/[cvs]/nfo/perl/libs/Data/Mungle/Compare/Struct.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Mungle/Compare/Struct.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Wed Jul 7 03:13:43 2004 UTC (20 years ago) by joko
Branch: MAIN
Changes since 1.6: +8 -3 lines
minor fix for "sub isEmpty"

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed