20 #include <tulip/GraphIterator.h> 22 template <
class Tnode,
class Tedge,
class Tprop>
26 nodeDefaultValue = Tnode::defaultValue();
27 edgeDefaultValue = Tedge::defaultValue();
28 nodeProperties.setAll(Tnode::defaultValue());
29 edgeProperties.setAll(Tedge::defaultValue());
30 Tprop::metaValueCalculator = NULL;
33 template <
class Tnode,
class Tedge,
class Tprop>
35 return nodeDefaultValue;
38 template <
class Tnode,
class Tedge,
class Tprop>
40 return edgeDefaultValue;
43 template <
class Tnode,
class Tedge,
class Tprop>
46 return nodeProperties.get(n.
id);
49 template <
class Tnode,
class Tedge,
class Tprop>
52 return edgeProperties.get(e.
id);
55 template <
class Tnode,
class Tedge,
class Tprop>
57 if (sg == NULL) sg = this->graph;
59 tlp::Iterator<unsigned int> *it = NULL;
61 if (sg == this->graph)
62 it = nodeProperties.findAll(val);
65 return new tlp::SGraphNodeIterator<typename Tnode::RealType>(sg, nodeProperties, val);
67 return (
new tlp::UINTIterator<node>(it));
70 template <
class Tnode,
class Tedge,
class Tprop>
72 if (sg == NULL) sg = this->graph;
74 tlp::Iterator<unsigned int> *it = NULL;
76 if (sg == this->graph)
77 it = edgeProperties.findAll(val);
80 return new tlp::SGraphEdgeIterator<typename Tedge::RealType>(sg, edgeProperties, val);
82 return (
new tlp::UINTIterator<edge>(it));
85 template <
class Tnode,
class Tedge,
class Tprop>
88 Tprop::notifyBeforeSetNodeValue(n);
89 nodeProperties.set(n.
id,v);
90 Tprop::notifyAfterSetNodeValue(n);
93 template <
class Tnode,
class Tedge,
class Tprop>
96 Tprop::notifyBeforeSetEdgeValue(e);
97 edgeProperties.set(e.
id,v);
98 Tprop::notifyAfterSetEdgeValue(e);
101 template <
class Tnode,
class Tedge,
class Tprop>
103 Tprop::notifyBeforeSetAllNodeValue();
104 nodeDefaultValue = v;
105 nodeProperties.setAll(v);
106 Tprop::notifyAfterSetAllNodeValue();
109 template <
class Tnode,
class Tedge,
class Tprop>
111 if (nodeDefaultValue == v) {
116 typename Tnode::RealType oldDefaultValue = nodeDefaultValue;
118 std::vector<tlp::node> nodesOldDefaultToUpdate;
119 std::vector<tlp::node> nodesDefaultToUpdate;
120 const std::vector<tlp::node> &nodes = this->getGraph()->nodes();
122 for (
size_t i = 0 ; i < nodes.size() ; ++i) {
123 const typename Tnode::RealType &val = this->getNodeValue(nodes[i]);
125 if (val == oldDefaultValue) {
126 nodesOldDefaultToUpdate.push_back(nodes[i]);
129 nodesDefaultToUpdate.push_back(nodes[i]);
134 nodeDefaultValue = v;
135 nodeProperties.setDefault(v);
139 for (
size_t i = 0 ; i < nodesOldDefaultToUpdate.size() ; ++i) {
140 nodeProperties.set(nodesOldDefaultToUpdate[i].
id, oldDefaultValue);
145 for (
size_t i = 0 ; i < nodesDefaultToUpdate.size() ; ++i) {
146 nodeProperties.set(nodesDefaultToUpdate[i].
id, v);
150 template <
class Tnode,
class Tedge,
class Tprop>
152 setValueToGraphNodes(v, graph);
155 template <
class Tnode,
class Tedge,
class Tprop>
157 if (this->getGraph() == graph || this->getGraph()->isDescendantGraph(graph)) {
158 const std::vector<node>& nodes = graph->
nodes();
159 unsigned int nbNodes = nodes.size();
161 for(
unsigned int i = 0; i < nbNodes; ++i) {
162 setNodeValue(nodes[i], v);
167 template <
class Tnode,
class Tedge,
class Tprop>
169 if (edgeDefaultValue == v) {
174 typename Tedge::RealType oldDefaultValue = edgeDefaultValue;
176 std::vector<tlp::edge> edgesOldDefaultToUpdate;
178 std::vector<tlp::edge> edgesDefaultToUpdate;
179 const std::vector<tlp::edge> &edges = this->getGraph()->edges();
181 for (
size_t i = 0 ; i < edges.size() ; ++i) {
182 const typename Tedge::RealType &val = this->getEdgeValue(edges[i]);
184 if (val == oldDefaultValue) {
185 edgesOldDefaultToUpdate.push_back(edges[i]);
188 edgesDefaultToUpdate.push_back(edges[i]);
193 edgeDefaultValue = v;
194 edgeProperties.setDefault(v);
198 for (
size_t i = 0 ; i < edgesOldDefaultToUpdate.size() ; ++i) {
199 edgeProperties.set(edgesOldDefaultToUpdate[i].
id, oldDefaultValue);
204 for (
size_t i = 0 ; i < edgesDefaultToUpdate.size() ; ++i) {
205 edgeProperties.set(edgesDefaultToUpdate[i].
id, v);
209 template <
class Tnode,
class Tedge,
class Tprop>
211 Tprop::notifyBeforeSetAllEdgeValue();
212 edgeDefaultValue = v;
213 edgeProperties.setAll(v);
214 Tprop::notifyAfterSetAllEdgeValue();
217 template <
class Tnode,
class Tedge,
class Tprop>
219 setValueToGraphEdges(v, graph);
222 template <
class Tnode,
class Tedge,
class Tprop>
224 if (this->getGraph() == graph || this->getGraph()->isDescendantGraph(graph)) {
225 const std::vector<edge>& edges = graph->
edges();
226 unsigned int nbEdges = edges.size();
228 for(
unsigned int i = 0; i < nbEdges; ++i) {
229 setEdgeValue(edges[i], v);
234 template <
class Tnode,
class Tedge,
class Tprop>
236 const typename Tnode::RealType& n1Value = getNodeValue(n1);
237 const typename Tnode::RealType& n2Value = getNodeValue(n2);
238 return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
241 template <
class Tnode,
class Tedge,
class Tprop>
243 const typename Tedge::RealType& e1Value = getEdgeValue(e1);
244 const typename Tedge::RealType& e2Value = getEdgeValue(e2);
245 return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
252 template <
typename ELT_TYPE>
253 class GraphEltIterator :
public tlp::Iterator<ELT_TYPE> {
256 ELT_TYPE tmp = curElt;
258 if ((_hasnext = it->hasNext())) {
261 while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
262 if (!it->hasNext())
break;
270 GraphEltIterator(
const tlp::Graph* g, tlp::Iterator<ELT_TYPE>* itN)
271 :it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(
false) {
278 ~GraphEltIterator() {
283 tlp::Iterator<ELT_TYPE> *it;
291 template <
class Tnode,
class Tedge,
class Tprop>
293 tlp::Iterator<tlp::node> *it =
294 new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue,
false));
296 if (Tprop::name.empty())
300 return new GraphEltIterator<tlp::node>(g != NULL ? g : Tprop::graph, it);
302 return ((g == NULL) || (g == Tprop::graph)) ? it :
new GraphEltIterator<tlp::node>(g, it);
305 template <
class Tnode,
class Tedge,
class Tprop>
308 return nodeProperties.numberOfNonDefaultValues();
311 unsigned int ret = 0;
313 forEach(n, getNonDefaultValuatedNodes(g)) {
320 template <
class Tnode,
class Tedge,
class Tprop>
322 return Tnode::valueSize();
325 template <
class Tnode,
class Tedge,
class Tprop>
327 Tnode::writeb(oss, nodeDefaultValue);
330 template <
class Tnode,
class Tedge,
class Tprop>
334 Tnode::writeb(oss, nodeProperties.get(n.
id));
337 template <
class Tnode,
class Tedge,
class Tprop>
339 if (Tnode::readb(iss, nodeDefaultValue)) {
340 nodeProperties.setAll(nodeDefaultValue);
347 template <
class Tnode,
class Tedge,
class Tprop>
350 typename Tnode::RealType val;
352 if (Tnode::readb(iss, val)) {
353 nodeProperties.set(n.
id, val);
360 template <
class Tnode,
class Tedge,
class Tprop>
362 tlp::Iterator<tlp::edge>* it =
363 new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue,
false));
365 if (Tprop::name.empty())
369 return new GraphEltIterator<tlp::edge>(g != NULL ? g : Tprop::graph, it);
371 return ((g == NULL) || (g == Tprop::graph)) ? it :
new GraphEltIterator<tlp::edge>(g, it);
374 template <
class Tnode,
class Tedge,
class Tprop>
377 return edgeProperties.numberOfNonDefaultValues();
380 unsigned int ret = 0;
382 forEach(e, getNonDefaultValuatedEdges(g)) {
389 template <
class Tnode,
class Tedge,
class Tprop>
391 return Tedge::valueSize();
394 template <
class Tnode,
class Tedge,
class Tprop>
396 Tedge::writeb(oss, edgeDefaultValue);
399 template <
class Tnode,
class Tedge,
class Tprop>
403 Tedge::writeb(oss, edgeProperties.get(e.
id));
406 template <
class Tnode,
class Tedge,
class Tprop>
408 if (Tedge::readb(iss, edgeDefaultValue)) {
409 edgeProperties.setAll(edgeDefaultValue);
416 template <
class Tnode,
class Tedge,
class Tprop>
419 typename Tedge::RealType val;
421 if (Tedge::readb(iss, val)) {
422 edgeProperties.set(e.
id, val);
429 template <
typename vectType,
typename eltType,
typename propType>
432 template <
typename vectType,
typename eltType,
typename propType>
433 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(
const node n,
const std::string& s,
char openChar,
char sepChar,
char closeChar) {
434 typename vectType::RealType v;
435 std::istringstream iss(s);
437 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
440 this->setNodeValue(n, v);
444 template <
typename vectType,
typename eltType,
typename propType>
445 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(
const edge e,
const std::string& s,
char openChar,
char sepChar,
char closeChar) {
446 typename vectType::RealType v;
447 std::istringstream iss(s);
449 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
452 this->setEdgeValue(e, v);
456 template <
typename vectType,
typename eltType,
typename propType>
457 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(
const node n,
unsigned int i,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
460 typename vectType::RealType& vect =
462 assert(vect.size() > i);
463 this->propType::notifyBeforeSetNodeValue(n);
468 typename vectType::RealType tmp(vect);
473 this->propType::notifyAfterSetNodeValue(n);
476 template <
typename vectType,
typename eltType,
typename propType>
477 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
478 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(
const node n,
unsigned int i)
const {
480 const typename vectType::RealType& vect =
482 assert(vect.size() > i);
486 template <
typename vectType,
typename eltType,
typename propType>
487 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(
const node n,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
490 typename vectType::RealType& vect =
492 this->propType::notifyBeforeSetNodeValue(n);
497 typename vectType::RealType tmp(vect);
502 this->propType::notifyAfterSetNodeValue(n);
505 template <
typename vectType,
typename eltType,
typename propType>
506 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(
const node n) {
509 typename vectType::RealType& vect =
511 this->propType::notifyBeforeSetNodeValue(n);
512 assert(isNotDefault);
514 this->propType::notifyAfterSetNodeValue(n);
517 template <
typename vectType,
typename eltType,
typename propType>
518 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(
const node n,
size_t size,
typename eltType::RealType elt) {
521 typename vectType::RealType& vect =
523 assert(isNotDefault);
524 this->propType::notifyBeforeSetNodeValue(n);
525 vect.resize(size, elt);
526 this->propType::notifyAfterSetNodeValue(n);
529 template <
typename vectType,
typename eltType,
typename propType>
530 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(
const edge e,
unsigned int i,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
533 typename vectType::RealType& vect =
535 assert(vect.size() > i);
536 this->propType::notifyBeforeSetEdgeValue(e);
541 typename vectType::RealType tmp(vect);
546 this->propType::notifyAfterSetEdgeValue(e);
549 template <
typename vectType,
typename eltType,
typename propType>
550 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
551 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(
const edge e,
unsigned int i)
const {
553 const typename vectType::RealType& vect =
555 assert(vect.size() > i);
558 template <
typename vectType,
typename eltType,
typename propType>
559 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(
const edge e,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
562 typename vectType::RealType& vect =
564 this->propType::notifyBeforeSetEdgeValue(e);
569 typename vectType::RealType tmp(vect);
574 this->propType::notifyAfterSetEdgeValue(e);
577 template <
typename vectType,
typename eltType,
typename propType>
578 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(
const edge e) {
581 typename vectType::RealType& vect =
583 this->propType::notifyBeforeSetEdgeValue(e);
584 assert(isNotDefault);
586 this->propType::notifyAfterSetEdgeValue(e);
589 template <
typename vectType,
typename eltType,
typename propType>
590 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(
const edge e,
size_t size,
typename eltType::RealType elt) {
593 typename vectType::RealType& vect =
595 assert(isNotDefault);
596 this->propType::notifyBeforeSetEdgeValue(e);
597 vect.resize(size, elt);
598 this->propType::notifyAfterSetEdgeValue(e);
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue getNodeValue(const node n) const
Returns the value associated with the node n in this property. If there is no value, it returns the default node value.
virtual tlp::Iterator< edge > * getEdgesEqualTo(typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v, const Graph *g=NULL) const
virtual void setAllEdgeValue(typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v)
Sets the value of all edges and notify the observers. All previous values are lost and the given valu...
virtual void setValueToGraphNodes(typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v, const Graph *graph)
Sets the value of all nodes in a graph and notify the observers. Only the nodes from that graph will ...
bool isValid() const
isValid checks if the node is valid. An invalid node is a node whose id is UINT_MAX.
virtual const std::vector< edge > & edges() const =0
Return a const reference on the vector of edges of the graph It is the fastest way to access to edges...
virtual const std::vector< node > & nodes() const =0
Return a const reference on the vector of nodes of the graph It is the fastest way to access to nodes...
Tnode::RealType getNodeDefaultValue() const
Gets the default node value of the property.
virtual void setAllNodeValue(typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v)
Sets the value of all nodes and notify the observers. All previous values are lost and the given valu...
The edge struct represents an edge in a Graph object.
The node struct represents a node in a Graph object.
virtual void setNodeDefaultValue(typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v)
Sets the value assigned as the default one to the future added nodes.
bool isValid() const
isValid checks if the edge is valid. An invalid edge is an edge whose id is UINT_MAX.
tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue getEdgeValue(const edge e) const
Returns the value associated to the edge e in this property. If there is no value, it returns the default edge value.
Tedge::RealType getEdgeDefaultValue() const
Gets the default edge value of the property.
virtual void setEdgeDefaultValue(typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v)
Sets the value assigned as the default one to the future added edges.
unsigned int id
id The identifier of the node.
virtual void setEdgeValue(const edge e, typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v)
Set the value of an edge and notify the observers of a modification.
virtual tlp::Iterator< node > * getNodesEqualTo(typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v, const Graph *g=NULL) const
virtual void setNodeValue(const node n, typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v)
Sets the value of a node and notify the observers of a modification.
unsigned int id
id The identifier of the edge.
virtual void setValueToGraphEdges(typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v, const Graph *graph)
Sets the value of all edges in a graph and notify the observers. Only the edges from that graph will ...