ROSE  0.11.145.0
Graph.h
1 // WARNING: Changes to this file must be contributed back to Sawyer or else they will
2 // be clobbered by the next update from Sawyer. The Sawyer repository is at
3 // https://github.com/matzke1/sawyer.
4 
5 
6 
7 
8 #ifndef Sawyer_Graph_H
9 #define Sawyer_Graph_H
10 
11 #include <Sawyer/Assert.h>
12 #include <Sawyer/DefaultAllocator.h>
13 #include <Sawyer/Exception.h>
14 #include <Sawyer/IndexedList.h>
15 #include <Sawyer/Map.h>
16 #include <Sawyer/Optional.h> // for Sawyer::Nothing
17 #include <Sawyer/Sawyer.h>
18 #include <boost/range/iterator_range.hpp>
19 #include <boost/serialization/access.hpp>
20 #include <boost/serialization/nvp.hpp>
21 #include <boost/serialization/split_member.hpp>
22 #include <boost/unordered_map.hpp>
23 #include <ostream>
24 #if 1 /*DEBUGGING [Robb Matzke 2014-04-21]*/
25 #include <iomanip>
26 #endif
27 
28 namespace Sawyer {
29 namespace Container {
30 
81 // Special vertex and edge key types.
84 
91 template<class VertexValue>
93 public:
94  GraphVertexNoKey() {}
95  explicit GraphVertexNoKey(const VertexValue&) {}
96 };
97 
104 template<class EdgeValue>
106 public:
107  GraphEdgeNoKey() {}
108  explicit GraphEdgeNoKey(const EdgeValue&) {}
109 };
110 
112 // Special vertex and edge indexing types.
114 
120 template<class VertexOrEdgeKey, class VertexOrEdgeConstIterator>
122 public:
126  void clear() {}
127 
134  void insert(const VertexOrEdgeKey&, const VertexOrEdgeConstIterator&) {}
135 
139  void erase(const VertexOrEdgeKey&) {}
140 
145  Optional<VertexOrEdgeConstIterator> lookup(const VertexOrEdgeKey&) const {
146  return Nothing();
147  }
148 };
149 
155 template<class VertexOrEdgeKey, class VertexOrEdgeConstIterator>
158 public:
162  void clear() {
163  map_.clear();
164  }
165 
169  void insert(const VertexOrEdgeKey &key, const VertexOrEdgeConstIterator &iter) {
170  map_.insert(key, iter); // Unlike std::map, Sawyer's "insert" always inserts
171  }
172 
176  void erase(const VertexOrEdgeKey &key) {
177  map_.erase(key);
178  }
179 
183  Optional<VertexOrEdgeConstIterator> lookup(const VertexOrEdgeKey &key) const {
184  return map_.getOptional(key);
185  }
186 };
187 
195 template<class VertexOrEdgeKey, class VertexOrEdgeConstIterator>
197  typedef boost::unordered_map<VertexOrEdgeKey, VertexOrEdgeConstIterator> Map;
198  Map map_;
199 public:
203  void clear() {
204  map_.clear();
205  }
206 
210  void insert(const VertexOrEdgeKey &key, const VertexOrEdgeConstIterator &iter) {
211  map_[key] = iter;
212  }
213 
217  void erase(const VertexOrEdgeKey &key) {
218  map_.erase(key);
219  }
220 
224  Optional<VertexOrEdgeConstIterator> lookup(const VertexOrEdgeKey &key) const {
225  typename Map::const_iterator found = map_.find(key);
226  if (found == map_.end())
227  return Nothing();
228  return found->second;
229  }
230 };
231 
240 template<class VertexOrEdgeKey, class VertexOrEdgeConstIterator>
244 };
245 
246 // Partial specialization for when there is no vertex index
247 template<class VertexValue, class ConstVertexIterator>
248 struct GraphIndexTraits<GraphVertexNoKey<VertexValue>, ConstVertexIterator> {
249  typedef GraphVoidIndex<GraphVertexNoKey<VertexValue>, ConstVertexIterator> Index;
250 };
251 
252 // Partial specialization for when there is no edge index.
253 template<class EdgeValue, class ConstEdgeIterator>
254 struct GraphIndexTraits<GraphEdgeNoKey<EdgeValue>, ConstEdgeIterator> {
255  typedef GraphVoidIndex<GraphEdgeNoKey<EdgeValue>, ConstEdgeIterator> Index;
256 };
257 
258 // A #define so users that don't understand C++ templates can still get by. See GraphIndexTraits doc for details.
259 // Must be used at global scope.
260 #define SAWYER_GRAPH_INDEXING_SCHEME_1(KEY_TYPE, INDEX_TYPE) \
261  namespace Sawyer { \
262  namespace Container { \
263  template<class VertexOrEdgeConstIterator> \
264  struct GraphIndexTraits<KEY_TYPE, VertexOrEdgeConstIterator> { \
265  typedef INDEX_TYPE<VertexOrEdgeConstIterator> Index; \
266  }; \
267  } \
268  }
269 
270 #define SAWYER_GRAPH_INDEXING_SCHEME_2(KEY_TYPE, INDEX_TYPE) \
271  namespace Sawyer { \
272  namespace Container { \
273  template<class VertexOrEdgeConstIterator> \
274  struct GraphIndexTraits<KEY_TYPE, VertexOrEdgeConstIterator> { \
275  typedef INDEX_TYPE<KEY_TYPE, VertexOrEdgeConstIterator> Index; \
276  }; \
277  } \
278  }
279 
280 
282 // Graph traits
284 
286 template<class G>
287 struct GraphTraits {
289  typedef typename G::EdgeIterator EdgeIterator;
290 
292  typedef typename G::EdgeValueIterator EdgeValueIterator;
293 
295  typedef typename G::VertexIterator VertexIterator;
296 
298  typedef typename G::VertexValueIterator VertexValueIterator;
299 
301  typedef typename G::Vertex Vertex;
302 
304  typedef typename G::Edge Edge;
305 
307  typedef typename G::VertexValue VertexValue;
308 
310  typedef typename G::EdgeValue EdgeValue;
311 };
312 
313 // GraphTraits specialization for const graphs.
314 template<class G>
315 struct GraphTraits<const G> {
316  typedef typename G::ConstEdgeIterator EdgeIterator;
317  typedef typename G::ConstEdgeValueIterator EdgeValueIterator;
318  typedef typename G::ConstVertexIterator VertexIterator;
319  typedef typename G::ConstVertexValueIterator VertexValueIterator;
320  typedef const typename G::Vertex Vertex;
321  typedef const typename G::Edge Edge;
322  typedef const typename G::VertexValue VertexValue;
323  typedef const typename G::EdgeValue EdgeValue;
324 };
325 
622 template<class V = Nothing, class E = Nothing,
623  class VKey = GraphVertexNoKey<V>, class EKey = GraphEdgeNoKey<E>,
624  class Alloc = DefaultAllocator>
625 class Graph {
626 public:
627  typedef V VertexValue;
628  typedef E EdgeValue;
629  typedef VKey VertexKey;
630  typedef EKey EdgeKey;
631  typedef Alloc Allocator;
632  class Vertex;
633  class Edge;
635 private:
636  enum EdgePhase { IN_EDGES=0, OUT_EDGES=1, N_PHASES=2 };
637  typedef IndexedList<Edge, Allocator> EdgeList;
638  typedef IndexedList<Vertex, Allocator> VertexList;
639 
640  template<class T>
641  class VirtualList {
642  VirtualList *next_[N_PHASES];
643  VirtualList *prev_[N_PHASES];
644  T *node_;
645  public:
646  VirtualList() {
647  reset(NULL);
648  }
649 
650  void reset(T* node) {
651  node_ = node;
652  for (size_t i=0; i<N_PHASES; ++i)
653  next_[i] = prev_[i] = this;
654  }
655 
656  bool isHead() const {
657  return node_ == NULL;
658  }
659 
660  bool isSingleton(EdgePhase phase) const {
661  ASSERT_require(phase < N_PHASES);
662  ASSERT_require((next_[phase]==this && prev_[phase]==this) || (next_[phase]!=this && prev_[phase]!=this));
663  return next_[phase]==this;
664  }
665 
666  bool isEmpty(EdgePhase phase) const {
667  ASSERT_require(isHead());
668  ASSERT_require((next_[phase]==this && prev_[phase]==this) || (next_[phase]!=this && prev_[phase]!=this));
669  return next_[phase]==this;
670  }
671 
672  void insert(EdgePhase phase, VirtualList *newNode) { // insert newNode before this
673  ASSERT_require(phase < N_PHASES);
674  ASSERT_not_null(newNode);
675  ASSERT_forbid(newNode->isHead());
676  ASSERT_require(newNode->isSingleton(phase)); // cannot be part of another sublist already
677  prev_[phase]->next_[phase] = newNode;
678  newNode->prev_[phase] = prev_[phase];
679  prev_[phase] = newNode;
680  newNode->next_[phase] = this;
681  }
682 
683  void remove(EdgePhase phase) { // Remove this node from the list
684  ASSERT_require(phase < N_PHASES);
685  ASSERT_forbid(isHead());
686  prev_[phase]->next_[phase] = next_[phase];
687  next_[phase]->prev_[phase] = prev_[phase];
688  next_[phase] = prev_[phase] = this;
689  }
690 
691  VirtualList& next(EdgePhase phase) { return *next_[phase]; }
692  const VirtualList& next(EdgePhase phase) const { return *next_[phase]; }
693  VirtualList& prev(EdgePhase phase) { return *prev_[phase]; }
694  const VirtualList& prev(EdgePhase phase) const { return *prev_[phase]; }
695 
696  T& dereference() { // Return the Edge to which this VirtualList node belongs
697  ASSERT_forbid(isHead()); // list head contains no user-data
698  return *(T*)this; // depends on VirtualList being at the beginning of Edge
699  }
700 
701  const T& dereference() const {
702  ASSERT_forbid(isHead());
703  return *(const T*)this;
704  }
705 
706 #if 1 /*DEBUGGING [Robb Matzke 2014-04-21]*/
707  void dump(EdgePhase phase, std::ostream &o) const {
708  const VirtualList *cur = this;
709  o <<" " <<std::setw(18) <<"Node"
710  <<"\t" <<std::setw(18) <<"This"
711  <<"\t" <<std::setw(18) <<"Next"
712  <<"\t" <<std::setw(18) <<"Prev\n";
713  do {
714  o <<" " <<std::setw(18) <<node_
715  <<"\t" <<std::setw(18) <<cur
716  <<"\t" <<std::setw(18) <<cur->next_[phase]
717  <<"\t" <<std::setw(18) <<cur->prev_[phase] <<"\n";
718  cur = cur->next_[phase];
719  } while (cur!=this && cur->next_[phase]!=cur);
720  }
721 #endif
722  };
723 
725  // Iterators
727 public: // public only for the sake of doxygen
730  template<class Derived, class Value, class Node, class BaseIter, class VList>
732  public:
733  // Five standard iterator types
734  using iterator_category = std::bidirectional_iterator_tag;
735  using value_type = Value;
736  using difference_type = std::ptrdiff_t;
737  using pointer = Value*;
738  using reference = Value&;
739 
740  private:
741  EdgePhase phase_; // IN_EDGES, OUT_EDGES or N_PHASES (graph edges)
742  BaseIter iter_; // EdgeList::NodeIterator or EdgeList::ConstNodeIterator
743  VList *vlist_; // (const) VirtualList<Edge> when phase_ is IN_EDGES or OUT_EDGES
744  protected:
745  friend class Graph;
746  EdgeBaseIterator() {}
747  EdgeBaseIterator(const EdgeBaseIterator &other): phase_(other.phase_), iter_(other.iter_), vlist_(other.vlist_) {}
748  EdgeBaseIterator(const BaseIter &iter): phase_(N_PHASES), iter_(iter), vlist_(NULL) {}
749  EdgeBaseIterator(EdgePhase phase, VList *vlist): phase_(phase), vlist_(vlist) {}
750  template<class BaseIter2> EdgeBaseIterator(EdgePhase phase, const BaseIter2 &iter, VList *vlist)
751  : phase_(phase), iter_(iter), vlist_(vlist) {}
752 
753  Node& dereference() const {
754  return N_PHASES==phase_ ? iter_->value() : vlist_->dereference();
755  }
756 
757  private:
758  Derived* derived() { return static_cast<Derived*>(this); }
759  const Derived* derived() const { return static_cast<const Derived*>(this); }
760 
761  public:
763  Derived& operator=(const Derived &other) {
764  phase_ = other.phase_;
765  iter_ = other.iter_;
766  vlist_ = other.vlist_;
767  return *derived();
768  }
769 
776  Derived& operator++() {
777  if (N_PHASES==phase_) {
778  ++iter_;
779  } else {
780  vlist_ = &vlist_->next(phase_);
781  }
782  return *derived();
783  }
784  Derived operator++(int) {
785  Derived old = *derived();
786  ++*this;
787  return old;
788  }
797  Derived& operator--() {
798  if (N_PHASES==phase_) {
799  --iter_;
800  } else {
801  vlist_ = &vlist_->prev(phase_);
802  }
803  return *derived();
804  }
805  Derived operator--(int) {
806  Derived old = *derived();
807  --*this;
808  return old;
809  }
819  template<class OtherIter>
820  bool operator==(const OtherIter &other) const {
821  Node *a = NULL;
822  if (N_PHASES==phase_) {
823  a = iter_.isAtEnd() ? NULL : &iter_->value();
824  } else {
825  a = vlist_->isHead() ? NULL : &vlist_->dereference();
826  }
827  Node *b = NULL;
828  if (N_PHASES==other.phase_) {
829  b = other.iter_.isAtEnd() ? NULL : &other.iter_->value();
830  } else {
831  b = other.vlist_->isHead() ? NULL : &other.vlist_->dereference();
832  }
833  return a == b;
834  }
835  template<class OtherIter>
836  bool operator!=(const OtherIter &other) const {
837  return !(*this==other);
838  }
842  bool isEmpty() const {
843  if (N_PHASES == phase_) {
844  return iter_.isAtEnd();
845  } else {
846  return vlist_->isHead();
847  }
848  }
849  };
850 
852  template<class Derived, class Value, class Node, class BaseIter>
854  public:
855  // Five standard iterator types
856  using iterator_category = std::bidirectional_iterator_tag;
857  using value_type = Value;
858  using difference_type = std::ptrdiff_t;
859  using pointer = Value*;
860  using reference = Value&;
861 
862  private:
863  BaseIter base_; // VertexList::NodeIterator or VertexList::ConstNodeIterator
864  protected:
865  friend class Graph;
866  VertexBaseIterator() {}
867  VertexBaseIterator(const VertexBaseIterator &other): base_(other.base_) {}
868  VertexBaseIterator(const BaseIter &base): base_(base) {}
869  Node& dereference() const { return base_->value(); }
870  public:
872  Derived& operator=(const Derived &other) { base_ = other.base_; return *derived(); }
873  VertexBaseIterator& operator=(const VertexBaseIterator &other) { base_ = other.base_; return *this; }
874 
881  Derived& operator++() { ++base_; return *derived(); }
882  Derived operator++(int) { Derived old=*derived(); ++*this; return old; }
891  Derived& operator--() { --base_; return *derived(); }
892  Derived operator--(int) { Derived old=*derived(); --*this; return old; }
900  template<class OtherIter> bool operator==(const OtherIter &other) const { return base_ == other.base_; }
901  template<class OtherIter> bool operator!=(const OtherIter &other) const { return base_ != other.base_; }
905  bool isEmpty() const {
906  return base_.base() == NULL;
907  }
908 
909  private:
910  Derived* derived() { return static_cast<Derived*>(this); }
911  const Derived* derived() const { return static_cast<const Derived*>(this); }
912  };
913 
914 public:
921  class EdgeIterator: public EdgeBaseIterator<EdgeIterator, Edge, Edge, typename EdgeList::NodeIterator,
922  VirtualList<Edge> > {
923  typedef EdgeBaseIterator<EdgeIterator, Edge, Edge, typename EdgeList::NodeIterator,
924  VirtualList<Edge> > Super;
925  public:
926  typedef Edge& Reference;
927  typedef Edge* Pointer;
928  EdgeIterator() {}
929  EdgeIterator(const EdgeIterator &other): Super(other) {}
930  EdgeIterator& operator=(const EdgeIterator &other) { this->Super::operator=(other); return *this; }
931  Edge& operator*() const { return this->dereference(); }
932  Edge* operator->() const { return &this->dereference(); }
933  private:
934  friend class Graph;
935  EdgeIterator(const typename EdgeList::NodeIterator &base): Super(base) {}
936  EdgeIterator(EdgePhase phase, VirtualList<Edge> *vlist): Super(phase, vlist) {}
937  };
938 
945  class ConstEdgeIterator: public EdgeBaseIterator<ConstEdgeIterator, const Edge, const Edge,
946  typename EdgeList::ConstNodeIterator,
947  const VirtualList<Edge> > {
948  typedef EdgeBaseIterator<ConstEdgeIterator, const Edge, const Edge,
949  typename EdgeList::ConstNodeIterator,
950  const VirtualList<Edge> > Super;
951  public:
952  typedef const Edge& Reference;
953  typedef const Edge* Pointer;
954  ConstEdgeIterator() {}
955  ConstEdgeIterator(const ConstEdgeIterator &other): Super(other) {}
956  ConstEdgeIterator(const EdgeIterator &other): Super(other.phase_, other.iter_, other.vlist_) {}
957  ConstEdgeIterator& operator=(const ConstEdgeIterator &other) { this->Super::operator=(other); return *this; }
958  const Edge& operator*() const { return this->dereference(); }
959  const Edge* operator->() const { return &this->dereference(); }
960  private:
961  friend class Graph;
962  ConstEdgeIterator(const typename EdgeList::ConstNodeIterator &base): Super(base) {}
963  ConstEdgeIterator(EdgePhase phase, const VirtualList<Edge> *vlist): Super(phase, vlist) {}
964  };
973  class EdgeValueIterator: public EdgeBaseIterator<EdgeValueIterator, EdgeValue, Edge, typename EdgeList::NodeIterator,
974  VirtualList<Edge> > {
975  typedef EdgeBaseIterator<EdgeValueIterator, EdgeValue, Edge, typename EdgeList::NodeIterator,
976  VirtualList<Edge> > Super;
977  public:
978  typedef EdgeValue& Reference;
979  typedef EdgeValue* Pointer;
980  EdgeValueIterator() {}
981  EdgeValueIterator(const EdgeValueIterator &other): Super(other) {}
982  EdgeValueIterator(const EdgeIterator &other): Super(other.phase_, other.iter_, other.vlist_) {}
983  EdgeValueIterator& operator=(const EdgeValueIterator &other) { this->Super::operator=(other); return *this; }
984  EdgeValue& operator*() const { return this->dereference().value(); }
985  EdgeValue* operator->() const { return &this->dereference().value(); }
986  private:
987  friend class Graph;
988  EdgeValueIterator(const typename EdgeList::NodeIterator &base): Super(base) {}
989  EdgeValueIterator(EdgePhase phase, VirtualList<Edge> *vlist): Super(phase, vlist) {}
990  };
991 
997  class ConstEdgeValueIterator: public EdgeBaseIterator<ConstEdgeValueIterator, const EdgeValue, const Edge,
998  typename EdgeList::ConstNodeIterator,
999  const VirtualList<Edge> > {
1000  typedef EdgeBaseIterator<ConstEdgeValueIterator, const EdgeValue, const Edge,
1001  typename EdgeList::ConstNodeIterator,
1002  const VirtualList<Edge> > Super;
1003  public:
1004  typedef const EdgeValue& Reference;
1005  typedef const EdgeValue* Pointer;
1006  ConstEdgeValueIterator() {}
1007  ConstEdgeValueIterator(const ConstEdgeValueIterator &other): Super(other) {}
1008  ConstEdgeValueIterator(const EdgeValueIterator &other): Super(other.phase_, other.iter_, other.vlist_) {}
1009  ConstEdgeValueIterator(const EdgeIterator &other): Super(other.phase_, other.iter_, other.vlist_) {}
1010  ConstEdgeValueIterator(const ConstEdgeIterator &other): Super(other.phase_, other.iter_, other.vlist_) {}
1011  ConstEdgeValueIterator& operator=(const EdgeValueIterator &other) { this->Super::operator=(other); return *this; }
1012  const EdgeValue& operator*() const { return this->dereference().value(); }
1013  const EdgeValue* operator->() const { return &this->dereference().value(); }
1014  private:
1015  friend class Graph;
1016  ConstEdgeValueIterator(const typename EdgeList::ConstNodeIterator &base): Super(base) {}
1017  ConstEdgeValueIterator(EdgePhase phase, const VirtualList<Edge> *vlist): Super(phase, vlist) {}
1018  };
1019 
1026  class VertexIterator: public VertexBaseIterator<VertexIterator, Vertex, Vertex,
1027  typename VertexList::NodeIterator> {
1028  typedef VertexBaseIterator<VertexIterator, Vertex, Vertex,
1029  typename VertexList::NodeIterator> Super;
1030  public:
1031  typedef Vertex& Reference;
1032  typedef Vertex* Pointer;
1033  VertexIterator() {}
1034  VertexIterator(const VertexIterator &other): Super(other) {}
1035  VertexIterator& operator=(const VertexIterator &other) { this->Super::operator=(other); return *this; }
1036  Vertex& operator*() const { return this->dereference(); }
1037  Vertex* operator->() const { return &this->dereference(); }
1038  private:
1039  friend class Graph;
1040  VertexIterator(const typename VertexList::NodeIterator &base): Super(base) {}
1041  };
1042 
1048  class ConstVertexIterator: public VertexBaseIterator<ConstVertexIterator, const Vertex, const Vertex,
1049  typename VertexList::ConstNodeIterator> {
1050  typedef VertexBaseIterator<ConstVertexIterator, const Vertex, const Vertex,
1051  typename VertexList::ConstNodeIterator> Super;
1052  public:
1053  typedef const Vertex& Reference;
1054  typedef const Vertex* Pointer;
1055  ConstVertexIterator() {}
1056  ConstVertexIterator(const ConstVertexIterator &other): Super(other) {}
1057  ConstVertexIterator(const VertexIterator &other): Super(other.base_) {}
1058  ConstVertexIterator& operator=(const ConstVertexIterator &other) { this->Super::operator=(other); return *this; }
1059  const Vertex& operator*() const { return this->dereference(); }
1060  const Vertex* operator->() const { return &this->dereference(); }
1061  private:
1062  friend class Graph;
1063  ConstVertexIterator(const typename VertexList::ConstNodeIterator &base): Super(base) {}
1064  };
1065 
1072  class VertexValueIterator: public VertexBaseIterator<VertexValueIterator, VertexValue, Vertex,
1073  typename VertexList::NodeIterator> {
1075  typename VertexList::NodeIterator> Super;
1076  public:
1077  typedef VertexValue& Reference;
1078  typedef VertexValue* Pointer;
1079  VertexValueIterator() {}
1080  VertexValueIterator(const VertexValueIterator &other): Super(other) {}
1081  VertexValueIterator(const VertexIterator &other): Super(other.base_) {}
1082  VertexValueIterator& operator=(const VertexValueIterator &other) { this->Super::operator=(other); return *this; }
1083  VertexValue& operator*() const { return this->dereference().value(); }
1084  VertexValue* operator->() const { return &this->dereference().value(); }
1085  private:
1086  friend class Graph;
1087  VertexValueIterator(const typename VertexList::NodeIterator &base): Super(base) {}
1088  };
1089 
1095  class ConstVertexValueIterator: public VertexBaseIterator<ConstVertexValueIterator, const VertexValue, const Vertex,
1096  typename VertexList::ConstNodeIterator> {
1098  typename VertexList::ConstNodeIterator> Super;
1099  public:
1100  typedef const VertexValue& Reference;
1101  typedef const VertexValue* Pointer;
1102  ConstVertexValueIterator() {}
1103  ConstVertexValueIterator(const ConstVertexValueIterator &other): Super(other) {}
1104  ConstVertexValueIterator(const VertexValueIterator &other): Super(other.base_) {}
1105  ConstVertexValueIterator(const VertexIterator &other): Super(other.base_) {}
1106  ConstVertexValueIterator(const ConstVertexIterator &other): Super(other.base_) {}
1107  ConstVertexValueIterator& operator=(const ConstVertexValueIterator &other) { this->Super::operator=(other); return *this; }
1108  const VertexValue& operator*() const { return this->dereference().value(); }
1109  const VertexValue* operator->() const { return &this->dereference().value(); }
1110  private:
1111  friend class Graph;
1112  ConstVertexValueIterator(const typename VertexList::ConstNodeIterator &base): Super(base) {}
1113  };
1114 
1115 
1117  // Storage nodes
1119 public:
1120 
1125  class Edge {
1126  VirtualList<Edge> edgeLists_; // links for in- and out-edge sublists; MUST BE FIRST
1127  EdgeValue value_; // user-defined data for each edge
1128  typename EdgeList::NodeIterator self_; // always points to itself so we can get to IndexedList::Node
1129  VertexIterator source_, target_; // starting and ending points of the edge are always required
1130  private:
1131  friend class Graph;
1132  Edge(const EdgeValue &value, const VertexIterator &source, const VertexIterator &target)
1133  : value_(value), source_(source), target_(target) {}
1134  public:
1144  const size_t& id() const { return self_->id(); }
1145 
1154  const VertexIterator& source() { return source_; }
1155  ConstVertexIterator source() const { return source_; }
1166  const VertexIterator& target() { return target_; }
1167  ConstVertexIterator target() const { return target_; }
1180  EdgeValue& value() { return value_; }
1181  const EdgeValue& value() const { return value_; }
1188  bool isSelfEdge() const {
1189  return source_ == target_;
1190  }
1191  };
1192 
1197  class Vertex {
1198  VertexValue value_; // user data for this vertex
1199  typename VertexList::NodeIterator self_; // always points to itself so we can get to IndexedList::Node
1200  VirtualList<Edge> edgeLists_; // this is the head node; points to the real edges
1201  size_t nInEdges_; // number of incoming edges
1202  size_t nOutEdges_; // number of outgoing edges
1203  private:
1204  friend class Graph;
1205  Vertex(const VertexValue &value): value_(value), nInEdges_(0), nOutEdges_(0) {}
1206  public:
1217  const size_t& id() const { return self_->id(); }
1218 
1228  boost::iterator_range<EdgeIterator> inEdges() {
1229  EdgeIterator begin(IN_EDGES, &edgeLists_.next(IN_EDGES));
1230  EdgeIterator end(IN_EDGES, &edgeLists_);
1231  return boost::iterator_range<EdgeIterator>(begin, end);
1232  }
1233  boost::iterator_range<ConstEdgeIterator> inEdges() const {
1234  ConstEdgeIterator begin(IN_EDGES, &edgeLists_.next(IN_EDGES));
1235  ConstEdgeIterator end(IN_EDGES, &edgeLists_);
1236  return boost::iterator_range<ConstEdgeIterator>(begin, end);
1237  }
1249  boost::iterator_range<EdgeIterator> outEdges() {
1250  EdgeIterator begin(OUT_EDGES, &edgeLists_.next(OUT_EDGES));
1251  EdgeIterator end(OUT_EDGES, &edgeLists_);
1252  return boost::iterator_range<EdgeIterator>(begin, end);
1253  }
1254  boost::iterator_range<ConstEdgeIterator> outEdges() const {
1255  ConstEdgeIterator begin(OUT_EDGES, &edgeLists_.next(OUT_EDGES));
1256  ConstEdgeIterator end(OUT_EDGES, &edgeLists_);
1257  return boost::iterator_range<ConstEdgeIterator>(begin, end);
1258  }
1264  size_t nInEdges() const {
1265  return nInEdges_;
1266  }
1267 
1271  size_t nOutEdges() const {
1272  return nOutEdges_;
1273  }
1274 
1279  size_t degree() const {
1280  return nInEdges_ + nOutEdges_;
1281  }
1282 
1293  VertexValue& value() { return value_; }
1294  const VertexValue& value() const { return value_; }
1296  };
1297 
1298 private:
1299  typedef typename GraphIndexTraits<VertexKey, ConstVertexIterator>::Index VertexIndex;
1300  typedef typename GraphIndexTraits<EdgeKey, ConstEdgeIterator>::Index EdgeIndex;
1301 
1302  EdgeList edges_; // all edges with integer ID numbers and O(1) insert/erase
1303  VertexList vertices_; // all vertices with integer ID numbers and O(1) insert/erase
1304  EdgeIndex edgeIndex_; // optional mapping between EdgeValue and ConstEdgeIterator
1305  VertexIndex vertexIndex_; // optional mapping between VertexValue and ConstVertexIterator
1306 
1308  // Serialization
1310 private:
1311  friend class boost::serialization::access;
1312 
1313  struct SerializableEdge {
1314  size_t srcId, tgtId;
1315  EdgeValue value;
1316 
1317  SerializableEdge()
1318  : srcId(-1), tgtId(-1) {}
1319 
1320  SerializableEdge(size_t srcId, size_t tgtId, const EdgeValue &value)
1321  : srcId(srcId), tgtId(tgtId), value(value) {}
1322 
1323  template<class S>
1324  void serialize(S &s, const unsigned /*version*/) {
1325  s & BOOST_SERIALIZATION_NVP(srcId);
1326  s & BOOST_SERIALIZATION_NVP(tgtId);
1327  s & BOOST_SERIALIZATION_NVP(value);
1328  }
1329  };
1330 
1331  template<class S>
1332  void save(S &s, const unsigned /*version*/) const {
1333  size_t nv = nVertices();
1334  s <<BOOST_SERIALIZATION_NVP(nv);
1335  for (size_t i=0; i<nv; ++i)
1336  s <<boost::serialization::make_nvp("vertex", findVertex(i)->value());
1337 
1338  size_t ne = nEdges();
1339  s <<BOOST_SERIALIZATION_NVP(ne);
1340  for (size_t i=0; i<ne; ++i) {
1341  ConstEdgeIterator edge = findEdge(i);
1342  SerializableEdge se(edge->source()->id(), edge->target()->id(), edge->value());
1343  s <<BOOST_SERIALIZATION_NVP(se);
1344  }
1345  }
1346 
1347  template<class S>
1348  void load(S &s, const unsigned /*version*/) {
1349  clear();
1350  size_t nv = 0;
1351  s >>BOOST_SERIALIZATION_NVP(nv);
1352  for (size_t i=0; i<nv; ++i) {
1353  VertexValue vv;
1354  s >>boost::serialization::make_nvp("vertex", vv);
1355  insertVertex(vv);
1356  }
1357 
1358  size_t ne = 0;
1359  s >>BOOST_SERIALIZATION_NVP(ne);
1360  for (size_t i=0; i<ne; ++i) {
1361  SerializableEdge se;
1362  s >>BOOST_SERIALIZATION_NVP(se);
1363  ASSERT_require(se.srcId < nv && se.tgtId < nv);
1364  insertEdge(findVertex(se.srcId), findVertex(se.tgtId), se.value);
1365  }
1366  }
1367 
1368  BOOST_SERIALIZATION_SPLIT_MEMBER();
1369 
1370 
1372  // Initialization
1374 public:
1375 
1381  Graph(const Allocator &allocator = Allocator()): edges_(allocator), vertices_(allocator) {};
1382 
1393  Graph(const Graph &other)
1394  : edges_(other.edges_.allocator()), vertices_(other.vertices_.allocator()) {
1395  *this = other;
1396  }
1397 
1406  template<class V2, class E2, class VKey2, class EKey2, class Alloc2>
1407  Graph(const Graph<V2, E2, VKey2, EKey2, Alloc2> &other, const Allocator &allocator = Allocator())
1408  : edges_(allocator), vertices_(allocator) {
1409  *this = other;
1410  }
1411 
1419  Graph& operator=(const Graph &other) {
1420  return operator=<V, E>(other);
1421  }
1422 
1434  template<class V2, class E2, class VKey2, class EKey2, class Alloc2>
1436  clear();
1437  for (size_t i=0; i<other.nVertices(); ++i) {
1439  VertexIterator inserted SAWYER_ATTR_UNUSED = insertVertex(VertexValue(vertex->value()));
1440  ASSERT_require(inserted->id() == i);
1441  }
1442  for (size_t i=0; i<other.nEdges(); ++i) {
1444  VertexIterator vsrc = findVertex(edge->source()->id());
1445  VertexIterator vtgt = findVertex(edge->target()->id());
1446  insertEdge(vsrc, vtgt, EdgeValue(edge->value()));
1447  }
1448  return *this;
1449  }
1450 
1454  const Allocator& allocator() {
1455  return vertices_.allocator();
1456  }
1457 
1459 public:
1460 
1469  boost::iterator_range<VertexIterator> vertices() {
1470  return boost::iterator_range<VertexIterator>(VertexIterator(vertices_.nodes().begin()),
1471  VertexIterator(vertices_.nodes().end()));
1472  }
1473  boost::iterator_range<ConstVertexIterator> vertices() const {
1474  return boost::iterator_range<ConstVertexIterator>(ConstVertexIterator(vertices_.nodes().begin()),
1475  ConstVertexIterator(vertices_.nodes().end()));
1476  }
1496  boost::iterator_range<VertexValueIterator> vertexValues() {
1497  return boost::iterator_range<VertexValueIterator>(VertexValueIterator(vertices_.nodes().begin()),
1498  VertexValueIterator(vertices_.nodes().end()));
1499  }
1500  boost::iterator_range<ConstVertexValueIterator> vertexValues() const {
1501  return boost::iterator_range<ConstVertexValueIterator>(ConstVertexValueIterator(vertices_.nodes().begin()),
1502  ConstVertexValueIterator(vertices_.nodes().end()));
1503  }
1516  VertexIterator findVertex(size_t id) {
1517  return VertexIterator(vertices_.find(id));
1518  }
1519  ConstVertexIterator findVertex(size_t id) const {
1520  return ConstVertexIterator(vertices_.find(id));
1521  }
1534  VertexIterator findVertexKey(const VertexKey &key) {
1535  if (Optional<ConstVertexIterator> ov = vertexIndex_.lookup(key))
1536  return findVertex((*ov)->id());
1537  return vertices().end();
1538  }
1539  ConstVertexIterator findVertexKey(const VertexKey &key) const {
1540  return vertexIndex_.lookup(key).orElse(vertices().end());
1541  }
1554  VertexIterator findVertexValue(const VertexValue &value) {
1555  return findVertexKey(VertexKey(value));
1556  }
1557  ConstVertexIterator findVertexValue(const VertexValue &value) const {
1558  return findVertexKey(VertexKey(value));
1559  }
1566  bool isValidVertex(const ConstVertexIterator &vertex) const {
1567  return vertex!=vertices().end() && vertex->id()<nVertices() && vertex==findVertex(vertex->id());
1568  }
1569 
1578  boost::iterator_range<EdgeIterator> edges() {
1579  return boost::iterator_range<EdgeIterator>(EdgeIterator(edges_.nodes().begin()),
1580  EdgeIterator(edges_.nodes().end()));
1581  }
1582  boost::iterator_range<ConstEdgeIterator> edges() const {
1583  return boost::iterator_range<ConstEdgeIterator>(ConstEdgeIterator(edges_.nodes().begin()),
1584  ConstEdgeIterator(edges_.nodes().end()));
1585  }
1605  boost::iterator_range<EdgeValueIterator> edgeValues() {
1606  return boost::iterator_range<EdgeValueIterator>(EdgeValueIterator(edges_.nodes().begin()),
1607  EdgeValueIterator(edges_.nodes().end()));
1608  }
1609  boost::iterator_range<ConstEdgeValueIterator> edgeValues() const {
1610  return boost::iterator_range<ConstEdgeValueIterator>(ConstEdgeValueIterator(edges_.nodes().begin()),
1611  ConstEdgeValueIterator(edges_.nodes().end()));
1612  }
1625  EdgeIterator findEdge(size_t id) {
1626  return EdgeIterator(edges_.find(id));
1627  }
1628  ConstEdgeIterator findEdge(size_t id) const {
1629  return ConstEdgeIterator(edges_.find(id));
1630  }
1643  EdgeIterator findEdgeKey(const EdgeKey &key) {
1644  if (Optional<ConstEdgeIterator> oe = edgeIndex_.lookup(key))
1645  return findEdge((*oe)->id());
1646  return edges().end();
1647  }
1648  ConstEdgeIterator findEdgeKey(const EdgeKey &key) const {
1649  return edgeIndex_.lookup(key).orElse(edges().end());
1650  }
1663  EdgeIterator findEdgeValue(const EdgeValue &value) {
1664  return findEdgeKey(EdgeKey(value));
1665  }
1666  ConstEdgeIterator findEdgeValue(const EdgeValue &value) const {
1667  return findEdgeValue(EdgeKey(value));
1668  }
1675  bool isValidEdge(const ConstEdgeIterator &edge) const {
1676  return edge!=edges().end() && edge->id()<nEdges() && edge==findEdge(edge->id());
1677  }
1678 
1685  size_t nVertices() const {
1686  return vertices_.size();
1687  }
1688 
1695  size_t nEdges() const {
1696  return edges_.size();
1697  }
1698 
1704  bool isEmpty() const {
1705  ASSERT_require(edges_.isEmpty() || !vertices_.isEmpty()); // existence of edges implies existence of vertices
1706  return vertices_.isEmpty();
1707  }
1708 
1721  VertexIterator insertVertex(const VertexValue &value = VertexValue()) {
1722  return insertVertexImpl(value, true /*strict*/);
1723  }
1724 
1733  VertexIterator insertVertexMaybe(const VertexValue &value) {
1734  return insertVertexImpl(value, false /*non-strict*/);
1735  }
1736 
1751  EdgeIterator insertEdge(const VertexIterator &sourceVertex, const VertexIterator &targetVertex,
1752  const EdgeValue &value = EdgeValue()) {
1753  return insertEdgeImpl(sourceVertex, targetVertex, value, true /*strict*/);
1754  }
1755  EdgeIterator insertEdge(const ConstVertexIterator &sourceVertex, const ConstVertexIterator &targetVertex,
1756  const EdgeValue &value = EdgeValue()) {
1757  ASSERT_require(isValidVertex(sourceVertex));
1758  ASSERT_require(isValidVertex(targetVertex));
1759  return insertEdge(findVertex(sourceVertex->id()), findVertex(targetVertex->id()), value);
1760  }
1773  EdgeIterator insertEdgeMaybe(const VertexIterator &sourceVertex, const VertexIterator &targetVertex,
1774  const EdgeValue &value = EdgeValue()) {
1775  return insertEdgeImpl(sourceVertex, targetVertex, value, false /*non-strict*/);
1776  }
1777  EdgeIterator insertEdgeMaybe(const ConstVertexIterator &sourceVertex, const ConstVertexIterator &targetVertex,
1778  const EdgeValue &value = EdgeValue()) {
1779  ASSERT_require(isValidVertex(sourceVertex));
1780  ASSERT_require(isValidVertex(targetVertex));
1781  return insertEdgeMaybe(findVertex(sourceVertex->id()), findVertex(targetVertex->id()), value);
1782  }
1789  EdgeIterator insertEdgeWithVertices(const VertexValue &sourceValue, const VertexValue &targetValue,
1790  const EdgeValue &edgeValue = EdgeValue()) {
1791  VertexIterator source = insertVertexMaybe(sourceValue);
1792  VertexIterator target = insertVertexMaybe(targetValue);
1793  return insertEdge(source, target, edgeValue);
1794  }
1795 
1811  EdgeIterator eraseEdge(const EdgeIterator &edge) {
1812  ASSERT_require(isValidEdge(edge));
1813  EdgeIterator next = edge; ++next; // advance before we delete edge
1814  edgeIndex_.erase(EdgeKey(edge->value()));
1815  --edge->source_->nOutEdges_;
1816  edge->edgeLists_.remove(OUT_EDGES);
1817  --edge->target_->nInEdges_;
1818  edge->edgeLists_.remove(IN_EDGES);
1819  edges_.eraseAt(edge->self_); // edge is now deleted
1820  return next;
1821  }
1822  EdgeIterator eraseEdge(const ConstEdgeIterator &edge) {
1823  ASSERT_require(isValidEdge(edge));
1824  return eraseEdge(findEdge(edge->id()));
1825  }
1837  EdgeIterator eraseEdgeWithVertices(const EdgeIterator &edge) {
1838  ASSERT_require(isValidEdge(edge));
1839  VertexIterator source = edge->source();
1840  VertexIterator target = edge->target();
1841  EdgeIterator retval = eraseEdge(edge);
1842  if (source == target) {
1843  if (source->degree() == 0)
1844  eraseVertex(source);
1845  } else {
1846  if (source->degree() == 0)
1847  eraseVertex(source);
1848  if (target->degree() == 0)
1849  eraseVertex(target);
1850  }
1851  return retval;
1852  }
1853 
1863  void eraseEdges(const VertexIterator &source, const VertexIterator &target) {
1864  ASSERT_require(isValidVertex(source));
1865  ASSERT_require(isValidVertex(target));
1866  if (source->nOutEdges() < target->nInEdges()) {
1867  EdgeIterator iter = source->outEdges().begin();
1868  while (iter != source->outEdges().end()) {
1869  if (iter->target() == target) {
1870  iter = eraseEdge(iter);
1871  } else {
1872  ++iter;
1873  }
1874  }
1875  } else {
1876  EdgeIterator iter = target->inEdges().begin();
1877  while (iter != target->inEdges().end()) {
1878  if (iter->source() == source) {
1879  iter = eraseEdge(iter);
1880  } else {
1881  ++iter;
1882  }
1883  }
1884  }
1885  }
1886  void eraseEdges(const ConstVertexIterator &source, const ConstVertexIterator &target) {
1887  ASSERT_require(isValidVertex(source));
1888  ASSERT_require(isValidVertex(target));
1889  eraseEdges(findVertex(source->id()), findVertex(target->id()));
1890  }
1909  VertexIterator eraseVertex(const VertexIterator &vertex) {
1910  ASSERT_require(isValidVertex(vertex));
1911  VertexIterator next = vertex; ++next; // advance before we delete vertex
1912  clearEdges(vertex);
1913  vertexIndex_.erase(VertexKey(vertex->value()));
1914  vertices_.eraseAt(vertex->self_); // vertex is now deleted
1915  return next;
1916  }
1917  VertexIterator eraseVertex(const ConstVertexIterator &vertex) {
1918  ASSERT_require(isValidVertex(vertex));
1919  return eraseVertex(findVertex(vertex->id()));
1920  }
1929  void clearEdges() {
1930  for (VertexIterator vertex=vertices().begin(); vertex!=vertices().end(); ++vertex) {
1931  vertex->inEdges().reset();
1932  vertex->outEdges().reset();
1933  }
1934  edges_.clear();
1935  edgeIndex_.clear();
1936  }
1937 
1948  void clearEdges(const VertexIterator &vertex) {
1949  clearOutEdges(vertex);
1950  clearInEdges(vertex);
1951  }
1952  void clearEdges(const ConstVertexIterator &vertex) {
1953  clearOutEdges(vertex);
1954  clearInEdges(vertex);
1955  }
1967  void clearOutEdges(const VertexIterator &vertex) {
1968  ASSERT_forbid(vertex==vertices().end());
1969  for (EdgeIterator edge=vertex->outEdges().begin(); edge!=vertex->outEdges().end(); /*void*/)
1970  edge = eraseEdge(edge);
1971  }
1972  void clearOutEdges(const ConstVertexIterator &vertex) {
1973  ASSERT_forbid(vertex==vertices().end());
1974  clearOutEdges(findVertex(vertex->id()));
1975  }
1987  void clearInEdges(const VertexIterator &vertex) {
1988  ASSERT_forbid(vertex==vertices().end());
1989  for (EdgeIterator edge=vertex->inEdges().begin(); edge!=vertex->inEdges().end(); /*void*/)
1990  edge = eraseEdge(edge);
1991  }
1992  void clearInEdges(const ConstVertexIterator &vertex) {
1993  ASSERT_forbid(vertex==vertices().end());
1994  clearInEdges(findVertex(vertex->id()));
1995  }
2005  void clear() {
2006  edges_.clear();
2007  vertices_.clear();
2008  edgeIndex_.clear();
2009  vertexIndex_.clear();
2010  }
2011 
2013  // Internal implementation details
2015 private:
2016  VertexIterator insertVertexImpl(const VertexValue &value, bool strict) {
2017  const VertexKey key(value);
2018  if (Optional<ConstVertexIterator> found = vertexIndex_.lookup(key)) {
2019  if (strict)
2020  throw Exception::AlreadyExists("cannot insert duplicate vertex when graph vertices are indexed");
2021  return findVertex((*found)->id());
2022  }
2023  typename VertexList::NodeIterator inserted = vertices_.insert(vertices_.nodes().end(), Vertex(value));
2024  inserted->value().self_ = inserted;
2025  inserted->value().edgeLists_.reset(NULL); // this is a sublist head, no edge node
2026  VertexIterator retval = VertexIterator(inserted);
2027  vertexIndex_.insert(key, retval);
2028  return retval;
2029  }
2030 
2031  EdgeIterator insertEdgeImpl(const VertexIterator &sourceVertex, const VertexIterator &targetVertex,
2032  const EdgeValue &value, bool strict) {
2033  const EdgeKey key(value);
2034  ASSERT_require(isValidVertex(sourceVertex));
2035  ASSERT_require(isValidVertex(targetVertex));
2036  if (Optional<ConstEdgeIterator> found = edgeIndex_.lookup(key)) {
2037  if (strict)
2038  throw Exception::AlreadyExists("cannot insert duplicate edge when graph edges are indexed");
2039  return findEdge((*found)->id());
2040  }
2041  typename EdgeList::NodeIterator inserted = edges_.insert(edges_.nodes().end(), Edge(value, sourceVertex, targetVertex));
2042  inserted->value().self_ = inserted;
2043  inserted->value().edgeLists_.reset(&inserted->value());
2044  EdgeIterator newEdge(inserted);
2045  sourceVertex->edgeLists_.insert(OUT_EDGES, &newEdge->edgeLists_);
2046  ++sourceVertex->nOutEdges_;
2047  targetVertex->edgeLists_.insert(IN_EDGES, &newEdge->edgeLists_);
2048  ++targetVertex->nInEdges_;
2049  edgeIndex_.insert(key, newEdge);
2050  return newEdge;
2051  }
2052 
2053 
2055  // Deprecated stuff
2057 public:
2058  // Deprecated [Robb Matzke 2015-03-28]: to be removed on or after 2015-09-28
2059  typedef Edge EdgeNode SAWYER_DEPRECATED("use Edge instead");
2060  typedef Vertex VertexNode SAWYER_DEPRECATED("use Vertex instead");
2061  typedef EdgeIterator EdgeNodeIterator SAWYER_DEPRECATED("use EdgeIterator instead");
2062  typedef ConstEdgeIterator ConstEdgeNodeIterator SAWYER_DEPRECATED("use ConstEdgeIterator instead");
2063  typedef VertexIterator VertexNodeIterator SAWYER_DEPRECATED("use VertexIterator instead");
2064  typedef ConstVertexIterator ConstVertexNodeIterator SAWYER_DEPRECATED("use ConstVertexIterator instead");
2065 };
2066 
2067 } // namespace
2068 } // namespace
2069 
2070 #endif
Optional< VertexOrEdgeConstIterator > lookup(const VertexOrEdgeKey &) const
Look up iterator for vertex or edge key.
Definition: Graph.h:145
bool operator!=(const OtherIter &other) const
Equality predicate.
Definition: Graph.h:836
size_t nVertices() const
Total number of vertices.
Definition: Graph.h:1685
void clearOutEdges(const VertexIterator &vertex)
Erase all edges emanating from a vertex.
Definition: Graph.h:1967
Derived operator++(int)
Increment.
Definition: Graph.h:784
EdgeIterator eraseEdgeWithVertices(const EdgeIterator &edge)
Erases and edge and possibly vertices.
Definition: Graph.h:1837
ConstEdgeIterator findEdgeValue(const EdgeValue &value) const
Finds an edge given its value.
Definition: Graph.h:1666
void clearEdges()
Erase all edges, but leave all vertices.
Definition: Graph.h:1929
void clear()
Erase all data from this index.
Definition: Graph.h:126
VertexIterator insertVertex(const VertexValue &value=VertexValue())
Insert a new vertex.
Definition: Graph.h:1721
const size_t & id() const
Unique vertex ID number.
Definition: Graph.h:1217
VertexIterator eraseVertex(const VertexIterator &vertex)
Erases a vertex and its incident edges.
Definition: Graph.h:1909
const size_t & id() const
Unique edge ID number.
Definition: Graph.h:1144
Graph containing user-defined vertices and edges.
Definition: Graph.h:625
ConstVertexIterator findVertexKey(const VertexKey &key) const
Finds a vertex given its key.
Definition: Graph.h:1539
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
Definition: Sawyer/Map.h:584
size_t nEdges() const
Total number of edges.
Definition: Graph.h:1695
bool operator==(const OtherIter &other) const
Equality predicate.
Definition: Graph.h:820
G::Edge Edge
Edge type including user type and connectivity.
Definition: Graph.h:304
Bidirectional edge value iterator.
Definition: Graph.h:973
Bidirectional vertex node iterator.
Definition: Graph.h:1026
boost::iterator_range< ConstVertexValueIterator > vertexValues() const
Iterators for all vertices.
Definition: Graph.h:1500
VertexIterator eraseVertex(const ConstVertexIterator &vertex)
Erases a vertex and its incident edges.
Definition: Graph.h:1917
const EdgeValue & value() const
User-defined value.
Definition: Graph.h:1181
Bidirectional edge value iterator.
Definition: Graph.h:997
Traits for graphs.
Definition: Graph.h:287
void erase(const VertexOrEdgeKey &key)
Erase an element from the map.
Definition: Graph.h:217
boost::iterator_range< EdgeIterator > inEdges()
List of incoming edges.
Definition: Graph.h:1228
Alloc Allocator
Allocator for vertex and edge nodes.
Definition: Graph.h:631
bool isValidVertex(const ConstVertexIterator &vertex) const
Determines whether the vertex iterator is valid.
Definition: Graph.h:1566
Optional< VertexOrEdgeConstIterator > lookup(const VertexOrEdgeKey &key) const
Lookup iterator for vertex or edge key.
Definition: Graph.h:183
bool isEmpty() const
True if iterator doesn't point to anything.
Definition: Graph.h:842
Derived & operator--()
Decrement.
Definition: Graph.h:891
EdgeIterator eraseEdge(const ConstEdgeIterator &edge)
Erases an edge.
Definition: Graph.h:1822
VKey VertexKey
Type for looking up a vertex.
Definition: Graph.h:629
EdgeIterator insertEdge(const ConstVertexIterator &sourceVertex, const ConstVertexIterator &targetVertex, const EdgeValue &value=EdgeValue())
Insert a new edge.
Definition: Graph.h:1755
Derived & operator=(const Derived &other)
Assignment.
Definition: Graph.h:872
VertexValue & value()
User-defined value.
Definition: Graph.h:1293
bool isValidEdge(const ConstEdgeIterator &edge) const
Determines whether the edge iterator is valid.
Definition: Graph.h:1675
boost::iterator_range< ConstEdgeIterator > inEdges() const
List of incoming edges.
Definition: Graph.h:1233
VertexIterator findVertexValue(const VertexValue &value)
Finds a vertex given its value.
Definition: Graph.h:1554
boost::iterator_range< ConstVertexIterator > vertices() const
Iterators for all vertices.
Definition: Graph.h:1473
Holds a value or nothing.
Definition: Optional.h:49
Graph & operator=(const Graph< V2, E2, VKey2, EKey2, Alloc2 > &other)
Assignment.
Definition: Graph.h:1435
void clearEdges(const VertexIterator &vertex)
Erase all edges incident to a vertex.
Definition: Graph.h:1948
void clear()
Erase all data from this index.
Definition: Graph.h:162
Type of vertex key for graphs that do not index their vertices.
Definition: Graph.h:92
Default allocator.
boost::iterator_range< VertexIterator > vertices()
Iterators for all vertices.
Definition: Graph.h:1469
Bidirectional vertex value iterator.
Definition: Graph.h:1095
void eraseEdges(const VertexIterator &source, const VertexIterator &target)
Erases all edges connecting two vertices.
Definition: Graph.h:1863
EdgeIterator insertEdge(const VertexIterator &sourceVertex, const VertexIterator &targetVertex, const EdgeValue &value=EdgeValue())
Insert a new edge.
Definition: Graph.h:1751
Map & clear()
Remove all nodes.
Definition: Sawyer/Map.h:714
void clearInEdges(const VertexIterator &vertex)
Erase all edges targeting a vertex.
Definition: Graph.h:1987
EdgeIterator findEdgeValue(const EdgeValue &value)
Finds an edge given its value.
Definition: Graph.h:1663
boost::iterator_range< EdgeValueIterator > edgeValues()
Iterators for all edges.
Definition: Graph.h:1605
Bidirectional vertex node iterator.
Definition: Graph.h:1048
void erase(const VertexOrEdgeKey &)
Erase an element from the map.
Definition: Graph.h:139
Graph(const Graph &other)
Copy constructor.
Definition: Graph.h:1393
G::Vertex Vertex
Vertex type including user type and connectivity.
Definition: Graph.h:301
Traits for vertex and edge indexing.
Definition: Graph.h:241
Name space for the entire library.
Definition: FeasiblePath.h:767
EdgeIterator insertEdgeMaybe(const VertexIterator &sourceVertex, const VertexIterator &targetVertex, const EdgeValue &value=EdgeValue())
Optionally insert a new edge.
Definition: Graph.h:1773
Base class for vertex iterators.
Definition: Graph.h:853
Derived & operator=(const Derived &other)
Assignment.
Definition: Graph.h:763
void insert(const VertexOrEdgeKey &key, const VertexOrEdgeConstIterator &iter)
Insert a new element into the map.
Definition: Graph.h:210
Hash-based indexing.
Definition: Graph.h:196
boost::iterator_range< EdgeIterator > outEdges()
List of outgoing edges.
Definition: Graph.h:1249
GraphBimapIndex< VertexOrEdgeKey, VertexOrEdgeConstIterator > Index
Type of index to use for the specified key type.
Definition: Graph.h:243
G::VertexValueIterator VertexValueIterator
Const or non-const vertex value iterator.
Definition: Graph.h:298
VertexIterator insertVertexMaybe(const VertexValue &value)
Optionally insert a new vertex.
Definition: Graph.h:1733
void clearInEdges(const ConstVertexIterator &vertex)
Erase all edges targeting a vertex.
Definition: Graph.h:1992
const VertexIterator & source()
Source vertex.
Definition: Graph.h:1154
EdgeValue & value()
User-defined value.
Definition: Graph.h:1180
boost::iterator_range< ConstEdgeValueIterator > edgeValues() const
Iterators for all edges.
Definition: Graph.h:1609
void clear()
Erase all data from this index.
Definition: Graph.h:203
Derived & operator++()
Increment.
Definition: Graph.h:776
bool operator!=(const OtherIter &other) const
Equality predicate.
Definition: Graph.h:901
ConstEdgeIterator findEdge(size_t id) const
Finds the edge with specified ID number.
Definition: Graph.h:1628
ConstVertexIterator source() const
Source vertex.
Definition: Graph.h:1155
void eraseEdges(const ConstVertexIterator &source, const ConstVertexIterator &target)
Erases all edges connecting two vertices.
Definition: Graph.h:1886
bool isEmpty() const
True if iterator doesn't point to anything.
Definition: Graph.h:905
Derived operator++(int)
Increment.
Definition: Graph.h:882
const char * EdgePhase(int64_t)
Convert Sawyer::Container::Graph::EdgePhase enum constant to a string.
Optional< VertexOrEdgeConstIterator > lookup(const VertexOrEdgeKey &key) const
Lookup iterator for vertex or edge key.
Definition: Graph.h:224
EdgeIterator insertEdgeWithVertices(const VertexValue &sourceValue, const VertexValue &targetValue, const EdgeValue &edgeValue=EdgeValue())
Insert an edge and its vertex end points.
Definition: Graph.h:1789
G::EdgeIterator EdgeIterator
Const or non-const edge iterator.
Definition: Graph.h:289
VertexIterator findVertexKey(const VertexKey &key)
Finds a vertex given its key.
Definition: Graph.h:1534
boost::iterator_range< VertexValueIterator > vertexValues()
Iterators for all vertices.
Definition: Graph.h:1496
VertexIterator findVertex(size_t id)
Finds the vertex with specified ID number.
Definition: Graph.h:1516
G::VertexValue VertexValue
User-defined vertex type without connectivity information.
Definition: Graph.h:307
ConstVertexIterator findVertex(size_t id) const
Finds the vertex with specified ID number.
Definition: Graph.h:1519
G::EdgeValue EdgeValue
User-defined edge type without connectivity information.
Definition: Graph.h:310
Error for existing values.
Bidirectional vertex value iterator.
Definition: Graph.h:1072
Extends std::map with methods that return optional values.
Definition: Map.h:10
Map based index is the default index type when indexes are present.
Definition: Graph.h:156
ConstEdgeIterator findEdgeKey(const EdgeKey &key) const
Finds an edge given its key.
Definition: Graph.h:1648
void clear()
Remove all vertices and edges.
Definition: Graph.h:2005
size_t degree() const
Number of incident edges.
Definition: Graph.h:1279
EdgeIterator findEdgeKey(const EdgeKey &key)
Finds an edge given its key.
Definition: Graph.h:1643
void insert(const VertexOrEdgeKey &key, const VertexOrEdgeConstIterator &iter)
Insert a new element into the map.
Definition: Graph.h:169
EdgeIterator findEdge(size_t id)
Finds the edge with specified ID number.
Definition: Graph.h:1625
Fake index for graphs that don't have an index.
Definition: Graph.h:121
boost::iterator_range< ConstEdgeIterator > outEdges() const
List of outgoing edges.
Definition: Graph.h:1254
size_t nInEdges() const
Number of incoming edges.
Definition: Graph.h:1264
void clearEdges(const ConstVertexIterator &vertex)
Erase all edges incident to a vertex.
Definition: Graph.h:1952
Map & erase(const Key &key)
Remove a node with specified key.
Definition: Sawyer/Map.h:726
Type of edge key for graphs that do not index their edges.
Definition: Graph.h:105
Base class for edge iterators.
Definition: Graph.h:731
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
Definition: Sawyer/Map.h:628
Bidirectional edge node iterator.
Definition: Graph.h:945
Bidirectional edge node iterator.
Definition: Graph.h:921
const Allocator & allocator()
Allocator.
Definition: Graph.h:1454
Derived & operator--()
Decrement.
Definition: Graph.h:797
void erase(const VertexOrEdgeKey &key)
Erase an element from the map.
Definition: Graph.h:176
G::VertexIterator VertexIterator
Const or non-const vertex iterator.
Definition: Graph.h:295
Graph(const Allocator &allocator=Allocator())
Default constructor.
Definition: Graph.h:1381
V VertexValue
User-level data associated with vertices.
Definition: Graph.h:627
size_t nOutEdges() const
Number of outgoing edges.
Definition: Graph.h:1271
ConstVertexIterator findVertexValue(const VertexValue &value) const
Finds a vertex given its value.
Definition: Graph.h:1557
EdgeIterator insertEdgeMaybe(const ConstVertexIterator &sourceVertex, const ConstVertexIterator &targetVertex, const EdgeValue &value=EdgeValue())
Optionally insert a new edge.
Definition: Graph.h:1777
boost::iterator_range< ConstEdgeIterator > edges() const
Iterators for all edges.
Definition: Graph.h:1582
Derived operator--(int)
Decrement.
Definition: Graph.h:805
Represents no value.
Definition: Optional.h:32
EKey EdgeKey
Type for looking up an edge.
Definition: Graph.h:630
boost::iterator_range< EdgeIterator > edges()
Iterators for all edges.
Definition: Graph.h:1578
bool isEmpty() const
True if graph is empty.
Definition: Graph.h:1704
bool operator==(const OtherIter &other) const
Equality predicate.
Definition: Graph.h:900
Graph & operator=(const Graph &other)
Assignment.
Definition: Graph.h:1419
Derived & operator++()
Increment.
Definition: Graph.h:881
EdgeIterator eraseEdge(const EdgeIterator &edge)
Erases an edge.
Definition: Graph.h:1811
ConstVertexIterator target() const
Target vertex.
Definition: Graph.h:1167
const VertexIterator & target()
Target vertex.
Definition: Graph.h:1166
G::EdgeValueIterator EdgeValueIterator
Const or non-const edge value iterator.
Definition: Graph.h:292
Graph(const Graph< V2, E2, VKey2, EKey2, Alloc2 > &other, const Allocator &allocator=Allocator())
Copy constructor.
Definition: Graph.h:1407
void insert(const VertexOrEdgeKey &, const VertexOrEdgeConstIterator &)
Insert a new element into the map.
Definition: Graph.h:134
E EdgeValue
User-level data associated with edges.
Definition: Graph.h:628
void clearOutEdges(const ConstVertexIterator &vertex)
Erase all edges emanating from a vertex.
Definition: Graph.h:1972
Derived operator--(int)
Decrement.
Definition: Graph.h:892
const VertexValue & value() const
User-defined value.
Definition: Graph.h:1294
bool isSelfEdge() const
Determines if edge is a self-edge.
Definition: Graph.h:1188