/[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.8 - (show annotations)
Thu Oct 7 14:06:46 2004 UTC (19 years, 9 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +6 -3 lines
 minor changes, comment-out debug dumper

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

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