ladanyi@1606: #include "mapstorage.h" ladanyi@1634: #include ladanyi@1442: ladanyi@1606: MapStorage::MapStorage() : modified(false), file_name("") ladanyi@1442: { ladanyi@1606: nodemap_storage["coordinates_x"] = new Graph::NodeMap(graph); ladanyi@1606: coords.setXMap(*nodemap_storage["coordinates_x"]); ladanyi@1606: nodemap_storage["coordinates_y"] = new Graph::NodeMap(graph); ladanyi@1606: coords.setYMap(*nodemap_storage["coordinates_y"]); ladanyi@1606: ladanyi@1606: nodemap_storage["id"] = new Graph::NodeMap(graph); ladanyi@1606: edgemap_storage["id"] = new Graph::EdgeMap(graph); ladanyi@1606: } ladanyi@1606: ladanyi@1606: MapStorage::~MapStorage() ladanyi@1606: { ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: } ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: } ladanyi@1606: } ladanyi@1442: ladanyi@1442: int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap *nodemap) ladanyi@1442: { hegyi@1597: if( nodemap_storage.find(name) == nodemap_storage.end() ) hegyi@1597: { hegyi@1597: nodemap_storage[name]=nodemap; hegyi@1597: return 0; hegyi@1597: } hegyi@1597: return 1; ladanyi@1442: } ladanyi@1442: ladanyi@1442: int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap *edgemap) ladanyi@1442: { hegyi@1597: if( edgemap_storage.find(name) == edgemap_storage.end() ) hegyi@1597: { hegyi@1597: edgemap_storage[name]=edgemap; hegyi@1597: return 0; hegyi@1597: } hegyi@1597: return 1; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::maxOfNodeMap(const std::string & name) ladanyi@1442: { ladanyi@1442: double max=0; ladanyi@1606: for (NodeIt j(graph); j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*nodemap_storage[name])[j]>max ) ladanyi@1442: { ladanyi@1442: max=(*nodemap_storage[name])[j]; ladanyi@1442: } ladanyi@1442: } ladanyi@1442: return max; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::maxOfEdgeMap(const std::string & name) ladanyi@1442: { ladanyi@1442: double max=0; ladanyi@1606: for (EdgeIt j(graph); j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*edgemap_storage[name])[j]>max ) ladanyi@1442: { ladanyi@1442: max=(*edgemap_storage[name])[j]; ladanyi@1442: } ladanyi@1442: } ladanyi@1442: return max; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::minOfNodeMap(const std::string & name) ladanyi@1442: { ladanyi@1606: NodeIt j(graph); hegyi@1617: double min; hegyi@1617: if(j!=INVALID) hegyi@1617: { hegyi@1617: min=(*nodemap_storage[name])[j]; hegyi@1617: } hegyi@1617: else hegyi@1617: { hegyi@1617: min=0; hegyi@1617: } ladanyi@1442: for (; j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*nodemap_storage[name])[j] * >::iterator ems_it; hegyi@1525: for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++) hegyi@1525: { hegyi@1525: (*((*ems_it).second))[e]=5; hegyi@1525: } hegyi@1509: } ladanyi@1606: ladanyi@1606: void MapStorage::readFromFile(const std::string &filename) ladanyi@1606: { ladanyi@1606: bool read_x = false; ladanyi@1606: bool read_y = false; ladanyi@1606: ladanyi@1606: try { ladanyi@1606: LemonReader lreader(filename); ladanyi@1606: ContentReader content(lreader); ladanyi@1606: lreader.run(); ladanyi@1606: ladanyi@1606: const std::vector& nodeMapNames = content.nodeSetMaps(0); ladanyi@1606: const std::vector& edgeMapNames = content.edgeSetMaps(0); ladanyi@1606: ladanyi@1606: GraphReader greader(filename, graph); ladanyi@1606: for (std::vector::const_iterator it = nodeMapNames.begin(); ladanyi@1606: it != nodeMapNames.end(); ++it) ladanyi@1606: { ladanyi@1606: if (*it == "coordinates_x") ladanyi@1606: { ladanyi@1606: read_x = true; ladanyi@1606: //std::cout << "read X nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else if (*it == "coordinates_y") ladanyi@1606: { ladanyi@1606: read_y = true; ladanyi@1606: //std::cout << "read Y nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else if (*it == "id") ladanyi@1606: { ladanyi@1606: //std::cout << "read id nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else ladanyi@1606: { ladanyi@1606: nodemap_storage[*it] = new Graph::NodeMap(graph); ladanyi@1606: //std::cout << "read " << *it << " nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: greader.readNodeMap(*it, *nodemap_storage[*it]); ladanyi@1606: } ladanyi@1606: for (std::vector::const_iterator it = edgeMapNames.begin(); ladanyi@1606: it != edgeMapNames.end(); ++it) ladanyi@1606: { ladanyi@1606: if (*it == "id") ladanyi@1606: { ladanyi@1606: //std::cout << "read id edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: else ladanyi@1606: { ladanyi@1606: edgemap_storage[*it] = new Graph::EdgeMap(graph); ladanyi@1606: //std::cout << "read " << *it << " edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: greader.readEdgeMap(*it, *edgemap_storage[*it]); ladanyi@1606: } ladanyi@1606: greader.run(); ladanyi@1606: } catch (DataFormatError& error) { ladanyi@1606: /* ladanyi@1606: Gtk::MessageDialog mdialog("Read Error"); ladanyi@1606: mdialog.set_message(error.what()); ladanyi@1606: mdialog.run(); ladanyi@1606: */ ladanyi@1606: // reset graph and mapstorage ? ladanyi@1606: return; ladanyi@1606: } ladanyi@1606: ladanyi@1606: if (!read_x || !read_y) ladanyi@1606: { ladanyi@1606: int node_num = 0; ladanyi@1606: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@1606: { ladanyi@1606: node_num++; ladanyi@1606: } ladanyi@1606: const double pi = 3.142; ladanyi@1606: double step = 2 * pi / (double) node_num; ladanyi@1606: int i = 0; ladanyi@1606: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@1606: { ladanyi@1606: nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step)); ladanyi@1606: nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step)); ladanyi@1606: i++; ladanyi@1606: } ladanyi@1606: } ladanyi@1606: } ladanyi@1606: ladanyi@1606: void MapStorage::writeToFile(const std::string &filename) ladanyi@1606: { ladanyi@1606: GraphWriter gwriter(filename, graph); ladanyi@1606: ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: gwriter.writeNodeMap(it->first, *(it->second)); ladanyi@1606: //std::cout << "wrote " << it->first << " nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: gwriter.writeEdgeMap(it->first, *(it->second)); ladanyi@1606: //std::cout << "wrote " << it->first << " edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: gwriter.run(); ladanyi@1606: } ladanyi@1606: ladanyi@1606: void MapStorage::clear() ladanyi@1606: { ladanyi@1606: for (std::map*>::iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: if ((it->first != "coordinates_x") && ladanyi@1606: (it->first != "coordinates_y") && ladanyi@1606: (it->first != "id")) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: nodemap_storage.erase(it); ladanyi@1606: } ladanyi@1606: } ladanyi@1606: for (std::map*>::iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: if (it->first != "id") ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: edgemap_storage.erase(it); ladanyi@1606: } ladanyi@1606: } ladanyi@1606: graph.clear(); ladanyi@1606: file_name = ""; ladanyi@1606: modified = false; ladanyi@1606: }