XLines.cpp
上传用户:yokoluohf
上传日期:2013-02-25
资源大小:769k
文件大小:4k
- #include "StdAfx.h"
- #include "XLines.h"
- XLines::XLines()
- {
- }
- XLines::~XLines()
- {
- }
- int XLines::Append (const XLine& obj)
- {
- //push_back(obj);
- (*this) [obj.name] = obj;
- return (int)size();
- }
- int XLines::Remove (const XLine& obj)
- {
- iterator pos;
- pos = find(obj.name);
- if (pos != end())
- erase (pos);
- return (int)size();
- }
- XLine* XLines::Find (const CString name)
- {
- iterator pos;
- pos = find(name);
- if (pos != end())
- return & ((*this) [name]);
- else
- return NULL;
- }
- int XLines::BuildStations(XStations& stationsMap)
- {
- for (XLines::iterator pos = begin(); pos != end(); pos ++ )
- {
- for(int i = 0; i < pos->second.GetCount(); i++)
- {
- XStation* station = stationsMap.Find(pos->second.spots[i].GetName());
- if (station)
- {
- station->Append(pos->second.name);
- }
- else
- {
- XStation station;
- station.name = pos->second.spots[i].GetName();
- station.position = pos->second.spots[i].GetPosition();
- station.Append(pos->second.name);
-
- stationsMap [pos->second.spots[i].GetName()] = station;
- }
- }
- }
- return stationsMap.GetCount ();
- }
- int XLines::GetCount ()
- {
- return (int) size();
- }
- int XLines::FromXML (CMarkup& markup)
- {
- while (markup.FindChildElem("line"))
{
XLine line;
line.FromXML(markup);
Append(line);
}
- markup.OutOfElem();
-
- BuildStations (allStations);
- return 0;
- }
- int XLines::ToXML (CMarkup& markup)
- {
- /*
- //保存线路信息
- markup.IntoElem ();
- for (size_t i = 0; i < size(); i++)
- at(i).ToXML(markup);
-
- markup.OutOfElem();
- */
- return 0;
- }
- int XLines::Merge (const vector<CString>& lines, vector<CString>& stations)
- {
- stations.clear ();
- for (size_t i = 0; i < lines.size (); i ++)
- {
- //lines [i]
- XLine* line = Find (lines [i]);
- if (line)
- {
- for (size_t j = 0; j < line->spots.size (); j ++)
- {
- if (line->spots [j].GetName () != "")
- stations.push_back (line->spots [j].GetName ());
- }
- }
- }
- sort (stations.begin (), stations.end ());
- vector<CString>::iterator pos = unique (stations.begin (), stations.end ());
- stations.erase (pos, stations.end ());
- return (int) stations.size ();
- }
- vector<CString> XLines::Intersection(const CString vStation1, const CString vStation2)
- {
- XStation* pStation1 = allStations.Find (vStation1);
- XStation* pStation2 = allStations.Find (vStation2);
-
- vector<CString> lines (1024);
- if (pStation1 && pStation2)
- {
- //找直达线路
- vector<CString> lines1 = pStation1->lines;
- vector<CString> lines2 = pStation2->lines;
- sort (lines1.begin (), lines1.end ());
- sort (lines2.begin (), lines2.end ());
- vector<CString>::iterator pos = set_intersection (lines1.begin(), lines1.end(),
- lines2.begin(), lines2.end(), lines.begin());
- lines.erase (pos);
- }
- return lines;
- }
- vector<XPath> XLines::Find (const CString vStation1, const CString vStation2, int type)
- {
- vector<XPath> paths;
- XStation* pStation1 = allStations.Find (vStation1);
- XStation* pStation2 = allStations.Find (vStation2);
- if (pStation1 && pStation2)
- {
- //1, 找直达线路
- vector<CString> lines;
- lines = Intersection (vStation1, vStation2);
- for (size_t i = 0; i < lines.size (); i ++)
- {
- XLine* line = Find (lines [i]);
- if (line)
- {
- XPath path (*line, vStation1, vStation2);
- if (path.lines == 1)
- paths.push_back(path);
- }
- }
- //2, 经过一个中转站
- for (size_t i = 0; i < pStation1->lines.size (); i ++)
- {
- for (size_t j = 0; j < pStation2->lines.size (); j ++)
- {
- XLine* line1 = Find (pStation1->lines [i]);
- XLine* line2 = Find (pStation2->lines [j]);
- if (line1 && line2)
- {
- //判断是否包含直达线路
- bool bNonstop = false;
- for (size_t k = 0; k < line1->spots.size (); k ++)
- {
- if (line1->spots [k].GetName () == vStation2)
- {
- bNonstop = true;
- break;
- }
- }
- for (size_t k = 0; k < line2->spots.size (); k ++)
- {
- if (line2->spots [k].GetName () == vStation1)
- {
- bNonstop = true;
- break;
- }
- }
- if (!bNonstop)
- {
- XPath path (*line1, *line2, vStation1, vStation2, false);
- if (path.lines == 2)
- paths.push_back(path);
- XPath path2 (*line1, *line2, vStation1, vStation2, true);
- if (path2.lines == 2)
- paths.push_back(path2);
- }
- }
- }
- }
- }
- sort (paths.begin (), paths.end ());
- return paths;
- }