9#ifndef Sawyer_GraphBoost_H
10#define Sawyer_GraphBoost_H
12#include <Sawyer/Graph.h>
13#include <Sawyer/Sawyer.h>
14#include <boost/foreach.hpp>
15#include <boost/graph/graph_traits.hpp>
16#include <boost/graph/properties.hpp>
75template<
class SawyerGraph,
class BoostGraph>
77 bg = BoostGraph(
sg.nVertices());
78 BOOST_FOREACH (
const typename SawyerGraph::Edge &edge,
sg.edges())
79 bg.add_edge(edge.source()->id(), edge.target()->id());
82template<
class SawyerGraph,
class BoostGraph>
103template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
107 using iterator_category = std::bidirectional_iterator_tag;
108 using value_type =
const size_t;
109 using difference_type = std::ptrdiff_t;
110 using pointer =
const size_t*;
111 using reference =
const size_t&;
127 const size_t& operator*()
const {
return base_->
id(); }
131template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
135 using iterator_category = std::bidirectional_iterator_tag;
136 using value_type =
const size_t;
137 using difference_type = std::ptrdiff_t;
138 using pointer =
const size_t*;
139 using reference =
const size_t&;
156 const size_t& operator*()
const {
return base_->
id(); }
160template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
164 using iterator_category = std::bidirectional_iterator_tag;
165 using value_type =
const size_t;
166 using difference_type = std::ptrdiff_t;
167 using pointer =
const size_t*;
168 using reference =
const size_t&;
182 bool operator==(
const EdgeOuterIterator &other)
const {
return base_ == other.base_; }
183 bool operator!=(
const EdgeOuterIterator &other)
const {
return base_ != other.base_; }
184 const size_t& operator*()
const {
return base_->
id(); }
188template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
192 using iterator_category = std::bidirectional_iterator_tag;
193 using value_type =
const size_t;
194 using difference_type = std::ptrdiff_t;
195 using pointer =
const size_t*;
196 using reference =
const size_t&;
213 const size_t& operator*()
const {
return base_->
id(); }
230 typedef typename Graph::VertexValue ValueType;
232 ValueType get(
size_t vertexId)
const {
233 return graph_.findVertex(vertexId)->value();
235 void put(
size_t vertexId,
const ValueType &value) {
236 graph_.findVertex(vertexId)->value() = value;
238 ValueType& at(
size_t vertexId) {
239 return graph_.findVertex(vertexId)->value();
241 const ValueType& at(
size_t vertexId)
const {
242 return graph_.findVertex(vertexId)->value();
251 typedef typename Graph::VertexValue ValueType;
253 ValueType get(
size_t vertexId)
const {
254 return graph_.findVertex(vertexId)->value();
256 const ValueType& at(
size_t vertexId)
const {
257 return graph_.findVertex(vertexId)->value();
266 typedef typename Graph::EdgeValue ValueType;
268 ValueType get(
size_t edgeId)
const {
269 return graph_.findEdge(edgeId)->value();
271 void put(
size_t edgeId,
const ValueType &value) {
272 graph_.findEdge(edgeId)->value() = value;
274 ValueType& at(
size_t edgeId) {
275 return graph_.findEdge(edgeId)->value();
277 const ValueType& at(
size_t edgeId)
const {
278 return graph_.findEdge(edgeId)->value();
287 typedef typename Graph::EdgeValue ValueType;
289 ValueType get(
size_t edgeId)
const {
290 return graph_.findEdge(edgeId)->value();
292 const ValueType& at(
size_t edgeId)
const {
293 return graph_.findEdge(edgeId)->value();
303 size_t get(
size_t vertexId)
const {
306 const size_t& at(
size_t vertexId)
const {
307 return graph_.findVertex(vertexId)->id();
317 size_t get(
size_t edgeId)
const {
320 const size_t& at(
size_t edgeId)
const {
321 return graph_.findEdge(edgeId)->id();
328 typedef boost::vertex_property_tag kind;
331 typedef boost::edge_property_tag kind;
335 typedef boost::vertex_property_tag kind;
339 typedef boost::edge_property_tag kind;
368struct property_traits<
Sawyer::Boost::VertexPropertyMap<Graph> > {
369 typedef typename Graph::VertexValue value_type;
370 typedef typename Graph::VertexValue &reference;
371 typedef size_t key_type;
372 typedef boost::read_write_property_map_tag category;
376struct property_traits<
Sawyer::Boost::ConstVertexPropertyMap<Graph> > {
377 typedef typename Graph::VertexValue value_type;
378 typedef typename Graph::VertexValue
const &reference;
379 typedef size_t key_type;
380 typedef boost::readable_property_map_tag category;
384struct property_traits<
Sawyer::Boost::EdgePropertyMap<Graph> > {
385 typedef typename Graph::EdgeValue value_type;
386 typedef typename Graph::EdgeValue &reference;
387 typedef size_t key_type;
388 typedef boost::read_write_property_map_tag category;
392struct property_traits<
Sawyer::Boost::ConstEdgePropertyMap<Graph> > {
393 typedef typename Graph::EdgeValue value_type;
394 typedef typename Graph::EdgeValue
const &reference;
395 typedef size_t key_type;
396 typedef boost::readable_property_map_tag category;
400struct property_traits<
Sawyer::Boost::ConstVertexIdPropertyMap<Graph> > {
401 typedef size_t value_type;
402 typedef const size_t &reference;
403 typedef size_t key_type;
404 typedef boost::readable_property_map_tag category;
408struct property_traits<
Sawyer::Boost::ConstEdgeIdPropertyMap<Graph> > {
409 typedef size_t value_type;
410 typedef const size_t &reference;
411 typedef size_t key_type;
412 typedef boost::readable_property_map_tag category;
419struct property_map<Graph,
Sawyer::Boost::vertex_value_t> {
425struct property_map<Graph,
Sawyer::Boost::edge_value_t> {
431struct property_map<Graph,
Sawyer::Boost::vertex_id_t> {
437struct property_map<Graph,
Sawyer::Boost::edge_id_t> {
449typename Graph::VertexValue&
455const typename Graph::VertexValue&
463 pmap.at(key) = value;
469typename Graph::EdgeValue&
475typename Graph::EdgeValue&
483 const typename Graph::EdgeValue &value) {
484 pmap.at(key) = value;
506template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
507struct graph_traits<
Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> > {
508 typedef bidirectional_graph_tag traversal_category;
511 typedef size_t vertex_descriptor;
512 typedef size_t edge_descriptor;
513 typedef directed_tag directed_category;
514 typedef allow_parallel_edge_tag edge_parallel_category;
515 static size_t null_vertex() {
return (
size_t)(-1); }
519 typedef size_t vertices_size_type;
523 typedef size_t edges_size_type;
527 typedef size_t degree_size_type;
537template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
538struct graph_traits<const
Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> > {
539 typedef bidirectional_graph_tag traversal_category;
542 typedef size_t vertex_descriptor;
543 typedef size_t edge_descriptor;
544 typedef directed_tag directed_category;
545 typedef allow_parallel_edge_tag edge_parallel_category;
549 typedef size_t vertices_size_type;
553 typedef size_t edges_size_type;
557 typedef size_t degree_size_type;
569template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
570typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
575template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
576typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
585template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
586std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator,
587 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator>
593template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
594std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator,
595 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator>
601template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
602typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
607template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
608typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
617template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
618std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator,
619 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator>
625template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
626std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator,
627 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator>
633template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
634typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
639template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
640typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
649template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
650typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
656template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
657typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
663template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
664typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
670template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
671typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
677template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
678std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator,
679 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator>
687template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
688std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator,
689 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator>
697template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
698typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
704template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
705typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
715template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
716std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator,
717 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator>
725template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
726std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator,
727 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator>
735template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
736typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
742template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
743typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
749template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
750typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
753 return in_degree(vertex) + out_degree(vertex);
756template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
757typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
760 return in_degree(vertex) + out_degree(vertex);
767template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
773template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
779template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
785template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
791template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
797template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
807template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
808std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
815 return std::make_pair(newEdge->
id(),
true);
818template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
828template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
835template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
837remove_edge_if(Predicate predicate,
840 while (edge != graph.
edges().end()) {
841 if (predicate(edge->
id())) {
849template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
856 while (edge != v->
outEdges().end()) {
857 if (predicate(edge->
id())) {
865template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
872 while (edge != v->
inEdges().end()) {
873 if (predicate(edge->
id())) {
881template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
882typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
887template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
894template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
906template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
907std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
915 return std::make_pair(newEdge->
id(),
true);
918template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
919typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
Bidirectional edge node iterator.
Bidirectional vertex node iterator.
Bidirectional edge node iterator.
const VertexIterator & target()
Target vertex.
const VertexIterator & source()
Source vertex.
const size_t & id() const
Unique edge ID number.
Bidirectional vertex node iterator.
boost::iterator_range< EdgeIterator > inEdges()
List of incoming edges.
boost::iterator_range< EdgeIterator > outEdges()
List of outgoing edges.
size_t nInEdges() const
Number of incoming edges.
size_t nOutEdges() const
Number of outgoing edges.
const size_t & id() const
Unique vertex ID number.
Graph containing user-defined vertices and edges.
E EdgeValue
User-level data associated with edges.
void clearEdges()
Erase all edges, but leave all vertices.
EdgeIterator insertEdge(const VertexIterator &sourceVertex, const VertexIterator &targetVertex, const EdgeValue &value=EdgeValue())
Insert a new edge.
boost::iterator_range< EdgeIterator > edges()
Iterators for all edges.
EdgeIterator findEdge(size_t id)
Finds the edge with specified ID number.
VertexIterator findVertex(size_t id)
Finds the vertex with specified ID number.
size_t nVertices() const
Total number of vertices.
EdgeIterator eraseEdge(const EdgeIterator &edge)
Erases an edge.
boost::iterator_range< VertexIterator > vertices()
Iterators for all vertices.
VertexIterator insertVertex(const VertexValue &value=VertexValue())
Insert a new vertex.
V VertexValue
User-level data associated with vertices.
void eraseEdges(const VertexIterator &source, const VertexIterator &target)
Erases all edges connecting two vertices.
size_t nEdges() const
Total number of edges.
VertexIterator eraseVertex(const VertexIterator &vertex)
Erases a vertex and its incident edges.
void sawyerGraphToBoostGraph(const SawyerGraph &sg, BoostGraph &bg)
Convert a Sawyer graph to a Boost graph.
bool get(const Word *words, size_t idx)
Return a single bit.
This namespace contains template functions that operate on the ROSE AST.