Better doc.
1 #include "mapstorage.h"
5 MapStorage::MapStorage() : modified(false), file_name("")
7 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
8 coords.setXMap(*nodemap_storage["coordinates_x"]);
9 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
10 coords.setYMap(*nodemap_storage["coordinates_y"]);
12 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
13 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
15 nodemap_default["id"] = 1.0;
16 edgemap_default["id"] = 1.0;
19 MapStorage::~MapStorage()
21 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
22 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
26 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
27 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
33 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
35 if( nodemap_storage.find(name) == nodemap_storage.end() )
37 nodemap_storage[name]=nodemap;
38 // set the maps default value
39 nodemap_default[name] = default_value;
45 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
47 if( edgemap_storage.find(name) == edgemap_storage.end() )
49 edgemap_storage[name]=edgemap;
50 // set the maps default value
51 edgemap_default[name] = default_value;
57 double MapStorage::maxOfNodeMap(const std::string & name)
60 for (NodeIt j(graph); j!=INVALID; ++j)
62 if( (*nodemap_storage[name])[j]>max )
64 max=(*nodemap_storage[name])[j];
70 double MapStorage::maxOfEdgeMap(const std::string & name)
73 for (EdgeIt j(graph); j!=INVALID; ++j)
75 if( (*edgemap_storage[name])[j]>max )
77 max=(*edgemap_storage[name])[j];
83 double MapStorage::minOfNodeMap(const std::string & name)
89 min=(*nodemap_storage[name])[j];
95 for (; j!=INVALID; ++j)
97 if( (*nodemap_storage[name])[j]<min )
99 min=(*nodemap_storage[name])[j];
105 double MapStorage::minOfEdgeMap(const std::string & name)
111 min=(*edgemap_storage[name])[j];
117 for (EdgeIt j(graph); j!=INVALID; ++j)
119 if( (*edgemap_storage[name])[j]<min )
121 min=(*edgemap_storage[name])[j];
127 int MapStorage::readFromFile(const std::string &filename)
131 bool read_edge_id = false;
134 LemonReader lreader(filename);
135 ContentReader content(lreader);
138 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
139 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
141 GraphReader<Graph> greader(filename, graph);
142 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
143 it != nodeMapNames.end(); ++it)
145 if (*it == "coordinates_x")
148 //std::cout << "read X nodemap" << std::endl;
150 else if (*it == "coordinates_y")
153 //std::cout << "read Y nodemap" << std::endl;
155 else if (*it == "id")
157 //std::cout << "read id nodemap" << std::endl;
161 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
162 //std::cout << "read " << *it << " nodemap" << std::endl;
164 greader.readNodeMap(*it, *nodemap_storage[*it]);
166 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
167 it != edgeMapNames.end(); ++it)
171 //std::cout << "read id edgemap" << std::endl;
175 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
176 //std::cout << "read " << *it << " edgemap" << std::endl;
178 greader.readEdgeMap(*it, *edgemap_storage[*it]);
181 } catch (DataFormatError& error) {
182 Gtk::MessageDialog mdialog("Read Error");
183 mdialog.set_message(error.what());
191 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
193 for (EdgeIt e(graph); e != INVALID; ++e)
195 (*edgemap_storage["id"])[e] = i++;
199 if (!read_x || !read_y)
202 for (NodeIt n(graph); n != INVALID; ++n)
206 const double pi = 3.142;
207 double step = 2 * pi / (double) node_num;
209 for (NodeIt n(graph); n != INVALID; ++n)
211 nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
212 nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
217 // fill in the default values for the maps
218 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
219 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
221 if ((it->first != "id") &&
222 (it->first != "coordiantes_x") &&
223 (it->first != "coordinates_y"))
225 nodemap_default[it->first] = 0.0;
227 else if (it->first == "id")
230 double max = (*nodemap_storage["id"])[n];
231 for (; n != INVALID; ++n)
233 if ((*nodemap_storage["id"])[n] > max)
234 max = (*nodemap_storage["id"])[n];
236 nodemap_default["id"] = max + 1.0;
239 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
240 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
242 if (it->first != "id")
244 edgemap_default[it->first] = 0.0;
248 double max = std::numeric_limits<double>::min();
249 for (EdgeIt e(graph); e != INVALID; ++e)
251 if ((*edgemap_storage["id"])[e] > max)
252 max = (*edgemap_storage["id"])[e];
254 if (max > std::numeric_limits<double>::min())
255 edgemap_default["id"] = max + 1.0;
257 edgemap_default["id"] = 1.0;
264 void MapStorage::writeToFile(const std::string &filename)
266 GraphWriter<Graph> gwriter(filename, graph);
268 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
269 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
271 gwriter.writeNodeMap(it->first, *(it->second));
272 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
274 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
275 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
277 gwriter.writeEdgeMap(it->first, *(it->second));
278 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
283 void MapStorage::clear()
285 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
286 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
288 if ((it->first != "coordinates_x") &&
289 (it->first != "coordinates_y") &&
293 nodemap_storage.erase(it);
296 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
297 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
299 if (it->first != "id")
302 edgemap_storage.erase(it);
305 for (std::map<std::string, double>::iterator it =
306 nodemap_default.begin(); it != nodemap_default.end(); ++it)
308 if (it->first != "id")
309 nodemap_default.erase(it);
311 for (std::map<std::string, double>::iterator it =
312 edgemap_default.begin(); it != edgemap_default.end(); ++it)
314 if (it->first != "id")
315 edgemap_default.erase(it);