43 #ifndef MAP_GRAPH_HPP_
44 #define MAP_GRAPH_HPP_
46 #include "Vector/map_vector.hpp"
48 #include "metis_util.hpp"
59 typedef boost::fusion::vector<> type;
65 static const unsigned int max_prop = 0;
68 template<
typename V,
typename E,
template<
typename,
typename,
typename,
unsigned int>
class VertexList,
template<
typename,
typename,
typename,
unsigned int>
class EdgeList,
typename Memory,
82 typedef boost::fusion::vector<size_t, size_t> type;
88 static const unsigned int vid = 0;
89 static const unsigned int eid = 1;
90 static const unsigned int max_prop = 2;
94 inline void setvid(
size_t vid_)
96 boost::fusion::at_c<0>(data) = vid_;
99 inline void seteid(
size_t eid_)
101 boost::fusion::at_c<1>(data) = eid_;
148 template<
typename Graph>
180 if (ek.pos_e + 1 >=
g.getNChilds(ek.pos))
187 while (ek.pos <
g.getNVertex() &&
g.getNChilds(ek.pos) == 0)
211 if (ek.pos <
g.getNVertex())
251 return g.getChild(ek.pos, ek.pos_e);
282 template<
typename V,
typename E = no_edge,
template<
typename,
typename,
typename,
unsigned int>
class VertexList =
openfpm::vector,
290 VertexList<V, Memory, grow_p, openfpm::vect_isel<V>::value>
v;
293 VertexList<size_t, Memory, grow_p, openfpm::vect_isel<size_t>::value>
v_l;
296 EdgeList<E, Memory, grow_p, openfpm::vect_isel<E>::value>
e;
299 EdgeList<e_map, Memory, grow_p, openfpm::vect_isel<e_map>::value>
e_l;
302 EdgeList<E, Memory, grow_p, openfpm::vect_isel<E>::value>
e_invalid;
313 template<
typename CheckPolicy = NoCheck>
inline size_t addEdge_(
size_t v1,
size_t v2)
316 if (CheckPolicy::valid(v1,
v.size()) ==
false)
318 if (CheckPolicy::valid(v2,
v.size()) ==
false)
322 size_t id_x_end =
v_l.template get<0>(v1);
327 if (v1 >=
v.size() || v2 >=
v.size())
329 std::cerr <<
"Error graph: trying to create an edge between vertex that does not exist" <<
"\n";
334 for (
size_t s = 0; s < id_x_end; s++)
336 if (
e_l.template get<e_map::vid>(v1 *
v_slot + s) == v2)
338 std::cerr <<
"Error graph: the edge already exist" <<
"\n";
356 for (
size_t i = 0; i <
v.size(); i++)
360 g_new.
v.set(i,
v, 2 * i);
372 if (id_x_end >=
e_l.size())
380 e_l.template get<e_map::vid>(v1 *
v_slot + id_x_end) = v2;
381 e_l.template get<e_map::eid>(v1 *
v_slot + id_x_end) =
e.size();
384 e.resize(
e.size() + 1);
387 ++
v_l.template get<0>(v1);
402 typedef typename VertexList<V, Memory, grow_p, openfpm::vect_isel<V>::value>::container
V_container;
405 typedef typename EdgeList<E, Memory, grow_p, openfpm::vect_isel<E>::value>::container
E_container;
421 dup.
v.swap(
v.duplicate());
422 dup.
v_l.swap(
v_l.duplicate());
423 dup.
e.swap(
e.duplicate());
424 dup.
e_l.swap(
e_l.duplicate());
461 v_l.resize(n_vertex);
487 template<
unsigned int i>
auto vertex_p(
size_t id) -> decltype(
v.template get<i>(
id) )
489 return v.template get<i>(id);
500 return v.template get<i>(id);
508 auto vertex(
size_t id) -> decltype(
v.get(
id) )
522 return v.get(
id.
get(0));
534 return v.get(
id.
get());
542 auto vertex(
size_t id)
const ->
const decltype(
v.get(
id) )
556 return v.get(
id.
get(0));
568 return v.get(
id.
get());
579 return e.template get<i>(id);
588 template<
unsigned int i>
auto edge_p(
size_t id) -> decltype (
e.template get<i>(
id) )
590 return e.template get<i>(id);
600 return e.get(
id.
get(0));
610 return e.get(
e_l.template get<e_map::eid>(ek.pos *
v_slot + ek.pos_e));
620 auto edge(
size_t id) -> decltype (
e.get(
id) )
632 return e.get(
id.
get(0));
642 return e.get(
e_l.template get<e_map::eid>(ek.pos *
v_slot + ek.pos_e));
652 auto edge(
size_t id)
const ->
const decltype (
e.get(
id) )
669 return v_l.template get<0>(c);
683 return v_l.template get<0>(c.get());
697 std::cerr <<
"Error node " <<
v <<
" does not have edge " << v_e <<
"\n";
700 if (
e.size() >=
e_l.template get<e_map::eid>(
v * v_slot + v_e))
702 std::cerr <<
"Error edge " <<
v <<
" does not have edge "<< v_e <<
"\n";
707 return e.get(
e_l.template get<e_map::eid>(
v * v_slot + v_e));
722 if (i >=
v_l.template get<0>(v) )
724 std::cerr <<
"Error " << __FILE__ <<
" line: " << __LINE__ <<
" vertex " << v <<
" does not have edge " << i <<
"\n";
727 if (
e.size() <=
e_l.template get<e_map::eid>(v *
v_slot + i))
729 std::cerr <<
"Error " << __FILE__ <<
" " << __LINE__ <<
" edge " << v <<
" does not have edge "<< i <<
"\n";
734 return e_l.template get<e_map::vid>(v *
v_slot + i);
749 if (i >=
v_l.template get<0>(v.get()) )
751 std::cerr <<
"Error " << __FILE__ <<
" line: " << __LINE__ <<
" vertex " << v.get() <<
" does not have edge " << i <<
"\n";
754 if (
e.size() <=
e_l.template get<e_map::eid>(v.get() *
v_slot + i))
756 std::cerr <<
"Error " << __FILE__ <<
" " << __LINE__ <<
" edge " << v.get() <<
" does not have edge "<< i <<
"\n";
761 return e_l.template get<e_map::vid>(v.get() *
v_slot + i);
806 template<
typename CheckPolicy = NoCheck>
inline auto addEdge(
size_t v1,
size_t v2,
const E & ed) -> decltype(
e.get(0))
808 long int id_x_end = addEdge_<CheckPolicy>(v1, v2);
811 if (id_x_end == NO_EDGE)
817 return e.get(id_x_end);
829 template<
typename CheckPolicy = NoCheck>
inline auto addEdge(
size_t v1,
size_t v2) -> decltype(
e.get(0))
832 long int id_x_end = addEdge_<CheckPolicy>(v1, v2);
834 if (id_x_end == NO_EDGE)
838 return e.get(id_x_end);
869 return v.getIterator();
932 template<
typename V,
typename E>
edge_iterator< Graph_CSR< V, E, VertexList, EdgeList, Memory > > getEdgeIterator() const
Get the vertex iterator.
auto vertex_p(grid_key_dx< 1 > id) -> decltype(v.template get< i >(id))
Access the vertex.
auto getVertexIterator() const -> decltype(v.getIterator())
Get the vertex iterator.
void swap(Graph_CSR< V, E, VertexList, EdgeList > &g)
swap the memory of g with this graph
Graph_CSR(Graph_CSR< V, E, VertexList, EdgeList, Memory > &&g)
Copy constructor.
EdgeList< E, Memory, grow_p, openfpm::vect_isel< E >::value > e
Structure that store the edge properties.
Grow policy define how the vector should grow every time we exceed the size.
auto edge(grid_key_dx< 1 > id) -> decltype(e.get(id.get(0)))
Access the edge.
grid_key_dx is the key to access any element in the grid
auto edge(size_t id) const -> const decltype(e.get(id))
operator to access the edge
auto edge(edge_key ek) -> decltype(e.get(0))
operator to access the edge
auto edge_p(grid_key_dx< 1 > id) -> decltype(e.template get< i >(id))
Access the edge.
auto getChildEdge(size_t v, size_t v_e) -> decltype(e.get(0))
Get the vertex edge.
VertexList< size_t, Memory, grow_p, openfpm::vect_isel< size_t >::value > v_l
Structure that store the number of adjacent vertex in e_l for each vertex.
EdgeList< e_map, Memory, grow_p, openfpm::vect_isel< e_map >::value > e_l
Structure that store for each vertex the adjacent the vertex id and edge id (for property into e) ...
auto vertex(size_t id) const -> const decltype(v.get(id))
Function to access the vertexes.
bool isNext()
Check if there is the next element.
edge_iterator & operator++()
Get the next element.
edge_iterator(const Graph &g)
Constructor.
auto vertex(size_t id) -> decltype(v.get(id))
Function to access the vertexes.
VertexList< V, Memory, grow_p, openfpm::vect_isel< V >::value > v
Structure that store the vertex properties.
auto edge(edge_key ek) const -> const decltype(e.get(0))
operator to access the edge
size_t source()
Return the source node.
auto edge(grid_key_dx< 1 > id) const -> const decltype(e.get(id.get(0)))
Access the edge.
auto addEdge(size_t v1, size_t v2, const E &ed) -> decltype(e.get(0))
add edge on the graph
Graph_CSR(size_t n_vertex, size_t n_slot)
Constructor.
size_t getNChilds(typename VertexList< V, Memory, grow_p, openfpm::vect_isel< V >::value >::iterator_key &c)
Return the number of childs of a vertex.
size_t target()
Return the target node.
EdgeList< E, Memory, grow_p, openfpm::vect_isel< E >::value >::container E_container
Object container for the edge, for example can be encap<...> (map_grid or openfpm::vector) ...
auto addEdge(size_t v1, size_t v2) -> decltype(e.get(0))
add edge on the graph
auto vertex(grid_key_dx< 1 > id) -> decltype(v.get(id.get(0)))
operator to access the vertex
size_t & target_t()
Return the target of this edge.
auto edge_p(size_t id) -> decltype(e.template get< i >(id))
Access the edge.
auto vertex(openfpm::vector_key_iterator id) -> decltype(v.get(0))
operator to access the vertex
void addVertex(const V &vrt)
add vertex
auto vertex(grid_key_dx< 1 > id) const -> const decltype(v.get(id.get(0)))
operator to access the vertex
Graph_CSR< V, E, VertexList, EdgeList, Memory, grow_p > duplicate()
It duplicate the graph.
auto vertex_p(size_t id) -> decltype(v.template get< i >(id))
operator to access the vertex
auto edge(size_t id) -> decltype(e.get(id))
operator to access the edge
Structure that store a graph in CSR format or basically in compressed adjacency matrix format...
inter_memc< T >::type type
for each element in the vector interleave memory_c
EdgeList< E, Memory, grow_p, openfpm::vect_isel< E >::value > e_invalid
invalid edge element, when a function try to create an in valid edge this object is returned ...
void addVertex()
add an empty vertex
size_t getNEdge() const
Return the number of edges.
size_t getNChilds(size_t c) const
Return the number of childs of a vertex.
size_t getChild(typename VertexList< V, Memory, grow_p, openfpm::vect_isel< V >::value >::iterator_key &v, size_t i)
Get the child edge.
Structure used inside GraphCSR an edge.
size_t getNVertex() const
Return the number of the vertex.
size_t v_slot
number of slot per vertex
size_t getChild(size_t v, size_t i) const
Get the child edge.
This class is a container for the memory interface like HeapMemory CudaMemory.
Simplified implementation of Graph_CSR.
Graph_CSR(size_t n_vertex)
Constructor.
size_t addEdge_(size_t v1, size_t v2)
add edge on the graph
size_t & source()
Get the source id of this edge.
Implementation of 1-D std::vector like structure.
auto vertex(openfpm::vector_key_iterator id) const -> const decltype(v.get(0))
operator to access the vertex
VertexList< V, Memory, grow_p, openfpm::vect_isel< V >::value >::container V_container
Object container for the vertex, for example can be encap<...> (map_grid or openfpm::vector) ...