Tulip  4.6.0
Better Visualization Through Research
library/tulip-core/include/tulip/minmaxproperty.h
00001 /*
00002  *
00003  * This file is part of Tulip (www.tulip-software.org)
00004  *
00005  * Authors: David Auber and the Tulip development Team
00006  * from LaBRI, University of Bordeaux
00007  *
00008  * Tulip is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation, either version 3
00011  * of the License, or (at your option) any later version.
00012  *
00013  * Tulip is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016  * See the GNU General Public License for more details.
00017  *
00018  */
00019 
00020 #ifndef MINMAXPROPERTY_H
00021 #define MINMAXPROPERTY_H
00022 
00023 #include <tulip/tuliphash.h>
00024 #include <tulip/Observable.h>
00025 #include <tulip/AbstractProperty.h>
00026 
00027 #define MINMAX_PAIR(TYPE) std::pair<typename TYPE::RealType, typename TYPE::RealType>
00028 #define MINMAX_MAP(TYPE) typename TLP_HASH_MAP<unsigned int, MINMAX_PAIR(TYPE) >
00029 
00030 namespace tlp {
00031 
00032 /**
00033  * @brief Abstracts the computation of minimal and maximal values on node and edge values of properties.
00034  *
00035  * The value is lazily computed on first request.
00036  * The value is cached, and the cache is invalidated whenever it cannot be simply updated.
00037  **/
00038 template<typename nodeType, typename edgeType, typename propType=PropertyInterface>
00039 class MinMaxProperty : public tlp::AbstractProperty<nodeType, edgeType, propType> {
00040 public:
00041   /**
00042    * @brief Constructs a MinMaxProperty.
00043    *
00044    * @param graph The graph to attach the property to.
00045    * @param name The name of the property.
00046    * @param NodeMin The minimal value the property can take for nodes (e.g. INT_MIN)
00047    * @param NodeMax The maximal value the property can take for nodes (e.g. INT_MIN)
00048    * @param EdgeMin The minimal value the property can take for edges (e.g. INT_MIN)
00049    * @param EdgeMax The maximal value the property can take for edges (e.g. INT_MIN)
00050    **/
00051   MinMaxProperty(tlp::Graph* graph, const std::string& name, typename nodeType::RealType NodeMin,
00052                  typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin, typename edgeType::RealType EdgeMax);
00053 
00054   virtual void treatEvent(const tlp::Event& ev);
00055 
00056   /**
00057    * @brief Gets the minimum value on the nodes.
00058    * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
00059    *
00060    * @param graph The graph on which to compute.
00061    * @return The minimal value on this graph for this property.
00062    **/
00063   typename nodeType::RealType getNodeMin(Graph* graph = NULL);
00064 
00065   /**
00066    * @brief Computes the maximum value on the nodes.
00067    * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
00068    *
00069    * @param graph The graph on which to compute.
00070    * @return The maximal value on this graph for this property.
00071    **/
00072   typename nodeType::RealType getNodeMax(Graph* graph = NULL);
00073 
00074   /**
00075    * @brief Computes the minimum value on the edges.
00076    * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
00077    *
00078    * @param graph The graph on which to compute.
00079    * @return The minimal value on this graph for this property.
00080    **/
00081   typename edgeType::RealType getEdgeMin(Graph* graph = NULL);
00082 
00083   /**
00084    * @brief Computes the maximum value on the edges.
00085    * It is only computed if it has never been retrieved before, or if the cached value could not be updated.
00086    *
00087    * @param graph The graph on which to compute.
00088    * @return The maximal value on this graph for this property.
00089    **/
00090   typename edgeType::RealType getEdgeMax(Graph* graph = NULL);
00091 
00092   /**
00093    * @brief Updates the value on a node, and updates the minimal/maximal cached values if necessary.
00094    * Should be called by subclasses in order to keep the cache up to date.
00095    *
00096    * @param n The node for which the value is updated.
00097    * @param newValue The new value on this node.
00098    **/
00099   void updateNodeValue(tlp::node n, typename nodeType::RealType newValue);
00100 
00101   /**
00102    * @brief Updates the value on an edge, and updates the minimal/maximal cached values if necessary.
00103    * Should be called by subclasses in order to keep the cache up to date.
00104    *
00105    * @param e The edge for which the value is updated.
00106    * @param newValue The new value on this edge.
00107    **/
00108   void updateEdgeValue(tlp::edge e, typename edgeType::RealType newValue);
00109 
00110   /**
00111    * @brief Updates the value of all nodes, setting the maximum and minimum values to this.
00112    * Should be called by subclasses in order to keep the cache up to date.
00113    *
00114    * @param newValue The new maximal and minimal value.
00115    **/
00116   void updateAllNodesValues(typename nodeType::RealType newValue);
00117 
00118   /**
00119    * @brief Updates the value of all edges, setting the maximum and minimum values to this.
00120    * Should be called by subclasses in order to keep the cache up to date.
00121    *
00122    * @param newValue The new maximal and minimal value.
00123    **/
00124   void updateAllEdgesValues(typename edgeType::RealType newValue);
00125 
00126 protected:
00127   MINMAX_MAP(nodeType) minMaxNode;
00128   MINMAX_MAP(edgeType) minMaxEdge;
00129 
00130   typename nodeType::RealType _nodeMin;
00131   typename nodeType::RealType _nodeMax;
00132   typename edgeType::RealType _edgeMin;
00133   typename edgeType::RealType _edgeMax;
00134 
00135   // this will indicate if we can stop propType::graph observation
00136   bool needGraphListener; // default is false
00137 
00138   MINMAX_PAIR(nodeType) computeMinMaxNode(Graph* graph);
00139   MINMAX_PAIR(edgeType) computeMinMaxEdge(Graph* graph);
00140   void removeListenersAndClearNodeMap();
00141   void removeListenersAndClearEdgeMap();
00142 };
00143 
00144 }
00145 
00146 #include "cxx/minmaxproperty.cxx"
00147 
00148 #endif //MINMAXPROPERTY_H
 All Classes Files Functions Variables Enumerations Enumerator Properties