DiaDes  0.1
DIAgnosis of Discrete-Event System
EdgeImpl.hh
Go to the documentation of this file.
1 #ifndef __DIADES__GRAPH__EDGEIMPL__HH
2 #define __DIADES__GRAPH__EDGEIMPL__HH
3 
4 #include <sstream>
5 #include <list>
6 #include <iterator>
10 #include <diades/graph/Edge.hh>
11 #include <diades/graph/Node.hh>
12 #include <diades/graph/GraphInt.hh>
13 
14 
15 
16 namespace Diades {
17  namespace Graph {
18  using std::stringstream;
19 
24  inline string EdgeData::details() const {
25  stringstream stream;
26  stream << "EdgeData address = " << this << "\n";
27  stream << "EdgeData _identifier = " << _identifier << "\n";
28  stream << "EdgeData _source = " << _source << "\n";
29  stream << "EdgeData _target = " << _target << "\n";
30  stream << "_sourcePos ? " << ((*_sourcePos).data() == this) << "\n";
31  stream << "_targetPos ? " << ((*_targetPos).data() == this) << "\n";
32  return stream.str();
33  }
34 
39  inline Node Edge::source() const {
40  return _eData->_source->_owner->source(*this);
41  }
42 
47  inline Node Edge::target() const {
48  return _eData->_source->_owner->target(*this);
49  }
50 
56  inline const Node & Edge::sourceRef() const {
57  return _eData->_source->_owner->sourceRef(*this);
58  }
59 
65  inline const Node & Edge::targetRef() const {
66  return _eData->_source->_owner->targetRef(*this);
67  }
68 
72  inline bool Edge::valid() const {
73  return (_eData != nullptr &&
74  _eData->_source != nullptr &&
75  _eData->_target != nullptr &&
76  (*this) == (*(_eData->_sourcePos))
77  && (*this) == (*(_eData->_targetPos))
78  && (_eData->_identifier < _eData->_source->owner().edgeCapacity()));
79  }
80 
85  inline const Graph & Edge::owner() const {
86  require(Exception, valid(), "create: invalid edge");
87  return _eData->_source->owner();
88  }
89 
93  inline bool Edge::operator<(const Edge & edge) const {
94  if (!valid()) {
95  return edge.valid();
96  }
97  if (!edge.valid()) {
98  return false;
99  }
100  if (&(owner()) != &(edge.owner())) {
101  return &(owner()) < &(edge.owner());
102  }
103  return id() < edge.id();
104  }
105 
115  inline void Edge::create(EdgeId idt, NodeData * s, NodeData * t) {
116  require(Exception, _eData == nullptr, "create: _eData!=nullptr");
117  require(Exception, s != nullptr && t != nullptr, "create: s==nullptr || t==nullptr)");
118  require(Exception, &(s->owner()) == &(t->owner()), "create: s and t do not have the same owner");
119  _eData = new EdgeData(idt, s, t);
120  _eData->setSourcePos(s->newOutEdge(_eData));
121  _eData->setTargetPos(t->newInEdge(_eData));
122  ensure(Exception, valid(), "create: invalid edge");
123  }
124 
129  inline void Edge::changeSource(NodeData * s) {
130  require(Exception, valid(), "changeSource: invalid edge");
131  require(Exception, s != nullptr, "changeSource: invalid state");
132  require(Exception, &(_eData->_target->owner()) == &(s->owner()), "changeSource: _eData->_target->owner()!=s->_owner");
133  _eData->_source->delOutEdge(_eData);
134  _eData->_source = s;
135  _eData->_sourcePos = s->newOutEdge(_eData);
136  ensure(Exception, valid(), "Edge::changeSource: invalid edge");
137  }
138 
143  inline void Edge::changeTarget(NodeData * t) {
144  require(Exception, valid(), "changeTarget: invalid edge");
145  require(Exception, t != nullptr, "changeTarget: invalid target state");
146  require(Exception, &(_eData->_source->owner()) == &(t->owner()), "Edge::changeTarget: source().owner()!=_target->_owner");
147  _eData->_target->delInEdge(_eData);
148  _eData->_target = t;
149  _eData->_targetPos = t->newInEdge(_eData);
150  ensure(Exception, valid(), "changeTarget: invalid edge");
151  }
152 
153 
154 
155  // template<class Archive>
156  // void Edge::save(Archive & ar, const unsigned int version) const
157  // {
158  // Graph g = owner();
159  // ar & g;
160  // unsigned idt = id();
161  // ar & idt;
162  // }
163  //
164  //
165  // template<class Archive>
166  // void Edge::load(Archive & ar, const unsigned int version)
167  // {
168  // Graph g;
169  // ar & g;
170  // unsigned id;
171  // ar & id;
172  // if(g.valid())
173  // {
174  // operator=(g.getEdge(id));
175  // }
176  // else
177  // {
178  // _eData = 0;
179  // }
180  // }
181 
182  };
183 };
184 
185 #endif
void changeTarget(NodeData *t)
Definition: EdgeImpl.hh:143
NodeData * _source
identifier of the Edge
Definition: EdgeData.hh:106
void create(EdgeId idt, NodeData *s, NodeData *t)
Definition: EdgeImpl.hh:115
NodeData * _target
pointer to the NodeData source of the Edge
Definition: EdgeData.hh:108
const Graph & owner() const
Definition: EdgeImpl.hh:85
const Graph & owner() const
Definition: NodeImpl.hh:31
#define ensure(Exception, expr, message)
Definition: Assertion.hh:98
bool valid() const
Definition: GraphIntOld.hh:683
#define require(Exception, expr, message)
Definition: Assertion.hh:90
Namespace of the Diades project.
list< Edge >::iterator newOutEdge(EdgeData *eData)
Definition: Edge.hh:306
bool operator<(const Edge &edge) const
Definition: EdgeImpl.hh:93
EdgeId id() const
Definition: Edge.hh:177
boost::adjacency_list Graph
Definition: ScaleFree.cc:9
string details() const
Definition: EdgeImpl.hh:24
list< Edge >::iterator newInEdge(EdgeData *eData)
Definition: Edge.hh:294
EdgeData::EdgeId EdgeId
Definition: Edge.hh:45
unsigned id() const
Definition: GraphInt.hh:242
bool valid() const
Definition: EdgeImpl.hh:72
const Node & sourceRef() const
Definition: EdgeImpl.hh:56
void changeSource(NodeData *s)
Definition: EdgeImpl.hh:129
Node target() const
Definition: EdgeImpl.hh:47
Node source() const
Definition: EdgeImpl.hh:39
const Node & targetRef() const
Definition: EdgeImpl.hh:65