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 *g) {
165 auto graph = this->getGraph();
166 if (v == nodeDefaultValue) {
170 else if (graph->isDescendantGraph(g)) {
171 auto it = getNonDefaultValuatedNodes(g);
172 while (it->hasNext()) {
173 setNodeValue(it->next(), v);
178 for (
auto n : g->
nodes()) {
184 template <
class Tnode,
class Tedge,
class Tprop>
186 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v) {
187 if (edgeDefaultValue == v) {
192 auto oldDefaultValue = edgeDefaultValue;
194 std::vector<tlp::edge> edgesOldDefaultToUpdate;
196 std::vector<tlp::edge> edgesDefaultToUpdate;
198 for (
auto e : this->getGraph()->edges()) {
199 auto val = edgeProperties.get(e.id);
201 if (val == oldDefaultValue) {
202 edgesOldDefaultToUpdate.push_back(e);
203 }
else if (val == v) {
204 edgesDefaultToUpdate.push_back(e);
209 edgeDefaultValue = v;
210 edgeProperties.setDefault(v);
214 for (
size_t i = 0; i < edgesOldDefaultToUpdate.size(); ++i) {
215 edgeProperties.set(edgesOldDefaultToUpdate[i].
id, oldDefaultValue);
220 for (
size_t i = 0; i < edgesDefaultToUpdate.size(); ++i) {
221 edgeProperties.set(edgesDefaultToUpdate[i].
id, v,
true);
225 template <
class Tnode,
class Tedge,
class Tprop>
227 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v) {
228 Tprop::notifyBeforeSetAllEdgeValue();
229 edgeDefaultValue = v;
230 edgeProperties.setAll(v);
231 Tprop::notifyAfterSetAllEdgeValue();
234 template <
class Tnode,
class Tedge,
class Tprop>
236 typename tlp::StoredType<typename Tedge::RealType>::ReturnedConstValue v,
const Graph *g) {
237 auto graph = this->getGraph();
238 if (v == edgeDefaultValue) {
242 else if (graph->isDescendantGraph(g)) {
243 auto it = getNonDefaultValuatedEdges(g);
244 while (it->hasNext()) {
245 setEdgeValue(it->next(), v);
250 for (
auto e : g->
edges()) {
256 template <
class Tnode,
class Tedge,
class Tprop>
258 const typename Tnode::RealType &n1Value = getNodeValue(n1);
259 const typename Tnode::RealType &n2Value = getNodeValue(n2);
260 return (n1Value < n2Value) ? -1 : ((n1Value == n2Value) ? 0 : 1);
263 template <
class Tnode,
class Tedge,
class Tprop>
265 const typename Tedge::RealType &e1Value = getEdgeValue(e1);
266 const typename Tedge::RealType &e2Value = getEdgeValue(e2);
267 return (e1Value < e2Value) ? -1 : ((e1Value == e2Value) ? 0 : 1);
274 template <
typename ELT_TYPE>
277 ELT_TYPE
next()
override {
280 if ((_hasnext = _it->hasNext())) {
283 while (!_graph->isElement(_elt)) {
284 if (!_it->hasNext()) {
296 : _it(it), _graph(g), _elt(ELT_TYPE()), _hasnext(false) {
304 ~GraphEltIterator()
override {
319 template <
typename ELT_TYPE,
typename VALUE_TYPE>
320 class GraphEltNonDefaultValueIterator :
public tlp::Iterator<ELT_TYPE> {
322 ELT_TYPE
next()
override {
325 if ((_hasnext = _it->hasNext())) {
328 while (_values.get(_elt.id) == _defaultValue) {
329 if (!_it->hasNext()) {
340 GraphEltNonDefaultValueIterator(
342 const tlp::MutableContainer<typename VALUE_TYPE::RealType> &values)
343 : _it(it), _values(values), _elt(ELT_TYPE()), _hasnext(false),
344 _defaultValue(values.getDefault()) {
351 ~GraphEltNonDefaultValueIterator()
override {
357 const tlp::MutableContainer<typename VALUE_TYPE::RealType> &_values;
360 typename tlp::StoredType<typename VALUE_TYPE::RealType>::ReturnedValue _defaultValue;
362 #define NB_THRESHOLD(nb) nb / 2
365 template <
class Tnode,
class Tedge,
class Tprop>
368 auto nb = nodeProperties.numberOfNonDefaultValues();
374 if (Tprop::name.empty() || (g->
numberOfNodes() > NB_THRESHOLD(nb))) {
376 new tlp::UINTIterator<tlp::node>(nodeProperties.findAll(nodeDefaultValue,
false));
378 if (Tprop::name.empty())
382 return new GraphEltIterator<tlp::node>(g, it);
384 return (g == Tprop::graph) ? it :
new GraphEltIterator<tlp::node>(g, it);
387 return new GraphEltNonDefaultValueIterator<tlp::node, Tnode>(g->
getNodes(), nodeProperties);
390 template <
class Tnode,
class Tedge,
class Tprop>
392 if ((g ==
nullptr) || ((g == Tprop::graph) && !Tprop::name.empty())) {
393 return nodeProperties.hasNonDefaultValues();
399 template <
class Tnode,
class Tedge,
class Tprop>
402 if ((g ==
nullptr) || ((g == Tprop::graph) && !Tprop::name.empty())) {
403 return nodeProperties.numberOfNonDefaultValues();
409 template <
class Tnode,
class Tedge,
class Tprop>
411 return Tnode::valueSize();
414 template <
class Tnode,
class Tedge,
class Tprop>
416 Tnode::writeb(oss, nodeDefaultValue);
419 template <
class Tnode,
class Tedge,
class Tprop>
422 Tnode::writeb(oss, nodeProperties.get(n.
id));
425 template <
class Tnode,
class Tedge,
class Tprop>
427 if (Tnode::readb(iss, nodeDefaultValue)) {
428 nodeProperties.setAll(nodeDefaultValue);
435 template <
class Tnode,
class Tedge,
class Tprop>
437 typename Tnode::RealType val;
439 if (Tnode::readb(iss, val)) {
440 nodeProperties.set(n.
id, val);
447 template <
class Tnode,
class Tedge,
class Tprop>
450 auto nb = edgeProperties.numberOfNonDefaultValues();
456 if (Tprop::name.empty() || (g->
numberOfEdges() > NB_THRESHOLD(nb))) {
458 new tlp::UINTIterator<tlp::edge>(edgeProperties.findAll(edgeDefaultValue,
false));
460 if (Tprop::name.empty())
464 return new GraphEltIterator<tlp::edge>(g !=
nullptr ? g : Tprop::graph, it);
466 return ((g ==
nullptr) || (g == Tprop::graph)) ? it :
new GraphEltIterator<tlp::edge>(g, it);
469 return new GraphEltNonDefaultValueIterator<tlp::edge, Tedge>(g->
getEdges(), edgeProperties);
472 template <
class Tnode,
class Tedge,
class Tprop>
474 if ((g ==
nullptr) || ((g == Tprop::graph) && !Tprop::name.empty())) {
475 return edgeProperties.hasNonDefaultValues();
481 template <
class Tnode,
class Tedge,
class Tprop>
484 if ((g ==
nullptr) || ((g == Tprop::graph) && !Tprop::name.empty())) {
485 return edgeProperties.numberOfNonDefaultValues();
491 template <
class Tnode,
class Tedge,
class Tprop>
493 return Tedge::valueSize();
496 template <
class Tnode,
class Tedge,
class Tprop>
498 Tedge::writeb(oss, edgeDefaultValue);
501 template <
class Tnode,
class Tedge,
class Tprop>
504 Tedge::writeb(oss, edgeProperties.get(e.
id));
507 template <
class Tnode,
class Tedge,
class Tprop>
509 if (Tedge::readb(iss, edgeDefaultValue)) {
510 edgeProperties.setAll(edgeDefaultValue);
517 template <
class Tnode,
class Tedge,
class Tprop>
519 typename Tedge::RealType val;
521 if (Tedge::readb(iss, val)) {
522 edgeProperties.set(e.
id, val);
529 template <
typename vectType,
typename eltType,
typename propType>
530 tlp::AbstractVectorProperty<vectType, eltType, propType>::AbstractVectorProperty(
534 template <
typename vectType,
typename eltType,
typename propType>
535 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::tokenize(
536 const std::string &s, std::vector<std::string> &vect,
char openChar,
char sepChar,
538 return vectType::tokenize(s, vect, openChar, sepChar, closeChar);
541 template <
typename vectType,
typename eltType,
typename propType>
542 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(
543 const node n,
const std::vector<std::string> &vs) {
544 typename vectType::RealType v;
545 if (!vectType::read(vs, v))
548 this->setNodeValue(n, v);
552 template <
typename vectType,
typename eltType,
typename propType>
553 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeStringValueAsVector(
554 const node n,
const std::string &s,
char openChar,
char sepChar,
char closeChar) {
555 typename vectType::RealType v;
556 std::istringstream iss(s);
558 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
561 this->setNodeValue(n, v);
565 template <
typename vectType,
typename eltType,
typename propType>
566 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(
567 const edge e,
const std::vector<std::string> &vs) {
568 typename vectType::RealType v;
569 if (!vectType::read(vs, v))
572 this->setEdgeValue(e, v);
576 template <
typename vectType,
typename eltType,
typename propType>
577 bool tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeStringValueAsVector(
578 const edge e,
const std::string &s,
char openChar,
char sepChar,
char closeChar) {
579 typename vectType::RealType v;
580 std::istringstream iss(s);
582 if (!vectType::read(iss, v, openChar, sepChar, closeChar))
585 this->setEdgeValue(e, v);
589 template <
typename vectType,
typename eltType,
typename propType>
590 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setNodeEltValue(
591 const node n,
unsigned int i,
592 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
595 typename vectType::RealType &vect =
596 AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
597 assert(vect.size() > i);
598 this->propType::notifyBeforeSetNodeValue(n);
603 typename vectType::RealType tmp(vect);
605 AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n.id, tmp);
608 this->propType::notifyAfterSetNodeValue(n);
611 template <
typename vectType,
typename eltType,
typename propType>
612 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
613 tlp::AbstractVectorProperty<vectType, eltType, propType>::getNodeEltValue(
const node n,
614 unsigned int i)
const {
616 const typename vectType::RealType &vect =
617 AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n);
618 assert(vect.size() > i);
622 template <
typename vectType,
typename eltType,
typename propType>
623 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackNodeEltValue(
624 const node n,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
627 typename vectType::RealType &vect =
628 AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
629 this->propType::notifyBeforeSetNodeValue(n);
634 typename vectType::RealType tmp(vect);
636 AbstractProperty<vectType, vectType, propType>::nodeProperties.set(n, tmp);
639 this->propType::notifyAfterSetNodeValue(n);
642 template <
typename vectType,
typename eltType,
typename propType>
643 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackNodeEltValue(
const node n) {
646 typename vectType::RealType &vect =
647 AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
648 this->propType::notifyBeforeSetNodeValue(n);
649 assert(isNotDefault);
651 this->propType::notifyAfterSetNodeValue(n);
654 template <
typename vectType,
typename eltType,
typename propType>
655 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeNodeValue(
656 const node n,
size_t size,
typename eltType::RealType elt) {
659 typename vectType::RealType &vect =
660 AbstractProperty<vectType, vectType, propType>::nodeProperties.get(n, isNotDefault);
661 assert(isNotDefault);
662 this->propType::notifyBeforeSetNodeValue(n);
663 vect.resize(size, elt);
664 this->propType::notifyAfterSetNodeValue(n);
667 template <
typename vectType,
typename eltType,
typename propType>
668 void tlp::AbstractVectorProperty<vectType, eltType, propType>::setEdgeEltValue(
669 const edge e,
unsigned int i,
670 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
673 typename vectType::RealType &vect =
674 AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
675 assert(vect.size() > i);
676 this->propType::notifyBeforeSetEdgeValue(e);
681 typename vectType::RealType tmp(vect);
683 AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
686 this->propType::notifyAfterSetEdgeValue(e);
689 template <
typename vectType,
typename eltType,
typename propType>
690 typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue
691 tlp::AbstractVectorProperty<vectType, eltType, propType>::getEdgeEltValue(
const edge e,
692 unsigned int i)
const {
694 const typename vectType::RealType &vect =
695 AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e);
696 assert(vect.size() > i);
699 template <
typename vectType,
typename eltType,
typename propType>
700 void tlp::AbstractVectorProperty<vectType, eltType, propType>::pushBackEdgeEltValue(
701 const edge e,
typename tlp::StoredType<typename eltType::RealType>::ReturnedConstValue v) {
704 typename vectType::RealType &vect =
705 AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
706 this->propType::notifyBeforeSetEdgeValue(e);
711 typename vectType::RealType tmp(vect);
713 AbstractProperty<vectType, vectType, propType>::edgeProperties.set(e, tmp);
716 this->propType::notifyAfterSetEdgeValue(e);
719 template <
typename vectType,
typename eltType,
typename propType>
720 void tlp::AbstractVectorProperty<vectType, eltType, propType>::popBackEdgeEltValue(
const edge e) {
723 typename vectType::RealType &vect =
724 AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
725 this->propType::notifyBeforeSetEdgeValue(e);
726 assert(isNotDefault);
728 this->propType::notifyAfterSetEdgeValue(e);
731 template <
typename vectType,
typename eltType,
typename propType>
732 void tlp::AbstractVectorProperty<vectType, eltType, propType>::resizeEdgeValue(
733 const edge e,
size_t size,
typename eltType::RealType elt) {
736 typename vectType::RealType &vect =
737 AbstractProperty<vectType, vectType, propType>::edgeProperties.get(e, isNotDefault);
738 assert(isNotDefault);
739 this->propType::notifyBeforeSetEdgeValue(e);
740 vect.resize(size, elt);
741 this->propType::notifyAfterSetEdgeValue(e);