Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #ifndef ctrlplane_db_graph_entry_h 6 : #define ctrlplane_db_graph_entry_h 7 : 8 : #include "db/db_entry.h" 9 : #include "db/db_graph_base.h" 10 : 11 : class DBGraph; 12 : class DBGraphEdge; 13 : class DBGraphVertex; 14 : 15 : namespace boost { 16 : template <> 17 : struct is_POD<DBGraphEdge> : public false_type {}; 18 : 19 : template <> 20 : struct is_POD<DBGraphVertex> : public false_type {}; 21 : } 22 : 23 : class DBGraphVertex : public DBEntry { 24 : public: 25 : typedef DBGraphBase::vertex_descriptor Vertex; 26 : typedef DBGraphBase::edge_descriptor Edge; 27 : 28 1062119 : DBGraphVertex() : vertex_id_(NULL), visited_at_(0) { } 29 : 30 : class adjacency_iterator : public boost::iterator_facade< 31 : adjacency_iterator, DBGraphVertex, boost::forward_traversal_tag 32 : > { 33 : public: 34 : adjacency_iterator(); 35 : adjacency_iterator(DBGraph *graph, Vertex vertex); 36 : private: 37 : friend class boost::iterator_core_access; 38 1474658 : void increment() { 39 1474658 : ++iter_; 40 1474658 : } 41 2309296 : bool equal(const adjacency_iterator &rhs) const { 42 2309296 : if (graph_ == NULL) { 43 0 : return (rhs.graph_ == NULL); 44 : } 45 2309296 : if (rhs.graph_ == NULL) { 46 2309296 : return iter_ == end_; 47 : } 48 0 : return iter_ == rhs.iter_; 49 : } 50 : DBGraphVertex &dereference() const; 51 : DBGraph *graph_; 52 : DBGraphBase::adjacency_iterator iter_; 53 : DBGraphBase::adjacency_iterator end_; 54 : }; 55 : 56 : class edge_iterator : public boost::iterator_facade< 57 : edge_iterator, DBGraphEdge, boost::forward_traversal_tag 58 : > { 59 : public: 60 : edge_iterator(); 61 : edge_iterator(DBGraph *graph, DBGraphVertex *vertex); 62 : DBGraphVertex *target() const; 63 : private: 64 : friend class boost::iterator_core_access; 65 741481 : void increment() { 66 741481 : ++iter_; 67 741481 : } 68 930909 : bool equal(const edge_iterator &rhs) const { 69 930909 : if (graph_ == NULL) { 70 0 : return (rhs.graph_ == NULL); 71 : } 72 930909 : if (rhs.graph_ == NULL) { 73 930909 : return iter_ == end_; 74 : } 75 0 : return iter_ == rhs.iter_; 76 : } 77 : DBGraphEdge &dereference() const; 78 : DBGraph *graph_; 79 : DBGraphVertex *vertex_; 80 : DBGraphBase::out_edge_iterator iter_; 81 : DBGraphBase::out_edge_iterator end_; 82 : }; 83 : 84 834638 : adjacency_iterator begin(DBGraph *graph) { 85 834638 : return adjacency_iterator(graph, vertex_id_); 86 : } 87 2309296 : adjacency_iterator end(DBGraph *graph) { 88 2309296 : return adjacency_iterator(); 89 : } 90 : 91 189428 : edge_iterator edge_list_begin(DBGraph *graph) { 92 189428 : return edge_iterator(graph, this); 93 : } 94 930909 : edge_iterator edge_list_end(DBGraph *graph) { 95 930909 : return edge_iterator(); 96 : } 97 : 98 : bool HasAdjacencies(DBGraph *graph) const; 99 : 100 189176 : void set_vertex(const Vertex &vertex_id) { 101 189176 : vertex_id_ = vertex_id; 102 189176 : } 103 : 104 189166 : void VertexInvalidate() { 105 189166 : vertex_id_ = NULL; 106 189166 : } 107 : 108 222 : bool IsVertexValid() { 109 222 : return (vertex_id_ != NULL); 110 : } 111 : 112 891752 : Vertex vertex() const { return vertex_id_; } 113 : 114 535 : void set_visited(uint64_t current_graph_walk_num) { 115 535 : visited_at_ = current_graph_walk_num; 116 535 : } 117 : 118 416 : bool visited(uint64_t current_graph_walk_num) { 119 416 : return (visited_at_ == current_graph_walk_num); 120 : } 121 : 122 : virtual std::string ToString() const = 0; 123 : 124 : private: 125 : Vertex vertex_id_; 126 : uint64_t visited_at_; 127 : }; 128 : 129 : #endif