2011-01-09 16:45:16 -05:00
|
|
|
/*
|
|
|
|
open source routing machine
|
|
|
|
Copyright (C) Dennis Luxen, others 2010
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU AFFERO General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
or see http://www.gnu.org/licenses/agpl.txt.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SEARCHENGINE_H_
|
|
|
|
#define SEARCHENGINE_H_
|
|
|
|
|
|
|
|
#include <climits>
|
|
|
|
#include <deque>
|
2011-12-17 14:17:40 -05:00
|
|
|
#include "SimpleStack.h"
|
2011-01-09 16:45:16 -05:00
|
|
|
|
2011-11-14 13:36:31 -05:00
|
|
|
#include <boost/thread.hpp>
|
|
|
|
|
2011-01-09 16:45:16 -05:00
|
|
|
#include "BinaryHeap.h"
|
2012-04-25 04:51:16 -04:00
|
|
|
#include "NodeInformationHelpDesk.h"
|
2011-01-09 16:45:16 -05:00
|
|
|
#include "PhantomNodes.h"
|
2012-06-15 12:47:27 -04:00
|
|
|
#include "../RoutingAlgorithms/AlternativePathRouting.h"
|
|
|
|
#include "../RoutingAlgorithms/BasicRoutingInterface.h"
|
|
|
|
#include "../RoutingAlgorithms/ShortestPathRouting.h"
|
|
|
|
|
2011-07-07 04:05:58 -04:00
|
|
|
#include "../Util/StringUtil.h"
|
2011-01-09 16:45:16 -05:00
|
|
|
#include "../typedefs.h"
|
|
|
|
|
|
|
|
struct _HeapData {
|
2011-12-31 10:18:52 -05:00
|
|
|
NodeID parent;
|
|
|
|
_HeapData( NodeID p ) : parent(p) { }
|
2011-12-20 12:45:48 -05:00
|
|
|
};
|
|
|
|
|
2012-06-15 12:47:27 -04:00
|
|
|
typedef boost::thread_specific_ptr<BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> > > SearchEngineHeapPtr;
|
|
|
|
|
|
|
|
template<class EdgeData, class GraphT>
|
|
|
|
struct SearchEngineData {
|
|
|
|
typedef SearchEngineHeapPtr HeapPtr;
|
|
|
|
typedef GraphT Graph;
|
|
|
|
SearchEngineData(GraphT * g, NodeInformationHelpDesk * nh, std::vector<string> & n) :graph(g), nodeHelpDesk(nh), names(n) {}
|
|
|
|
const GraphT * graph;
|
|
|
|
NodeInformationHelpDesk * nodeHelpDesk;
|
|
|
|
std::vector<string> & names;
|
|
|
|
static HeapPtr forwardHeap;
|
|
|
|
static HeapPtr backwardHeap;
|
|
|
|
static HeapPtr forwardHeap2;
|
|
|
|
static HeapPtr backwardHeap2;
|
2012-06-19 11:26:34 -04:00
|
|
|
static HeapPtr forwardHeap3;
|
|
|
|
static HeapPtr backwardHeap3;
|
2012-06-15 12:47:27 -04:00
|
|
|
|
|
|
|
inline void InitializeOrClearFirstThreadLocalStorage() {
|
|
|
|
if(!forwardHeap.get()) {
|
|
|
|
forwardHeap.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
forwardHeap->Clear();
|
|
|
|
|
|
|
|
if(!backwardHeap.get()) {
|
|
|
|
backwardHeap.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
backwardHeap->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void InitializeOrClearSecondThreadLocalStorage() {
|
|
|
|
if(!forwardHeap2.get()) {
|
|
|
|
forwardHeap2.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
forwardHeap2->Clear();
|
|
|
|
|
|
|
|
if(!backwardHeap2.get()) {
|
|
|
|
backwardHeap2.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
backwardHeap2->Clear();
|
|
|
|
}
|
2012-06-19 11:26:34 -04:00
|
|
|
|
|
|
|
inline void InitializeOrClearThirdThreadLocalStorage() {
|
|
|
|
if(!forwardHeap3.get()) {
|
|
|
|
forwardHeap3.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
forwardHeap3->Clear();
|
|
|
|
|
|
|
|
if(!backwardHeap3.get()) {
|
|
|
|
backwardHeap3.reset(new BinaryHeap< NodeID, NodeID, int, _HeapData, UnorderedMapStorage<NodeID, int> >(nodeHelpDesk->getNumberOfNodes()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
backwardHeap3->Clear();
|
|
|
|
}
|
2012-06-15 12:47:27 -04:00
|
|
|
};
|
2011-01-09 16:45:16 -05:00
|
|
|
|
2011-11-14 13:36:31 -05:00
|
|
|
template<class EdgeData, class GraphT>
|
2011-01-09 16:45:16 -05:00
|
|
|
class SearchEngine {
|
|
|
|
private:
|
2012-06-15 12:47:27 -04:00
|
|
|
typedef SearchEngineData<EdgeData, GraphT> SearchEngineDataT;
|
|
|
|
SearchEngineDataT _queryData;
|
|
|
|
|
2011-12-31 10:18:52 -05:00
|
|
|
inline double absDouble(double input) { if(input < 0) return input*(-1); else return input;}
|
2011-01-09 16:45:16 -05:00
|
|
|
public:
|
2012-06-15 12:47:27 -04:00
|
|
|
ShortestPathRouting<SearchEngineDataT> shortestPath;
|
|
|
|
AlternativeRouting<SearchEngineDataT> alternativePaths;
|
|
|
|
|
|
|
|
SearchEngine(GraphT * g, NodeInformationHelpDesk * nh, std::vector<string> & n) :
|
|
|
|
_queryData(g, nh, n),
|
|
|
|
shortestPath(_queryData),
|
|
|
|
alternativePaths(_queryData)
|
|
|
|
{}
|
2011-12-31 10:18:52 -05:00
|
|
|
~SearchEngine() {}
|
|
|
|
|
2012-09-19 07:48:04 -04:00
|
|
|
inline void GetCoordinatesForNodeID(NodeID id, _Coordinate& result) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
result.lat = _queryData.nodeHelpDesk->getLatitudeOfNode(id);
|
|
|
|
result.lon = _queryData.nodeHelpDesk->getLongitudeOfNode(id);
|
2011-12-31 10:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void FindRoutingStarts(const _Coordinate & start, const _Coordinate & target, PhantomNodes & routingStarts) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
_queryData.nodeHelpDesk->FindRoutingStarts(start, target, routingStarts);
|
2011-12-31 10:18:52 -05:00
|
|
|
}
|
|
|
|
|
2012-07-13 11:01:21 -04:00
|
|
|
inline void FindPhantomNodeForCoordinate(const _Coordinate & location, PhantomNode & result, unsigned zoomLevel) const {
|
|
|
|
_queryData.nodeHelpDesk->FindPhantomNodeForCoordinate(location, result, zoomLevel);
|
2011-12-31 10:18:52 -05:00
|
|
|
}
|
|
|
|
|
2012-04-25 05:07:28 -04:00
|
|
|
inline NodeID GetNameIDForOriginDestinationNodeID(const NodeID s, const NodeID t) const {
|
2011-12-31 10:18:52 -05:00
|
|
|
if(s == t)
|
|
|
|
return 0;
|
|
|
|
|
2012-06-15 12:47:27 -04:00
|
|
|
EdgeID e = _queryData.graph->FindEdge(s, t);
|
2011-12-31 10:18:52 -05:00
|
|
|
if(e == UINT_MAX)
|
2012-06-15 12:47:27 -04:00
|
|
|
e = _queryData.graph->FindEdge( t, s );
|
2011-12-31 10:18:52 -05:00
|
|
|
if(UINT_MAX == e) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
assert(e != UINT_MAX);
|
2012-06-15 12:47:27 -04:00
|
|
|
const EdgeData ed = _queryData.graph->GetEdgeData(e);
|
2011-12-31 10:18:52 -05:00
|
|
|
return ed.via;
|
|
|
|
}
|
|
|
|
|
2012-04-25 04:51:16 -04:00
|
|
|
inline std::string GetEscapedNameForNameID(const unsigned nameID) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
return ((nameID >= _queryData.names.size() || nameID == 0) ? std::string("") : HTMLEntitize(_queryData.names.at(nameID)));
|
2011-12-31 10:18:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string GetEscapedNameForEdgeBasedEdgeID(const unsigned edgeID) const {
|
2012-06-15 12:47:27 -04:00
|
|
|
const unsigned nameID = _queryData.graph->GetEdgeData(edgeID).nameID1;
|
2011-12-31 10:18:52 -05:00
|
|
|
return GetEscapedNameForNameID(nameID);
|
|
|
|
}
|
|
|
|
|
2011-01-09 16:45:16 -05:00
|
|
|
};
|
|
|
|
|
2012-06-15 12:47:27 -04:00
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::forwardHeap;
|
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::backwardHeap;
|
|
|
|
|
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::forwardHeap2;
|
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::backwardHeap2;
|
2011-12-20 12:45:48 -05:00
|
|
|
|
2012-06-19 11:26:34 -04:00
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::forwardHeap3;
|
|
|
|
template<class EdgeData, class GraphT> SearchEngineHeapPtr SearchEngineData<EdgeData, GraphT>::backwardHeap3;
|
|
|
|
|
2011-01-09 16:45:16 -05:00
|
|
|
#endif /* SEARCHENGINE_H_ */
|