:github_url: https://github.com/svenevs/exhale-companion .. _program_listing_file_xnetwork_classes_digraphs.hpp: Program Listing for File digraphs.hpp ===================================== |exhale_lsh| :ref:`Return to documentation for file ` (``xnetwork/classes/digraphs.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include #include #include #include #include #include // import AtlasView, AdjacencyView #include #include // import NodeView, EdgeView, DegreeView namespace xn { template , int>> class DiGraphS : public Graph { using _Base = Graph; public: using Node = typename _Base::Node; // luk using edge_t = std::pair; using graph_attr_dict_factory = typename _Base::graph_attr_dict_factory; using adjlist_outer_dict_factory = typename _Base::adjlist_outer_dict_factory; using key_type = typename _Base::key_type; using value_type = typename _Base::value_type; public: adjlist_outer_dict_factory& _succ; // successor explicit DiGraphS(const nodeview_t& Nodes) : _Base {Nodes} , _succ {_Base::_adj} { } explicit DiGraphS(int num_nodes) : _Base {py::range(num_nodes)} , _succ {_Base::_adj} { } auto adj() const { using T = decltype(this->_succ); return AdjacencyView(this->_succ); } auto succ() const { using T = decltype(this->_succ); return AdjacencyView(this->_succ); } template typename std::enable_if::value>::type add_edge( const Node& u, const Node& v) { // auto [u, v] = u_of_edge, v_of_edge; // add nodes assert(this->_node.contains(u)); assert(this->_node.contains(v)); // add the edge // datadict = this->_adj[u].get(v, this->edge_attr_dict_factory()); // datadict.update(attr); this->_succ[u].insert(v); // this->_prev[v].insert(u); this->_num_of_edges += 1; } template typename std::enable_if::value>::type add_edge( const Node& u, const Node& v) { // auto [u, v] = u_of_edge, v_of_edge; // add nodes assert(this->_node.contains(u)); assert(this->_node.contains(v)); // add the edge // datadict = this->_adj[u].get(v, this->edge_attr_dict_factory()); // datadict.update(attr); using T = typename adjlist_t::mapped_type; auto data = this->_adj[u].get(v, T {}); this->_succ[u][v] = data; // this->_prev[v][u] = data; this->_num_of_edges += 1; } template auto add_edge(const Node& u, const Node& v, const T& data) { assert(this->_node.contains(u)); assert(this->_node.contains(v)); this->_succ[u][v] = data; this->_num_of_edges += 1; } template auto add_edges_from(const C1& edges, const C2& data) { auto N = edges.size(); for (auto i = 0; i != N; ++i) { const auto& e = edges[i]; this->add_edge(e.first, e.second, data[i]); } } auto has_successor(const Node& u, const Node& v) -> bool { return this->_node.contains(u) && this->_succ[u].contains(v); } auto& successors(const Node& n) { return this->_succ[n]; } const auto& successors(const Node& n) const { return this->_succ[n]; } using coro_t = boost::coroutines2::coroutine; using pull_t = typename coro_t::pull_type; auto edges() const -> pull_t { auto func = [&](typename coro_t::push_type& yield) { for (auto&& rslt : this->_nodes_nbrs()) { auto&& n = std::get<0>(rslt); auto&& nbrs = std::get<1>(rslt); for (auto&& nbr : nbrs) { yield(edge_t {Node(n), Node(nbr)}); } } }; return pull_t(func); } // auto edges() { // return OutEdgeView(*this); // } // auto in_edges() { // return InEdgeView(*this); // } auto degree(const Node& n) const { return this->_succ[n].size(); } auto clear() { this->_succ.clear(); // this->_pred.clear() // this->_node.clear(); this->graph.clear(); } auto is_multigraph() { return false; } auto is_directed() { return true; } }; using SimpleDiGraphS = DiGraphS(1)), py::dict>; // template DiGraphS(int ) // -> DiGraphS(1)), py::set>; } // namespace xn