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