// graph-tool -- a general graph modification and manipulation thingy // // Copyright (C) 2006-2015 Tiago de Paula Peixoto // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 3 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . #include "graph_python_interface.hh" #include "graph.hh" #include "graph_properties.hh" #include "graph_filtering.hh" #include "graph_selectors.hh" #include "graph_util.hh" #include #include #include using namespace std; using namespace boost; using namespace graph_tool; namespace graph_tool { // global property types' names const char* type_names[] = {"bool", "int16_t", "int32_t", "int64_t", "double", "long double", "string", "vector", "vector", "vector", "vector", "vector", "vector", "vector", "python::object"}; struct shift_vertex_property { template void operator()(PropertyMap, const GraphInterface::multigraph_t& g, boost::any map, const Vec& vi, bool& found) const { try { PropertyMap pmap = any_cast(map); size_t back = num_vertices(g) - 1; for (auto v : vi) { for (size_t i = v; i < back; ++i) pmap[vertex(i, g)] = pmap[vertex(i + 1, g)]; back--; } found = true; } catch (bad_any_cast&) {} } }; // this function will shift all the properties when a vertex is to be deleted void GraphInterface::ShiftVertexProperty(boost::any prop, python::object oindex) const { boost::multi_array_ref index = get_array(oindex); bool found = false; mpl::for_each (std::bind(shift_vertex_property(), std::placeholders::_1, std::ref(*_mg), prop, index, std::ref(found))); if (!found) throw GraphException("invalid writable property map"); } struct move_vertex_property { template void operator()(PropertyMap, const GraphInterface::multigraph_t& g, boost::any map, const Vec& vi, size_t back, bool& found) const { try { PropertyMap pmap = any_cast(map); for (auto v : vi) { pmap[vertex(v, g)] = pmap[vertex(back, g)]; back--; } found = true; } catch (bad_any_cast&) {} } }; // this function will move the back of the property map when a vertex in the middle is to be deleted void GraphInterface::MoveVertexProperty(boost::any prop, python::object oindex) const { boost::multi_array_ref index = get_array(oindex); size_t back = num_vertices(*_mg) - 1; bool found = false; mpl::for_each (std::bind(move_vertex_property(), std::placeholders::_1, std::ref(*_mg), prop, index, back, std::ref(found))); if (!found) throw GraphException("invalid writable property map"); } struct reindex_vertex_property { template void operator()(PropertyMap, const GraphInterface::multigraph_t& g, boost::any map, IndexMap old_index, bool& found) const { try { PropertyMap pmap = any_cast(map); for (size_t i = 0; i < num_vertices(g); ++i) { GraphInterface::vertex_t v = vertex(i, g); if (old_index[v] != int(i)) pmap[v] = pmap[vertex(old_index[v], g)]; } found = true; } catch (bad_any_cast&) {} } }; void GraphInterface::ReIndexVertexProperty(boost::any map, boost::any aold_index) const { typedef property_map_type::apply::type index_prop_t; index_prop_t old_index = any_cast(aold_index); bool found = false; mpl::for_each (std::bind(reindex_vertex_property(), std::placeholders::_1, std::ref(*_mg), map, old_index, std::ref(found))); if (!found) throw GraphException("invalid writable property map"); } } // graph_tool namespace struct do_infect_vertex_property { template void operator()(Graph& g, IndexMap index, PropertyMap prop, boost::python::object oval) const { typedef typename property_traits::value_type val_t; bool all = false; std::unordered_set > vals; if (oval == boost::python::object()) { all = true; } else { for (int i = 0; i < len(oval); ++i) { val_t val = boost::python::extract(oval[i]); vals.insert(val); } } unchecked_vector_property_map marked(index, num_vertices(g)); int i, N = num_vertices(g); #pragma omp parallel for default(shared) private(i) for (i = 0; i < N; ++i) { typename graph_traits::vertex_descriptor v = vertex(i, g); if (v == graph_traits::null_vertex()) continue; bool skip; { #pragma omp critical skip = marked[v]; } if (skip) continue; if (!all && vals.find(prop[v]) == vals.end()) continue; typename graph_traits::adjacency_iterator a, a_end; for (tie(a, a_end) = adjacent_vertices(v, g); a != a_end; ++a) { if (prop[*a] == prop[v]) continue; { #pragma omp critical marked[*a] = true; } prop[*a] = prop[v]; } } } }; void infect_vertex_property(GraphInterface& gi, boost::any prop, boost::python::object val) { run_action<>()(gi, std::bind(do_infect_vertex_property(), std::placeholders::_1, gi.GetVertexIndex(), std::placeholders::_2, val), writable_vertex_properties())(prop); } template vector operator-(const vector& a, const vector& b) { vector c(a); c.resize(max(a.size(), b.size()), Value(0)); for (size_t i = 0; i < b.size(); ++i) c[i] = a[i] - b[i]; return c; } struct do_mark_edges { template void operator()(Graph& g, EdgePropertyMap prop) const { int i, N = num_vertices(g); #pragma omp parallel for default(shared) private(i) \ schedule(runtime) if (N > 100) for (i = 0; i < N; ++i) { typename graph_traits::vertex_descriptor v = vertex(i, g); if (v == graph_traits::null_vertex()) continue; typename graph_traits::out_edge_iterator e, e_end; for (tie(e, e_end) = out_edges(v, g); e != e_end; ++e) prop[*e] = true; } } }; void mark_edges(GraphInterface& gi, boost::any prop) { run_action()(gi, bind(do_mark_edges(), _1, _2), writable_edge_scalar_properties())(prop); } struct do_perfect_vhash { template void operator()(Graph& g, VertexPropertyMap prop, HashProp hprop, boost::any& adict) const { typedef typename property_traits::value_type val_t; typedef typename property_traits::value_type hash_t; typedef unordered_map dict_t; if (adict.empty()) adict = dict_t(); dict_t& dict = any_cast(adict); for (auto v : vertices_range(g)) { auto val = prop[v]; auto iter = dict.find(val); hash_t h; if (iter == dict.end()) h = dict[val] = dict.size() - 1; else h = iter->second; hprop[v] = h; } } }; void perfect_vhash(GraphInterface& gi, boost::any prop, boost::any hprop, boost::any& dict) { run_action() (gi, std::bind(do_perfect_vhash(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::ref(dict)), vertex_properties(), writable_vertex_scalar_properties()) (prop, hprop); } struct do_perfect_ehash { template void operator()(Graph& g, EdgePropertyMap prop, HashProp hprop, boost::any& adict) const { typedef typename property_traits::value_type val_t; typedef typename property_traits::value_type hash_t; typedef unordered_map dict_t; if (adict.empty()) adict = dict_t(); dict_t& dict = any_cast(adict); for (auto e : edges_range(g)) { auto val = prop[e]; auto iter = dict.find(val); hash_t h; if (iter == dict.end()) h = dict[val] = dict.size() - 1; else h = iter->second; hprop[e] = h; } } }; void perfect_ehash(GraphInterface& gi, boost::any prop, boost::any hprop, boost::any& dict) { run_action() (gi, std::bind(do_perfect_ehash(), std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::ref(dict)), edge_properties(), writable_edge_scalar_properties()) (prop, hprop); }