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