Tulip  5.0.0
Large graphs analysis and drawing
SortIterator.h
1 /*
2  *
3  * This file is part of Tulip (www.tulip-software.org)
4  *
5  * Authors: David Auber and the Tulip development Team
6  * from LaBRI, University of Bordeaux
7  *
8  * Tulip is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation, either version 3
11  * of the License, or (at your option) any later version.
12  *
13  * Tulip is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef TULIP_SORTITERATOR_H
21 #define TULIP_SORTITERATOR_H
22 #include <vector>
23 #include <algorithm>
24 #include <tulip/Iterator.h>
25 #include <tulip/StableIterator.h>
26 #include <tulip/NumericProperty.h>
27 #include <tulip/Graph.h>
28 #include <tulip/Vector.h>
29 
30 namespace tlp {
31 class Graph;
32 
33 ///@cond DOXYGEN_HIDDEN
34 struct LessThan {
35  LessThan(const tlp::NumericProperty* m): metric(m) {
36  }
37  const tlp::NumericProperty* metric;
38  bool operator() (const node &n1, const node &n2) const {
39  return (metric->getNodeDoubleValue(n1) < metric->getNodeDoubleValue(n2));
40  }
41  bool operator() (const edge &e1, const edge &e2) const {
42  return (metric->getEdgeDoubleValue(e1) < metric->getEdgeDoubleValue(e2));
43  }
44 };
45 
46 struct LessThanEdgeTargetMetric {
47  LessThanEdgeTargetMetric(const Graph *sg, const tlp::NumericProperty* metric):
48  metric(metric), sg(sg) {
49  }
50  bool operator() (const edge &e1, const edge &e2) const {
51  return (metric->getNodeDoubleValue(sg->target(e1)) < metric->getNodeDoubleValue(sg->target(e2)));
52  }
53 private:
54  const tlp::NumericProperty* metric;
55  const Graph *sg;
56 };
57 
58 struct LessThanEdgeSourceMetric {
59  LessThanEdgeSourceMetric(const Graph *sg, const tlp::NumericProperty* metric):
60  metric(metric), sg(sg) {
61  }
62  bool operator() (const edge &e1, const edge &e2) const {
63  return (metric->getNodeDoubleValue(sg->source(e1)) < metric->getNodeDoubleValue(sg->source(e2)));
64  }
65 private:
66  const tlp::NumericProperty* metric;
67  const Graph *sg;
68 };
69 
70 struct LessThanEdgeExtremitiesMetric {
71  LessThanEdgeExtremitiesMetric(const Graph *sg, const tlp::NumericProperty* metric):
72  metric(metric), sg(sg) {
73  }
74  bool operator() (const edge &e1, const edge &e2) const {
75  std::pair<node, node> ends = sg->ends(e1);
76  Vec2d v1(metric->getNodeDoubleValue(ends.first), metric->getNodeDoubleValue(ends.second));
77  ends = sg->ends(e2);
78  Vec2d v2(metric->getNodeDoubleValue(ends.first), metric->getNodeDoubleValue(ends.second));
79  return v1 < v2;
80  }
81 private:
82  const tlp::NumericProperty* metric;
83  const Graph *sg;
84 };
85 ///@endcond
86 
87 /**
88 * @brief This Iterator sorts the nodes in a sequence based on their values in a NumericProperty.
89 **/
90 struct SortNodeIterator : public StableIterator<tlp::node> {
91  ///
92  SortNodeIterator(Iterator<tlp::node> *itIn, const tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::node>(itIn) {
93  LessThan tmp(metric);
94  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
95 
96  if (!ascendingOrder) {
97  reverse(sequenceCopy.begin(),sequenceCopy.end());
98  }
99 
100  copyIterator=sequenceCopy.begin();
101  }
102  ///
103  ~SortNodeIterator() {}
104 };
105 
106 /**
107 * @brief This Iterator sorts the edges in a sequence based on their values in a NumericProperty.
108 **/
109 struct SortEdgeIterator : public StableIterator<tlp::edge> {
110  ///
111  SortEdgeIterator(Iterator<tlp::edge> *itIn, const tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
112  LessThan tmp(metric);
113  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
114 
115  if (!ascendingOrder) {
116  reverse(sequenceCopy.begin(),sequenceCopy.end());
117  }
118 
119  copyIterator=sequenceCopy.begin();
120  }
121  ///
122  ~SortEdgeIterator() {}
123 };
124 
125 /**
126 * @brief This Iterator sorts the edges based on the values of their target nodes in a NumericProperty.
127 **/
128 struct SortTargetEdgeIterator : public StableIterator<tlp::edge> {
129  ///
130  SortTargetEdgeIterator(Iterator<tlp::edge> *itIn, const Graph* sg, const tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
131  LessThanEdgeTargetMetric tmp(sg,metric);
132  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
133 
134  if (!ascendingOrder) {
135  reverse(sequenceCopy.begin(),sequenceCopy.end());
136  }
137 
138  copyIterator=sequenceCopy.begin();
139  }
140  ///
142 };
143 
144 /**
145 * @brief This Iterator sorts the edges based on the values of their source nodes in a NumericProperty.
146 **/
147 struct SortSourceEdgeIterator : public StableIterator<tlp::edge> {
148  ///
149  SortSourceEdgeIterator(Iterator<tlp::edge> *itIn, const Graph* sg, const tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
150  LessThanEdgeSourceMetric tmp(sg,metric);
151  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
152 
153  if (!ascendingOrder) {
154  reverse(sequenceCopy.begin(),sequenceCopy.end());
155  }
156 
157  copyIterator=sequenceCopy.begin();
158  }
159  ///
161 };
162 
163 /**
164 * @brief This Iterator sorts the edges based on the values of their extremities nodes in a NumericProperty.
165 **/
166 struct SortExtremitiesEdgeIterator : public StableIterator<tlp::edge> {
167  ///
168  SortExtremitiesEdgeIterator(Iterator<tlp::edge> *itIn, const Graph* sg, const tlp::NumericProperty* metric, bool ascendingOrder = true) : StableIterator<tlp::edge>(itIn) {
169  LessThanEdgeExtremitiesMetric tmp(sg,metric);
170  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
171 
172  if (!ascendingOrder) {
173  reverse(sequenceCopy.begin(),sequenceCopy.end());
174  }
175 
176  copyIterator=sequenceCopy.begin();
177  }
178  ///
180 };
181 
182 }
183 #endif
This Iterator sorts the nodes in a sequence based on their values in a NumericProperty.
Definition: SortIterator.h:90
This Iterator sorts the edges based on the values of their target nodes in a NumericProperty.
Definition: SortIterator.h:128
virtual double getNodeDoubleValue(const node n) const =0
Returns the value associated with the node n in this property.
This Iterator sorts the edges based on the values of their source nodes in a NumericProperty.
Definition: SortIterator.h:147
Interface all numerical properties. Property values are always returned as double.
This Iterator sorts the edges in a sequence based on their values in a NumericProperty.
Definition: SortIterator.h:109
This Iterator sorts the edges based on the values of their extremities nodes in a NumericProperty...
Definition: SortIterator.h:166
virtual double getEdgeDoubleValue(const edge e) const =0
Returns the value associated with the edge e in this property.