12 my ($root, $parent_fetcher, $cache) = @_;
15 my @STACK; # stack for simulating recursion
17 my $pfetcher_is_coderef = ref($parent_fetcher) eq 'CODE';
19 unless ($pfetcher_is_coderef or $root->can($parent_fetcher)) {
20 confess "Could not find method $parent_fetcher in $root";
23 my $current_root = $root;
24 my $current_parents = [ $root->$parent_fetcher ];
25 my $recurse_mergeout = [];
30 if($i < @$current_parents) {
31 my $new_root = $current_parents->[$i++];
32 die "Infinite loop detected" if $seen{$new_root}++;
34 unless ($pfetcher_is_coderef or $new_root->can($parent_fetcher)) {
35 confess "Could not find method $parent_fetcher in $new_root";
45 $current_root = $new_root;
46 $current_parents = $cache->{pfetch}->{$current_root} ||= [ $current_root->$parent_fetcher ];
47 $recurse_mergeout = [];
52 my $mergeout = $cache->{merge}->{$current_root} ||= do {
54 # This do-block is the code formerly known as the function
55 # that was a perl-port of the python code at
56 # http://www.python.org/2.3/mro.html :)
58 # Initial set (make sure everything is copied - it will be modded)
59 my @seqs = map { [@$_] } (@$recurse_mergeout, $current_parents);
61 # Construct the tail-checking hash
63 foreach my $seq (@seqs) {
64 $tails{$_}++ for (@$seq[1..$#$seq]);
67 my @res = ( $current_root );
73 if(!$winner) { # looking for a winner
74 $cand = $_->[0]; # seq head is candidate
75 next if $tails{$cand}; # he loses if in %tails
76 push @res => $winner = $cand;
78 if($_->[0] eq $winner) {
79 shift @$_; # strip off our winner
80 $tails{$_->[0]}-- if @$_; # keep %tails sane
84 die q{Inconsistent hierarchy found while merging '}
85 . $current_root . qq{':\n\t}
86 . qq{current merge results [\n\t\t}
87 . (join ",\n\t\t" => @res)
88 . qq{\n\t]\n\t} . qq{merging failed on '$cand'\n}
94 return @$mergeout if !@STACK;
96 $seen{$current_root}--;
98 ($current_root, $current_parents, $recurse_mergeout, $i)
101 push(@$recurse_mergeout, $mergeout);
113 Algorithm::C3 - A module for merging hierarchies using the C3 algorithm
119 # merging a classic diamond
120 # inheritence graph like this:
128 my @merged = Algorithm::C3::merge(
131 # extract the ISA array
138 print join ", " => @merged; # prints D, B, C, A
142 This module implements the C3 algorithm. I have broken this out
143 into it's own module because I found myself copying and pasting
144 it way too often for various needs. Most of the uses I have for
145 C3 revolve around class building and metamodels, but it could
146 also be used for things like dependency resolution as well since
147 it tends to do such a nice job of preserving local precendence
150 Below is a brief explanation of C3 taken from the L<Class::C3>
151 module. For more detailed information, see the L<SEE ALSO> section
156 C3 is the name of an algorithm which aims to provide a sane method
157 resolution order under multiple inheritence. It was first introduced
158 in the langauge Dylan (see links in the L<SEE ALSO> section), and
159 then later adopted as the prefered MRO (Method Resolution Order)
160 for the new-style classes in Python 2.3. Most recently it has been
161 adopted as the 'canonical' MRO for Perl 6 classes, and the default
162 MRO for Parrot objects as well.
164 =head2 How does C3 work.
166 C3 works by always preserving local precendence ordering. This
167 essentially means that no class will appear before any of it's
168 subclasses. Take the classic diamond inheritence pattern for
177 The standard Perl 5 MRO would be (D, B, A, C). The result being that
178 B<A> appears before B<C>, even though B<C> is the subclass of B<A>.
179 The C3 MRO algorithm however, produces the following MRO (D, B, C, A),
180 which does not have this same issue.
182 This example is fairly trival, for more complex examples and a deeper
183 explaination, see the links in the L<SEE ALSO> section.
189 =item B<merge ($root, $func_to_fetch_parent, $cache)>
191 This takes a C<$root> node, which can be anything really it
192 is up to you. Then it takes a C<$func_to_fetch_parent> which
193 can be either a CODE reference (see L<SYNOPSIS> above for an
194 example), or a string containing a method name to be called
195 on all the items being linearized. An example of how this
211 our @ISA = ('B', 'C');
214 print join ", " => Algorithm::C3::merge('D', 'supers');
216 The purpose of C<$func_to_fetch_parent> is to provide a way
217 for C<merge> to extract the parents of C<$root>. This is
218 needed for C3 to be able to do it's work.
220 The C<$cache> parameter is an entirely optional performance
221 measure, and should not change behavior.
223 If supplied, it should be a hashref that merge can use as a
224 private cache between runs to speed things up. Generally
225 speaking, if you will be calling merge many times on related
226 things, and the parent fetching function will return constant
227 results given the same arguments during all of these calls,
228 you can and should reuse the same shared cache hash for all
229 of the calls. Example:
231 sub do_some_merging {
233 my @foo_mro = Algorithm::C3::Merge('Foo', \&get_supers, \%merge_cache);
234 my @bar_mro = Algorithm::C3::Merge('Bar', \&get_supers, \%merge_cache);
235 my @baz_mro = Algorithm::C3::Merge('Baz', \&get_supers, \%merge_cache);
236 my @quux_mro = Algorithm::C3::Merge('Quux', \&get_supers, \%merge_cache);
244 I use B<Devel::Cover> to test the code coverage of my tests, below
245 is the B<Devel::Cover> report on this module's test suite.
247 ------------------------ ------ ------ ------ ------ ------ ------ ------
248 File stmt bran cond sub pod time total
249 ------------------------ ------ ------ ------ ------ ------ ------ ------
250 Algorithm/C3.pm 100.0 100.0 100.0 100.0 100.0 100.0 100.0
251 ------------------------ ------ ------ ------ ------ ------ ------ ------
252 Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0
253 ------------------------ ------ ------ ------ ------ ------ ------ ------
257 =head2 The original Dylan paper
261 =item L<http://www.webcom.com/haahr/dylan/linearization-oopsla96.html>
265 =head2 The prototype Perl 6 Object Model uses C3
269 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/>
273 =head2 Parrot now uses C3
277 =item L<http://aspn.activestate.com/ASPN/Mail/Message/perl6-internals/2746631>
279 =item L<http://use.perl.org/~autrijus/journal/25768>
283 =head2 Python 2.3 MRO related links
287 =item L<http://www.python.org/2.3/mro.html>
289 =item L<http://www.python.org/2.2.2/descrintro.html#mro>
293 =head2 C3 for TinyCLOS
297 =item L<http://www.call-with-current-continuation.org/eggs/c3.html>
303 Stevan Little, E<lt>stevan@iinteractive.comE<gt>
305 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
307 =head1 COPYRIGHT AND LICENSE
309 Copyright 2006 by Infinity Interactive, Inc.
311 L<http://www.iinteractive.com>
313 This library is free software; you can redistribute it and/or modify
314 it under the same terms as Perl itself.