00001 // This is brl/bbas/bgrl/bgrl_graph.h 00002 #ifndef bgrl_graph_h_ 00003 #define bgrl_graph_h_ 00004 //: 00005 // \file 00006 // \brief A general graph object 00007 // \author Matt Leotta, (mleotta@lems.brown.edu) 00008 // \date March 17, 2004 00009 // 00010 // The graph object maintains pointers to all of the vertices 00011 // in the graph. It also handles the creation and destruction of 00012 // edges between vertices. The graph object also contains search 00013 // iterators to iterate through the vertices along edges using search 00014 // algorithms such as depth-first or breadth-first. 00015 // 00016 // \verbatim 00017 // Modifications 00018 // <none yet> 00019 // \endverbatim 00020 00021 00022 #include <vcl_set.h> 00023 #include <vsl/vsl_binary_io.h> 00024 #include <vbl/vbl_ref_count.h> 00025 #include "bgrl_vertex_sptr.h" 00026 #include "bgrl_edge_sptr.h" 00027 #include "bgrl_graph_sptr.h" 00028 #include "bgrl_search_func_sptr.h" 00029 #include "bgrl_search_func.h" 00030 00031 //: The graph 00032 class bgrl_graph : public vbl_ref_count 00033 { 00034 public: 00035 00036 typedef vcl_set<bgrl_vertex_sptr>::iterator vertex_iterator; 00037 typedef vcl_set<bgrl_edge_sptr>::iterator edge_iterator; 00038 00039 //: Constructor 00040 bgrl_graph(); 00041 00042 //: Copy Constructor 00043 // \note this provides a deep copy of the graph 00044 bgrl_graph(const bgrl_graph& graph); 00045 00046 //: Destructor 00047 virtual ~bgrl_graph(){} 00048 00049 //: Adds a new vertex to the graph 00050 // \retval true if the vertex was added 00051 // \retval false if the vertex could not be added 00052 bool add_vertex(const bgrl_vertex_sptr& vertex); 00053 00054 //: Deletes a vertex in the graph 00055 // \retval true if the vertex was deleted 00056 // \retval false if the vertex was not found in the graph 00057 bool remove_vertex(const bgrl_vertex_sptr& vertex); 00058 00059 //: Add an edge between \p v1 and \p v2 00060 bgrl_edge_sptr add_edge( const bgrl_vertex_sptr& v1, 00061 const bgrl_vertex_sptr& v2, 00062 const bgrl_edge_sptr& model_edge = NULL); 00063 00064 //: Add an edge between \p v1 and \p v2 00065 bool remove_edge( const bgrl_vertex_sptr& v1, const bgrl_vertex_sptr& v2 ); 00066 00067 //: Remove all edges to NULL vertices and vertices not found in this graph 00068 // \retval true if any edges have been purged 00069 // \retval false if all edges were found to be valid 00070 bool purge(); 00071 00072 //: Returns the number of vertices in the graph 00073 int size() const; 00074 00075 //: Return a platform independent string identifying the class 00076 virtual vcl_string is_a() const; 00077 00078 //: Create a copy of the object on the heap. 00079 // The caller is responsible for deletion 00080 virtual bgrl_graph* clone() const; 00081 00082 //: Binary save self to stream. 00083 void b_write(vsl_b_ostream &os) const; 00084 00085 //: Binary load self from stream. 00086 void b_read(vsl_b_istream &is); 00087 00088 //: Return IO version number; 00089 short version() const; 00090 00091 //: Print an ascii summary to the stream 00092 void print_summary(vcl_ostream &os) const; 00093 00094 private: 00095 //: The vector of vertices 00096 vcl_set<bgrl_vertex_sptr> vertices_; 00097 00098 00099 public: 00100 class iterator 00101 { 00102 public: 00103 //: Constructor 00104 iterator( bgrl_graph* graph, bgrl_search_func_sptr func ); 00105 00106 //: Constructor - for end iterator 00107 iterator( bgrl_graph* graph ); 00108 00109 //: Destructor 00110 virtual ~iterator() {} 00111 00112 bgrl_graph* graph() const { return graph_; } 00113 00114 //: Pre-Increment 00115 iterator& operator++ (); 00116 //: Post-Increment 00117 iterator operator++ (int); 00118 00119 //: Dereference 00120 bgrl_vertex_sptr operator -> () const; 00121 //: Dereference 00122 bgrl_vertex_sptr operator * () const; 00123 00124 //: Equality comparison 00125 bool operator == (const iterator& rhs) const; 00126 00127 //: Inequality comparison 00128 bool operator != (const iterator& rhs) const; 00129 00130 protected: 00131 bgrl_graph* graph_; 00132 bgrl_search_func_sptr search_func_; 00133 00134 bool use_internal_; 00135 vertex_iterator internal_; 00136 }; 00137 00138 friend class bgrl_graph::iterator; 00139 00140 //: Depth first search begin iterator 00141 iterator begin(const bgrl_search_func_sptr& func = NULL) { return iterator(this, func); } 00142 //: Depth first search end iterator 00143 iterator end() { return iterator(this); } 00144 }; 00145 00146 00147 //: Binary save bgrl_graph to stream. 00148 void vsl_b_write(vsl_b_ostream &os, const bgrl_graph* g); 00149 00150 //: Binary load bgrl_graph from stream. 00151 void vsl_b_read(vsl_b_istream &is, bgrl_graph* &g); 00152 00153 //: Print an ASCII summary to the stream 00154 void vsl_print_summary(vcl_ostream &os, const bgrl_graph* g); 00155 00156 00157 #endif // bgrl_graph_h_
1.7.5.1