Otawa  0.10
TreePath.h
Go to the documentation of this file.
1 /*
2  * TreePath class interface
3  *
4  * This file is part of OTAWA
5  * Copyright (c) 2006-07, IRIT UPS.
6  *
7  * OTAWA is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * OTAWA is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Foobar; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 #ifndef OTAWA_TSIM_TREEPATH_H
22 #define OTAWA_TSIM_TREEPATH_H
23 #include <elm/util/Option.h>
24 #include <elm/genstruct/Vector.h>
25 
26 #define DEFAULT_MAX_CHILDS 8
27 
28 namespace otawa { namespace tsim {
29 
30 template <class T1, class T2>
31 class TreePath
32 {
33  T1 _label;
37 
38  inline TreePath(const T1 label, int max_childs = DEFAULT_MAX_CHILDS);
39  inline TreePath<T1,T2>* searchChild(const T1 &label) const;
40 
41 public:
42  inline TreePath(int max_childs = DEFAULT_MAX_CHILDS);
43  inline TreePath(const T1 label, const T2 data, int max_childs = DEFAULT_MAX_CHILDS);
44  inline TreePath(elm::genstruct::Vector<T1> &path, const T2 data, int max_childs = DEFAULT_MAX_CHILDS);
45  inline TreePath(elm::genstruct::Vector<T1> *path, const T2 data, int max_childs = DEFAULT_MAX_CHILDS);
46  inline ~TreePath();
47 
48  // Accessors
49  inline T1 rootLabel() const;
50  inline T2 rootData() const;
51  inline elm::Option<T2> get(const elm::genstruct::Vector<T1> &path, int from_index = 0);
52 
53  // Mutators
54  inline void add(elm::genstruct::Vector<T1> &path, const T2 data, int from_index = 0);
55  inline void add(elm::genstruct::Vector<T1> *path, const T2 data, int from_index = 0);
56 
57  // Iterator
58  class Iterator: public elm::genstruct::Vector<TreePath<T1, T2> *>::Iterator {
59  public:
60  inline Iterator(const TreePath<T1, T2> *tree);
61  inline Iterator(const TreePath<T1, T2>::Iterator& iter);
62  };
63 };
64 
65 
66 // TreePath private methods
67 template <class T1, class T2>
68 inline TreePath<T1,T2>::TreePath(const T1 label, int max_childs)
69 : _max_childs(max_childs), _childs(max_childs) {
70  _label = label;
71  _data = elm::none;
72 }
73 
74 template <class T1, class T2>
75 inline TreePath<T1,T2>* TreePath<T1,T2>::searchChild(const T1 &label) const{
76  int l = _childs.length();
77  for(int i=0 ; i < l ; i++){
78  TreePath<T1,T2> *child = _childs[i];
79  if(child->_label == label)
80  return child;
81  }
82  return 0;
83 }
84 
85 // TreePath public methods
86 template <class T1, class T2>
87 inline TreePath<T1,T2>::TreePath(int max_childs)
88 :_childs(max_childs), _max_childs(max_childs){
89  _data = elm::none;
90 }
91 
92 template <class T1, class T2>
93 inline TreePath<T1,T2>::TreePath(const T1 label, const T2 data, int max_childs)
94 : _max_childs(max_childs), _childs(max_childs) {
95  _label = label;
96  _data = data;
97 }
98 
99 template <class T1, class T2>
100 inline TreePath<T1,T2>::TreePath(elm::genstruct::Vector<T1> &path, const T2 data, int max_childs)
101 : _max_childs(max_childs), _childs(max_childs){
102  _label = path[0];
103  _data = elm::none;
104  add(path,data,1);
105 }
106 
107 template <class T1, class T2>
108 inline TreePath<T1,T2>::TreePath(elm::genstruct::Vector<T1> *path, const T2 data, int max_childs)
109 :_childs(max_childs), _max_childs(max_childs){
110  _label = path[0];
111  _data = elm::none;
112  add(*path,data,1);
113 }
114 
115 template <class T1, class T2>
117  int l = _childs.length();
118  for(int i=0 ; i < l ; i++){
119  delete _childs[i];
120  }
121 }
122 
123 template <class T1, class T2>
124 inline void TreePath<T1,T2>::add(elm::genstruct::Vector<T1> &path, const T2 data, int from_index){
125  TreePath<T1,T2> *cur = this;
126  int l = path.length();
127  for(int i = from_index ; i < l ; i++){
128  T1 &label = path[i];
130  cur = cur->searchChild(label);
131  if(!cur){
132  cur = new TreePath<T1,T2>(label,_max_childs);
133  childs.add(cur);
134  }
135  }
136  cur->_data = data;
137 }
138 
139 template <class T1, class T2>
140 inline void TreePath<T1,T2>::add(elm::genstruct::Vector<T1> *path, const T2 data, int from_index){
141  add(*path, data, from_index);
142 }
143 
144 template <class T1, class T2>
146  TreePath<T1,T2> *cur = this;
147  int l = path.length();
148  for(int i=from_index ; i < l ; i++){
149  cur = cur->searchChild(path[i]);
150  if(!cur)
151  return elm::none;
152  }
153  return cur->_data;
154 }
155 
156 template <class T1, class T2>
157 inline T1 TreePath<T1,T2>::rootLabel() const {return _label;}
158 
159 template <class T1, class T2>
160 inline T2 TreePath<T1,T2>::rootData() const {return _data;}
161 
162 
163 // TreePath<T1, T2>::Iterator Inlines
164 template <class T1, class T2>
166 : elm::genstruct::Vector<TreePath<T1, T2> *>::Iterator(tree->_childs) {
167  ASSERT(tree);
168 }
169 
170 template <class T1, class T2>
172 : elm::genstruct::Vector<TreePath<T1, T2> *>::Iterator(iter) {
173 }
174 
175 } } //namespace otawa::tsim
176 
177 #endif /*OTAWA_IPET_TSIM_TREEPATH_H*/
Iterator(const TreePath< T1, T2 > *tree)
Definition: TreePath.h:165
int _max_childs
Definition: TreePath.h:35
elm::Option< T2 > get(const elm::genstruct::Vector< T1 > &path, int from_index=0)
Gives an optional value attached to the path given.
Definition: TreePath.h:145
TreePath< T1, T2 > * searchChild(const T1 &label) const
Definition: TreePath.h:75
T1 rootLabel() const
Definition: TreePath.h:157
int length(void) const
~TreePath()
Destroys all his childs.
Definition: TreePath.h:116
elm::genstruct::Vector< TreePath< T1, T2 > * > _childs
Definition: TreePath.h:36
T1 _label
Definition: TreePath.h:33
inst add(int d, int a, int b)
Definition: inst.h:163
Definition: TreePath.h:58
TreePath(const T1 label, int max_childs=DEFAULT_MAX_CHILDS)
Definition: TreePath.h:68
Definition: TreePath.h:31
elm::Option< T2 > _data
Definition: TreePath.h:34
T2 rootData() const
Definition: TreePath.h:160
const OptionalNone none
void add(elm::genstruct::Vector< T1 > &path, const T2 data, int from_index=0)
Adds a value attached to the path given.
Definition: TreePath.h:124
#define DEFAULT_MAX_CHILDS
Definition: TreePath.h:26