1Algorithm::C3(3)      User Contributed Perl Documentation     Algorithm::C3(3)
2
3
4

NAME

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

SYNOPSIS

9         use Algorithm::C3;
10
11         # merging a classic diamond
12         # inheritance graph like this:
13         #
14         #    <A>
15         #   /   \
16         # <B>   <C>
17         #   \   /
18         #    <D>
19
20         my @merged = Algorithm::C3::merge(
21             'D',
22             sub {
23                 # extract the ISA array
24                 # from the package
25                 no strict 'refs';
26                 @{$_[0] . '::ISA'};
27             }
28         );
29
30         print join ", " => @merged; # prints D, B, C, A
31

DESCRIPTION

33       This module implements the C3 algorithm. I have broken this out into
34       it's own module because I found myself copying and pasting it way too
35       often for various needs. Most of the uses I have for C3 revolve around
36       class building and metamodels, but it could also be used for things
37       like dependency resolution as well since it tends to do such a nice job
38       of preserving local precedence orderings.
39
40       Below is a brief explanation of C3 taken from the Class::C3 module. For
41       more detailed information, see the "SEE ALSO" section and the links
42       there.
43
44   What is C3?
45       C3 is the name of an algorithm which aims to provide a sane method
46       resolution order under multiple inheritance. It was first introduced in
47       the language Dylan (see links in the "SEE ALSO" section), and then
48       later adopted as the preferred MRO (Method Resolution Order) for the
49       new-style classes in Python 2.3. Most recently it has been adopted as
50       the 'canonical' MRO for Perl 6 classes, and the default MRO for Parrot
51       objects as well.
52
53   How does C3 work.
54       C3 works by always preserving local precedence ordering. This
55       essentially means that no class will appear before any of it's
56       subclasses. Take the classic diamond inheritance pattern for instance:
57
58            <A>
59           /   \
60         <B>   <C>
61           \   /
62            <D>
63
64       The standard Perl 5 MRO would be (D, B, A, C). The result being that A
65       appears before C, even though C is the subclass of A.  The C3 MRO
66       algorithm however, produces the following MRO (D, B, C, A), which does
67       not have this same issue.
68
69       This example is fairly trivial, for more complex examples and a deeper
70       explanation, see the links in the "SEE ALSO" section.
71

FUNCTION

73       merge ($root, $func_to_fetch_parent, $cache)
74           This takes a $root node, which can be anything really it is up to
75           you. Then it takes a $func_to_fetch_parent which can be either a
76           CODE reference (see SYNOPSIS above for an example), or a string
77           containing a method name to be called on all the items being
78           linearized. An example of how this might look is below:
79
80             {
81                 package A;
82
83                 sub supers {
84                     no strict 'refs';
85                     @{$_[0] . '::ISA'};
86                 }
87
88                 package C;
89                 our @ISA = ('A');
90                 package B;
91                 our @ISA = ('A');
92                 package D;
93                 our @ISA = ('B', 'C');
94             }
95
96             print join ", " => Algorithm::C3::merge('D', 'supers');
97
98           The purpose of $func_to_fetch_parent is to provide a way for
99           "merge" to extract the parents of $root. This is needed for C3 to
100           be able to do it's work.
101
102           The $cache parameter is an entirely optional performance measure,
103           and should not change behavior.
104
105           If supplied, it should be a hashref that merge can use as a private
106           cache between runs to speed things up.  Generally speaking, if you
107           will be calling merge many times on related things, and the parent
108           fetching function will return constant results given the same
109           arguments during all of these calls, you can and should reuse the
110           same shared cache hash for all of the calls.  Example:
111
112             sub do_some_merging {
113                 my %merge_cache;
114                 my @foo_mro = Algorithm::C3::Merge('Foo', \&get_supers, \%merge_cache);
115                 my @bar_mro = Algorithm::C3::Merge('Bar', \&get_supers, \%merge_cache);
116                 my @baz_mro = Algorithm::C3::Merge('Baz', \&get_supers, \%merge_cache);
117                 my @quux_mro = Algorithm::C3::Merge('Quux', \&get_supers, \%merge_cache);
118                 # ...
119             }
120

CODE COVERAGE

122       I use Devel::Cover to test the code coverage of my tests, below is the
123       Devel::Cover report on this module's test suite.
124
125        ------------------------ ------ ------ ------ ------ ------ ------ ------
126        File                       stmt   bran   cond    sub    pod   time  total
127        ------------------------ ------ ------ ------ ------ ------ ------ ------
128        Algorithm/C3.pm           100.0  100.0  100.0  100.0  100.0  100.0  100.0
129        ------------------------ ------ ------ ------ ------ ------ ------ ------
130        Total                     100.0  100.0  100.0  100.0  100.0  100.0  100.0
131        ------------------------ ------ ------ ------ ------ ------ ------ ------
132

SEE ALSO

134   The original Dylan paper
135       <http://www.webcom.com/haahr/dylan/linearization-oopsla96.html>
136
137   The prototype Perl 6 Object Model uses C3
138       <http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/>
139
140   Parrot now uses C3
141       <http://aspn.activestate.com/ASPN/Mail/Message/perl6-internals/2746631>
142       <http://use.perl.org/~autrijus/journal/25768>
143
144   Python 2.3 MRO related links
145       <http://www.python.org/2.3/mro.html>
146       <http://www.python.org/2.2.2/descrintro.html#mro>
147
148   C3 for TinyCLOS
149       <http://www.call-with-current-continuation.org/eggs/c3.html>
150

AUTHORS

152       Stevan Little, <stevan@iinteractive.com>
153
154       Brandon L. Black, <blblack@gmail.com>
155
157       Copyright 2006 by Infinity Interactive, Inc.
158
159       <http://www.iinteractive.com>
160
161       This library is free software; you can redistribute it and/or modify it
162       under the same terms as Perl itself.
163
164
165
166perl v5.32.0                      2020-07-28                  Algorithm::C3(3)
Impressum