/[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.5 - (show annotations)
Thu Mar 27 15:17:05 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.4: +5 -2 lines
namespace fixes for Data::Mungle::*

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

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