New directory tree, with preprocessor/ inside interpretor/.
[Faustine.git] / interpretor / preprocessor / faust-0.9.47mr3 / compiler / tlib / occurrences.cpp
1 /************************************************************************
2 ************************************************************************
3 FAUST compiler
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.
10
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.
15
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 ************************************************************************/
21 /**
22 * @file occurrences.cpp
23 * Count the number of occurences of each subtree of a root tree.
24 */
25
26
27
28
29 // construction des representations graphiques
30
31
32 #include "occurrences.hh"
33 #include "compatibility.hh"
34
35 /**
36 * Count the number of occurrences of each subtree of root
37 */
38 Occurrences::Occurrences(Tree root)
39 {
40 fKey = specificKey(root);
41 countOccurrences(root);
42 setCount(root,0); // root as no occurences in itself
43 }
44
45 /**
46 * Get the number of occurrences of t
47 */
48 int Occurrences::getCount(Tree t)
49 {
50 Tree c;
51 return (getProperty(t, fKey, c)) ? c->node().getInt() : 0;
52 }
53
54 /**
55 * Set the number of occurrences of t
56 */
57 void Occurrences::setCount(Tree t, int c)
58 {
59 setProperty(t, fKey, tree(c));
60 }
61
62
63
64 /**
65 * Creates a specific property key for occurrences count in root
66 */
67 Tree Occurrences::specificKey(Tree root)
68 {
69 char keyname[256];
70 snprintf(keyname, 256, "OCCURRENCES COUNT IN %p : ", (CTree*)root);
71
72 return tree(unique(keyname));
73 }
74
75 /**
76 * Increment the occurrences count of t and its subtrees
77 */
78 void Occurrences::countOccurrences(Tree t)
79 {
80 setCount(t, getCount(t)+1); // increment t occurrences count
81 for (int i=0; i<t->arity(); i++) {
82 countOccurrences(t->branch(i));
83 }
84 }
85