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