COIN-OR::LEMON - Graph Library

source: glemon-0.x/mapstorage.cc @ 77:ba023627ff1a

gui
Last change on this file since 77:ba023627ff1a was 77:ba023627ff1a, checked in by Balazs Dezso, 15 years ago

Using std:: prefix for the functions in the c* headers

File size: 8.0 KB
Line 
1#include "mapstorage.h"
2#include <gtkmm.h>
3#include <cmath>
4
5#include <cmath>
6
7MapStorage::MapStorage() : modified(false), file_name("")
8{
9  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
10  coords.setXMap(*nodemap_storage["coordinates_x"]);
11  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
12  coords.setYMap(*nodemap_storage["coordinates_y"]);
13
14  nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
15  edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
16
17  nodemap_default["id"] = 1.0;
18  edgemap_default["id"] = 1.0;
19}
20
21MapStorage::~MapStorage()
22{
23  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
24      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
25  {
26    delete it->second;
27  }
28  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
29      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
30  {
31    delete it->second;
32  }
33}
34
35int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
36{
37  if( nodemap_storage.find(name) == nodemap_storage.end() )
38    {
39      nodemap_storage[name]=nodemap;
40      // set the maps default value
41      nodemap_default[name] = default_value;
42      return 0;
43    }
44  return 1;
45}
46
47int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
48{
49  if( edgemap_storage.find(name) == edgemap_storage.end() )
50    {
51      edgemap_storage[name]=edgemap;
52      // set the maps default value
53      edgemap_default[name] = default_value;
54      return 0;
55    }
56  return 1;
57}
58
59double MapStorage::maxOfNodeMap(const std::string & name)
60{
61  double max=0;
62  for (NodeIt j(graph); j!=INVALID; ++j)
63  {
64    if( (*nodemap_storage[name])[j]>max )
65    {
66      max=(*nodemap_storage[name])[j];
67    }
68  }
69  return max;
70}
71
72double MapStorage::maxOfEdgeMap(const std::string & name)
73{
74  double max=0;
75  for (EdgeIt j(graph); j!=INVALID; ++j)
76  {
77    if( (*edgemap_storage[name])[j]>max )
78    {
79      max=(*edgemap_storage[name])[j];
80    }
81  }
82  return max;
83}
84
85double MapStorage::minOfNodeMap(const std::string & name)
86{
87  NodeIt j(graph);
88  double min;
89  if(j!=INVALID)
90    {
91      min=(*nodemap_storage[name])[j];
92    }
93  else
94    {
95      min=0;
96    }
97  for (; j!=INVALID; ++j)
98  {
99    if( (*nodemap_storage[name])[j]<min )
100    {
101      min=(*nodemap_storage[name])[j];
102    }
103  }
104  return min;
105}
106
107double MapStorage::minOfEdgeMap(const std::string & name)
108{
109  EdgeIt j(graph);
110  double min;
111  if(j!=INVALID)
112    {
113      min=(*edgemap_storage[name])[j];
114    }
115  else
116    {
117      min=0;
118    }
119  for (EdgeIt j(graph); j!=INVALID; ++j)
120  {
121    if( (*edgemap_storage[name])[j]<min )
122    {
123      min=(*edgemap_storage[name])[j];
124    }
125  }
126  return min;
127}
128
129int MapStorage::readFromFile(const std::string &filename)
130{
131  bool read_x = false;
132  bool read_y = false;
133  bool read_edge_id = false;
134
135  try {
136    LemonReader lreader(filename);
137    ContentReader content(lreader);
138    lreader.run();
139
140    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
141    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
142
143    GraphReader<Graph> greader(filename, graph);
144    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
145        it != nodeMapNames.end(); ++it)
146    {
147      if (*it == "coordinates_x")
148      {
149        read_x = true;
150        //std::cout << "read X nodemap" << std::endl;
151      }
152      else if (*it == "coordinates_y")
153      {
154        read_y = true;
155        //std::cout << "read Y nodemap" << std::endl;
156      }
157      else if (*it == "id")
158      {
159        //std::cout << "read id nodemap" << std::endl;
160      }
161      else
162      {
163        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
164        //std::cout << "read " << *it << " nodemap" << std::endl;
165      }
166      greader.readNodeMap(*it, *nodemap_storage[*it]);
167    }
168    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
169        it != edgeMapNames.end(); ++it)
170    {
171      if (*it == "id")
172      {
173        //std::cout << "read id edgemap" << std::endl;
174      }
175      else
176      {
177        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
178        //std::cout << "read " << *it << " edgemap" << std::endl;
179      }
180      greader.readEdgeMap(*it, *edgemap_storage[*it]);
181    }
182    greader.run();
183  } catch (DataFormatError& error) {
184    Gtk::MessageDialog mdialog("Read Error");
185    mdialog.set_message(error.what());
186    mdialog.run();
187    clear();
188    return 1;
189  }
190
191  if (!read_edge_id)
192  {
193    edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
194    int i = 1;
195    for (EdgeIt e(graph); e != INVALID; ++e)
196    {
197      (*edgemap_storage["id"])[e] = i++;
198    }
199  }
200
201  if (!read_x || !read_y)
202  {
203    int node_num = 0;
204    for (NodeIt n(graph); n != INVALID; ++n)
205    {
206      node_num++;
207    }
208    const double pi = 3.142;
209    double step = 2 * pi / (double) node_num;
210    int i = 0;
211    for (NodeIt n(graph); n != INVALID; ++n)
212    {
213      nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
214      nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
215      i++;
216    }
217  }
218
219  // fill in the default values for the maps
220  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
221      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
222  {
223    if ((it->first != "id") &&
224        (it->first != "coordiantes_x") &&
225        (it->first != "coordinates_y"))
226    {
227      nodemap_default[it->first] = 0.0;
228    }
229    else if (it->first == "id")
230    {
231      NodeIt n(graph);
232      double max = (*nodemap_storage["id"])[n];
233      for (; n != INVALID; ++n)
234      {
235        if ((*nodemap_storage["id"])[n] > max)
236          max = (*nodemap_storage["id"])[n];
237      }
238      nodemap_default["id"] = max + 1.0;
239    }
240  }
241  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
242      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
243  {
244    if (it->first != "id")
245    {
246      edgemap_default[it->first] = 0.0;
247    }
248    else
249    {
250      double max = std::numeric_limits<double>::min();
251      for (EdgeIt e(graph); e != INVALID; ++e)
252      {
253        if ((*edgemap_storage["id"])[e] > max)
254          max = (*edgemap_storage["id"])[e];
255      }
256      if (max > std::numeric_limits<double>::min())
257        edgemap_default["id"] = max + 1.0;
258      else
259        edgemap_default["id"] = 1.0;
260    }
261  }
262
263  return 0;
264}
265
266void MapStorage::writeToFile(const std::string &filename)
267{
268  GraphWriter<Graph> gwriter(filename, graph);
269
270  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
271      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
272  {
273    gwriter.writeNodeMap(it->first, *(it->second));
274    //std::cout << "wrote " << it->first << " nodemap" << std::endl;
275  }
276  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
277      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
278  {
279    gwriter.writeEdgeMap(it->first, *(it->second));
280    //std::cout << "wrote " << it->first << " edgemap" << std::endl;
281  }
282  gwriter.run();
283}
284
285void MapStorage::clear()
286{
287  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
288      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
289  {
290    if ((it->first != "coordinates_x") &&
291        (it->first != "coordinates_y") &&
292        (it->first != "id"))
293    {
294      delete it->second;
295      nodemap_storage.erase(it);
296    }
297  }
298  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
299      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
300  {
301    if (it->first != "id")
302    {
303      delete it->second;
304      edgemap_storage.erase(it);
305    }
306  }
307  for (std::map<std::string, double>::iterator it =
308      nodemap_default.begin(); it != nodemap_default.end(); ++it)
309  {
310    if (it->first != "id")
311      nodemap_default.erase(it);
312  }
313  for (std::map<std::string, double>::iterator it =
314      edgemap_default.begin(); it != edgemap_default.end(); ++it)
315  {
316    if (it->first != "id")
317      edgemap_default.erase(it);
318  }
319  graph.clear();
320  file_name = "";
321  modified = false;
322}
Note: See TracBrowser for help on using the repository browser.