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