Tulip  5.6.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 <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 
65  void treatEvent(const tlp::Event &ev) override;
66 
67  /**
68  * @brief Gets the minimum value on the nodes.
69  * It is only computed if it has never been retrieved before, or if the cached value could not be
70  *updated.
71  *
72  * @param graph The graph on which to compute.
73  * @return The minimal value on this graph for this property.
74  **/
75  CONST_NODE_VALUE getNodeMin(const Graph *graph = nullptr) {
76  return getNodeMinMax(graph).first;
77  }
78 
79  /**
80  * @brief Computes the maximum value on the nodes.
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 maximal value on this graph for this property.
86  **/
87  CONST_NODE_VALUE getNodeMax(const Graph *graph = nullptr) {
88  return getNodeMinMax(graph).second;
89  }
90 
91  /**
92  * @brief Computes the minimum value on the edges.
93  * It is only computed if it has never been retrieved before, or if the cached value could not be
94  *updated.
95  *
96  * @param graph The graph on which to compute.
97  * @return The minimal value on this graph for this property.
98  **/
99  CONST_EDGE_VALUE getEdgeMin(const Graph *graph = nullptr) {
100  return getEdgeMinMax(graph).first;
101  }
102 
103  /**
104  * @brief Computes the maximum value on the edges.
105  * It is only computed if it has never been retrieved before, or if the cached value could not be
106  *updated.
107  *
108  * @param graph The graph on which to compute.
109  * @return The maximal value on this graph for this property.
110  **/
111  CONST_EDGE_VALUE getEdgeMax(const Graph *graph = nullptr) {
112  return getEdgeMinMax(graph).second;
113  }
114 
115  /**
116  * @brief Updates the value on a node, and updates the minimal/maximal cached values if necessary.
117  * Should be called by subclasses in order to keep the cache up to date.
118  *
119  * @param n The node for which the value is updated.
120  * @param newValue The new value on this node.
121  **/
122  void updateNodeValue(tlp::node n, CONST_NODE_VALUE newValue);
123 
124  /**
125  * @brief Updates the value on an edge, and updates the minimal/maximal cached values if
126  *necessary.
127  * Should be called by subclasses in order to keep the cache up to date.
128  *
129  * @param e The edge for which the value is updated.
130  * @param newValue The new value on this edge.
131  **/
132  void updateEdgeValue(tlp::edge e, CONST_EDGE_VALUE newValue);
133 
134  /**
135  * @brief Updates the value of all nodes, setting the maximum and minimum values to this.
136  * Should be called by subclasses in order to keep the cache up to date.
137  *
138  * @param newValue The new maximal and minimal value.
139  **/
140  void updateAllNodesValues(CONST_NODE_VALUE newValue);
141 
142  /**
143  * @brief Updates the value of all edges, setting the maximum and minimum values to this.
144  * Should be called by subclasses in order to keep the cache up to date.
145  *
146  * @param newValue The new maximal and minimal value.
147  **/
148  void updateAllEdgesValues(CONST_EDGE_VALUE newValue);
149 
150 protected:
151  MINMAX_MAP(nodeType) minMaxNode;
152  MINMAX_MAP(edgeType) minMaxEdge;
153 
154  NODE_VALUE _nodeMin;
155  NODE_VALUE _nodeMax;
156  EDGE_VALUE _edgeMin;
157  EDGE_VALUE _edgeMax;
158 
159  // this will indicate if we can stop propType::graph observation
160  bool needGraphListener; // default is false
161 
162  const MINMAX_PAIR(nodeType) & computeMinMaxNode(const Graph *graph);
163  const MINMAX_PAIR(edgeType) & computeMinMaxEdge(const Graph *graph);
164  void removeListenersAndClearNodeMap();
165  void removeListenersAndClearEdgeMap();
166 };
167 } // namespace tlp
168 
169 #include "cxx/minmaxproperty.cxx"
170 
171 #endif // MINMAXPROPERTY_H
tlp::MinMaxProperty::getEdgeMin
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,...
Definition: minmaxproperty.h:99
tlp::MinMaxProperty::getEdgeMax
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,...
Definition: minmaxproperty.h:111
tlp::MinMaxProperty::MinMaxProperty
MinMaxProperty(tlp::Graph *graph, const std::string &name, NODE_VALUE NodeMin, NODE_VALUE NodeMax, EDGE_VALUE EdgeMin, EDGE_VALUE EdgeMax)
Constructs a MinMaxProperty.
Definition: minmaxproperty.cxx:23
tlp::MinMaxProperty::updateAllEdgesValues
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...
Definition: minmaxproperty.cxx:271
tlp::Graph
Definition: Graph.h:234
tlp::MinMaxProperty::treatEvent
void treatEvent(const tlp::Event &ev) override
This function is called when events are sent to the Listeners, and Listeners only.
Definition: minmaxproperty.cxx:284
tlp::MinMaxProperty::updateEdgeValue
void updateEdgeValue(tlp::edge e, CONST_EDGE_VALUE newValue)
Updates the value on an edge, and updates the minimal/maximal cached values if necessary....
Definition: minmaxproperty.cxx:224
tlp::MinMaxProperty::updateAllNodesValues
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...
Definition: minmaxproperty.cxx:258
tlp::MinMaxProperty::getNodeMax
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,...
Definition: minmaxproperty.h:87
tlp::AbstractProperty
This class extends upon PropertyInterface, and adds type-safe methods to get and set the node and edg...
Definition: AbstractProperty.h:55
tlp::Event
Event is the base class for all events used in the Observation mechanism.
Definition: Observable.h:52
tlp::MinMaxProperty::getNodeMin
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,...
Definition: minmaxproperty.h:75
tlp::MinMaxProperty::updateNodeValue
void updateNodeValue(tlp::node n, CONST_NODE_VALUE newValue)
Updates the value on a node, and updates the minimal/maximal cached values if necessary....
Definition: minmaxproperty.cxx:190
tlp
Definition: AbstractProperty.h:34
tlp::edge
The edge struct represents an edge in a Graph object.
Definition: Edge.h:40
tlp::node
The node struct represents a node in a Graph object.
Definition: Node.h:40
tlp::MinMaxProperty
Abstracts the computation of minimal and maximal values on node and edge values of properties.
Definition: minmaxproperty.h:46