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