Tulip  4.4.0
Better Visualization Through Research
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Groups Pages
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 1 and Inria Bordeaux - Sud Ouest
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 namespace tlp {
28 
29 /**
30  * @brief Abstracts the computation of minimal and maximal values on node and edge values of properties.
31  *
32  * The value is lazily computed on first request.
33  * The value is cached, and the cache is invalidated whenever it cannot be simply updated.
34  **/
35 template<typename nodeType, typename edgeType, typename propType=PropertyInterface>
36 class MinMaxProperty : public tlp::AbstractProperty<nodeType, edgeType, propType> {
37 public:
38  /**
39  * @brief Constructs a MinMaxProperty.
40  *
41  * @param graph The graph to attach the property to.
42  * @param name The name of the property.
43  * @param NodeMin The minimal value the property can take for nodes (e.g. INT_MIN)
44  * @param NodeMax The maximal value the property can take for nodes (e.g. INT_MIN)
45  * @param EdgeMin The minimal value the property can take for edges (e.g. INT_MIN)
46  * @param EdgeMax The maximal value the property can take for edges (e.g. INT_MIN)
47  **/
48  MinMaxProperty(tlp::Graph* graph, std::string name, typename nodeType::RealType NodeMin,
49  typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin, typename edgeType::RealType EdgeMax);
50 
51  virtual void treatEvent(const tlp::Event& ev);
52 
53  /**
54  * @brief Gets the minimum value on the nodes.
55  * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
56  *
57  * @param graph The graph on which to compute.
58  * @return The minimal value on this graph for this property.
59  **/
60  typename nodeType::RealType getNodeMin(Graph* graph = NULL);
61 
62  /**
63  * @brief Computes the maximum value on the nodes.
64  * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
65  *
66  * @param graph The graph on which to compute.
67  * @return The maximal value on this graph for this property.
68  **/
69  typename nodeType::RealType getNodeMax(Graph* graph = NULL);
70 
71  /**
72  * @brief Computes the minimum value on the edges.
73  * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
74  *
75  * @param graph The graph on which to compute.
76  * @return The minimal value on this graph for this property.
77  **/
78  typename edgeType::RealType getEdgeMin(Graph* graph = NULL);
79 
80  /**
81  * @brief Computes the maximum value on the edges.
82  * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
83  *
84  * @param graph The graph on which to compute.
85  * @return The maximal value on this graph for this property.
86  **/
87  typename edgeType::RealType getEdgeMax(Graph* graph = NULL);
88 
89  /**
90  * @brief Updates the value on a node, and updates the minimal/maximal cached values if necessary.
91  * Should be called by subclasses in order to keep the cache up to date.
92  *
93  * @param n The node for which the value is updated.
94  * @param newValue The new value on this node.
95  **/
96  void updateNodeValue(tlp::node n, typename nodeType::RealType newValue);
97 
98  /**
99  * @brief Updates the value on an edge, 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 e The edge for which the value is updated.
103  * @param newValue The new value on this edge.
104  **/
105  void updateEdgeValue(tlp::edge e, typename edgeType::RealType newValue);
106 
107  /**
108  * @brief Updates the value of all nodes, setting the maximum and minimum values to this.
109  * Should be called by subclasses in order to keep the cache up to date.
110  *
111  * @param newValue The new maximal and minimal value.
112  **/
113  void updateAllNodesValues(typename nodeType::RealType newValue);
114 
115  /**
116  * @brief Updates the value of all edges, setting the maximum and minimum values to this.
117  * Should be called by subclasses in order to keep the cache up to date.
118  *
119  * @param newValue The new maximal and minimal value.
120  **/
121  void updateAllEdgesValues(typename edgeType::RealType newValue);
122 
123 protected:
124  TLP_HASH_MAP<unsigned int, typename nodeType::RealType> maxNode, minNode;
125  TLP_HASH_MAP<unsigned int, typename edgeType::RealType> maxEdge, minEdge;
126  TLP_HASH_MAP<unsigned int, bool> nodeValueUptodate;
127  TLP_HASH_MAP<unsigned int, bool> edgeValueUptodate;
128 
129  typename nodeType::RealType _nodeMin;
130  typename nodeType::RealType _nodeMax;
131  typename edgeType::RealType _edgeMin;
132  typename edgeType::RealType _edgeMax;
133 
134  void computeMinMaxNode(Graph* graph);
135  void computeMinMaxEdge(Graph* graph);
136 };
137 
138 }
139 
140 #include "cxx/minmaxproperty.cxx"
141 
142 #endif //MINMAXPROPERTY_H