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