github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/runtime/nr/graph/graph.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  #pragma once
    21  #include "common/address.h"
    22  #include "common/common.h"
    23  #include <boost/graph/adjacency_list.hpp>
    24  
    25  namespace boost {
    26  enum edge_timestamp_t { edge_timestamp };
    27  enum edge_sort_id_t { edge_sort_id };
    28  enum edge_check_id_t { edge_check_id };
    29  
    30  BOOST_INSTALL_PROPERTY(edge, timestamp);
    31  BOOST_INSTALL_PROPERTY(edge, sort_id);
    32  BOOST_INSTALL_PROPERTY(edge, check_id);
    33  } // namespace boost
    34  
    35  namespace neb {
    36  namespace rt {
    37  
    38  class transaction_graph {
    39  public:
    40    typedef boost::property<
    41        boost::edge_weight_t, wei_t,
    42        boost::property<boost::edge_timestamp_t, int64_t,
    43                        boost::property<boost::edge_sort_id_t, int64_t>>>
    44        edge_property_t;
    45  
    46    typedef boost::adjacency_list<
    47        boost::vecS, boost::vecS, boost::bidirectionalS,
    48        boost::property<boost::vertex_name_t, std::string>, edge_property_t>
    49        internal_graph_t;
    50  
    51    typedef typename boost::graph_traits<internal_graph_t>::vertex_descriptor
    52        vertex_descriptor_t;
    53    typedef typename boost::graph_traits<internal_graph_t>::edge_descriptor
    54        edge_descriptor_t;
    55  
    56    typedef typename boost::graph_traits<
    57        transaction_graph::internal_graph_t>::vertex_iterator viterator_t;
    58    typedef typename boost::graph_traits<
    59        transaction_graph::internal_graph_t>::in_edge_iterator ieiterator_t;
    60    typedef typename boost::graph_traits<
    61        transaction_graph::internal_graph_t>::out_edge_iterator oeiterator_t;
    62  
    63    transaction_graph();
    64  
    65    void add_edge(const address_t &from, const address_t &to, wei_t val,
    66                  int64_t ts);
    67  
    68    void write_to_graphviz(const std::string &filename);
    69  
    70    bool read_from_graphviz(const std::string &filename);
    71  
    72    inline internal_graph_t &internal_graph() { return m_graph; }
    73    inline const internal_graph_t &internal_graph() const { return m_graph; }
    74    inline int64_t edge_num() const { return m_edge_index; }
    75    inline int64_t vertex_num() const { return m_cur_max_index; }
    76  
    77  protected:
    78    internal_graph_t m_graph;
    79  
    80    std::unordered_map<int64_t, address_t> m_vertex_to_addr;
    81    std::unordered_map<address_t, int64_t> m_addr_to_vertex;
    82  
    83    uint64_t m_cur_max_index;
    84    uint64_t m_edge_index;
    85  }; // end class transaction_graph
    86  
    87  using transaction_graph_ptr_t = std::unique_ptr<transaction_graph>;
    88  
    89  transaction_graph_ptr_t build_graph_from_internal(
    90      const transaction_graph::internal_graph_t &internal_graph);
    91  
    92  } // namespace rt
    93  } // namespace neb