Tulip  4.0.0
Better Visualization Through Research
 All Classes Files Functions Variables Enumerations Enumerator Properties Groups Pages
minmaxproperty.cxx
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 #include <tulip/Graph.h>
20 #include <tulip/Coord.h>
21 
22 template<typename nodeType, typename edgeType>
23 tlp::MinMaxProperty<nodeType, edgeType>::MinMaxProperty(tlp::Graph* graph, std::string name, typename nodeType::RealType NodeMin,
24  typename nodeType::RealType NodeMax, typename edgeType::RealType EdgeMin, typename edgeType::RealType EdgeMax)
25  : AbstractProperty<nodeType, edgeType>(graph, name), nodeValueUptodate(false), edgeValueUptodate(false), _nodeMin(NodeMin), _nodeMax(NodeMax), _edgeMin(EdgeMin), _edgeMax(EdgeMax) {
26 }
27 
28 template<typename nodeType, typename edgeType>
30  if(!graph) {
31  graph = this->graph;
32  }
33 
34  unsigned int graphID = graph->getId();
35  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(graphID);
36 
37  if ((it == nodeValueUptodate.end()) || ((*it).second == false))
38  computeMinMaxNode(graph);
39 
40  return minNode[graphID];
41 }
42 
43 template<typename nodeType, typename edgeType>
45  if(!graph) {
46  graph = this->graph;
47  }
48 
49  unsigned int graphID = graph->getId();
50  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(graphID);
51 
52  if ((it == nodeValueUptodate.end()) || ((*it).second == false))
53  computeMinMaxNode(graph);
54 
55  return maxNode[graphID];
56 }
57 
58 template<typename nodeType, typename edgeType>
60  if(!graph) {
61  graph = this->graph;
62  }
63 
64  unsigned int graphID = graph->getId();
65  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(graphID);
66 
67  if ((it == edgeValueUptodate.end()) || ((*it).second == false))
68  computeMinMaxEdge(graph);
69 
70  return minEdge[graphID];
71 }
72 
73 template<typename nodeType, typename edgeType>
75  if(!graph) {
76  graph = this->graph;
77  }
78 
79  unsigned int graphID = graph->getId();
80  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(graphID);
81 
82  if ((it == edgeValueUptodate.end()) || ((*it).second == false))
83  computeMinMaxEdge(graph);
84 
85  return maxEdge[graphID];
86 }
87 
88 template<typename nodeType, typename edgeType>
90  if(!graph) {
91  graph = this->graph;
92  }
93 
94  typename nodeType::RealType maxN2 = _nodeMin, minN2 = _nodeMax;
95 
96  Iterator<node>* nodeIterator = graph->getNodes();
97 
98  while (nodeIterator->hasNext()) {
99  node n=nodeIterator->next();
100  typename nodeType::RealType tmp = this->getNodeValue(n);
101 
102  if (tmp > maxN2) {
103  maxN2 = tmp;
104  }
105 
106  if (tmp < minN2) {
107  minN2 = tmp;
108  }
109  }
110 
111  delete nodeIterator;
112 
113  unsigned int sgi = graph->getId();
114 
115  nodeValueUptodate[sgi]=true;
116  minNode[sgi] = minN2;
117  maxNode[sgi] = maxN2;
118 }
119 
120 template<typename nodeType, typename edgeType>
122  typename edgeType::RealType maxE2 = _edgeMin, minE2 = _edgeMax;
123 
124  Iterator<edge>* edgeIterator = graph->getEdges();
125 
126  while (edgeIterator->hasNext()) {
127  edge ite=edgeIterator->next();
128  typename edgeType::RealType tmp = this->getEdgeValue(ite);
129 
130  if (tmp>maxE2)
131  maxE2 = tmp;
132 
133  if (tmp<minE2)
134  minE2 = tmp;
135  }
136 
137  delete edgeIterator;
138 
139  unsigned int sgi = graph->getId();
140 
141  edgeValueUptodate[sgi]=true;
142  minEdge[sgi]=minE2;
143  maxEdge[sgi]=maxE2;
144 }
145 
146 template<typename nodeType, typename edgeType>
147 void tlp::MinMaxProperty<nodeType, edgeType>::updateNodeValue(tlp::node n, typename nodeType::RealType newValue) {
148  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.begin();
149 
150  if (it != nodeValueUptodate.end()) {
151  typename nodeType::RealType oldV = this->getNodeValue(n);
152 
153  if (newValue != oldV) {
154  // loop on subgraph min/max
155  for(; it != nodeValueUptodate.end(); ++it) {
156  // if min/max is ok for the current subgraph
157  // check if min or max has to be updated
158  if ((*it).second == true) {
159  unsigned int gid = (*it).first;
160  typename nodeType::RealType minV = minNode[gid];
161  typename nodeType::RealType maxV = maxNode[gid];
162 
163  // check if min or max has to be updated
164  if ((newValue < minV) || (newValue > maxV) || (oldV == minV) || (oldV == maxV)) {
165  nodeValueUptodate.clear();
166  break;
167  }
168  }
169  }
170  }
171  }
172 }
173 
174 template<typename nodeType, typename edgeType>
175 void tlp::MinMaxProperty<nodeType, edgeType>::updateEdgeValue(tlp::edge e, typename edgeType::RealType newValue) {
176  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.begin();
177 
178  if (it != edgeValueUptodate.end()) {
179  typename edgeType::RealType oldV = this->getEdgeValue(e);
180 
181  if (newValue != oldV) {
182  // loop on subgraph min/max
183  for(; it != edgeValueUptodate.end(); ++it) {
184  // if min/max is ok for the current subgraph
185  // check if min or max has to be updated
186  if ((*it).second == true) {
187  unsigned int gid = (*it).first;
188  typename edgeType::RealType minV = minEdge[gid];
189  typename edgeType::RealType maxV = maxEdge[gid];
190 
191  // check if min or max has to be updated
192  if ((newValue < minV) || (newValue > maxV) || (oldV == minV) || (oldV == maxV)) {
193  edgeValueUptodate.clear();
194  break;
195  }
196  }
197  }
198  }
199  }
200 }
201 
202 template<typename nodeType, typename edgeType>
203 void tlp::MinMaxProperty<nodeType, edgeType>::updateAllNodesValues(typename nodeType::RealType newValue) {
204  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.begin();
205 
206  if (it != nodeValueUptodate.end()) {
207  // loop on subgraph min/max
208  for(; it != nodeValueUptodate.end(); ++it) {
209  unsigned int gid = (*it).first;
210  minNode[gid] = maxNode[gid] = newValue;
211  nodeValueUptodate[gid] = true;
212  }
213  }
214 }
215 
216 template<typename nodeType, typename edgeType>
217 void tlp::MinMaxProperty<nodeType, edgeType>::updateAllEdgesValues(typename edgeType::RealType newValue) {
218  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.begin();
219 
220  if (it != edgeValueUptodate.end()) {
221  // loop on subgraph min/max
222  for(; it != edgeValueUptodate.end(); ++it) {
223  unsigned int gid = (*it).first;
224  minEdge[gid] = maxEdge[gid] = newValue;
225  edgeValueUptodate[gid] = true;
226  }
227  }
228 }
229 
230 template<typename nodeType, typename edgeType>
232  const GraphEvent* graphEvent = dynamic_cast<const tlp::GraphEvent*>(&ev);
233 
234  if (graphEvent) {
235  tlp::Graph* graph = graphEvent->getGraph();
236 
237  switch(graphEvent->getType()) {
238  case GraphEvent::TLP_ADD_NODE:
239  nodeValueUptodate.clear();
240  break;
241 
242  case GraphEvent::TLP_DEL_NODE: {
243  unsigned int sgi = graph->getId();
244  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = nodeValueUptodate.find(sgi);
245 
246  if (it != nodeValueUptodate.end() && it->second) {
247  typename nodeType::RealType oldV = this->getNodeValue(graphEvent->getNode());
248 
249  // check if min or max has to be updated
250  if ((oldV == minNode[sgi]) || (oldV == maxNode[sgi]))
251  nodeValueUptodate[sgi] = false;
252  }
253 
254  break;
255  }
256 
257  case GraphEvent::TLP_ADD_EDGE:
258  edgeValueUptodate.clear();
259  break;
260 
261  case GraphEvent::TLP_DEL_EDGE: {
262  unsigned int sgi = graph->getId();
263  TLP_HASH_MAP<unsigned int, bool>::const_iterator it = edgeValueUptodate.find(sgi);
264 
265  if (it != edgeValueUptodate.end() && it->second) {
266  typename edgeType::RealType oldV = this->getEdgeValue(graphEvent->getEdge());
267 
268  // check if min or max has to be updated
269  if ((oldV == minEdge[sgi]) || (oldV == maxEdge[sgi])) {
270  edgeValueUptodate[sgi] = false;
271  }
272  }
273 
274  break;
275  }
276 
277  case GraphEvent::TLP_AFTER_ADD_SUBGRAPH:
278  graphEvent->getSubGraph()->addListener(this);
279  break;
280 
281  case GraphEvent::TLP_BEFORE_DEL_SUBGRAPH:
282  graphEvent->getSubGraph()->removeListener(this);
283  break;
284 
285  default:
286  // we don't care about the rest
287  break;
288  }
289  }
290 }