refactor Hashtable

This commit is contained in:
Dennis Luxen 2013-08-06 16:39:04 +02:00
parent e7cec83a4c
commit e06fe6935a
9 changed files with 30 additions and 47 deletions

View File

@ -24,56 +24,35 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef HASHTABLE_H_ #ifndef HASHTABLE_H_
#define HASHTABLE_H_ #define HASHTABLE_H_
#include <boost/ref.hpp>
#include <boost/unordered_map.hpp> #include <boost/unordered_map.hpp>
template<typename keyT, typename valueT> template<typename keyT, typename valueT>
class HashTable { class HashTable : public boost::unordered_map<keyT, valueT> {
typedef boost::unordered_map<keyT, valueT> MyHashTable; private:
typedef boost::unordered_map<keyT, valueT> super;
public: public:
typedef typename boost::unordered_map<keyT, valueT>::const_iterator MyIterator; HashTable() : super() { }
typedef MyIterator iterator;
HashTable() { } HashTable(const unsigned size) : super(size) { }
HashTable(const unsigned size) {
table.resize(size);
}
inline void Add(const keyT& key, const valueT& value){ inline void Add(const keyT& key, const valueT& value){
table[key] = value; super::insert(std::make_pair(key, value));
}
inline void Set(const keyT& key, const valueT& value){
table[key] = value;
} }
inline valueT Find(const keyT& key) const { inline valueT Find(const keyT& key) const {
if(table.find(key) == table.end()) if(super::find(key) == super::end()) {
return valueT(); return valueT();
return table.find(key)->second; }
return boost::ref(super::find(key)->second);
} }
inline bool Holds(const keyT& key) const { inline bool Holds(const keyT& key) const {
if(table.find(key) == table.end()) if(super::find(key) == super::end()) {
return false; return false;
}
return true; return true;
} }
void EraseAll() {
if(table.size() > 0)
table.clear();
}
inline valueT operator[] (keyT key) const {
if(table.find(key) == table.end())
return valueT();
return table.find(key)->second;
}
inline unsigned Size() const {
return table.size();
}
MyIterator begin() const {
return table.begin();
}
MyIterator end() const {
return table.end();
}
private:
MyHashTable table;
}; };
#endif /* HASHTABLE_H_ */ #endif /* HASHTABLE_H_ */

View File

@ -46,7 +46,7 @@ struct ImportNode : public _Node {
HashTable<std::string, std::string> keyVals; HashTable<std::string, std::string> keyVals;
inline void Clear() { inline void Clear() {
keyVals.EraseAll(); keyVals.clear();
lat = 0; lon = 0; id = 0; bollard = false; trafficLight = false; lat = 0; lon = 0; id = 0; bollard = false; trafficLight = false;
} }
}; };

View File

@ -49,7 +49,7 @@ struct ExtractionWay {
id = UINT_MAX; id = UINT_MAX;
nameID = UINT_MAX; nameID = UINT_MAX;
path.clear(); path.clear();
keyVals.EraseAll(); keyVals.clear();
direction = ExtractionWay::notSure; direction = ExtractionWay::notSure;
speed = -1; speed = -1;
backward_speed = -1; backward_speed = -1;

View File

@ -177,7 +177,7 @@ inline void PBFParser::parseDenseNode(_ThreadData * threadData) {
const int keyValue = dense.keys_vals ( denseTagIndex+1 ); const int keyValue = dense.keys_vals ( denseTagIndex+1 );
const std::string & key = threadData->PBFprimitiveBlock.stringtable().s(tagValue).data(); const std::string & key = threadData->PBFprimitiveBlock.stringtable().s(tagValue).data();
const std::string & value = threadData->PBFprimitiveBlock.stringtable().s(keyValue).data(); const std::string & value = threadData->PBFprimitiveBlock.stringtable().s(keyValue).data();
extracted_nodes_vector[i].keyVals.Add(key, value); extracted_nodes_vector[i].keyVals.insert(std::make_pair(key, value));
denseTagIndex += 2; denseTagIndex += 2;
} }
} }
@ -306,7 +306,7 @@ inline void PBFParser::parseWay(_ThreadData * threadData) {
for(int j = 0; j < number_of_keys; ++j) { for(int j = 0; j < number_of_keys; ++j) {
const std::string & key = threadData->PBFprimitiveBlock.stringtable().s(inputWay.keys(j)); const std::string & key = threadData->PBFprimitiveBlock.stringtable().s(inputWay.keys(j));
const std::string & val = threadData->PBFprimitiveBlock.stringtable().s(inputWay.vals(j)); const std::string & val = threadData->PBFprimitiveBlock.stringtable().s(inputWay.vals(j));
parsed_way_vector[i].keyVals.Add(key, val); parsed_way_vector[i].keyVals.insert(std::make_pair(key, val));
} }
} }

