Algorithm-C3
view release on metacpan or search on metacpan
DESCRIPTION
This module implements the C3 algorithm. I have broken this out into
it's own module because I found myself copying and pasting it way too
often for various needs. Most of the uses I have for C3 revolve around
class building and metamodels, but it could also be used for things like
dependency resolution as well since it tends to do such a nice job of
preserving local precedence orderings.
Below is a brief explanation of C3 taken from the Class::C3 module. For
more detailed information, see the "SEE ALSO" section and the links
there.
What is C3?
C3 is the name of an algorithm which aims to provide a sane method
resolution order under multiple inheritance. It was first introduced in
the language Dylan (see links in the "SEE ALSO" section), and then later
adopted as the preferred MRO (Method Resolution Order) for the new-style
classes in Python 2.3. Most recently it has been adopted as the
'canonical' MRO for Perl 6 classes, and the default MRO for Parrot
objects as well.
How does C3 work.
C3 works by always preserving local precedence ordering. This
essentially means that no class will appear before any of it's
subclasses. Take the classic diamond inheritance pattern for instance:
<B> <C>
\ /
<D>
The standard Perl 5 MRO would be (D, B, A, C). The result being that A
appears before C, even though C is the subclass of A. The C3 MRO
algorithm however, produces the following MRO (D, B, C, A), which does
not have this same issue.
This example is fairly trivial, for more complex examples and a deeper
explanation, see the links in the "SEE ALSO" section.
FUNCTION
merge ($root, $func_to_fetch_parent, $cache)
This takes a $root node, which can be anything really it is up to
you. Then it takes a $func_to_fetch_parent which can be either a
CODE reference (see SYNOPSIS above for an example), or a string
containing a method name to be called on all the items being
linearized. An example of how this might look is below:
{
lib/Algorithm/C3.pm view on Meta::CPAN
This module implements the C3 algorithm. I have broken this out
into it's own module because I found myself copying and pasting
it way too often for various needs. Most of the uses I have for
C3 revolve around class building and metamodels, but it could
also be used for things like dependency resolution as well since
it tends to do such a nice job of preserving local precedence
orderings.
Below is a brief explanation of C3 taken from the L<Class::C3>
module. For more detailed information, see the L<SEE ALSO> section
and the links there.
=head2 What is C3?
C3 is the name of an algorithm which aims to provide a sane method
resolution order under multiple inheritance. It was first introduced
in the language Dylan (see links in the L<SEE ALSO> section), and
then later adopted as the preferred MRO (Method Resolution Order)
for the new-style classes in Python 2.3. Most recently it has been
adopted as the 'canonical' MRO for Perl 6 classes, and the default
MRO for Parrot objects as well.
=head2 How does C3 work.
C3 works by always preserving local precedence ordering. This
essentially means that no class will appear before any of it's
subclasses. Take the classic diamond inheritance pattern for
lib/Algorithm/C3.pm view on Meta::CPAN
<B> <C>
\ /
<D>
The standard Perl 5 MRO would be (D, B, A, C). The result being that
B<A> appears before B<C>, even though B<C> is the subclass of B<A>.
The C3 MRO algorithm however, produces the following MRO (D, B, C, A),
which does not have this same issue.
This example is fairly trivial, for more complex examples and a deeper
explanation, see the links in the L<SEE ALSO> section.
=head1 FUNCTION
=over 4
=item B<merge ($root, $func_to_fetch_parent, $cache)>
This takes a C<$root> node, which can be anything really it
is up to you. Then it takes a C<$func_to_fetch_parent> which
can be either a CODE reference (see L<SYNOPSIS> above for an
t/003_merge.t view on Meta::CPAN
use Test::More tests => 1;
use Algorithm::C3;
=pod
This example is take from: http://www.python.org/2.3/mro.html
"My second example"
class O: pass
class F(O): pass
class E(O): pass
class D(O): pass
class C(D,F): pass
class B(E,D): pass
class A(B,C): pass
6
---
( run in 1.053 second using v1.01-cache-2.11-cpan-39bf76dae61 )