Tulip  5.3.1
Large graphs analysis and drawing
minmaxproperty.h
1 /*
2  *
3  * This file is part of Tulip (http://tulip.labri.fr)
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 MINMAXPROPERTY_H
21 #define MINMAXPROPERTY_H
22 
23 #include <unordered_map>
24 
25 #include <tulip/Observable.h>
26 #include <tulip/AbstractProperty.h>
27 
28 #define MINMAX_PAIR(TYPE) std::pair<typename TYPE::RealType, typename TYPE::RealType>
29 #define MINMAX_MAP(TYPE) typename std::unordered_map<unsigned int, MINMAX_PAIR(TYPE)>
30 
31 namespace tlp {
32 
33 /**
34  * @brief Abstracts the computation of minimal and maximal values on node and edge values of
35  *properties.
36  *
37  * The value is lazily computed on first request.
38  * The value is cached, and the cache is invalidated whenever it cannot be simply updated.
39  **/
40 template <typename nodeType, typename edgeType, typename propType = PropertyInterface>
41 class MinMaxProperty : public tlp::AbstractProperty<nodeType, edgeType, propType> {
42 public:
43  /**
44  * @brief Constructs a MinMaxProperty.
45  *
46  * @param graph The graph to attach the property to.
47  * @param name The name of the property.
48  * @param NodeMin The minimal value the property can take for nodes (e.g. INT_MIN)
49  * @param NodeMax The maximal value the property can take for nodes (e.g. INT_MIN)
50  * @param EdgeMin The minimal value the property can take for edges (e.g. INT_MIN)
51  * @param EdgeMax The maximal value the property can take for edges (e.g. INT_MIN)
52  **/
53  MinMaxProperty(tlp::Graph *graph, const std::string &name, typename nodeType::RealType NodeMin,
54  typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin,
55  typename edgeType::RealType EdgeMax);
56 
57  void treatEvent(const tlp::Event &ev) override;
58 
59  /**
60  * @brief Gets the minimum value on the nodes.
61  * It is only computed if it has never been retrieved before, or if the cached value could not be
62  *updated.
63  *
64  * @param graph The graph on which to compute.
65  * @return The minimal value on this graph for this property.
66  **/
67  typename nodeType::RealType getNodeMin(const Graph *graph = nullptr);
68 
69  /**
70  * @brief Computes the maximum value on the nodes.
71  * It is only computed if it has never been retrieved before, or if the cached value could not be
72  *updated.
73  *
74  * @param graph The graph on which to compute.
75  * @return The maximal value on this graph for this property.
76  **/
77  typename nodeType::RealType getNodeMax(const Graph *graph = nullptr);
78 
79  /**
80  * @brief Computes the minimum value on the edges.
81  * It is only computed if it has never been retrieved before, or if the cached value could not be
82  *updated.
83  *
84  * @param graph The graph on which to compute.
85  * @return The minimal value on this graph for this property.
86  **/
87  typename edgeType::RealType getEdgeMin(const Graph *graph = nullptr);
88 
89  /**
90  * @brief Computes the maximum value on the edges.
91  * It is only computed if it has never been retrieved before, or if the cached value could not be
92  *updated.
93  *
94  * @param graph The graph on which to compute.
95  * @return The maximal value on this graph for this property.
96  **/
97  typename edgeType::RealType getEdgeMax(const Graph *graph = nullptr);
98 
99  /**
100  * @brief Updates the value on a node, and updates the minimal/maximal cached values if necessary.
101  * Should be called by subclasses in order to keep the cache up to date.
102  *
103  * @param n The node for which the value is updated.
104  * @param newValue The new value on this node.
105  **/
106  void updateNodeValue(tlp::node n, typename nodeType::RealType newValue);
107 
108  /**
109  * @brief Updates the value on an edge, and updates the minimal/maximal cached values if
110  *necessary.
111  * Should be called by subclasses in order to keep the cache up to date.
112  *
113  * @param e The edge for which the value is updated.
114  * @param newValue The new value on this edge.
115  **/
116  void updateEdgeValue(tlp::edge e, typename edgeType::RealType newValue);
117 
118  /**
119  * @brief Updates the value of all nodes, setting the maximum and minimum values to this.
120  * Should be called by subclasses in order to keep the cache up to date.
121  *
122  * @param newValue The new maximal and minimal value.
123  **/
124  void updateAllNodesValues(typename nodeType::RealType newValue);
125 
126  /**
127  * @brief Updates the value of all edges, setting the maximum and minimum values to this.
128  * Should be called by subclasses in order to keep the cache up to date.
129  *
130  * @param newValue The new maximal and minimal value.
131  **/
132  void updateAllEdgesValues(typename edgeType::RealType newValue);
133 
134 protected:
135  MINMAX_MAP(nodeType) minMaxNode;
136  MINMAX_MAP(edgeType) minMaxEdge;
137 
138  typename nodeType::RealType _nodeMin;
139  typename nodeType::RealType _nodeMax;
140  typename edgeType::RealType _edgeMin;
141  typename edgeType::RealType _edgeMax;
142 
143  // this will indicate if we can stop propType::graph observation
144  bool needGraphListener; // default is false
145 
146  MINMAX_PAIR(nodeType) computeMinMaxNode(const Graph *graph);
147  MINMAX_PAIR(edgeType) computeMinMaxEdge(const Graph *graph);
148  void removeListenersAndClearNodeMap();
149  void removeListenersAndClearEdgeMap();
150 };
151 } // namespace tlp
152 
153 #include "cxx/minmaxproperty.cxx"
154 
155 #endif // MINMAXPROPERTY_H
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
Abstracts the computation of minimal and maximal values on node and edge values of properties...
void updateAllNodesValues(typename nodeType::RealType newValue)
Updates the value of all nodes, setting the maximum and minimum values to this. Should be called by s...
MinMaxProperty(tlp::Graph *graph, const std::string &name, typename nodeType::RealType NodeMin, typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin, typename edgeType::RealType EdgeMax)
Constructs a MinMaxProperty.
edgeType::RealType getEdgeMax(const Graph *graph=nullptr)
Computes the maximum value on the edges. It is only computed if it has never been retrieved before...
nodeType::RealType getNodeMin(const Graph *graph=nullptr)
Gets the minimum value on the nodes. It is only computed if it has never been retrieved before...
The edge struct represents an edge in a Graph object.
Definition: Edge.h:40
The node struct represents a node in a Graph object.
Definition: Node.h:40
nodeType::RealType getNodeMax(const Graph *graph=nullptr)
Computes the maximum value on the nodes. It is only computed if it has never been retrieved before...
edgeType::RealType getEdgeMin(const Graph *graph=nullptr)
Computes the minimum value on the edges. It is only computed if it has never been retrieved before...
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
void updateAllEdgesValues(typename edgeType::RealType newValue)
Updates the value of all edges, setting the maximum and minimum values to this. Should be called by s...
void updateEdgeValue(tlp::edge e, typename edgeType::RealType newValue)
Updates the value on an edge, and updates the minimal/maximal cached values if necessary. Should be called by subclasses in order to keep the cache up to date.
void updateNodeValue(tlp::node n, typename nodeType::RealType newValue)
Updates the value on a node, and updates the minimal/maximal cached values if necessary. Should be called by subclasses in order to keep the cache up to date.