Tulip  4.3.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
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 1 and Inria Bordeaux - Sud Ouest
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 ///@cond DOXYGEN_HIDDEN
20 
21 #ifndef TULIP_SORTITERATOR_H
22 #define TULIP_SORTITERATOR_H
23 #include <vector>
24 #include <algorithm>
25 #include <tulip/Iterator.h>
26 #include <tulip/StableIterator.h>
27 #include <tulip/DoubleProperty.h>
28 
29 namespace tlp {
30 class Graph;
31 
32 #ifndef DOXYGEN_NOTFOR_DEVEL
33 struct LessThan {
34  LessThan(DoubleProperty* m): metric(m) {
35  }
36  DoubleProperty* metric;
37  bool operator() (node n1,node n2) {
38  return (metric->getNodeValue(n1) < metric->getNodeValue(n2));
39  }
40 };
41 
42 struct LessThanEdgeTargetMetric {
43  LessThanEdgeTargetMetric(Graph *sg, DoubleProperty* metric):
44  metric(metric), sg(sg) {
45  }
46  bool operator() (edge e1,edge e2) {
47  return (metric->getNodeValue(sg->target(e1)) < metric->getNodeValue(sg->target(e2)));
48  }
49 private:
50  DoubleProperty* metric;
51  Graph *sg;
52 };
53 #endif // DOXYGEN_NOTFOR_DEVEL
54 
55 
56 /**
57 * @brief This Iterator sorts the nodes in a sequence based on their values in a DoubleProperty.
58 **/
59 struct SortNodeIterator : public StableIterator<tlp::node> {
60  ///
61  SortNodeIterator(Iterator<tlp::node> *itIn, DoubleProperty* metric):StableIterator<tlp::node>(itIn) {
62  LessThan tmp(metric);
63  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
64  copyIterator=sequenceCopy.begin();
65  }
66  ///
67  ~SortNodeIterator() {}
68 };
69 
70 /**
71 * @brief This Iterator sorts the edges based on the values of their target nodes in a DoubleProperty.
72 **/
73 struct SortTargetEdgeIterator : public StableIterator<tlp::edge> {
74  ///
75  SortTargetEdgeIterator(Iterator<tlp::edge> *itIn, Graph* sg, DoubleProperty* metric):StableIterator<tlp::edge>(itIn) {
76  LessThanEdgeTargetMetric tmp(sg,metric);
77  sort(sequenceCopy.begin(),sequenceCopy.end(),tmp);
78  copyIterator=sequenceCopy.begin();
79  }
80  ///
81  ~SortTargetEdgeIterator() {}
82 };
83 
84 }
85 #endif
86 ///@endcond