1 /************************************************************************
2 ************************************************************************
4 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
5 ---------------------------------------------------------------------
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 ************************************************************************
20 ************************************************************************/
24 /*****************************************************************************
25 ******************************************************************************
27 Y. Orlarey, (c) Grame 2002
28 ------------------------------------------------------------------------------
29 The sharing analysis of tree t is the annotation of all its subtrees t'
30 with their number of occurences in t. As this annotation of t' depends of
31 a context (the tree t for which t' is a subtree) a specific property key
32 unique to each sharing analysis must be generated.
37 shprkey(t) -> k = unique sharing property key of t
38 shcount(k,t') -> n = returns the number of occurences of t' in t (where k = shprkey(t))
39 shlysis(t) -> k = annotated the subtrees of t with prop (key sharing-count)
40 (0 if t' is not a subtree of t)
44 2002-04-08 : First version
45 2006-03-25 : Modifications for new symbolic rec trees
47 ******************************************************************************
48 *****************************************************************************/
52 * The sharing analysis of tree t is the annotation of all its subtrees t'
53 * with their number of occurences in t. As this annotation of t' depends of
54 * a context (the tree t for which t' is a subtree) a specific property key
55 * unique to each sharing analysis must be generated.
63 #include "compatibility.hh"
66 * Create a specific property key for the sharing count of subtrees of t
72 snprintf(name
, 256, "SHARED IN %p : ", (CTree
*)t
);
73 return tree(unique(name
));
78 * Return the value of sharing count or 0
81 int shcount(Tree key
, Tree t
)
84 if (getProperty(t
, key
, c
)) {
85 return c
->node().getInt();
93 //------------------------------------------------------------------------------
94 // Create a specific property key for the sharing count of subtrees of t
95 //------------------------------------------------------------------------------
97 static void annotate(Tree k
, Tree t
, barrier foo
);
99 static bool nobarrier (const Tree
& t
) { return false; }
102 * Do a sharing analysis : annotates all the subtrees of t
103 * with there occurences
105 Tree
shlysis(Tree t
, barrier foo
)
114 * Do a sharing analysis : annotates all the subtrees of t
115 * with there occurences
120 annotate(k
, t
, nobarrier
);
126 * Recursively increment the occurences count
127 * of t and its subtrees
129 static void annotate(Tree k
, Tree t
, barrier foo
)
131 cerr
<< "Annotate " << *t
<< endl
;
132 int c
= shcount(k
,t
);
136 if (isRec(t
, var
, body
)) {
137 // special case for recursive trees
138 setProperty(t
, k
, tree(1));
139 annotate(k
, body
, foo
);
143 if (n
>0 && ! foo(t
)) {
144 for (int i
=0; i
<n
; i
++) annotate(k
, t
->branch(i
), foo
);
148 //printf(" annotate %p with %d\n", (CTree*)t, c+1);
150 setProperty(t
, k
, tree(c
+1));