00001 // This is brl/bbas/bgrl/bgrl_vertex.h 00002 #ifndef bgrl_vertex_h_ 00003 #define bgrl_vertex_h_ 00004 //: 00005 // \file 00006 // \brief A vertex in a graph 00007 // \author Matt Leotta, (mleotta@lems.brown.edu) 00008 // \date March 17, 2004 00009 // 00010 // The vertex contains sets of incoming and outgoing 00011 // edges to other vertices in the graph 00012 // 00013 // \verbatim 00014 // Modifications 00015 // \endverbatim 00016 00017 #include <bgrl/bgrl_edge_sptr.h> 00018 #include <vbl/vbl_ref_count.h> 00019 #include <vsl/vsl_binary_io.h> 00020 #include <vcl_set.h> 00021 #include <vcl_string.h> 00022 #include <vcl_iosfwd.h> 00023 00024 #include "bgrl_vertex_sptr.h" 00025 00026 // forward declare the edge 00027 class bgrl_edge; 00028 00029 //: A vertex in a graph 00030 class bgrl_vertex : public vbl_ref_count 00031 { 00032 public: 00033 00034 typedef vcl_set<bgrl_edge_sptr>::iterator edge_iterator; 00035 friend class bgrl_graph; 00036 00037 //: Constructor 00038 bgrl_vertex(); 00039 00040 //: Copy Constructor 00041 bgrl_vertex(const bgrl_vertex& vertex); 00042 00043 //: Destructor 00044 virtual ~bgrl_vertex(){} 00045 00046 //: Returns an iterator to the beginning of the set of outgoing edges 00047 edge_iterator begin(); 00048 00049 //: Returns an iterator to the end of the list of outgoing edges 00050 edge_iterator end(); 00051 00052 //: Returns the total number of edges at this vertex 00053 int degree() const { return this->in_degree() + this->out_degree(); } 00054 00055 //: Returns the number of incoming edges to this vertex 00056 unsigned int in_degree() const { return in_edges_.size(); } 00057 00058 //: Returns the number of outgoing edges to this vertex 00059 unsigned int out_degree() const { return out_edges_.size(); } 00060 00061 //: Return a platform independent string identifying the class 00062 virtual vcl_string is_a() const; 00063 00064 //: Create a copy of the object on the heap. 00065 // The caller is responsible for deletion 00066 virtual bgrl_vertex* clone() const; 00067 00068 //: Binary save self to stream. 00069 void b_write(vsl_b_ostream &os) const; 00070 00071 //: Binary load self from stream. 00072 void b_read(vsl_b_istream &is); 00073 00074 //: Return IO version number; 00075 short version() const; 00076 00077 //: Print an ascii summary to the stream 00078 void print_summary(vcl_ostream &os) const; 00079 00080 protected: 00081 //: Create an outgoing edge to \p vertex 00082 // \return a smart pointer to the edge if the vertex was added successfully 00083 // or a NULL smart pointer if the edge is not valid or already exists 00084 bgrl_edge_sptr add_edge_to( const bgrl_vertex_sptr& vertex, 00085 const bgrl_edge_sptr& model_edge ); 00086 00087 //: Remove the outgoing edge to \p vertex 00088 // \retval true if the edge was removed successfully 00089 // \retval false if the edge was not found 00090 bool remove_edge_to(const bgrl_vertex_sptr& vertex); 00091 00092 //: Strip all of the edges from this vertex 00093 // This also removes edges to and from this vertex in neighboring vertices 00094 void strip(); 00095 00096 //: Remove any edges to or from NULL vertices 00097 // \retval true if any edges were removed 00098 // \retval false if all edges are valid 00099 bool purge(); 00100 00101 00102 //: The pointers to outgoing edges 00103 vcl_set<bgrl_edge_sptr> out_edges_; 00104 00105 //: The pointers to incoming edges 00106 vcl_set<bgrl_edge_sptr> in_edges_; 00107 }; 00108 00109 00110 //: Allows derived class to be loaded by base-class pointer 00111 // A loader object exists which is invoked by calls 00112 // of the form "vsl_b_read(os,base_ptr)". This loads derived class 00113 // objects from the disk, places them on the heap and 00114 // returns a base class pointer. 00115 // In order to work the loader object requires 00116 // an instance of each derived class that might be 00117 // found. This function gives the model class to 00118 // the appropriate loader. 00119 void vsl_add_to_binary_loader(const bgrl_vertex& v); 00120 00121 //: Print an ASCII summary to the stream 00122 void vsl_print_summary(vcl_ostream &os, const bgrl_vertex* v); 00123 00124 #endif // bgrl_vertex_h_