@@ -13,6 +13,7 @@ Date: April 2016
13
13
14
14
#include " class_hierarchy.h"
15
15
16
+ #include < iterator>
16
17
#include < ostream>
17
18
18
19
#include < util/json_stream.h>
@@ -88,6 +89,70 @@ void class_hierarchy_grapht::populate(const symbol_tablet &symbol_table)
88
89
}
89
90
}
90
91
92
+ // / Helper function that converts a vector of node_indext to a vector of ids
93
+ // / that are stored in the corresponding nodes in the graph.
94
+ class_hierarchy_grapht::idst class_hierarchy_grapht::ids_from_indices (
95
+ const std::vector<node_indext> &node_indices) const
96
+ {
97
+ idst result;
98
+ std::transform (
99
+ node_indices.begin (),
100
+ node_indices.end (),
101
+ back_inserter (result),
102
+ [&](const node_indext &node_index) {
103
+ return (*this )[node_index].class_identifier ;
104
+ });
105
+ return result;
106
+ }
107
+
108
+ // / Get all the classes that directly (i.e. in one step) inherit from class c.
109
+ // / \param c: The class to consider
110
+ // / \return A list containing ids of all direct children of c.
111
+ class_hierarchy_grapht::idst
112
+ class_hierarchy_grapht::get_direct_children (const irep_idt &c) const
113
+ {
114
+ const node_indext &node_index = nodes_by_name.at (c);
115
+ const auto &child_indices = get_successors (node_index);
116
+ return ids_from_indices (child_indices);
117
+ }
118
+
119
+ // / Helper function for `get_children_trans` and `get_parents_trans`
120
+ class_hierarchy_grapht::idst class_hierarchy_grapht::get_other_reachable_ids (
121
+ const irep_idt &c,
122
+ bool forwards) const
123
+ {
124
+ idst direct_child_ids;
125
+ const node_indext &node_index = nodes_by_name.at (c);
126
+ const auto &reachable_indices = get_reachable (node_index, forwards);
127
+ auto reachable_ids = ids_from_indices (reachable_indices);
128
+ // Remove c itself from the list
129
+ // TODO Adding it first and then removing it is not ideal. It would be
130
+ // better to define a function grapht::get_other_reachable and directly use
131
+ // that here.
132
+ reachable_ids.erase (
133
+ std::remove (reachable_ids.begin (), reachable_ids.end (), c),
134
+ reachable_ids.end ());
135
+ return reachable_ids;
136
+ }
137
+
138
+ // / Get all the classes that inherit (directly or indirectly) from class c.
139
+ // / \param c: The class to consider
140
+ // / \return A list containing ids of all classes that eventually inherit from c.
141
+ class_hierarchy_grapht::idst
142
+ class_hierarchy_grapht::get_children_trans (const irep_idt &c) const
143
+ {
144
+ return get_other_reachable_ids (c, true );
145
+ }
146
+
147
+ // / Get all the classes that class c inherits from (directly or indirectly).
148
+ // / \param c: The class to consider
149
+ // / \return A list of class ids that c eventually inherits from.
150
+ class_hierarchy_grapht::idst
151
+ class_hierarchy_grapht::get_parents_trans (const irep_idt &c) const
152
+ {
153
+ return get_other_reachable_ids (c, false );
154
+ }
155
+
91
156
void class_hierarchyt::get_children_trans_rec (
92
157
const irep_idt &c,
93
158
idst &dest) const
@@ -105,7 +170,7 @@ void class_hierarchyt::get_children_trans_rec(
105
170
get_children_trans_rec (child, dest);
106
171
}
107
172
108
- // / Get all the classes that inherit (directly or indirectly) from class c . The
173
+ // / Get all the classes that class c inherits from (directly or indirectly). The
109
174
// / first element(s) will be the immediate parents of c, though after this
110
175
// / the order is all the parents of the first immediate parent
111
176
// / \param c: The class to consider
0 commit comments