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>
82 template<
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());
89 template<
class SawyerGraph,
class BoostGraph>
110 template<
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(); }
138 template<
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(); }
167 template<
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(); }
195 template<
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(); }
233 template<
class Graph>
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();
254 template<
class Graph>
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();
269 template<
class Graph>
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();
290 template<
class Graph>
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();
305 template<
class Graph>
310 size_t get(
size_t vertexId)
const {
313 const size_t& at(
size_t vertexId)
const {
314 return graph_.findVertex(vertexId)->id();
319 template<
class Graph>
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;
364 #if 0 // [Robb P. Matzke 2014-05-23]: Temporarily disabled because it matches too much.
374 template<
class Graph>
375 struct 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;
382 template<
class Graph>
383 struct 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;
390 template<
class Graph>
391 struct 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;
398 template<
class Graph>
399 struct 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;
406 template<
class Graph>
407 struct 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;
414 template<
class Graph>
415 struct 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;
425 template<
class Graph>
426 struct property_map<Graph,
Sawyer::Boost::vertex_value_t> {
431 template<
class Graph>
432 struct property_map<Graph,
Sawyer::Boost::edge_value_t> {
437 template<
class Graph>
438 struct property_map<Graph,
Sawyer::Boost::vertex_id_t> {
443 template<
class Graph>
444 struct property_map<Graph,
Sawyer::Boost::edge_id_t> {
455 template<
class Graph>
456 typename Graph::VertexValue&
461 template<
class Graph>
462 const typename Graph::VertexValue&
467 template<
class Graph>
470 pmap.at(key) = value;
475 template<
class Graph>
476 typename Graph::EdgeValue&
481 template<
class Graph>
482 typename Graph::EdgeValue&
487 template<
class Graph>
490 const typename Graph::EdgeValue &value) {
491 pmap.at(key) = value;
496 template<
class Graph>
502 template<
class Graph>
513 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
514 struct 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;
544 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
545 struct 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;
576 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
577 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
582 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
583 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
592 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
593 std::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>
600 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
601 std::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>
608 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
609 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
614 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
615 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertices_size_type
624 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
625 std::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>
632 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
633 std::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>
640 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
641 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
646 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
647 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edges_size_type
656 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
657 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
660 return graph.
findEdge(edge)->source()->id();
663 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
664 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
667 return graph.
findEdge(edge)->source()->id();
670 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
671 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
674 return graph.
findEdge(edge)->target()->id();
677 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
678 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
681 return graph.
findEdge(edge)->target()->id();
684 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
685 std::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>
694 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
695 std::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>
704 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
705 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
711 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
712 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
722 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
723 std::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>
732 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
733 std::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>
742 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
743 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
749 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
750 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
756 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
757 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
760 return in_degree(vertex) + out_degree(vertex);
763 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
764 typename graph_traits<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::degree_size_type
767 return in_degree(vertex) + out_degree(vertex);
774 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
780 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
786 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
792 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
798 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
804 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
805 typename property_map<const Sawyer::Container::Graph<V, E, VKey, EKey, Alloc>,
Sawyer::Boost::edge_id_t>::const_type
814 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
815 std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
822 return std::make_pair(newEdge->
id(),
true);
825 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
835 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
842 template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
844 remove_edge_if(Predicate predicate,
847 while (edge != graph.
edges().end()) {
848 if (predicate(edge->
id())) {
856 template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
863 while (edge != v->
outEdges().end()) {
864 if (predicate(edge->
id())) {
872 template<
class Predicate,
class V,
class E,
class VKey,
class EKey,
class Alloc>
879 while (edge != v->
inEdges().end()) {
880 if (predicate(edge->
id())) {
888 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
889 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor
894 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
901 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
913 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
914 std::pair<typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::edge_descriptor,
bool>
922 return std::make_pair(newEdge->
id(),
true);
925 template<
class V,
class E,
class VKey,
class EKey,
class Alloc>
926 typename graph_traits<Sawyer::Container::Graph<V, E, VKey, EKey, Alloc> >::vertex_descriptor