1Algorithm::C3(3) User Contributed Perl Documentation Algorithm::C3(3)
2
3
4
6 Algorithm::C3 - A module for merging hierarchies using the C3 algorithm
7
9 use Algorithm::C3;
10
11 # merging a classic diamond
12 # inheritence 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
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 precendence 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
46 C3 is the name of an algorithm which aims to provide a sane method res‐
47 olution order under multiple inheritence. It was first introduced in
48 the langauge Dylan (see links in the "SEE ALSO" section), and then
49 later adopted as the prefered MRO (Method Resolution Order) for the
50 new-style classes in Python 2.3. Most recently it has been adopted as
51 the 'canonical' MRO for Perl 6 classes, and the default MRO for Parrot
52 objects as well.
53
54 How does C3 work.
55
56 C3 works by always preserving local precendence ordering. This essen‐
57 tially means that no class will appear before any of it's subclasses.
58 Take the classic diamond inheritence pattern for instance:
59
60 <A>
61 / \
62 <B> <C>
63 \ /
64 <D>
65
66 The standard Perl 5 MRO would be (D, B, A, C). The result being that A
67 appears before C, even though C is the subclass of A. The C3 MRO algo‐
68 rithm however, produces the following MRO (D, B, C, A), which does not
69 have this same issue.
70
71 This example is fairly trival, for more complex examples and a deeper
72 explaination, see the links in the "SEE ALSO" section.
73
75 merge ($root, $func_to_fetch_parent, $cache)
76 This takes a $root node, which can be anything really it is up to
77 you. Then it takes a $func_to_fetch_parent which can be either a
78 CODE reference (see SYNOPSIS above for an example), or a string
79 containing a method name to be called on all the items being lin‐
80 earized. An example of how this might look is below:
81
82 {
83 package A;
84
85 sub supers {
86 no strict 'refs';
87 @{$_[0] . '::ISA'};
88 }
89
90 package C;
91 our @ISA = ('A');
92 package B;
93 our @ISA = ('A');
94 package D;
95 our @ISA = ('B', 'C');
96 }
97
98 print join ", " => Algorithm::C3::merge('D', 'supers');
99
100 The purpose of $func_to_fetch_parent is to provide a way for
101 "merge" to extract the parents of $root. This is needed for C3 to
102 be able to do it's work.
103
104 The $cache parameter is an entirely optional performance measure,
105 and should not change behavior.
106
107 If supplied, it should be a hashref that merge can use as a private
108 cache between runs to speed things up. Generally speaking, if you
109 will be calling merge many times on related things, and the parent
110 fetching function will return constant results given the same argu‐
111 ments during all of these calls, you can and should reuse the same
112 shared cache hash for all of the calls. Example:
113
114 sub do_some_merging {
115 my %merge_cache;
116 my @foo_mro = Algorithm::C3::Merge('Foo', \&get_supers, \%merge_cache);
117 my @bar_mro = Algorithm::C3::Merge('Bar', \&get_supers, \%merge_cache);
118 my @baz_mro = Algorithm::C3::Merge('Baz', \&get_supers, \%merge_cache);
119 my @quux_mro = Algorithm::C3::Merge('Quux', \&get_supers, \%merge_cache);
120 # ...
121 }
122
124 I use Devel::Cover to test the code coverage of my tests, below is the
125 Devel::Cover report on this module's test suite.
126
127 ------------------------ ------ ------ ------ ------ ------ ------ ------
128 File stmt bran cond sub pod time total
129 ------------------------ ------ ------ ------ ------ ------ ------ ------
130 Algorithm/C3.pm 100.0 100.0 100.0 100.0 100.0 100.0 100.0
131 ------------------------ ------ ------ ------ ------ ------ ------ ------
132 Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0
133 ------------------------ ------ ------ ------ ------ ------ ------ ------
134
136 The original Dylan paper
137
138 <http://www.webcom.com/haahr/dylan/linearization-oopsla96.html>
139
140 The prototype Perl 6 Object Model uses C3
141
142 <http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/>
143
144 Parrot now uses C3
145
146 <http://aspn.activestate.com/ASPN/Mail/Message/perl6-internals/2746631>
147 <http://use.perl.org/~autrijus/journal/25768>
148
149 Python 2.3 MRO related links
150
151 <http://www.python.org/2.3/mro.html>
152 <http://www.python.org/2.2.2/descrintro.html#mro>
153
154 C3 for TinyCLOS
155
156 <http://www.call-with-current-continuation.org/eggs/c3.html>
157
159 Stevan Little, <stevan@iinteractive.com>
160
161 Brandon L. Black, <blblack@gmail.com>
162
164 Copyright 2006 by Infinity Interactive, Inc.
165
166 <http://www.iinteractive.com>
167
168 This library is free software; you can redistribute it and/or modify it
169 under the same terms as Perl itself.
170
171
172
173perl v5.8.8 2007-05-31 Algorithm::C3(3)