#include "mapstorage.h" #include #include MapStorage::MapStorage() : modified(false), file_name("") { nodemap_storage["coordinates_x"] = new Graph::NodeMap(graph); coords.setXMap(*nodemap_storage["coordinates_x"]); nodemap_storage["coordinates_y"] = new Graph::NodeMap(graph); coords.setYMap(*nodemap_storage["coordinates_y"]); nodemap_storage["id"] = new Graph::NodeMap(graph); edgemap_storage["id"] = new Graph::EdgeMap(graph); nodemap_default["id"] = 1.0; edgemap_default["id"] = 1.0; } MapStorage::~MapStorage() { for (std::map*>::const_iterator it = nodemap_storage.begin(); it != nodemap_storage.end(); ++it) { delete it->second; } for (std::map*>::const_iterator it = edgemap_storage.begin(); it != edgemap_storage.end(); ++it) { delete it->second; } } int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap *nodemap, double default_value = 0.0) { if( nodemap_storage.find(name) == nodemap_storage.end() ) { nodemap_storage[name]=nodemap; // set the maps default value nodemap_default[name] = default_value; return 0; } return 1; } int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap *edgemap, double default_value = 0.0) { if( edgemap_storage.find(name) == edgemap_storage.end() ) { edgemap_storage[name]=edgemap; // set the maps default value edgemap_default[name] = default_value; return 0; } return 1; } double MapStorage::maxOfNodeMap(const std::string & name) { double max=0; for (NodeIt j(graph); j!=INVALID; ++j) { if( (*nodemap_storage[name])[j]>max ) { max=(*nodemap_storage[name])[j]; } } return max; } double MapStorage::maxOfEdgeMap(const std::string & name) { double max=0; for (EdgeIt j(graph); j!=INVALID; ++j) { if( (*edgemap_storage[name])[j]>max ) { max=(*edgemap_storage[name])[j]; } } return max; } double MapStorage::minOfNodeMap(const std::string & name) { NodeIt j(graph); double min; if(j!=INVALID) { min=(*nodemap_storage[name])[j]; } else { min=0; } for (; j!=INVALID; ++j) { if( (*nodemap_storage[name])[j]& nodeMapNames = content.nodeSetMaps(0); const std::vector& edgeMapNames = content.edgeSetMaps(0); GraphReader greader(filename, graph); for (std::vector::const_iterator it = nodeMapNames.begin(); it != nodeMapNames.end(); ++it) { if (*it == "coordinates_x") { read_x = true; //std::cout << "read X nodemap" << std::endl; } else if (*it == "coordinates_y") { read_y = true; //std::cout << "read Y nodemap" << std::endl; } else if (*it == "id") { //std::cout << "read id nodemap" << std::endl; } else { nodemap_storage[*it] = new Graph::NodeMap(graph); //std::cout << "read " << *it << " nodemap" << std::endl; } greader.readNodeMap(*it, *nodemap_storage[*it]); } for (std::vector::const_iterator it = edgeMapNames.begin(); it != edgeMapNames.end(); ++it) { if (*it == "id") { //std::cout << "read id edgemap" << std::endl; } else { edgemap_storage[*it] = new Graph::EdgeMap(graph); //std::cout << "read " << *it << " edgemap" << std::endl; } greader.readEdgeMap(*it, *edgemap_storage[*it]); } greader.run(); } catch (DataFormatError& error) { Gtk::MessageDialog mdialog("Read Error"); mdialog.set_message(error.what()); mdialog.run(); clear(); return 1; } if (!read_edge_id) { edgemap_storage["id"] = new Graph::EdgeMap(graph); int i = 1; for (EdgeIt e(graph); e != INVALID; ++e) { (*edgemap_storage["id"])[e] = i++; } } if (!read_x || !read_y) { int node_num = 0; for (NodeIt n(graph); n != INVALID; ++n) { node_num++; } const double pi = 3.142; double step = 2 * pi / (double) node_num; int i = 0; for (NodeIt n(graph); n != INVALID; ++n) { nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step)); nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step)); i++; } } // fill in the default values for the maps for (std::map*>::const_iterator it = nodemap_storage.begin(); it != nodemap_storage.end(); ++it) { if ((it->first != "id") && (it->first != "coordiantes_x") && (it->first != "coordinates_y")) { nodemap_default[it->first] = 0.0; } else if (it->first == "id") { NodeIt n(graph); double max = (*nodemap_storage["id"])[n]; for (; n != INVALID; ++n) { if ((*nodemap_storage["id"])[n] > max) max = (*nodemap_storage["id"])[n]; } nodemap_default["id"] = max + 1.0; } } for (std::map*>::const_iterator it = edgemap_storage.begin(); it != edgemap_storage.end(); ++it) { if (it->first != "id") { edgemap_default[it->first] = 0.0; } else { double max = std::numeric_limits::min(); for (EdgeIt e(graph); e != INVALID; ++e) { if ((*edgemap_storage["id"])[e] > max) max = (*edgemap_storage["id"])[e]; } if (max > std::numeric_limits::min()) edgemap_default["id"] = max + 1.0; else edgemap_default["id"] = 1.0; } } return 0; } void MapStorage::writeToFile(const std::string &filename) { GraphWriter gwriter(filename, graph); for (std::map*>::const_iterator it = nodemap_storage.begin(); it != nodemap_storage.end(); ++it) { gwriter.writeNodeMap(it->first, *(it->second)); //std::cout << "wrote " << it->first << " nodemap" << std::endl; } for (std::map*>::const_iterator it = edgemap_storage.begin(); it != edgemap_storage.end(); ++it) { gwriter.writeEdgeMap(it->first, *(it->second)); //std::cout << "wrote " << it->first << " edgemap" << std::endl; } gwriter.run(); } void MapStorage::clear() { for (std::map*>::iterator it = nodemap_storage.begin(); it != nodemap_storage.end(); ++it) { if ((it->first != "coordinates_x") && (it->first != "coordinates_y") && (it->first != "id")) { delete it->second; nodemap_storage.erase(it); } } for (std::map*>::iterator it = edgemap_storage.begin(); it != edgemap_storage.end(); ++it) { if (it->first != "id") { delete it->second; edgemap_storage.erase(it); } } for (std::map::iterator it = nodemap_default.begin(); it != nodemap_default.end(); ++it) { if (it->first != "id") nodemap_default.erase(it); } for (std::map::iterator it = edgemap_default.begin(); it != edgemap_default.end(); ++it) { if (it->first != "id") edgemap_default.erase(it); } graph.clear(); file_name = ""; modified = false; }