/[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.6 - (show annotations)
Wed May 12 13:53:19 2004 UTC (20 years, 1 month ago) by jonen
Branch: MAIN
Changes since 1.5: +15 -2 lines
+ bugfix related to declaration of arrays,
    solved error: 'Bizarre copy of ARRAY in aassign at...'

1 ## --------------------------------------------------------------------------------
2 ## $Id: Struct.pm,v 1.5 2003/03/27 15:17:05 joko Exp $
3 ## --------------------------------------------------------------------------------
4 ## $Log: Struct.pm,v $
5 ## Revision 1.5 2003/03/27 15:17:05 joko
6 ## namespace fixes for Data::Mungle::*
7 ##
8 ## Revision 1.4 2002/12/19 01:06:27 joko
9 ## + fixed isEmpty with ARRAYs
10 ##
11 ## Revision 1.3 2002/12/13 21:47:04 joko
12 ## + fixes to 'sub isEmpty'
13 ##
14 ## Revision 1.2 2002/12/05 07:56:34 joko
15 ## + updated/enhanced isEmpty (hacky?)
16 ##
17 ## Revision 1.1 2002/11/29 04:49:30 joko
18 ## + initial check-in
19 ##
20 ## --------------------------------------------------------------------------------
21
22
23 package Data::Mungle::Compare::Struct;
24
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 # 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 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
85 my $var = shift;
86 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 #print Dumper($var);
93
94 my $found;
95
96 if ($ref eq 'Set::Object') {
97 $found = 1;
98 #print "members:", "\n";
99 #print Dumper($var->members());
100 #print "\n";
101 my $tc = { ok => 0, count => 0, empty => 0 };
102 foreach ($var->members()) {
103 #print "member!!", "\n";
104 #print "empty: ", isEmpty($_), "\n";
105 #$tc->{ok} = 1;
106 $tc->{count}++;
107 $tc->{empty}++ if isEmpty($_);
108 #return $result if $result;
109 }
110
111 #print Dumper($tc);
112
113 return 1 if ($tc->{count} == $tc->{empty});
114 return 0;
115
116 }
117
118 if ($reftype eq 'ARRAY') {
119 my @arr = @$var;
120 my $isReallyEmpty = ($#arr == -1 ? 1 : 0);
121 return 1 if $isReallyEmpty;
122 $found = 1;
123 foreach (@arr) {
124 #return 0 if defined $_;
125 #return 1 if isEmpty($_);
126 return 0 if !isEmpty($_);
127 }
128
129 } elsif ($reftype eq 'HASH') {
130 my $empty = 1;
131 $found = 1;
132 foreach (keys %$var) {
133 # TODO: review and fix this!
134 =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 #return 1 if isEmpty($var->{$_});
147 my $elem_empty = isEmpty($var->{$_});
148
149 #print "elem: $elem_empty\n";
150 $empty &&= $elem_empty;
151 }
152 return $empty;
153 #return 0;
154
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 }
163
164 #print "EMPTY!!!", "\n";
165
166 return 1;
167
168 }
169
170 1;

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