Show IGraph.h syntax highlighted
#ifndef __IGRAPH_H__
#define __IGRAPH_H__ 1
//#include <boost/config.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/property_map.hpp>
#include <algorithm>
#include <osg/Referenced>
namespace osgIntrospectionToolKit
{
template <class OutEdgeListS,
class VertexListS,
class DirectedS,
class VertexProperty,
class EdgeProperty,
class GraphProperty,
class EdgeListS>
class IGraph: public osg::Referenced
{
public:
IGraph()
{ _root = boost::add_vertex(_graph_base); }
protected:
virtual ~IGraph() {}
public:
// ** boost::adjacency_list ** //
typedef typename boost::adjacency_list<OutEdgeListS,
VertexListS,
DirectedS,
VertexProperty,
EdgeProperty,
GraphProperty,
EdgeListS> graph_base;
// ** boost::adjacency_list used typedef ** /
typedef typename graph_base::adjacency_iterator adjacency_iterator;
typedef typename graph_base::inv_adjacency_iterator inv_adjacency_iterator;
typedef typename graph_base::out_edge_iterator out_edge_iterator;
typedef typename graph_base::in_edge_iterator in_edge_iterator;
typedef typename graph_base::vertex_iterator vertex_iterator;
typedef typename graph_base::vertex_descriptor vertex_descriptor;
typedef typename graph_base::edge_iterator edge_iterator;
typedef typename graph_base::edge_descriptor edge_descriptor;
typedef std::pair<adjacency_iterator, adjacency_iterator> adjacency_range;
typedef std::pair<inv_adjacency_iterator, inv_adjacency_iterator> inv_adjacency_range;
typedef std::pair<out_edge_iterator, out_edge_iterator> out_edge_range;
typedef std::pair<in_edge_iterator, in_edge_iterator> in_edge_range;
typedef std::pair<vertex_iterator, vertex_iterator> vertex_range;
typedef std::pair<edge_iterator, edge_iterator> edge_range;
typedef std::vector<vertex_descriptor> VertexList;
public:
// ** ACCESSOR ** //
// ** graph_base GLOBAL FUNCTION
/** return the list of vertex */
inline const vertex_range vertices() const
{ return (boost::vertices(_graph_base)); }
/** return the list of edge */
inline const edge_range edges() const
{ return (boost::edges(_graph_base)); }
/** return the number of vertex */
inline std::size_t num_vertices() const
{ return (boost::num_vertices(_graph_base)); }
/** return the number of edge */
inline std::size_t num_edges() const
{ return (boost::num_edges(_graph_base)); }
/** return the index of the vertex 'vertex' */
inline std::size_t find_vertex(vertex_descriptor vertex) const
{
vertex_range vr = boost::vertices(_graph_base);
return (std::distance(vr.first, std::find(vr.first, vr.second, vertex)));
}
/** return the index of the vertex 'vertex' */
inline bool is_valid_vertex(vertex_descriptor vertex) const
{
vertex_range vr = boost::vertices(_graph_base);
return (std::find(vr.first, vr.second, vertex) != vr.second);
}
// ** NODE FUNCTION
/** return the first and last iterator of the child list */
inline adjacency_range adj_vertices(vertex_descriptor vd) const
{ return (boost::adjacent_vertices(vd, _graph_base)); }
/** return the first and last iterator of the parent list */
inline inv_adjacency_range inv_adj_vertices(vertex_descriptor vd) const
{ return (boost::inv_adjacent_vertices(vd, _graph_base)); }
/** return the first and last iterator of the edge child list */
inline out_edge_range out_edges(vertex_descriptor vd) const
{ return (boost::out_edges(vd, _graph_base)); }
/** return the first and last iterator of the edge parent list */
inline in_edge_range in_edges(vertex_descriptor vd) const
{ return (boost::in_edges(vd, _graph_base)); }
/** return the number of child */
inline std::size_t out_degree(vertex_descriptor vd) const
{ return (boost::out_degree(vd, _graph_base)); }
/** return the number of parent */
inline std::size_t in_degree(vertex_descriptor vd) const
{ return (boost::in_degree(vd, _graph_base)); }
// ** EDGE FUNCTION
/** return the parent of the edge */
inline const vertex_descriptor source(edge_descriptor ed) const
{ return (boost::source(ed, _graph_base)); }
/** return the child of the edge */
inline const vertex_descriptor target(edge_descriptor ed) const
{ return (boost::target(ed, _graph_base)); }
/** return the 'idx' child of 'vd' */
inline vertex_descriptor child(vertex_descriptor vd, unsigned int idx) const
{
adjacency_range ar = boost::adjacent_vertices(vd, _graph_base);
std::advance(ar.first, idx);
return (*ar.first);
}
inline std::pair<edge_descriptor, bool> edge(vertex_descriptor u, vertex_descriptor v)
{ return (boost::edge(u, v, _graph_base)); }
/** return the first parent of vd */
inline vertex_descriptor parent(vertex_descriptor vd) const
{ return (*(boost::inv_adjacent_vertices(vd, _graph_base).first)); }
/** return the 'idx' parent of 'vd' */
inline vertex_descriptor parent(vertex_descriptor vd, unsigned int idx) const
{
inv_adjacency_range iar = boost::inv_adjacent_vertices(vd, _graph_base);
std::advance(iar.first, idx);
return (*iar.first);
}
/** return the index of the child 'child' of 'parent' */
inline std::size_t find_child(vertex_descriptor parent, vertex_descriptor child) const
{
adjacency_range ar = boost::adjacent_vertices(parent, _graph_base);
return (std::distance(ar.first, std::find(ar.first, ar.second, child)));
}
/** return the index of the parent 'parent' of 'child' */
inline std::size_t find_parent(vertex_descriptor child, vertex_descriptor parent) const
{
inv_adjacency_range iar = boost::inv_adjacent_vertices(child, _graph_base);
return (std::distance(iar.first, std::find(iar.first, iar.second, parent)));
}
inline std::size_t getChildList(vertex_descriptor vd, VertexList& childList)
{
std::vector<adjacency_range> stackAR;
stackAR.push_back(adj_vertices(vd));
while (stackAR.size())
{
if (stackAR.back().first != stackAR.back().second)
{
childList.push_back(*stackAR.back().first);
stackAR.push_back(adj_vertices(*(stackAR.back().first++)));
}
else
stackAR.pop_back();
}
return (childList.size());
}
//
// ** MANIPULATOR ** //
//
/** return the new vertex */
inline vertex_descriptor add_vertex()
{ return (boost::add_vertex(_graph_base)); }
/** return the new vertex initialized with the 'vertex' property */
inline vertex_descriptor add_vertex(VertexProperty property)
{ return (boost::add_vertex(property, _graph_base)); }
/** return the new edge */
inline std::pair<edge_descriptor, bool> add_edge(vertex_descriptor u, vertex_descriptor v)
{ return (boost::add_edge(u, v, _graph_base)); }
/** return the new edge initialized with the 'edge' property */
inline std::pair<edge_descriptor, bool> add_edge(vertex_descriptor u, vertex_descriptor v, EdgeProperty property)
{ return (boost::add_edge(u, v, property, _graph_base)); }
/** return the edge 'u' 'v' */
inline void remove_edge(vertex_descriptor u, vertex_descriptor v)
{ boost::add_edge(u, v, _graph_base); }
/** remove the edge 'ed' */
inline void remove_edge(edge_descriptor ed)
{ boost::remove_edge(ed, _graph_base); }
/** remove the edge 'ed' */
inline void remove_edge(out_edge_iterator ed)
{ boost::remove_edge(ed, _graph_base); }
/** remove the vertex 'u' */
inline void remove_vertex(vertex_descriptor u)
{ boost::remove_vertex(u, _graph_base); }
/** clear the vertex's edge */
inline void clear_vertex(vertex_descriptor u)
{ boost::clear_vertex(u, _graph_base); }
inline void clear_and_remove_vertex(vertex_descriptor u)
{ clear_vertex(u); remove_vertex(u); }
/** clear the in edges */
inline void clear_in_edges(vertex_descriptor u)
{ boost::clear_in_edges(u, _graph_base); }
/** clear the out edges */
inline void clear_out_edges(vertex_descriptor u)
{ boost::clear_out_edges(u, _graph_base); }
inline edge_descriptor add_child(vertex_descriptor parent)
{ return (add_edge(parent, add_vertex()).first); }
inline void clear()
{ _graph_base.clear(); _root = add_vertex(); }
const vertex_descriptor root() const
{ return _root; }
const graph_base& tree() const
{ return _graph_base; }
vertex_descriptor root()
{ return _root; }
graph_base& tree()
{ return _graph_base; }
// private:
graph_base _graph_base;
vertex_descriptor _root;
};
}
#endif // ** __IGRAPH_H__ ** //
See more files for this project here