20 #include <tulip/BasicIterators.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 =
nullptr;
33 template <
class Tnode,
class Tedge,
class Tprop>
34 inline typename Tnode::RealType
36 return nodeDefaultValue;
39 template <
class Tnode,
class Tedge,
class Tprop>
40 inline typename Tedge::RealType
42 return edgeDefaultValue;
45 template <
class Tnode,
class Tedge,
class Tprop>
46 inline typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue
49 return nodeProperties.get(n.
id);
52 template <
class Tnode,
class Tedge,
class Tprop>
53 inline typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue
56 return edgeProperties.get(e.
id);
59 template <
class Tnode,
class Tedge,
class Tprop>
61 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue val,
62 const Graph *sg)
const {
68 if (sg == this->graph)
69 it = nodeProperties.findAll(val);
72 return new tlp::SGraphNodeIterator<typename Tnode::RealType>(sg, nodeProperties, val);
74 return (
new tlp::UINTIterator<node>(it));
77 template <
class Tnode,
class Tedge,
class Tprop>
79 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue val,
80 const Graph *sg)
const {
86 if (sg == this->graph)
87 it = edgeProperties.findAll(val);
90 return new tlp::SGraphEdgeIterator<typename Tedge::RealType>(sg, edgeProperties, val);
92 return (
new tlp::UINTIterator<edge>(it));
95 template <
class Tnode,
class Tedge,
class Tprop>
97 const tlp::node n,
typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue v) {
99 Tprop::notifyBeforeSetNodeValue(n);
100 nodeProperties.set(n.
id, v);
101 Tprop::notifyAfterSetNodeValue(n);
104 template <
class Tnode,
class Tedge,
class Tprop>
106 const tlp::edge e,
typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v) {
108 Tprop::notifyBeforeSetEdgeValue(e);
109 edgeProperties.set(e.
id, v);
110 Tprop::notifyAfterSetEdgeValue(e);
113 template <
class Tnode,
class Tedge,
class Tprop>
115 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue v) {
116 Tprop::notifyBeforeSetAllNodeValue();
117 nodeDefaultValue = v;
118 nodeProperties.setAll(v);
119 Tprop::notifyAfterSetAllNodeValue();
122 template <
class Tnode,
class Tedge,
class Tprop>
124 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue v) {
125 if (nodeDefaultValue == v) {
130 auto oldDefaultValue = nodeDefaultValue;
132 std::vector<tlp::node> nodesOldDefaultToUpdate;
133 std::vector<tlp::node> nodesDefaultToUpdate;
135 for (
auto n : this->getGraph()->nodes()) {
136 auto val = nodeProperties.get(n.id);
138 if (val == oldDefaultValue) {
139 nodesOldDefaultToUpdate.push_back(n);
140 }
else if (val == v) {
141 nodesDefaultToUpdate.push_back(n);
146 nodeDefaultValue = v;
147 nodeProperties.setDefault(v);
151 for (
size_t i = 0; i < nodesOldDefaultToUpdate.size(); ++i) {
152 nodeProperties.set(nodesOldDefaultToUpdate[i].
id, oldDefaultValue);
157 for (
size_t i = 0; i < nodesDefaultToUpdate.size(); ++i) {
158 nodeProperties.set(nodesDefaultToUpdate[i].
id, v,
true);
162 template <
class Tnode,
class Tedge,
class Tprop>
164 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue v,
const Graph *graph) {
165 setValueToGraphNodes(v, graph);
168 template <
class Tnode,
class Tedge,
class Tprop>
170 typename tlp::StoredType<typename Tnode::RealType>::ReturnedConstValue v,
const Graph *graph) {
171 if (this->getGraph() == graph || this->getGraph()->isDescendantGraph(graph)) {
172 for (
auto n : graph->
nodes()) {
178 template <
class Tnode,
class Tedge,
class Tprop>
180 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v) {
181 if (edgeDefaultValue == v) {
186 auto oldDefaultValue = edgeDefaultValue;
188 std::vector<tlp::edge> edgesOldDefaultToUpdate;
190 std::vector<tlp::edge> edgesDefaultToUpdate;
192 for (
auto e : this->getGraph()->edges()) {
193 auto val = edgeProperties.get(e.id);
195 if (val == oldDefaultValue) {
196 edgesOldDefaultToUpdate.push_back(e);
197 }
else if (val == v) {
198 edgesDefaultToUpdate.push_back(e);
203 edgeDefaultValue = v;
204 edgeProperties.setDefault(v);
208 for (
size_t i = 0; i < edgesOldDefaultToUpdate.size(); ++i) {
209 edgeProperties.set(edgesOldDefaultToUpdate[i].
id, oldDefaultValue);
214 for (
size_t i = 0; i < edgesDefaultToUpdate.size(); ++i) {
215 edgeProperties.set(edgesDefaultToUpdate[i].
id, v,
true);
219 template <
class Tnode,
class Tedge,
class Tprop>
221 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v) {
222 Tprop::notifyBeforeSetAllEdgeValue();
223 edgeDefaultValue = v;
224 edgeProperties.setAll(v);
225 Tprop::notifyAfterSetAllEdgeValue();
228 template <
class Tnode,
class Tedge,
class Tprop>
230 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v,
const Graph *graph) {
231 setValueToGraphEdges(v, graph);
234 template <
class Tnode,
class Tedge,
class Tprop>
236 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v,
const Graph *graph) {
237 if (this->getGraph() == graph || this->getGraph()->isDescendantGraph(graph)) {
238 for (
auto e : graph->
edges()) {
244 template <
class Tnode,
class Tedge,
class Tprop>
246 const typename Tnode::RealType &n1Value = getNodeValue(n1);
247 const typename Tnode::RealType &n2Value = getNodeValue(n2);
248 return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
251 template <
class Tnode,
class Tedge,
class Tprop>
253 const typename Tedge::RealType &e1Value = getEdgeValue(e1);
254 const typename Tedge::RealType &e2Value = getEdgeValue(e2);
255 return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
262 template <
typename ELT_TYPE>
265 ELT_TYPE
next()
override {
266 ELT_TYPE tmp = curElt;
268 if ((_hasnext = it->hasNext())) {
271 while (!(_hasnext = (!graph || graph->isElement(curElt)))) {
282 : it(itN), graph(g), curElt(ELT_TYPE()), _hasnext(
false) {
289 ~GraphEltIterator()
override {
302 template <
class Tnode,
class Tedge,
class Tprop>
306 new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue,
false));
308 if (Tprop::name.empty())
312 return new GraphEltIterator<tlp::node>(g !=
nullptr ? g : Tprop::graph, it);
314 return ((g ==
nullptr) || (g == Tprop::graph)) ? it :
new GraphEltIterator<tlp::node>(g, it);
317 template <
class Tnode,
class Tedge,
class Tprop>
320 return nodeProperties.hasNonDefaultValues();
326 template <
class Tnode,
class Tedge,
class Tprop>
330 return nodeProperties.numberOfNonDefaultValues();
336 template <
class Tnode,
class Tedge,
class Tprop>
338 return Tnode::valueSize();
341 template <
class Tnode,
class Tedge,
class Tprop>
343 Tnode::writeb(oss, nodeDefaultValue);
346 template <
class Tnode,
class Tedge,
class Tprop>
349 Tnode::writeb(oss, nodeProperties.get(n.
id));
352 template <
class Tnode,
class Tedge,
class Tprop>
354 if (Tnode::readb(iss, nodeDefaultValue)) {
355 nodeProperties.setAll(nodeDefaultValue);
362 template <
class Tnode,
class Tedge,
class Tprop>
364 typename Tnode::RealType val;
366 if (Tnode::readb(iss, val)) {
367 nodeProperties.set(n.
id, val);
374 template <
class Tnode,
class Tedge,
class Tprop>
378 new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue,
false));
380 if (Tprop::name.empty())
384 return new GraphEltIterator<tlp::edge>(g !=
nullptr ? g : Tprop::graph, it);
386 return ((g ==
nullptr) || (g == Tprop::graph)) ? it :
new GraphEltIterator<tlp::edge>(g, it);
389 template <
class Tnode,
class Tedge,
class Tprop>
392 return edgeProperties.hasNonDefaultValues();
398 template <
class Tnode,
class Tedge,
class Tprop>
402 return edgeProperties.numberOfNonDefaultValues();
408 template <
class Tnode,
class Tedge,
class Tprop>
410 return Tedge::valueSize();
413 template <
class Tnode,
class Tedge,
class Tprop>
415 Tedge::writeb(oss, edgeDefaultValue);
418 template <
class Tnode,
class Tedge,
class Tprop>
421 Tedge::writeb(oss, edgeProperties.get(e.
id));
424 template <
class Tnode,
class Tedge,
class Tprop>
426 if (Tedge::readb(iss, edgeDefaultValue)) {
427 edgeProperties.setAll(edgeDefaultValue);
434 template <
class Tnode,
class Tedge,
class Tprop>
436 typename Tedge::RealType val;
438 if (Tedge::readb(iss, val)) {
439 edgeProperties.set(e.
id, val);
446 template <
typename vectType,
typename eltType,
typename propType>
447 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(
451 template <
typename vectType,
typename eltType,
typename propType>
452 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::tokenize(
453 const std::string &s, std::vector<std::string> &vect,
char openChar,
char sepChar,
455 return vectType::tokenize(s, vect, openChar, sepChar, closeChar);
458 template <
typename vectType,
typename eltType,
typename propType>
459 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(
460 const node n,
const std::vector<std::string> &vs) {
461 typename vectType::RealType v;
462 if (!vectType::read(vs, v))
465 this->setNodeValue(n, v);
469 template <
typename vectType,
typename eltType,
typename propType>
470 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(
471 const node n,
const std::string &s,
char openChar,
char sepChar,
char closeChar) {
472 typename vectType::RealType v;
473 std::istringstream iss(s);
475 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
478 this->setNodeValue(n, v);
482 template <
typename vectType,
typename eltType,
typename propType>
483 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(
484 const edge e,
const std::vector<std::string> &vs) {
485 typename vectType::RealType v;
486 if (!vectType::read(vs, v))
489 this->setEdgeValue(e, v);
493 template <
typename vectType,
typename eltType,
typename propType>
494 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(
495 const edge e,
const std::string &s,
char openChar,
char sepChar,
char closeChar) {
496 typename vectType::RealType v;
497 std::istringstream iss(s);
499 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
502 this->setEdgeValue(e, v);
506 template <
typename vectType,
typename eltType,
typename propType>
507 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(
508 const node n,
unsigned int i,
509 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
512 typename vectType::RealType &vect =
514 assert(vect.size() > i);
515 this->propType::notifyBeforeSetNodeValue(n);
520 typename vectType::RealType tmp(vect);
525 this->propType::notifyAfterSetNodeValue(n);
528 template <
typename vectType,
typename eltType,
typename propType>
529 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
530 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(
const node n,
531 unsigned int i)
const {
533 const typename vectType::RealType &vect =
535 assert(vect.size() > i);
539 template <
typename vectType,
typename eltType,
typename propType>
540 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(
541 const node n,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
544 typename vectType::RealType &vect =
546 this->propType::notifyBeforeSetNodeValue(n);
551 typename vectType::RealType tmp(vect);
556 this->propType::notifyAfterSetNodeValue(n);
559 template <
typename vectType,
typename eltType,
typename propType>
560 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(
const node n) {
563 typename vectType::RealType &vect =
565 this->propType::notifyBeforeSetNodeValue(n);
566 assert(isNotDefault);
568 this->propType::notifyAfterSetNodeValue(n);
571 template <
typename vectType,
typename eltType,
typename propType>
572 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(
573 const node n,
size_t size,
typename eltType::RealType elt) {
576 typename vectType::RealType &vect =
578 assert(isNotDefault);
579 this->propType::notifyBeforeSetNodeValue(n);
580 vect.resize(size, elt);
581 this->propType::notifyAfterSetNodeValue(n);
584 template <
typename vectType,
typename eltType,
typename propType>
585 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(
586 const edge e,
unsigned int i,
587 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
590 typename vectType::RealType &vect =
592 assert(vect.size() > i);
593 this->propType::notifyBeforeSetEdgeValue(e);
598 typename vectType::RealType tmp(vect);
603 this->propType::notifyAfterSetEdgeValue(e);
606 template <
typename vectType,
typename eltType,
typename propType>
607 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
608 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(
const edge e,
609 unsigned int i)
const {
611 const typename vectType::RealType &vect =
613 assert(vect.size() > i);
616 template <
typename vectType,
typename eltType,
typename propType>
617 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(
618 const edge e,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
621 typename vectType::RealType &vect =
623 this->propType::notifyBeforeSetEdgeValue(e);
628 typename vectType::RealType tmp(vect);
633 this->propType::notifyAfterSetEdgeValue(e);
636 template <
typename vectType,
typename eltType,
typename propType>
637 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(
const edge e) {
640 typename vectType::RealType &vect =
642 this->propType::notifyBeforeSetEdgeValue(e);
643 assert(isNotDefault);
645 this->propType::notifyAfterSetEdgeValue(e);
648 template <
typename vectType,
typename eltType,
typename propType>
649 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(
650 const edge e,
size_t size,
typename eltType::RealType elt) {
653 typename vectType::RealType &vect =
655 assert(isNotDefault);
656 this->propType::notifyBeforeSetEdgeValue(e);
657 vect.resize(size, elt);
658 this->propType::notifyAfterSetEdgeValue(e);
bool iteratorEmpty(Iterator< T > *it)
Checks if an iterator is empty.
unsigned int iteratorCount(Iterator< T > *it)
Counts the number of iterated elements.
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
virtual T next()=0
Moves the Iterator on the next element.
Tedge::RealType getEdgeDefaultValue() const
Gets the default edge value of the property.
virtual tlp::Iterator< node > * getNodesEqualTo(typename tlp::StoredType< typename Tnode::RealType >::ReturnedConstValue v, const Graph *g=nullptr) 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...
Tnode::RealType getNodeDefaultValue() const
Gets the default node value of the property.
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 ...
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...
bool isValid() const
isValid checks if the edge is valid. An invalid edge is an edge whose id is UINT_MAX.
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...
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.
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.
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.
virtual tlp::Iterator< edge > * getEdgesEqualTo(typename tlp::StoredType< typename Tedge::RealType >::ReturnedConstValue v, const Graph *g=nullptr) const
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.
bool isValid() const
isValid checks if the node is valid. An invalid node is a node whose id is UINT_MAX.
virtual bool hasNext()=0
Tells if the sequence is at its end.
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 ...