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