4 /************************************************************************
5 ************************************************************************
7 Copyright (C) 2003-2004 GRAME, Centre National de Creation Musicale
8 ---------------------------------------------------------------------
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 ************************************************************************
23 ************************************************************************/
27 /*****************************************************************************
28 ******************************************************************************
30 Y. Orlarey, (c) Grame 2002
31 ------------------------------------------------------------------------------
32 Tlib is a simple tree library inspired by the ATerm library. It is made of
33 five elements : symbols, nodes, smartpointers, trees and lists :
35 +------------------------+
37 +------------------------+
39 |------------------------|
41 |------------------------|
43 |------------------------|
45 |---------| smartpointer |
47 +---------+--------------+
53 Sym q = symbol("abcd"); : returns a symbol q of name "abcd"
54 const char* s = name(q); : returns the name of symbol q
58 Node(symbol("abcd")); : node with symbol content
59 Node(10); : node with int content
60 Node(3.14159); : node with float content
62 n->type(); : kIntNode or kFloatNode or kSymNode
64 n->getInt(); : int content of n
65 n->getFloat(); : float content of n
66 n->getSym(); : symbol content of n
70 if (isInt(n, &i)) ... : int i = int content of n
71 if (isFloat(n, &f)) ... : float f = float content of n
72 if (isSym(n, &s)) ... : Sym s = Sym content of n
76 tree (n) : tree of node n with no branch
77 tree (n, t1) : tree of node n with a branch t
78 tree (n, t1,...,tm) : tree of node n with m branches t1,...,tm
82 if (isTree (t, n)) ... : t has node n and no branches;
83 if (isTree (t, n, &t1) ... : t has node n and 1 branch, t1 is set accordingly;
84 if (isTree (t, n, &t1...&tm)...: t has node n and m branches, ti's are set accordingly;
88 t->node() : the node of t
89 t->arity() : the number of branches of t
90 t->branch(i) : the ith branch of t
95 nil = predefined empty list
96 cons (x,l) = create a new list of head x and tail l
97 list(a,b,..) = cons(a, list(b,...))
101 nth(l,i) = ith element of l (or nil)
102 len(l) = number of elements of l
104 isNil(nil) = true (false otherwise)
105 isList(cons(x,l)) = true (false otherwise)
107 lmap(f, cons(x,l)) = cons(f(x), lmap(f,l))
108 reverse([a,b,..,z]) = [z,..,b,a]
109 reverseall([a,b,..,z]) = [ra(z),..,ra(b),ra(a)] where ra is reverseall
112 (Sets are implemented as ordered lists of elements without duplication)
114 isElement(e,s) = true if e is an element of set s, false otherwise
115 addElement(e,s) = s U {e}
117 list2set(l) = convert a list into a set
118 setUnion(s1,s2) = s1 U s2
119 setIntersection(s1,s2) = s1 intersection s2
120 setIntersection(s1,s2) = s1 - s2
124 pushEnv (key, val, env) -> env' create a new environment
125 searchEnv (key,&v,env) -> bool search for key in env and set v accordingly
129 setProperty (t, key, val) -> t add the association (key x val) to the pl of t
130 getProperty (t, key, &val) -> bool search the pp of t for the value associated to key
131 remProperty (t, key) -> t remove any association (key x ?) from the pl of t
136 rid() = a unique ID (a tree) used to identify recursive trees
137 rec(id, t) = a tree containing recursive references 'ref(id)'
138 ref(id) = a reference to a surrounding 'rec(id,t)'
139 isRec(t, id, t') = true if t = rec(id,t')
140 isRef(t, id) = true if t = ref(id)
142 areEquiv(t,t') = alpha equivalence of trees
143 shmax(t) = maximize the sharing of recursive subtrees
146 6) Sharing Analysis :
147 ---------------------
149 shprkey(t) -> k = unique sharing property key of t
150 shcount(k,t') -> n = returns the number of occurences of t' in t (where k = shprkey(t))
151 shlysis(t) -> k = annotated the subtrees of t with prop (key sharing-count)
152 (0 if t' is not a subtree of t)
157 2002-02-08 : First version
158 2002-02-20 : New description of the API
159 2002-04-07 : Added Sharing Analysis 'shlysis.h'
161 ******************************************************************************
162 *****************************************************************************/
169 #include "shlysis.hh"
170 //#include "recness.hh"