Algorithm::C3 - A module for merging hierarchies using the C3 algorithm


Back to Top


NAME

Algorithm::C3 - A module for merging hierarchies using the C3 algorithm

Back to Top


SYNOPSIS

  use Algorithm::C3;
  # merging a classic diamond
  # inheritance graph like this:
  #
  #    <A>
  #   /   \
  # <B>   <C>
  #   \   /
  #    <D>
  my @merged = Algorithm::C3::merge(
      'D',
      sub {
          # extract the ISA array
          # from the package
          no strict 'refs';
          @{$_[0] . '::ISA'};
      }
  );
  print join ", " => @merged; # prints D, B, C, A

Back to Top


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 the Class::C3 manpage 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 subclasn