00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 # ifndef GRAPH_TO_TREE_H
00045 # define GRAPH_TO_TREE_H
00046
00047 # include <tpl_tree_node.H>
00048 # include <tpl_graph.H>
00049 # include <tpl_graph_utils.H>
00050
00051 namespace Aleph
00052 {
00053
00054 template <typename GT, typename Key, class Convert> static
00055 Tree_Node<Key> * graph_to_tree_node(GT & g, typename GT::Node * groot);
00056 template <typename GT, typename Key, class Convert> static void
00057 __graph_to_tree_node(GT & g, typename GT::Node * groot, Tree_Node<Key> * troot);
00126 template <typename GT, typename Key, class Convert> inline
00127 Tree_Node<Key> * graph_to_tree_node(GT & g, typename GT::Node * groot)
00128 {
00129
00130 if (not Aleph::is_acyclique(g))
00131 throw std::domain_error("Graph is not a tree (not acyclique)");
00132
00133 Tree_Node<Key> * troot = new Tree_Node<Key>;
00134
00135 Convert () (groot, troot);
00136
00137
00138 __graph_to_tree_node <GT, Key, Convert> (g, groot, troot);
00139
00140 return troot;
00141 }
00142 template <typename GT, typename Key, typename Convert> static
00143 void __graph_to_tree_node(GT & g,
00144 typename GT::Node * groot,
00145 Tree_Node<Key> * troot)
00146 {
00147 typedef typename GT::Node_Arc_Iterator Iterator;
00148 typedef typename GT::Node Node;
00149 typedef typename GT::Arc Arc;
00150
00151
00152 for (Iterator it(groot); it.has_current(); it.next())
00153 {
00154 Arc * arc = it.get_current_arc();
00155
00156 if (IS_ARC_VISITED(arc, Convert_Tree))
00157 continue;
00158
00159 ARC_BITS(arc).set_bit(Convert_Tree, true);
00160
00161 Node * gtgt = it.get_tgt_node();
00162
00163 Tree_Node<Key> * ttgt = new Tree_Node<Key>;
00164
00165 Convert () (gtgt, ttgt);
00166
00167 troot->insert_rightmost_child(ttgt);
00168
00169 __graph_to_tree_node <GT, Key, Convert> (g, gtgt, ttgt);
00170 }
00171 }
00172
00173 }
00174
00175 # endif // GRAPH_TO_TREE_H
00176