View File

@ -25,8 +25,9 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) {
INFO("Using script " << fileName); INFO("Using script " << fileName);
// Create a new lua state // Create a new lua state
for(int i = 0; i < omp_get_max_threads(); ++i) for(int i = 0; i < omp_get_max_threads(); ++i) {
luaStateVector.push_back(luaL_newstate()); luaStateVector.push_back(luaL_newstate());
}
// Connect LuaBind to this lua state for all threads // Connect LuaBind to this lua state for all threads
#pragma omp parallel #pragma omp parallel
@ -85,6 +86,7 @@ ScriptingEnvironment::ScriptingEnvironment(const char * fileName) {
luabind::value("opposite", 3) luabind::value("opposite", 3)
] ]
]; ];
luabind::module(myLuaState) [ luabind::module(myLuaState) [
luabind::class_<std::vector<std::string> >("vector") luabind::class_<std::vector<std::string> >("vector")
.def("Add", &std::vector<std::string>::push_back) .def("Add", &std::vector<std::string>::push_back)

View File

@ -38,8 +38,8 @@ public:
NearestPlugin(QueryObjectsStorage * objects) : names(objects->names) { NearestPlugin(QueryObjectsStorage * objects) : names(objects->names) {
nodeHelpDesk = objects->nodeHelpDesk; nodeHelpDesk = objects->nodeHelpDesk;
descriptorTable.Set("", 0); //default descriptor descriptorTable.insert(std::make_pair("" , 0)); //default descriptor
descriptorTable.Set("json", 1); descriptorTable.insert(std::make_pair("json", 1));
} }
std::string GetDescriptor() const { return std::string("nearest"); } std::string GetDescriptor() const { return std::string("nearest"); }
std::string GetVersionString() const { return std::string("0.3 (DL)"); } std::string GetVersionString() const { return std::string("0.3 (DL)"); }

View File

@ -53,7 +53,7 @@ struct RouteParameters {
std::string language; std::string language;
std::vector<std::string> hints; std::vector<std::string> hints;
std::vector<_Coordinate> coordinates; std::vector<_Coordinate> coordinates;
typedef HashTable<std::string, std::string>::MyIterator OptionsIterator; typedef HashTable<std::string, std::string>::const_iterator OptionsIterator;
void setZoomLevel(const short i) { void setZoomLevel(const short i) {
if (18 > i && 0 < i) if (18 > i && 0 < i)

View File

@ -58,9 +58,9 @@ public:
searchEnginePtr = new SearchEngine(graph, nodeHelpDesk, names); searchEnginePtr = new SearchEngine(graph, nodeHelpDesk, names);
descriptorTable.Set("", 0); //default descriptor descriptorTable.insert(std::make_pair("" , 0)); //default descriptor
descriptorTable.Set("json", 0); descriptorTable.insert(std::make_pair("json", 0));
descriptorTable.Set("gpx", 1); descriptorTable.insert(std::make_pair("gpx" , 1));
} }
virtual ~ViaRoutePlugin() { virtual ~ViaRoutePlugin() {

View File

@ -60,7 +60,9 @@ int main (int argc, char *argv[]) {
unsigned number_of_threads = omp_get_num_procs(); unsigned number_of_threads = omp_get_num_procs();
if(testDataFile("extractor.ini")) { if(testDataFile("extractor.ini")) {
BaseConfiguration extractorConfig("extractor.ini"); BaseConfiguration extractorConfig("extractor.ini");
INFO("2");
unsigned rawNumber = stringToInt(extractorConfig.GetParameter("Threads")); unsigned rawNumber = stringToInt(extractorConfig.GetParameter("Threads"));
INFO("3");
if( rawNumber != 0 && rawNumber <= number_of_threads) { if( rawNumber != 0 && rawNumber <= number_of_threads) {
number_of_threads = rawNumber; number_of_threads = rawNumber;
} }