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