16#ifndef Sawyer_GraphBoost_H
17#define Sawyer_GraphBoost_H
19#include <Sawyer/Graph.h>
20#include <Sawyer/Sawyer.h>
21#include <boost/foreach.hpp>
22#include <boost/graph/graph_traits.hpp>
23#include <boost/graph/properties.hpp>
82template<
class SawyerGraph,
class BoostGraph>
84 bg = BoostGraph(
sg.nVertices());
85 BOOST_FOREACH (
const typename SawyerGraph::Edge &edge,
sg.edges())
86 bg.add_edge(edge.source()->id(), edge.target()->id());
89template<
class SawyerGraph,
class BoostGraph>
110template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
114 using iterator_category = std::bidirectional_iterator_tag;
115 using value_type =
const size_t;
116 using difference_type = std::ptrdiff_t;
117 using pointer =
const size_t*;
118 using reference =
const size_t&;
134 const size_t& operator*()
const {
return base_->
id(); }
138template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
142 using iterator_category = std::bidirectional_iterator_tag;
143 using value_type =
const size_t;
144 using difference_type = std::ptrdiff_t;
145 using pointer =
const size_t*;
146 using reference =
const size_t&;
163 const size_t& operator*()
const {
return base_->
id(); }
167template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
171 using iterator_category = std::bidirectional_iterator_tag;
172 using value_type =
const size_t;
173 using difference_type = std::ptrdiff_t;
174 using pointer =
const size_t*;
175 using reference =
const size_t&;
189 bool operator==(
const EdgeOuterIterator &other)
const {
return base_ == other.base_; }
190 bool operator!=(
const EdgeOuterIterator &other)
const {
return base_ != other.base_; }
191 const size_t& operator*()
const {
return base_->
id(); }
195template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
199 using iterator_category = std::bidirectional_iterator_tag;
200 using value_type =
const size_t;
201 using difference_type = std::ptrdiff_t;
202 using pointer =
const size_t*;
203 using reference =
const size_t&;
220 const size_t& operator*()
const {
return base_->
id(); }
237 typedef typename Graph::VertexValue ValueType;
239 ValueType get(
size_t vertexId)
const {
240 return graph_.findVertex(vertexId)->value();
242 void put(
size_t vertexId,
const ValueType &value) {
243 graph_.findVertex(vertexId)->value() = value;
245 ValueType& at(
size_t vertexId) {
246 return graph_.findVertex(vertexId)->value();
248 const ValueType& at(
size_t vertexId)
const {
249 return graph_.findVertex(vertexId)->value();
258 typedef typename Graph::VertexValue ValueType;
260 ValueType get(
size_t vertexId)
const {
261 return graph_.findVertex(vertexId)->value();
263 const ValueType& at(
size_t vertexId)
const {
264 return graph_.findVertex(vertexId)->value();
273 typedef typename Graph::EdgeValue ValueType;
275 ValueType get(
size_t edgeId)
const {
276 return graph_.findEdge(edgeId)->value();
278 void put(
size_t edgeId,
const ValueType &value) {
279 graph_.findEdge(edgeId)->value() = value;
281 ValueType& at(
size_t edgeId) {
282 return graph_.findEdge(edgeId)->value();
284 const ValueType& at(
size_t edgeId)
const {
285 return graph_.findEdge(edgeId)->value();
294 typedef typename Graph::EdgeValue ValueType;
296 ValueType get(
size_t edgeId)
const {
297 return graph_.findEdge(edgeId)->value();
299 const ValueType& at(
size_t edgeId)
const {
300 return graph_.findEdge(edgeId)->value();
310 size_t get(
size_t vertexId)
const {
313 const size_t& at(
size_t vertexId)
const {
314 return graph_.findVertex(vertexId)->id();
324 size_t get(
size_t edgeId)
const {
327 const size_t& at(
size_t edgeId)
const {
328 return graph_.findEdge(edgeId)->id();
335 typedef boost::vertex_property_tag kind;
338 typedef boost::edge_property_tag kind;
342 typedef boost::vertex_property_tag kind;
346 typedef boost::edge_property_tag kind;
375struct property_traits<
Sawyer::Boost::VertexPropertyMap<Graph> > {
376 typedef typename Graph::VertexValue value_type;
377 typedef typename Graph::VertexValue &reference;
378 typedef size_t key_type;
379 typedef boost::read_write_property_map_tag category;
383struct property_traits<
Sawyer::Boost::ConstVertexPropertyMap<Graph> > {
384 typedef typename Graph::VertexValue value_type;
385 typedef typename Graph::VertexValue
const &reference;
386 typedef size_t key_type;
387 typedef boost::readable_property_map_tag category;
391struct property_traits<
Sawyer::Boost::EdgePropertyMap<Graph> > {
392 typedef typename Graph::EdgeValue value_type;
393 typedef typename Graph::EdgeValue &reference;
394 typedef size_t key_type;
395 typedef boost::read_write_property_map_tag category;
399struct property_traits<
Sawyer::Boost::ConstEdgePropertyMap<Graph> > {
400 typedef typename Graph::EdgeValue value_type;
401 typedef typename Graph::EdgeValue
const &reference;
402 typedef size_t key_type;
403 typedef boost::readable_property_map_tag category;
407struct property_traits<
Sawyer::Boost::ConstVertexIdPropertyMap<Graph> > {
408 typedef size_t value_type;
409 typedef const size_t &reference;
410 typedef size_t key_type;
411 typedef boost::readable_property_map_tag category;
415struct property_traits<
Sawyer::Boost::ConstEdgeIdPropertyMap<Graph> > {
416 typedef size_t value_type;
417 typedef const size_t &reference;
418 typedef size_t key_type;
419 typedef boost::readable_property_map_tag category;
426struct property_map<Graph,
Sawyer::Boost::vertex_value_t> {
432struct property_map<Graph,
Sawyer::Boost::edge_value_t> {
438struct property_map<Graph,
Sawyer::Boost::vertex_id_t> {
444struct property_map<Graph,
Sawyer::Boost::edge_id_t> {
456typename Graph::VertexValue&
462const typename Graph::VertexValue&
470 pmap.at(key) = value;
476typename Graph::EdgeValue&
482typename Graph::EdgeValue&
490 const typename Graph::EdgeValue &value) {
491 pmap.at(key) = value;
513template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
514struct graph_traits<
Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> > {
515 typedef bidirectional_graph_tag traversal_category;
518 typedef size_t vertex_descriptor;
519 typedef size_t edge_descriptor;
520 typedef directed_tag directed_category;
521 typedef allow_parallel_edge_tag edge_parallel_category;
522 static size_t null_vertex() {
return (
size_t)(-1); }
526 typedef size_t vertices_size_type;
530 typedef size_t edges_size_type;
534 typedef size_t degree_size_type;
544template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
545struct graph_traits<const
Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> > {
546 typedef bidirectional_graph_tag traversal_category;
549 typedef size_t vertex_descriptor;
550 typedef size_t edge_descriptor;
551 typedef directed_tag directed_category;
552 typedef allow_parallel_edge_tag edge_parallel_category;
556 typedef size_t vertices_size_type;
560 typedef size_t edges_size_type;
564 typedef size_t degree_size_type;
576template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
577typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
582template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
583typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
592template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
593std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator,
594 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator>
600template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
601std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator,
602 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_iterator>
608template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
609typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
614template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
615typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
624template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
625std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator,
626 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator>
632template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
633std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator,
634 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_iterator>
640template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
641typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
646template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
647typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
656template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
657typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
663template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
664typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
670template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
671typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
677template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
678typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
684template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
685std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator,
686 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator>
694template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
695std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator,
696 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::out_edge_iterator>
704template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
705typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
711template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
712typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
722template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
723std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator,
724 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator>
732template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
733std::pair<typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator,
734 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::in_edge_iterator>
742template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
743typename graph_traits<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<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
756template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
757typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
760 return in_degree(vertex) + out_degree(vertex);
763template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
764typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
767 return in_degree(vertex) + out_degree(vertex);
774template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
780template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
786template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
792template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
798template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
804template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
814template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
815std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
822 return std::make_pair(newEdge->
id(),
true);
825template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
835template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
842template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
844remove_edge_if(Predicate predicate,
847 while (edge != graph.
edges().end()) {
848 if (predicate(edge->
id())) {
856template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
863 while (edge != v->
outEdges().end()) {
864 if (predicate(edge->
id())) {
872template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
879 while (edge != v->
inEdges().end()) {
880 if (predicate(edge->
id())) {
888template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
889typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
894template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
901template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
913template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
914std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
922 return std::make_pair(newEdge->
id(),
true);
925template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
926typename 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.